Program for the water play project for the course Software Testing Practical 2016 given at the VU University

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

Committer:
sbouber1
Date:
Tue Jun 14 15:31:30 2016 +0000
Revision:
35:c9261391a995
Parent:
13:1323e2c0b697
Child:
43:0abb54448b75
Heating in PIDController

Who changed what in which revision?

UserRevisionLine numberNew contents of line
6366295 0:dab140a197e0 1 #include "TemperatureController.h"
sbouber1 13:1323e2c0b697 2 #include "testing.h"
6366295 0:dab140a197e0 3
6366295 0:dab140a197e0 4 AnalogIn temperature_sensor(p20);
6366295 0:dab140a197e0 5
sbouber1 10:fd4670ec0806 6 float TemperatureController::getValue() {
sbouber1 10:fd4670ec0806 7 return this->temperature;
6366295 0:dab140a197e0 8 }
6366295 0:dab140a197e0 9
sbouber1 10:fd4670ec0806 10 std::string TemperatureController::get_name() {
sbouber1 10:fd4670ec0806 11 return "TemperatureController";
sbouber1 9:b3674516729d 12 }
sbouber1 9:b3674516729d 13
sbouber1 9:b3674516729d 14 void TemperatureController::update() {
sbouber1 9:b3674516729d 15 this->temperature = readSensor();
sbouber1 9:b3674516729d 16 }
sbouber1 9:b3674516729d 17
6366295 0:dab140a197e0 18 // Function reads sensor values and averages N sensor values
6366295 0:dab140a197e0 19 float TemperatureController::readSensor()
6366295 0:dab140a197e0 20 {
6366295 1:eb527bc93b62 21 float analogin_value = 0;
6366295 1:eb527bc93b62 22 float temperature = 0;
6366295 0:dab140a197e0 23
sbouber1 9:b3674516729d 24 Timer t;
sbouber1 9:b3674516729d 25
sbouber1 9:b3674516729d 26 t.start();
sbouber1 9:b3674516729d 27
6366295 0:dab140a197e0 28 // Read 0-1.0 value
6366295 0:dab140a197e0 29 for(int i = 0; i < N; i++)
6366295 0:dab140a197e0 30 {
6366295 0:dab140a197e0 31 analogin_value += temperature_sensor.read();
sbouber1 9:b3674516729d 32 Thread::wait(100);
6366295 0:dab140a197e0 33 }
sbouber1 9:b3674516729d 34
sbouber1 9:b3674516729d 35 t.stop();
sbouber1 9:b3674516729d 36
sbouber1 9:b3674516729d 37 printf("We did %d averages of temperature in %d ms\r\n", N, t.read_ms());
sbouber1 9:b3674516729d 38
sbouber1 9:b3674516729d 39 // Average N sensor values
6366295 0:dab140a197e0 40 analogin_value /= (float) N;
6366295 0:dab140a197e0 41
6366295 0:dab140a197e0 42 temperature = analoginToCelsius(analogin_value);
6366295 0:dab140a197e0 43
6366295 0:dab140a197e0 44 return temperature;
6366295 0:dab140a197e0 45 }
6366295 0:dab140a197e0 46
6366295 0:dab140a197e0 47 // Function converts average sensor value to ppt
6366295 0:dab140a197e0 48 float TemperatureController::analoginToCelsius(float analogin_value)
6366295 0:dab140a197e0 49 {
6366295 1:eb527bc93b62 50 float voltage = 0;
6366295 1:eb527bc93b62 51 float offset = 0;
6366295 1:eb527bc93b62 52 float rt = 0;
6366295 1:eb527bc93b62 53 float logrt = 0;
6366295 1:eb527bc93b62 54 float temperature = 0;
6366295 0:dab140a197e0 55
6366295 0:dab140a197e0 56 // Convert average sensor value to value with range 0-5.0v
6366295 1:eb527bc93b62 57 voltage = analogin_value * 3.3f * (5.0f/3.0f);
6366295 1:eb527bc93b62 58 //voltage /= 5.0;
6366295 1:eb527bc93b62 59 //voltage *= 1023;
6366295 1:eb527bc93b62 60
6366295 1:eb527bc93b62 61 offset = getOffset(voltage);
6366295 1:eb527bc93b62 62
6366295 1:eb527bc93b62 63 voltage += offset;
6366295 0:dab140a197e0 64
6366295 0:dab140a197e0 65 // Calcuate R_T
6366295 0:dab140a197e0 66 rt = (15000.0f * voltage) / (4.8f - voltage);
6366295 0:dab140a197e0 67
6366295 0:dab140a197e0 68 logrt = log(rt);
6366295 0:dab140a197e0 69 temperature = 1.0 / (0.00102119 + (0.000222468 * logrt) + (0.000000133342 * logrt*logrt*logrt));
6366295 0:dab140a197e0 70
6366295 0:dab140a197e0 71 temperature = temperature - 273.15;
6366295 0:dab140a197e0 72
6366295 0:dab140a197e0 73 return temperature;
6366295 0:dab140a197e0 74 }
6366295 0:dab140a197e0 75
6366295 1:eb527bc93b62 76 float TemperatureController::getOffset(float voltage)
6366295 1:eb527bc93b62 77 {
6366295 1:eb527bc93b62 78 float denominator_voltage, numerator_voltage, ratio, interpolation;
6366295 1:eb527bc93b62 79
6366295 1:eb527bc93b62 80 float voltage_table[15] = {1.43, 1.55, 1.53, 1.56, 1.62, 1.65, 1.67, 1.73, 1.75, 1.80, 1.82, 1.88, 1.93, 1.98, 2.00};
6366295 1:eb527bc93b62 81 float offset_table[15] = {0.33, 0.33, 0.40, 0.40, 0.39, 0.42, 0.43, 0.41, 0.45, 0.50, 0.54, 0.53, 0.52, 0.53, 0.56};
6366295 1:eb527bc93b62 82
6366295 1:eb527bc93b62 83 for(int i = 0; i < 14; i++)
6366295 1:eb527bc93b62 84 {
6366295 1:eb527bc93b62 85 if(voltage_table[i] <= voltage && voltage_table[i+1] >= voltage)
6366295 1:eb527bc93b62 86 {
6366295 1:eb527bc93b62 87 denominator_voltage = voltage_table[i+1] - voltage_table[i];
6366295 1:eb527bc93b62 88 numerator_voltage = voltage - voltage_table[i];
6366295 1:eb527bc93b62 89
6366295 1:eb527bc93b62 90 if(denominator_voltage != 0)
6366295 1:eb527bc93b62 91 {
6366295 1:eb527bc93b62 92 ratio = numerator_voltage / denominator_voltage;
6366295 1:eb527bc93b62 93
6366295 1:eb527bc93b62 94 interpolation = ((offset_table[i+1] - offset_table[i]) * ratio) + offset_table[i];
6366295 1:eb527bc93b62 95
6366295 1:eb527bc93b62 96 return interpolation;
6366295 1:eb527bc93b62 97 } else {
6366295 1:eb527bc93b62 98 return offset_table[i];
6366295 1:eb527bc93b62 99 }
6366295 1:eb527bc93b62 100 }
6366295 1:eb527bc93b62 101 }
6366295 1:eb527bc93b62 102
6366295 1:eb527bc93b62 103 if(voltage_table[14] < voltage)
6366295 1:eb527bc93b62 104 {
6366295 1:eb527bc93b62 105 return offset_table[14];
6366295 1:eb527bc93b62 106 } else {
6366295 1:eb527bc93b62 107 return offset_table[0];
6366295 1:eb527bc93b62 108 }
6366295 0:dab140a197e0 109 }