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@2:f6ebc225f232, 2016-06-07 (annotated)
- Committer:
- 6366295
- Date:
- Tue Jun 07 11:28:49 2016 +0000
- Revision:
- 2:f6ebc225f232
- Parent:
- 1:eb527bc93b62
- Child:
- 6:067e999b9c6e
Added motor controller
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 | |
6366295 | 0:dab140a197e0 | 3 | AnalogIn temperature_sensor(p20); |
6366295 | 2:f6ebc225f232 | 4 | DigitalOut heater(p18); |
6366295 | 0:dab140a197e0 | 5 | |
6366295 | 0:dab140a197e0 | 6 | TemperatureController::TemperatureController() |
6366295 | 0:dab140a197e0 | 7 | { |
6366295 | 0:dab140a197e0 | 8 | printf("Initiate Temperature Controller\r\n"); |
6366295 | 0:dab140a197e0 | 9 | } |
6366295 | 0:dab140a197e0 | 10 | |
6366295 | 0:dab140a197e0 | 11 | // Function reads sensor values and averages N sensor values |
6366295 | 0:dab140a197e0 | 12 | float TemperatureController::readSensor() |
6366295 | 0:dab140a197e0 | 13 | { |
6366295 | 1:eb527bc93b62 | 14 | float analogin_value = 0; |
6366295 | 1:eb527bc93b62 | 15 | float temperature = 0; |
6366295 | 0:dab140a197e0 | 16 | |
6366295 | 0:dab140a197e0 | 17 | // Read 0-1.0 value |
6366295 | 0:dab140a197e0 | 18 | for(int i = 0; i < N; i++) |
6366295 | 0:dab140a197e0 | 19 | { |
6366295 | 0:dab140a197e0 | 20 | analogin_value += temperature_sensor.read(); |
6366295 | 0:dab140a197e0 | 21 | } |
6366295 | 0:dab140a197e0 | 22 | // Average 10 sensor values |
6366295 | 0:dab140a197e0 | 23 | analogin_value /= (float) N; |
6366295 | 0:dab140a197e0 | 24 | |
6366295 | 0:dab140a197e0 | 25 | temperature = analoginToCelsius(analogin_value); |
6366295 | 0:dab140a197e0 | 26 | |
6366295 | 0:dab140a197e0 | 27 | return temperature; |
6366295 | 0:dab140a197e0 | 28 | } |
6366295 | 0:dab140a197e0 | 29 | |
6366295 | 0:dab140a197e0 | 30 | // Function converts average sensor value to ppt |
6366295 | 0:dab140a197e0 | 31 | float TemperatureController::analoginToCelsius(float analogin_value) |
6366295 | 0:dab140a197e0 | 32 | { |
6366295 | 1:eb527bc93b62 | 33 | float voltage = 0; |
6366295 | 1:eb527bc93b62 | 34 | float offset = 0; |
6366295 | 1:eb527bc93b62 | 35 | float rt = 0; |
6366295 | 1:eb527bc93b62 | 36 | float logrt = 0; |
6366295 | 1:eb527bc93b62 | 37 | float temperature = 0; |
6366295 | 0:dab140a197e0 | 38 | |
6366295 | 0:dab140a197e0 | 39 | // Convert average sensor value to value with range 0-5.0v |
6366295 | 1:eb527bc93b62 | 40 | voltage = analogin_value * 3.3f * (5.0f/3.0f); |
6366295 | 1:eb527bc93b62 | 41 | //voltage /= 5.0; |
6366295 | 1:eb527bc93b62 | 42 | //voltage *= 1023; |
6366295 | 1:eb527bc93b62 | 43 | |
6366295 | 1:eb527bc93b62 | 44 | offset = getOffset(voltage); |
6366295 | 1:eb527bc93b62 | 45 | |
6366295 | 1:eb527bc93b62 | 46 | voltage += offset; |
6366295 | 0:dab140a197e0 | 47 | |
6366295 | 0:dab140a197e0 | 48 | // Calcuate R_T |
6366295 | 0:dab140a197e0 | 49 | rt = (15000.0f * voltage) / (4.8f - voltage); |
6366295 | 0:dab140a197e0 | 50 | |
6366295 | 0:dab140a197e0 | 51 | logrt = log(rt); |
6366295 | 0:dab140a197e0 | 52 | temperature = 1.0 / (0.00102119 + (0.000222468 * logrt) + (0.000000133342 * logrt*logrt*logrt)); |
6366295 | 0:dab140a197e0 | 53 | |
6366295 | 0:dab140a197e0 | 54 | temperature = temperature - 273.15; |
6366295 | 0:dab140a197e0 | 55 | |
6366295 | 0:dab140a197e0 | 56 | return temperature; |
6366295 | 0:dab140a197e0 | 57 | } |
6366295 | 0:dab140a197e0 | 58 | |
6366295 | 0:dab140a197e0 | 59 | // Function prints temperature sensor value in celsius |
6366295 | 1:eb527bc93b62 | 60 | void TemperatureController::displayTemperature() |
6366295 | 0:dab140a197e0 | 61 | { |
6366295 | 1:eb527bc93b62 | 62 | float temperature = 0; |
6366295 | 0:dab140a197e0 | 63 | |
6366295 | 0:dab140a197e0 | 64 | temperature = readSensor(); |
6366295 | 0:dab140a197e0 | 65 | |
6366295 | 0:dab140a197e0 | 66 | printf("Temperature: %f Celsius\r\n", temperature); |
6366295 | 1:eb527bc93b62 | 67 | } |
6366295 | 1:eb527bc93b62 | 68 | |
6366295 | 1:eb527bc93b62 | 69 | float TemperatureController::getOffset(float voltage) |
6366295 | 1:eb527bc93b62 | 70 | { |
6366295 | 1:eb527bc93b62 | 71 | float denominator_voltage, numerator_voltage, ratio, interpolation; |
6366295 | 1:eb527bc93b62 | 72 | |
6366295 | 1:eb527bc93b62 | 73 | 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 | 74 | 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 | 75 | |
6366295 | 1:eb527bc93b62 | 76 | for(int i = 0; i < 14; i++) |
6366295 | 1:eb527bc93b62 | 77 | { |
6366295 | 1:eb527bc93b62 | 78 | if(voltage_table[i] <= voltage && voltage_table[i+1] >= voltage) |
6366295 | 1:eb527bc93b62 | 79 | { |
6366295 | 1:eb527bc93b62 | 80 | denominator_voltage = voltage_table[i+1] - voltage_table[i]; |
6366295 | 1:eb527bc93b62 | 81 | numerator_voltage = voltage - voltage_table[i]; |
6366295 | 1:eb527bc93b62 | 82 | |
6366295 | 1:eb527bc93b62 | 83 | if(denominator_voltage != 0) |
6366295 | 1:eb527bc93b62 | 84 | { |
6366295 | 1:eb527bc93b62 | 85 | ratio = numerator_voltage / denominator_voltage; |
6366295 | 1:eb527bc93b62 | 86 | |
6366295 | 1:eb527bc93b62 | 87 | interpolation = ((offset_table[i+1] - offset_table[i]) * ratio) + offset_table[i]; |
6366295 | 1:eb527bc93b62 | 88 | |
6366295 | 1:eb527bc93b62 | 89 | return interpolation; |
6366295 | 1:eb527bc93b62 | 90 | } else { |
6366295 | 1:eb527bc93b62 | 91 | return offset_table[i]; |
6366295 | 1:eb527bc93b62 | 92 | } |
6366295 | 1:eb527bc93b62 | 93 | } |
6366295 | 1:eb527bc93b62 | 94 | } |
6366295 | 1:eb527bc93b62 | 95 | |
6366295 | 1:eb527bc93b62 | 96 | if(voltage_table[14] < voltage) |
6366295 | 1:eb527bc93b62 | 97 | { |
6366295 | 1:eb527bc93b62 | 98 | return offset_table[14]; |
6366295 | 1:eb527bc93b62 | 99 | } else { |
6366295 | 1:eb527bc93b62 | 100 | return offset_table[0]; |
6366295 | 1:eb527bc93b62 | 101 | } |
6366295 | 1:eb527bc93b62 | 102 | } |
6366295 | 1:eb527bc93b62 | 103 | |
6366295 | 1:eb527bc93b62 | 104 | void TemperatureController::controlHeater() |
6366295 | 1:eb527bc93b62 | 105 | { |
6366295 | 1:eb527bc93b62 | 106 | if(readSensor() < 32.0f) |
6366295 | 1:eb527bc93b62 | 107 | { |
6366295 | 1:eb527bc93b62 | 108 | heater = 1; |
6366295 | 1:eb527bc93b62 | 109 | } else { |
6366295 | 1:eb527bc93b62 | 110 | heater = 0; |
6366295 | 1:eb527bc93b62 | 111 | } |
6366295 | 1:eb527bc93b62 | 112 | |
6366295 | 0:dab140a197e0 | 113 | } |