start

Dependencies:   DRV88255 mbed

Committer:
BillyGrande
Date:
Tue Jun 27 21:13:09 2017 +0000
Revision:
2:5dd057d67746
Parent:
1:924bead61d21
start

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BillyGrande 1:924bead61d21 1 #include "temp.h"
BillyGrande 1:924bead61d21 2 #include "TextLCD.h"
BillyGrande 1:924bead61d21 3 #include <cmath>
BillyGrande 1:924bead61d21 4
BillyGrande 1:924bead61d21 5 AnalogIn temp(p19);
BillyGrande 1:924bead61d21 6 DigitalOut htr(p27);
BillyGrande 1:924bead61d21 7 extern Serial pc;
BillyGrande 1:924bead61d21 8 extern TextLCD lcd;
BillyGrande 1:924bead61d21 9 extern bool htr_suspension, rfl_flag;
BillyGrande 1:924bead61d21 10 extern float air_temp;
BillyGrande 1:924bead61d21 11
BillyGrande 1:924bead61d21 12 float calc_temp() {
BillyGrande 1:924bead61d21 13 float rt, t, voltage = 3.3 * temp.read();;
BillyGrande 1:924bead61d21 14 rt = R1*voltage/(3.3-voltage);
BillyGrande 1:924bead61d21 15 t = 1 / (K0 + K1 * log(rt) + K2 * pow(log(rt),3.0f));
BillyGrande 1:924bead61d21 16 return t - 273.15;
BillyGrande 1:924bead61d21 17 }
BillyGrande 1:924bead61d21 18
BillyGrande 1:924bead61d21 19 float take_air_temp() {
BillyGrande 1:924bead61d21 20 pc.printf("Going to take air temperature. Please make sure that the sensor is in the air and connected to the controller\n");
BillyGrande 1:924bead61d21 21 pc.printf("If you are ready, please press switch 3\n");
BillyGrande 1:924bead61d21 22 //pc.printf("rfl_flag = %s\n", rfl_flag ? "true":"false");
BillyGrande 1:924bead61d21 23 while (!rfl_flag) {wait(0.1);} //Wait until switch 4 has been pressed
BillyGrande 1:924bead61d21 24 rfl_flag = false;
BillyGrande 1:924bead61d21 25 return calc_temp();
BillyGrande 1:924bead61d21 26 }
BillyGrande 1:924bead61d21 27
BillyGrande 1:924bead61d21 28 void print_temp(float temp) {
BillyGrande 1:924bead61d21 29 pc.printf("Temperature = %.3f\n", temp);
BillyGrande 1:924bead61d21 30 lcd.cls();
BillyGrande 1:924bead61d21 31 lcd.printf("Temp: %.3f\n", temp);
BillyGrande 1:924bead61d21 32 }
BillyGrande 1:924bead61d21 33
BillyGrande 1:924bead61d21 34 bool sensor_out_of_water_check(float cT) {
BillyGrande 1:924bead61d21 35 if (air_temp-ATB<cT && cT<air_temp+ATB) { //sensor out of water
BillyGrande 1:924bead61d21 36 pc.printf("Temperature similar to air temperature. Highly likely that the sensor is out of the water\n");
BillyGrande 1:924bead61d21 37 pc.printf("Please check the temperature sensor position\n");
BillyGrande 1:924bead61d21 38 htr_suspension = true; //Suspend heater
BillyGrande 1:924bead61d21 39 pc.printf("For safety reasons, heater operation has been suspended. If everything is OK, resume operation by pressing switch 4\n");
BillyGrande 1:924bead61d21 40 return false;
BillyGrande 1:924bead61d21 41 }
BillyGrande 1:924bead61d21 42 return true;
BillyGrande 1:924bead61d21 43 }
BillyGrande 1:924bead61d21 44
BillyGrande 1:924bead61d21 45 bool connection_check(float cT) {
BillyGrande 1:924bead61d21 46 if (cT<LC) { //Connection lost
BillyGrande 1:924bead61d21 47 pc.printf("Temperature extremely freezingly low! Highly likely that the temp sensor connection is lost\n");
BillyGrande 1:924bead61d21 48 pc.printf("Please check the temperature sensor connection\n");
BillyGrande 1:924bead61d21 49 htr_suspension = true; //Suspend heater
BillyGrande 1:924bead61d21 50 pc.printf("For safety reasons, heater operation has been suspended. If everything is OK, resume operation by pressing switch 4\n");
BillyGrande 1:924bead61d21 51 return false;
BillyGrande 1:924bead61d21 52 }
BillyGrande 1:924bead61d21 53 return true;
BillyGrande 1:924bead61d21 54 }
BillyGrande 1:924bead61d21 55
BillyGrande 1:924bead61d21 56 bool malfunction_check(float cT) {
BillyGrande 1:924bead61d21 57 if (cT<TMLL || cT>TMUL) { //Bizzare temps
BillyGrande 1:924bead61d21 58 pc.printf("Temperature sensor is giving bizzare temps\n");
BillyGrande 1:924bead61d21 59 pc.printf("Temperature sensor malfunction is higly likely! Please check the sensor\n");
BillyGrande 1:924bead61d21 60 htr_suspension = true; //Suspend heater
BillyGrande 1:924bead61d21 61 pc.printf("For safety reasons, heater operation has been suspended. If everything is OK, resume operation by pressing switch 4\n");
BillyGrande 1:924bead61d21 62 return false;
BillyGrande 1:924bead61d21 63 }
BillyGrande 1:924bead61d21 64 return true;
BillyGrande 1:924bead61d21 65 }
BillyGrande 1:924bead61d21 66
BillyGrande 1:924bead61d21 67 void heater_control(float cT) {
BillyGrande 1:924bead61d21 68 if (cT<=TSLL) {
BillyGrande 1:924bead61d21 69 if(!htr_suspension) {
BillyGrande 1:924bead61d21 70 htr=1; pc.printf("Heater activated\n");
BillyGrande 1:924bead61d21 71 pc.printf("Expecting temperature to rise\n");
BillyGrande 1:924bead61d21 72 }
BillyGrande 1:924bead61d21 73 else { pc.printf("Heater operation is suspended! If you want to resume operation press switch 4\n");}
BillyGrande 1:924bead61d21 74 }
BillyGrande 1:924bead61d21 75 else if(cT>=TSUL) {
BillyGrande 1:924bead61d21 76 htr=0;
BillyGrande 1:924bead61d21 77 pc.printf("Heater deactivated\n");
BillyGrande 1:924bead61d21 78 pc.printf("Expecting temperature to fall\n");
BillyGrande 1:924bead61d21 79 }
BillyGrande 1:924bead61d21 80 }
BillyGrande 1:924bead61d21 81
BillyGrande 1:924bead61d21 82 bool temp_control(){
BillyGrande 1:924bead61d21 83 bool temp_state = true; //Assume normal temperature
BillyGrande 1:924bead61d21 84 float cT = calc_temp(); //Calculate temperature
BillyGrande 1:924bead61d21 85 print_temp(cT); //Print temp to lcd and pc
BillyGrande 1:924bead61d21 86 temp_state = connection_check(cT); //connection check
BillyGrande 1:924bead61d21 87 temp_state = malfunction_check(cT); //Malfunction check
BillyGrande 1:924bead61d21 88 temp_state = sensor_out_of_water_check(cT); //Sensor out of water check
BillyGrande 1:924bead61d21 89 heater_control(cT); //Heater control
BillyGrande 1:924bead61d21 90 wait(3); //Introduce a 3" delay in order for the changes to have an effect
BillyGrande 1:924bead61d21 91 cT = calc_temp(); //Calculate temperature again
BillyGrande 1:924bead61d21 92 if (cT<TALL) {
BillyGrande 1:924bead61d21 93 temp_state = false;
BillyGrande 1:924bead61d21 94 pc.printf("Temperature really low! Heater must be activated!\n");
BillyGrande 1:924bead61d21 95 if(htr==1) pc.printf("Heater already activated so beaker is exposed to freezing conditions");
BillyGrande 1:924bead61d21 96 }
BillyGrande 1:924bead61d21 97 if (cT>TAUL) {
BillyGrande 1:924bead61d21 98 temp_state = false;
BillyGrande 1:924bead61d21 99 pc.printf("Temperature really high!\n");
BillyGrande 1:924bead61d21 100 if(htr==0) pc.printf("Heater already deactivated so beaker is exposed to an external heating source or heater is powered directly from the mains!\nPlease check heater connection!");
BillyGrande 1:924bead61d21 101 }
BillyGrande 1:924bead61d21 102 pc.printf("\n");
BillyGrande 1:924bead61d21 103 return temp_state;
BillyGrande 1:924bead61d21 104 }