PDA

View Full Version : Writing code for a feedback loop



Christopher4187
- 21st December 2012, 13:20
I'm having some difficulty writing a routine for a feedback loop. What I need to do is read a value taken from the network. Then I adjust the PWM based upon the value. In laymans terms, it would look like this:


IF TEMP > 105 AND TEMP < 170 THEN 'IF TEMP IS IN THE NORMAL RANGE, WE FORCE IT. OUTSIDE NORMAL RANGE, WE DON'T TOUCH IT.
FORCE_FLAG=1 'THE FIRST TIME THIS ROUTINE IS ENTERED
DUTYCYCLE=(170-TEMP/3)+222 'THIS GETS ME IN THE BALLPARK
FORCE_FLAG=0 'ROUTINE HAS BEEN ENTERED SO THE FLAG IS NOW RESET AND
'THIS ROUTINE WON'T BE ENTERED AGAIN UNTIL THE TEMP IS OUTSIDE THE NORMAL RANGE
ELSE
FORCE_FLAG=1 'THE NEXT TIME THROUGH, WE NEED TO CHECK THE INITIAL ROUTINE
DUTYCYCLE=0 'THE TEMP IS OUTSIDE NORMAL VALUES SO WE DON'T TOUCH IT.
ENDIF

IF FORCE_FLAG=0 THEN 'THE INITAL ROUTINE WAS ENTERED SO NOW WE NEED TO FINE TUNE IT

This is where I'm stuck. Between 105 and 170, the PWM range will be approximately 22 steps (222 at 170 and 244 at 105). In the first routine I got the value close but how do I adjust the PWM signal without over or undershooting the perfect value?

LinkMTech
- 21st December 2012, 16:38
I may be over simplifying it but, if the TEMP value is within the adjustment range of 105 and 170, it seems that the IF/THEN condition would fine tune your PWM signal already.
I also notice the DUTYCYCLE math needed the parentheses moved.



IF TEMP > 105 AND TEMP < 170 THEN
DUTYCYCLE = (170-TEMP)/3 + 222
ELSE
DUTYCYCLE = 0
ENDIF