PIDHeater82
Dependencies: PID mbed millis ttmath
Fork of PIDHeater by
main.cpp@3:00a491d8ed0c, 2017-06-23 (annotated)
- Committer:
- Cedricbts
- Date:
- Fri Jun 23 14:53:24 2017 +0000
- Revision:
- 3:00a491d8ed0c
- Parent:
- 2:6e731a17523c
PIDHeater82
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cedricbts | 3:00a491d8ed0c | 1 | /* |
unix_guru | 2:6e731a17523c | 2 | +3.3v |
unix_guru | 2:6e731a17523c | 3 | | |
unix_guru | 2:6e731a17523c | 4 | \ |
Cedricbts | 3:00a491d8ed0c | 5 | / 10k Résistance série |
unix_guru | 2:6e731a17523c | 6 | \ |
unix_guru | 2:6e731a17523c | 7 | / |
unix_guru | 2:6e731a17523c | 8 | | |
Cedricbts | 3:00a491d8ed0c | 9 | .-----------O Vers Anolog 0 de la carte |
unix_guru | 2:6e731a17523c | 10 | | |
unix_guru | 2:6e731a17523c | 11 | \ |
unix_guru | 2:6e731a17523c | 12 | / |
Cedricbts | 3:00a491d8ed0c | 13 | Thermistance 10k Nominal |
unix_guru | 2:6e731a17523c | 14 | \ |
unix_guru | 2:6e731a17523c | 15 | / |
unix_guru | 2:6e731a17523c | 16 | | |
unix_guru | 2:6e731a17523c | 17 | --- |
unix_guru | 2:6e731a17523c | 18 | GND |
unix_guru | 0:8b77aea74642 | 19 | * |
unix_guru | 0:8b77aea74642 | 20 | * Author(s): Michael Ball unix_guru@hotmail.com |
unix_guru | 0:8b77aea74642 | 21 | * |
unix_guru | 0:8b77aea74642 | 22 | */ |
unix_guru | 0:8b77aea74642 | 23 | |
unix_guru | 0:8b77aea74642 | 24 | #include "mbed.h" |
unix_guru | 2:6e731a17523c | 25 | #include "millis.h" |
unix_guru | 2:6e731a17523c | 26 | #include "PID.h" |
unix_guru | 2:6e731a17523c | 27 | |
unix_guru | 2:6e731a17523c | 28 | float getTemperature(); |
unix_guru | 2:6e731a17523c | 29 | |
unix_guru | 0:8b77aea74642 | 30 | Serial pc(USBTX, USBRX); |
unix_guru | 2:6e731a17523c | 31 | Ticker PrintTicker; // Send process results to Console once per second |
unix_guru | 2:6e731a17523c | 32 | Ticker ticker; // Set up the millis() ticker. |
unix_guru | 2:6e731a17523c | 33 | |
Cedricbts | 3:00a491d8ed0c | 34 | #define DEFAULT_Kp 100 |
Cedricbts | 3:00a491d8ed0c | 35 | #define DEFAULT_Ki 0 |
Cedricbts | 3:00a491d8ed0c | 36 | #define DEFAULT_Kd 0 |
unix_guru | 0:8b77aea74642 | 37 | |
unix_guru | 2:6e731a17523c | 38 | #define AUTOMATIC 1 |
unix_guru | 2:6e731a17523c | 39 | #define MANUAL 0 |
unix_guru | 2:6e731a17523c | 40 | #define DIRECT 0 |
unix_guru | 2:6e731a17523c | 41 | #define REVERSE 1 |
Cedricbts | 3:00a491d8ed0c | 42 | #define thermistor A0 // Analog input pin A0 |
Cedricbts | 3:00a491d8ed0c | 43 | #define driver PB_3 // PWM output pin PB_3 |
Cedricbts | 3:00a491d8ed0c | 44 | #define PERIOD 2.0 |
Cedricbts | 3:00a491d8ed0c | 45 | AnalogIn Thermistor(thermistor); // Read temperature value from thermistor on A0 |
Cedricbts | 3:00a491d8ed0c | 46 | PwmOut Driver(PB_3); // PWM drive values are 0-1.0 |
unix_guru | 2:6e731a17523c | 47 | // For 0-100% |
Cedricbts | 3:00a491d8ed0c | 48 | float temp; |
unix_guru | 2:6e731a17523c | 49 | float Input, Output, Setpoint; |
unix_guru | 2:6e731a17523c | 50 | PID controller(&Input, &Output, &Setpoint, DEFAULT_Kp , DEFAULT_Ki , DEFAULT_Kd , DIRECT); |
unix_guru | 2:6e731a17523c | 51 | |
Cedricbts | 3:00a491d8ed0c | 52 | #define RATE 2.0 // Print rate once every two seconds |
unix_guru | 2:6e731a17523c | 53 | |
unix_guru | 2:6e731a17523c | 54 | void PrintValues() { // Routine to print out results to console |
unix_guru | 2:6e731a17523c | 55 | pc.printf("Input Output Setpoint Kp Ki Kd time\r\n"); |
unix_guru | 2:6e731a17523c | 56 | pc.printf("%f, %f, %f, %f, %f, %f, %d \r\n", |
unix_guru | 2:6e731a17523c | 57 | Input, Output, Setpoint, controller.GetKp() , controller.GetKi() , controller.GetKd() , millis() ); |
unix_guru | 2:6e731a17523c | 58 | |
unix_guru | 0:8b77aea74642 | 59 | } |
unix_guru | 0:8b77aea74642 | 60 | |
unix_guru | 2:6e731a17523c | 61 | |
unix_guru | 0:8b77aea74642 | 62 | int main(){ |
unix_guru | 0:8b77aea74642 | 63 | |
unix_guru | 2:6e731a17523c | 64 | startMillis(); // Initialize timer. |
unix_guru | 2:6e731a17523c | 65 | |
Cedricbts | 3:00a491d8ed0c | 66 | pc.baud(9600); |
Cedricbts | 3:00a491d8ed0c | 67 | //pc.printf("\r\nThermistor PID Test - Build " __DATE__ " " __TIME__ "\r\n"); |
unix_guru | 0:8b77aea74642 | 68 | |
Cedricbts | 3:00a491d8ed0c | 69 | PrintTicker.attach(&PrintValues,RATE); // Start PID process running at 2s rate. |
Cedricbts | 3:00a491d8ed0c | 70 | controller.SetSampleTime(2000); |
Cedricbts | 3:00a491d8ed0c | 71 | Setpoint = 24; // Set target temperature in degrees Celcius. |
unix_guru | 2:6e731a17523c | 72 | controller.SetMode(AUTOMATIC); // Turn PID controller on. |
unix_guru | 0:8b77aea74642 | 73 | |
unix_guru | 0:8b77aea74642 | 74 | while(1){ |
unix_guru | 0:8b77aea74642 | 75 | |
unix_guru | 2:6e731a17523c | 76 | Input = getTemperature(); // Actual temperature in Degrees Celcius |
unix_guru | 2:6e731a17523c | 77 | |
unix_guru | 2:6e731a17523c | 78 | controller.Compute(); // Process PID loop. |
unix_guru | 2:6e731a17523c | 79 | |
unix_guru | 2:6e731a17523c | 80 | Driver = Output/1000; // Sent PWM value scaled 0 - 1.0 as mbed requires |
Cedricbts | 3:00a491d8ed0c | 81 | |
unix_guru | 0:8b77aea74642 | 82 | } |
unix_guru | 0:8b77aea74642 | 83 | |
unix_guru | 2:6e731a17523c | 84 | } |
unix_guru | 2:6e731a17523c | 85 | |
unix_guru | 2:6e731a17523c | 86 | |
unix_guru | 2:6e731a17523c | 87 | float getTemperature() { |
Cedricbts | 3:00a491d8ed0c | 88 | float h = Thermistor.read_u16(); |
Cedricbts | 3:00a491d8ed0c | 89 | h = (h/65536)*3.3; //Conversion Bit to Voltage |
Cedricbts | 3:00a491d8ed0c | 90 | pc.printf("voltage %f\r\n", h); |
Cedricbts | 3:00a491d8ed0c | 91 | temp = -30.705*h+91.788; //Converson Voltage to °C |
Cedricbts | 3:00a491d8ed0c | 92 | pc.printf("temp = %f\r\n",temp); |
unix_guru | 2:6e731a17523c | 93 | |
Cedricbts | 3:00a491d8ed0c | 94 | return temp; |
unix_guru | 2:6e731a17523c | 95 | |
unix_guru | 2:6e731a17523c | 96 | } |
Cedricbts | 3:00a491d8ed0c | 97 |