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
TemperatureController.cpp@13:1323e2c0b697, 2016-06-11 (annotated)
- Committer:
- sbouber1
- Date:
- Sat Jun 11 13:19:20 2016 +0000
- Revision:
- 13:1323e2c0b697
- Parent:
- 10:fd4670ec0806
- Child:
- 35:c9261391a995
.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
6366295 | 0:dab140a197e0 | 1 | #include "TemperatureController.h" |
sbouber1 | 9:b3674516729d | 2 | #include "rtos.h" |
sbouber1 | 13:1323e2c0b697 | 3 | #include "testing.h" |
6366295 | 0:dab140a197e0 | 4 | |
6366295 | 0:dab140a197e0 | 5 | AnalogIn temperature_sensor(p20); |
6366295 | 2:f6ebc225f232 | 6 | DigitalOut heater(p18); |
6366295 | 0:dab140a197e0 | 7 | |
sbouber1 | 10:fd4670ec0806 | 8 | float TemperatureController::getValue() { |
sbouber1 | 10:fd4670ec0806 | 9 | return this->temperature; |
6366295 | 0:dab140a197e0 | 10 | } |
6366295 | 0:dab140a197e0 | 11 | |
sbouber1 | 10:fd4670ec0806 | 12 | std::string TemperatureController::get_name() { |
sbouber1 | 10:fd4670ec0806 | 13 | return "TemperatureController"; |
sbouber1 | 9:b3674516729d | 14 | } |
sbouber1 | 9:b3674516729d | 15 | |
sbouber1 | 9:b3674516729d | 16 | void TemperatureController::update() { |
sbouber1 | 9:b3674516729d | 17 | this->temperature = readSensor(); |
sbouber1 | 9:b3674516729d | 18 | } |
sbouber1 | 9:b3674516729d | 19 | |
sbouber1 | 13:1323e2c0b697 | 20 | bool TemperatureController::is_heating() { |
sbouber1 | 13:1323e2c0b697 | 21 | return this->heating; |
sbouber1 | 13:1323e2c0b697 | 22 | } |
sbouber1 | 13:1323e2c0b697 | 23 | |
sbouber1 | 13:1323e2c0b697 | 24 | void TemperatureController::set_heating(bool enabled) { |
sbouber1 | 13:1323e2c0b697 | 25 | if(enabled == this->heating) return; |
sbouber1 | 13:1323e2c0b697 | 26 | |
sbouber1 | 13:1323e2c0b697 | 27 | this->heating = enabled; |
sbouber1 | 13:1323e2c0b697 | 28 | if(enabled) { |
sbouber1 | 13:1323e2c0b697 | 29 | #ifdef RUN_TESTS |
sbouber1 | 13:1323e2c0b697 | 30 | printf("Should set heater to 1\r\n"); |
sbouber1 | 13:1323e2c0b697 | 31 | #else |
sbouber1 | 13:1323e2c0b697 | 32 | heater = 1; |
sbouber1 | 13:1323e2c0b697 | 33 | #endif |
sbouber1 | 13:1323e2c0b697 | 34 | } else { |
sbouber1 | 13:1323e2c0b697 | 35 | #ifdef RUN_TESTS |
sbouber1 | 13:1323e2c0b697 | 36 | printf("Should set heater to 0\r\n"); |
sbouber1 | 13:1323e2c0b697 | 37 | #else |
sbouber1 | 13:1323e2c0b697 | 38 | heater = 0; |
sbouber1 | 13:1323e2c0b697 | 39 | #endif |
sbouber1 | 13:1323e2c0b697 | 40 | } |
sbouber1 | 13:1323e2c0b697 | 41 | } |
sbouber1 | 13:1323e2c0b697 | 42 | |
6366295 | 0:dab140a197e0 | 43 | // Function reads sensor values and averages N sensor values |
6366295 | 0:dab140a197e0 | 44 | float TemperatureController::readSensor() |
6366295 | 0:dab140a197e0 | 45 | { |
6366295 | 1:eb527bc93b62 | 46 | float analogin_value = 0; |
6366295 | 1:eb527bc93b62 | 47 | float temperature = 0; |
6366295 | 0:dab140a197e0 | 48 | |
sbouber1 | 9:b3674516729d | 49 | Timer t; |
sbouber1 | 9:b3674516729d | 50 | |
sbouber1 | 9:b3674516729d | 51 | t.start(); |
sbouber1 | 9:b3674516729d | 52 | |
6366295 | 0:dab140a197e0 | 53 | // Read 0-1.0 value |
6366295 | 0:dab140a197e0 | 54 | for(int i = 0; i < N; i++) |
6366295 | 0:dab140a197e0 | 55 | { |
6366295 | 0:dab140a197e0 | 56 | analogin_value += temperature_sensor.read(); |
sbouber1 | 9:b3674516729d | 57 | Thread::wait(100); |
6366295 | 0:dab140a197e0 | 58 | } |
sbouber1 | 9:b3674516729d | 59 | |
sbouber1 | 9:b3674516729d | 60 | t.stop(); |
sbouber1 | 9:b3674516729d | 61 | |
sbouber1 | 9:b3674516729d | 62 | printf("We did %d averages of temperature in %d ms\r\n", N, t.read_ms()); |
sbouber1 | 9:b3674516729d | 63 | |
sbouber1 | 9:b3674516729d | 64 | // Average N sensor values |
6366295 | 0:dab140a197e0 | 65 | analogin_value /= (float) N; |
6366295 | 0:dab140a197e0 | 66 | |
6366295 | 0:dab140a197e0 | 67 | temperature = analoginToCelsius(analogin_value); |
6366295 | 0:dab140a197e0 | 68 | |
6366295 | 0:dab140a197e0 | 69 | return temperature; |
6366295 | 0:dab140a197e0 | 70 | } |
6366295 | 0:dab140a197e0 | 71 | |
6366295 | 0:dab140a197e0 | 72 | // Function converts average sensor value to ppt |
6366295 | 0:dab140a197e0 | 73 | float TemperatureController::analoginToCelsius(float analogin_value) |
6366295 | 0:dab140a197e0 | 74 | { |
6366295 | 1:eb527bc93b62 | 75 | float voltage = 0; |
6366295 | 1:eb527bc93b62 | 76 | float offset = 0; |
6366295 | 1:eb527bc93b62 | 77 | float rt = 0; |
6366295 | 1:eb527bc93b62 | 78 | float logrt = 0; |
6366295 | 1:eb527bc93b62 | 79 | float temperature = 0; |
6366295 | 0:dab140a197e0 | 80 | |
6366295 | 0:dab140a197e0 | 81 | // Convert average sensor value to value with range 0-5.0v |
6366295 | 1:eb527bc93b62 | 82 | voltage = analogin_value * 3.3f * (5.0f/3.0f); |
6366295 | 1:eb527bc93b62 | 83 | //voltage /= 5.0; |
6366295 | 1:eb527bc93b62 | 84 | //voltage *= 1023; |
6366295 | 1:eb527bc93b62 | 85 | |
6366295 | 1:eb527bc93b62 | 86 | offset = getOffset(voltage); |
6366295 | 1:eb527bc93b62 | 87 | |
6366295 | 1:eb527bc93b62 | 88 | voltage += offset; |
6366295 | 0:dab140a197e0 | 89 | |
6366295 | 0:dab140a197e0 | 90 | // Calcuate R_T |
6366295 | 0:dab140a197e0 | 91 | rt = (15000.0f * voltage) / (4.8f - voltage); |
6366295 | 0:dab140a197e0 | 92 | |
6366295 | 0:dab140a197e0 | 93 | logrt = log(rt); |
6366295 | 0:dab140a197e0 | 94 | temperature = 1.0 / (0.00102119 + (0.000222468 * logrt) + (0.000000133342 * logrt*logrt*logrt)); |
6366295 | 0:dab140a197e0 | 95 | |
6366295 | 0:dab140a197e0 | 96 | temperature = temperature - 273.15; |
6366295 | 0:dab140a197e0 | 97 | |
6366295 | 0:dab140a197e0 | 98 | return temperature; |
6366295 | 0:dab140a197e0 | 99 | } |
6366295 | 0:dab140a197e0 | 100 | |
6366295 | 0:dab140a197e0 | 101 | // Function prints temperature sensor value in celsius |
6366295 | 1:eb527bc93b62 | 102 | void TemperatureController::displayTemperature() |
6366295 | 0:dab140a197e0 | 103 | { |
6366295 | 1:eb527bc93b62 | 104 | float temperature = 0; |
6366295 | 0:dab140a197e0 | 105 | |
6366295 | 0:dab140a197e0 | 106 | temperature = readSensor(); |
6366295 | 0:dab140a197e0 | 107 | |
6366295 | 0:dab140a197e0 | 108 | printf("Temperature: %f Celsius\r\n", temperature); |
6366295 | 1:eb527bc93b62 | 109 | } |
6366295 | 1:eb527bc93b62 | 110 | |
6366295 | 1:eb527bc93b62 | 111 | float TemperatureController::getOffset(float voltage) |
6366295 | 1:eb527bc93b62 | 112 | { |
6366295 | 1:eb527bc93b62 | 113 | float denominator_voltage, numerator_voltage, ratio, interpolation; |
6366295 | 1:eb527bc93b62 | 114 | |
6366295 | 1:eb527bc93b62 | 115 | 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 | 116 | 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 | 117 | |
6366295 | 1:eb527bc93b62 | 118 | for(int i = 0; i < 14; i++) |
6366295 | 1:eb527bc93b62 | 119 | { |
6366295 | 1:eb527bc93b62 | 120 | if(voltage_table[i] <= voltage && voltage_table[i+1] >= voltage) |
6366295 | 1:eb527bc93b62 | 121 | { |
6366295 | 1:eb527bc93b62 | 122 | denominator_voltage = voltage_table[i+1] - voltage_table[i]; |
6366295 | 1:eb527bc93b62 | 123 | numerator_voltage = voltage - voltage_table[i]; |
6366295 | 1:eb527bc93b62 | 124 | |
6366295 | 1:eb527bc93b62 | 125 | if(denominator_voltage != 0) |
6366295 | 1:eb527bc93b62 | 126 | { |
6366295 | 1:eb527bc93b62 | 127 | ratio = numerator_voltage / denominator_voltage; |
6366295 | 1:eb527bc93b62 | 128 | |
6366295 | 1:eb527bc93b62 | 129 | interpolation = ((offset_table[i+1] - offset_table[i]) * ratio) + offset_table[i]; |
6366295 | 1:eb527bc93b62 | 130 | |
6366295 | 1:eb527bc93b62 | 131 | return interpolation; |
6366295 | 1:eb527bc93b62 | 132 | } else { |
6366295 | 1:eb527bc93b62 | 133 | return offset_table[i]; |
6366295 | 1:eb527bc93b62 | 134 | } |
6366295 | 1:eb527bc93b62 | 135 | } |
6366295 | 1:eb527bc93b62 | 136 | } |
6366295 | 1:eb527bc93b62 | 137 | |
6366295 | 1:eb527bc93b62 | 138 | if(voltage_table[14] < voltage) |
6366295 | 1:eb527bc93b62 | 139 | { |
6366295 | 1:eb527bc93b62 | 140 | return offset_table[14]; |
6366295 | 1:eb527bc93b62 | 141 | } else { |
6366295 | 1:eb527bc93b62 | 142 | return offset_table[0]; |
6366295 | 1:eb527bc93b62 | 143 | } |
6366295 | 1:eb527bc93b62 | 144 | } |
6366295 | 1:eb527bc93b62 | 145 | |
6366295 | 1:eb527bc93b62 | 146 | void TemperatureController::controlHeater() |
6366295 | 1:eb527bc93b62 | 147 | { |
sbouber1 | 13:1323e2c0b697 | 148 | this->set_heating(this->getValue() < 32.0f); |
6366295 | 1:eb527bc93b62 | 149 | |
6366295 | 0:dab140a197e0 | 150 | } |