
1. Re: simple PID like algorithm
JoMe_264151 Jan 27, 2016 1:41 PM (in response to userc_40302)Depending on the time the system uses to react your closed loop will either overshoot or converge asymptotic. Only by finetuning the PID parameters and by stabilizing (thermal Isolation) of the heater and / or forcibly stirring the heated liquid will help.
Bob

2. Re: simple PID like algorithm
HeLi_263931 Jan 27, 2016 1:53 PM (in response to userc_40302)1 of 1 people found this helpfulI can only recommend to follow the "improving the beginners PID" tutorial series. Its quite comprehensive  I learned PID from there, and its not exactly complicated (read the value you want to control, make some calculations, write the value to the peripheral that controls your setup).
The tricky part is in finding out the correct P,I, and D values. For tuning look at the ZieglerNicholsmethod (tl;dr: set I and D to 0, start increasing P until your control loop oscillates. With this frequency you then calculate P, I, and D).

3. Re: simple PID like algorithm
userc_40302 Jan 27, 2016 2:10 PM (in response to userc_40302)thanks for your quick replies folks. say i only use the the P term for initial testing and understanding then Output = kp * error
Another thing is how do i relate the output to a PWM duty cycle? using an 8 bit PWM with a range of 0255
thank you guys

4. Re: simple PID like algorithm
JoMe_264151 Jan 28, 2016 7:38 AM (in response to userc_40302)The function PWM_WriteCompare() will set the dutycycle . Use a value between 1 and the programmed period value.
Bob

5. Re: simple PID like algorithm
userc_40302 Jan 29, 2016 7:04 AM (in response to userc_40302)Hi bob, The PWM_WriteCompare() function is being used to drive a h bridge, At the moment the value of between 0 and 255 was randomly chosen, How does the Proportional output alter the PWM duty cycle? would i not need to relate the output function with the proportional function to the PWM's duty cycle somehow as it should take into account he overshoot in temperature and compensate for it?
ive got a setpoint which is a temperature reading in degrees C and the feedback is from a temp sensor also in degree C.
Sorry if my question seems a bit redundant
thanks

6. Re: simple PID like algorithm
JoMe_264151 Jan 29, 2016 11:24 AM (in response to userc_40302)Duty Cycle is Period / Compare *100, Quite easy.
The PID controller parameters (KP, KI, KD) are taking care of overshoots. Proceed as hli suggested, this is the only way.
Bob

7. Re: simple PID like algorithm
userc_40302 Jan 30, 2016 8:58 AM (in response to userc_40302)Hi Bob, thanks for the reply. Ive read the link hli sent to me, Its just i still have an issue with what you're saying. it makes a lot of sense. I know that 255 will equal 100% duty cycle and 128 will be 50%. Its the PID output function that i have an issue with. I know it can get the error term by reading the difference between set_temp and actual temp. but wont the PID function I called output need to be multiplied or somehow control the duty cycle in the PWM_writeCompare() function?
I have the PID output which is currently just the error*Kp, but doesnt the output function need to be in the WPM_writecompare() function? the value i set in the write compare function directly controls the duty cycle and therefore the heater.
ive been doing something like this PWM_WriteCompare(192), roughly 75% Duty cycle. I have two PWM_WriteCompare functions because the two PWM outputs are connected to IN1 and IN2 of the Hbridge im using. I can reverse the direction by giving a duty cycle value to one write compare function and have the other PWM write compare function tied to ground.
The two PWM_writeCompre functions are my only outputs.
If have simple if statement that i know causes an overshoot.
its simply
if(set_point>actual_temp) //cooling direction
{
PWM_writeCompare1(192); //etc
PWM_writeComopare(0); //it tied to ground
}
then to heat the heater/peltier i do the opposite with the write compare.
How does the PID output function interact with the PWM_writeCompare function?
Sorry for posting a silly question, thanks for all your help so far i greatly appreciate it.

8. Re: simple PID like algorithm
JoMe_264151 Jan 30, 2016 10:44 AM (in response to userc_40302)The PWM has two outputs which can be programmed with a deadband suitable for hbridge. Because the characteristics of the peltier are different for heating and cooling you may need two different PIDs connected to two PWMs. You may select which one is active using a MUX.
wont the PID function I called output need to be multiplied or somehow control the duty cycle in the PWM_writeCompare() function? This the KP performs. You might use a limiting function for duty cycle >= 0 and <= 100%
Bob

9. Re: simple PID like algorithm
userc_40302 Jan 30, 2016 12:13 PM (in response to userc_40302)Ok,
So would it be something like this? For the moment i only have the Proportional and i will put limits on the PWM as well as implement the Intergral and derivative functionalites.
Output = kp * error;
PWM_WriteCompare1(00000);
PWM_WriteCompare2(Output);thanks very much for your help


11. Re: simple PID like algorithm
userc_40302 Jan 30, 2016 1:32 PM (in response to userc_40302)Ok thanks. my PWM config is 8 bit resolution, two PWM outputs, with deadbands for 24 clock cycles. a 100kHz clock giving a period of 2.56mSec. its also a less than type count.
My duty cycle is between 0 and 255, Its just how will the output function error*Kp increment my duty cycle?
ive set KP initially to 1, and the error will presumably be setpointactual_temp.
given a set point of 30c and a measured temp of 21C. the error will be 9, and the output will be equal to 9? wont that mean that the pwm duty cycle will be 9/255? PWM_WriteCompare1(Output or 9/255)
Will this value only increase with the use of the full formula i.e the integral and derivative?
Sorry its the output function controlling the duty cycle im having issues with.
thanks for your assistance

12. Re: simple PID like algorithm
JoMe_264151 Jan 30, 2016 3:39 PM (in response to userc_40302)A pure Pcontroller always has got a (small) error.
An Icomponent will make sure that there is no longterm error adding up.
The Dcomponent will allow for a short reaction time (Not usable for slow reactiontimes as heating or cooling.
Your example from above is right, duty cycle will be 900/255 = ~4%. When the temperature climbs too slow, you can increase KP.
Assume KP = 50, So in your above example you get 9*50 = 450. Limited to 0..255 you get 255, system is heating up. When the difference is only 1°C you get 1*50 that is 5000/255 = ~20% duty cycle.. When that is about what radiation cools your system, you have a stable situation.
Bob

13. Re: simple PID like algorithm
userc_40302 Jan 31, 2016 2:54 PM (in response to userc_40302)1 of 1 people found this helpfulHi Bob,
thanks for the reply. I see now how it controls the duty cycle. simple yet clever as you and hli have been putting it ot me.
Ive read that the derivative might not be applicable to a heating cooling situation especially given the slow time constant of heating/cooling.
I was looking at the integral component.
This is defined as: ki * errSum
where the error sum is equal to error * timechange.
unsigned long now = millis();
double timeChange = (double)(now  lastTime);
it gives a variable 'now' and its equal to millis(), im just trying to figure out how this is calculated or is it a random milliseconds number.i.e millis(100); is it the same a cydelay in a manner of speaking,
thanks

14. Re: simple PID like algorithm
JoMe_264151 Feb 1, 2016 2:18 AM (in response to userc_40302)1 of 1 people found this helpfulThe Ipart of the PID ensures that the error may become zero within a longer time. A side effect will be that the summed error will be corrected later producing some unwanted variance at that time. The Ipart is good when you mix two chemical parts with two controlled pumps. At the end all errors are extinguished and in the resulting reservoir the concentration is exactly as wanted. What are you designing? A DNA multiplier?
Bob