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@58:b5f0c0f305ff, 2016-06-19 (annotated)
- Committer:
- sbouber1
- Date:
- Sun Jun 19 22:39:16 2016 +0000
- Revision:
- 58:b5f0c0f305ff
- Parent:
- 43:0abb54448b75
- Child:
- 66:133398875949
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
6366295 | 0:dab140a197e0 | 1 | #include "TemperatureController.h" |
6366295 | 0:dab140a197e0 | 2 | |
sbouber1 | 58:b5f0c0f305ff | 3 | static AnalogIn temperature_sensor(p20); |
6366295 | 0:dab140a197e0 | 4 | |
sbouber1 | 10:fd4670ec0806 | 5 | float TemperatureController::getValue() { |
sbouber1 | 10:fd4670ec0806 | 6 | return this->temperature; |
6366295 | 0:dab140a197e0 | 7 | } |
6366295 | 0:dab140a197e0 | 8 | |
sbouber1 | 58:b5f0c0f305ff | 9 | std::string TemperatureController::getName() { |
sbouber1 | 10:fd4670ec0806 | 10 | return "TemperatureController"; |
sbouber1 | 9:b3674516729d | 11 | } |
sbouber1 | 9:b3674516729d | 12 | |
sbouber1 | 9:b3674516729d | 13 | void TemperatureController::update() { |
sbouber1 | 9:b3674516729d | 14 | this->temperature = readSensor(); |
sbouber1 | 9:b3674516729d | 15 | } |
sbouber1 | 9:b3674516729d | 16 | |
sbouber1 | 58:b5f0c0f305ff | 17 | // Function reads sensor values and averages NUM_MEASUREMENTS sensor values, as defined in settings.h |
sbouber1 | 58:b5f0c0f305ff | 18 | float TemperatureController::readSensor() { |
6366295 | 1:eb527bc93b62 | 19 | float analogin_value = 0; |
6366295 | 1:eb527bc93b62 | 20 | float temperature = 0; |
6366295 | 0:dab140a197e0 | 21 | |
6366295 | 0:dab140a197e0 | 22 | // Read 0-1.0 value |
sbouber1 | 58:b5f0c0f305ff | 23 | for(int i = 0; i < NUM_MEASUREMENTS; i++) { |
6366295 | 0:dab140a197e0 | 24 | analogin_value += temperature_sensor.read(); |
sbouber1 | 58:b5f0c0f305ff | 25 | Thread::wait(MEASUREMENT_DELAY); |
6366295 | 0:dab140a197e0 | 26 | } |
sbouber1 | 9:b3674516729d | 27 | |
sbouber1 | 9:b3674516729d | 28 | // Average N sensor values |
sbouber1 | 58:b5f0c0f305ff | 29 | analogin_value /= (float) NUM_MEASUREMENTS; |
6366295 | 0:dab140a197e0 | 30 | |
6366295 | 0:dab140a197e0 | 31 | temperature = analoginToCelsius(analogin_value); |
6366295 | 0:dab140a197e0 | 32 | |
6366295 | 0:dab140a197e0 | 33 | return temperature; |
6366295 | 0:dab140a197e0 | 34 | } |
6366295 | 0:dab140a197e0 | 35 | |
6366295 | 0:dab140a197e0 | 36 | // Function converts average sensor value to ppt |
sbouber1 | 58:b5f0c0f305ff | 37 | float TemperatureController::analoginToCelsius(float analogin_value) { |
6366295 | 1:eb527bc93b62 | 38 | float voltage = 0; |
6366295 | 1:eb527bc93b62 | 39 | float offset = 0; |
6366295 | 1:eb527bc93b62 | 40 | float rt = 0; |
6366295 | 1:eb527bc93b62 | 41 | float logrt = 0; |
6366295 | 1:eb527bc93b62 | 42 | float temperature = 0; |
6366295 | 0:dab140a197e0 | 43 | |
6366295 | 0:dab140a197e0 | 44 | // Convert average sensor value to value with range 0-5.0v |
6366295 | 1:eb527bc93b62 | 45 | voltage = analogin_value * 3.3f * (5.0f/3.0f); |
joran | 43:0abb54448b75 | 46 | |
6366295 | 1:eb527bc93b62 | 47 | |
6366295 | 1:eb527bc93b62 | 48 | offset = getOffset(voltage); |
6366295 | 1:eb527bc93b62 | 49 | voltage += offset; |
6366295 | 0:dab140a197e0 | 50 | |
6366295 | 0:dab140a197e0 | 51 | // Calcuate R_T |
6366295 | 0:dab140a197e0 | 52 | rt = (15000.0f * voltage) / (4.8f - voltage); |
6366295 | 0:dab140a197e0 | 53 | |
6366295 | 0:dab140a197e0 | 54 | logrt = log(rt); |
6366295 | 0:dab140a197e0 | 55 | temperature = 1.0 / (0.00102119 + (0.000222468 * logrt) + (0.000000133342 * logrt*logrt*logrt)); |
6366295 | 0:dab140a197e0 | 56 | |
6366295 | 0:dab140a197e0 | 57 | temperature = temperature - 273.15; |
6366295 | 0:dab140a197e0 | 58 | |
6366295 | 0:dab140a197e0 | 59 | return temperature; |
6366295 | 0:dab140a197e0 | 60 | } |
6366295 | 0:dab140a197e0 | 61 | |
sbouber1 | 58:b5f0c0f305ff | 62 | float TemperatureController::getOffset(float voltage) { |
6366295 | 1:eb527bc93b62 | 63 | float denominator_voltage, numerator_voltage, ratio, interpolation; |
6366295 | 1:eb527bc93b62 | 64 | |
6366295 | 1:eb527bc93b62 | 65 | 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 | 66 | 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 | 67 | |
sbouber1 | 58:b5f0c0f305ff | 68 | for(int i = 0; i < 14; i++) { |
sbouber1 | 58:b5f0c0f305ff | 69 | if(voltage_table[i] <= voltage && voltage_table[i+1] >= voltage) { |
6366295 | 1:eb527bc93b62 | 70 | denominator_voltage = voltage_table[i+1] - voltage_table[i]; |
6366295 | 1:eb527bc93b62 | 71 | numerator_voltage = voltage - voltage_table[i]; |
6366295 | 1:eb527bc93b62 | 72 | |
sbouber1 | 58:b5f0c0f305ff | 73 | if(denominator_voltage != 0) { |
6366295 | 1:eb527bc93b62 | 74 | ratio = numerator_voltage / denominator_voltage; |
6366295 | 1:eb527bc93b62 | 75 | |
6366295 | 1:eb527bc93b62 | 76 | interpolation = ((offset_table[i+1] - offset_table[i]) * ratio) + offset_table[i]; |
6366295 | 1:eb527bc93b62 | 77 | |
6366295 | 1:eb527bc93b62 | 78 | return interpolation; |
6366295 | 1:eb527bc93b62 | 79 | } else { |
6366295 | 1:eb527bc93b62 | 80 | return offset_table[i]; |
6366295 | 1:eb527bc93b62 | 81 | } |
6366295 | 1:eb527bc93b62 | 82 | } |
6366295 | 1:eb527bc93b62 | 83 | } |
6366295 | 1:eb527bc93b62 | 84 | |
sbouber1 | 58:b5f0c0f305ff | 85 | if(voltage_table[14] < voltage) { |
6366295 | 1:eb527bc93b62 | 86 | return offset_table[14]; |
6366295 | 1:eb527bc93b62 | 87 | } else { |
6366295 | 1:eb527bc93b62 | 88 | return offset_table[0]; |
6366295 | 1:eb527bc93b62 | 89 | } |
6366295 | 0:dab140a197e0 | 90 | } |