diff options
author | Ben V. Brown <[email protected]> | 2016-09-21 00:17:26 +1000 |
---|---|---|
committer | Ben V. Brown <[email protected]> | 2016-09-21 00:17:26 +1000 |
commit | 65659b7b995fbbcd40444733e4fab55f626f3570 (patch) | |
tree | cd651ad2840784152d019f1e65864e69735730da | |
parent | 52e92feae7f2a33ed301ba18aae4ffa14f75d22e (diff) | |
download | IronOS-65659b7b995fbbcd40444733e4fab55f626f3570.tar.gz IronOS-65659b7b995fbbcd40444733e4fab55f626f3570.zip |
Rough PID added
-rw-r--r-- | workspace/ts100/inc/PID.h | 18 | ||||
-rw-r--r-- | workspace/ts100/src/PID.c | 45 |
2 files changed, 63 insertions, 0 deletions
diff --git a/workspace/ts100/inc/PID.h b/workspace/ts100/inc/PID.h new file mode 100644 index 00000000..98dc88ba --- /dev/null +++ b/workspace/ts100/inc/PID.h @@ -0,0 +1,18 @@ +/* + * PID.h + * + * Created on: 20 Sep 2016 + * Author: ralim + */ + +#ifndef PID_H_ +#define PID_H_ +#include "Analog.h" +#include "Interrupt.h" +struct { + uint32_t kp, ki, kd; //PID values +} pidSettings; + +int32_t computePID(uint16_t setpoint); +void setupPID(void); +#endif /* PID_H_ */ diff --git a/workspace/ts100/src/PID.c b/workspace/ts100/src/PID.c new file mode 100644 index 00000000..e1b4a98c --- /dev/null +++ b/workspace/ts100/src/PID.c @@ -0,0 +1,45 @@ +/* + * PID.c + * + * Created on: 20 Sep 2016 + * Author: ralim + */ + +#include "PID.h" +#define MAXPIDOUTPUT 500000 +//This funtion computes the new value for the ON time of the system +//This is the return value from this function +int32_t computePID(uint16_t setpoint) { + static uint32_t lastSample = 0; + int32_t ITerm = 0; + static int16_t lastReading = 0; + if (millis() - lastSample > 50) { + //only sample every 100 milliseconds + uint16_t currentReading = readIronTemp(0); //get the current temp of the iron + uint16_t error = setpoint - currentReading; //calculate the error term + ITerm += (pidSettings.ki * error); + if (ITerm > MAXPIDOUTPUT) + ITerm = MAXPIDOUTPUT; + else if (ITerm < 0) + ITerm = 0; //cap at 0 since we cant force the iron to cool itself :) + + int16_t DInput = (currentReading - lastReading); //compute the input to the D term + int32_t output = (pidSettings.kp * error) + (ITerm) + - (pidSettings.kd * DInput); + if (output > MAXPIDOUTPUT) + output = MAXPIDOUTPUT; + else if (output < 0) + output = 0; + lastSample = millis(); + lastReading = currentReading; //storing values for next iteration of the loop + return output; + } + return -1; //called too fast +} + +void setupPID(void) { + pidSettings.kp = 15; + pidSettings.ki = 2; + pidSettings.kd = 1; + +} |