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
SalinityController.cpp@7:8b3aef52aa7b, 2016-06-10 (annotated)
- Committer:
- joran
- Date:
- Fri Jun 10 12:29:33 2016 +0000
- Revision:
- 7:8b3aef52aa7b
- Parent:
- 6:067e999b9c6e
- Child:
- 10:fd4670ec0806
Salinity update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
6366295 | 0:dab140a197e0 | 1 | #include "SalinityController.h" |
6366295 | 0:dab140a197e0 | 2 | |
6366295 | 0:dab140a197e0 | 3 | AnalogIn salinity_sensor(p19); |
6366295 | 0:dab140a197e0 | 4 | |
6366295 | 0:dab140a197e0 | 5 | SalinityController::SalinityController() |
6366295 | 0:dab140a197e0 | 6 | { |
6366295 | 0:dab140a197e0 | 7 | printf("Initiate Salinity Controller\r\n"); |
6366295 | 0:dab140a197e0 | 8 | } |
6366295 | 0:dab140a197e0 | 9 | |
6366295 | 0:dab140a197e0 | 10 | // Function reads sensor values and averages N sensor values |
6366295 | 0:dab140a197e0 | 11 | float SalinityController::readSensor() |
6366295 | 0:dab140a197e0 | 12 | { |
6366295 | 0:dab140a197e0 | 13 | float analogin_value = 0; |
6366295 | 0:dab140a197e0 | 14 | float ppt = 0; |
6366295 | 0:dab140a197e0 | 15 | |
6366295 | 0:dab140a197e0 | 16 | // Read 0-1.0 value |
6366295 | 0:dab140a197e0 | 17 | for(int i = 0; i < N; i++) |
6366295 | 0:dab140a197e0 | 18 | { |
6366295 | 0:dab140a197e0 | 19 | analogin_value += salinity_sensor.read(); |
6366295 | 0:dab140a197e0 | 20 | } |
6366295 | 0:dab140a197e0 | 21 | // Average 10 sensor values |
6366295 | 0:dab140a197e0 | 22 | analogin_value /= (float) N; |
6366295 | 0:dab140a197e0 | 23 | |
6366295 | 0:dab140a197e0 | 24 | ppt = analoginToPPT(analogin_value); |
6366295 | 0:dab140a197e0 | 25 | |
6366295 | 0:dab140a197e0 | 26 | return ppt; |
6366295 | 0:dab140a197e0 | 27 | } |
6366295 | 0:dab140a197e0 | 28 | |
6366295 | 0:dab140a197e0 | 29 | // Function converts average sensor value to ppt |
6366295 | 0:dab140a197e0 | 30 | float SalinityController::analoginToPPT(float analogin_value) |
6366295 | 0:dab140a197e0 | 31 | { |
6366295 | 0:dab140a197e0 | 32 | float voltage = 0; |
6366295 | 0:dab140a197e0 | 33 | float ppt = 0; |
6366295 | 0:dab140a197e0 | 34 | |
6366295 | 0:dab140a197e0 | 35 | // Convert average sensor value to value with range 0-5.0v |
6366295 | 0:dab140a197e0 | 36 | voltage = analogin_value * 3.3f * (5.0f/3.0f); |
6366295 | 0:dab140a197e0 | 37 | |
joran | 6:067e999b9c6e | 38 | // printf("Sensor Voltage: %f \t", voltage); |
6366295 | 1:eb527bc93b62 | 39 | |
6366295 | 0:dab140a197e0 | 40 | // Convert voltage to ppt |
6366295 | 4:4011a1562a77 | 41 | // ppt = voltage * 16.3; |
6366295 | 4:4011a1562a77 | 42 | ppt = voltage * 15.1747217178 - 2.89491343498; |
6366295 | 0:dab140a197e0 | 43 | |
6366295 | 0:dab140a197e0 | 44 | return ppt; |
6366295 | 0:dab140a197e0 | 45 | } |
6366295 | 0:dab140a197e0 | 46 | |
joran | 6:067e999b9c6e | 47 | float SalinityController::getPPT() |
joran | 6:067e999b9c6e | 48 | { |
joran | 6:067e999b9c6e | 49 | float ppt = 0; |
joran | 6:067e999b9c6e | 50 | |
joran | 6:067e999b9c6e | 51 | ppt = readSensor(); |
joran | 6:067e999b9c6e | 52 | |
joran | 6:067e999b9c6e | 53 | return ppt; |
joran | 6:067e999b9c6e | 54 | } |
joran | 6:067e999b9c6e | 55 | |
joran | 6:067e999b9c6e | 56 | float SalinityController::getVoltage() |
joran | 6:067e999b9c6e | 57 | { |
joran | 6:067e999b9c6e | 58 | float voltage = 0; |
joran | 6:067e999b9c6e | 59 | |
joran | 6:067e999b9c6e | 60 | float analogin_value = 0; |
joran | 7:8b3aef52aa7b | 61 | |
joran | 6:067e999b9c6e | 62 | |
joran | 6:067e999b9c6e | 63 | // Read 0-1.0 value |
joran | 6:067e999b9c6e | 64 | for(int i = 0; i < N; i++) |
joran | 6:067e999b9c6e | 65 | { |
joran | 6:067e999b9c6e | 66 | analogin_value += salinity_sensor.read(); |
joran | 6:067e999b9c6e | 67 | } |
joran | 6:067e999b9c6e | 68 | // Average 10 sensor values |
joran | 6:067e999b9c6e | 69 | analogin_value /= (float) N; |
joran | 6:067e999b9c6e | 70 | voltage = analogin_value * 3.3f * (5.0f/3.0f); |
joran | 6:067e999b9c6e | 71 | |
joran | 6:067e999b9c6e | 72 | return voltage; |
joran | 6:067e999b9c6e | 73 | } |
joran | 6:067e999b9c6e | 74 | |
joran | 7:8b3aef52aa7b | 75 | float SalinityController::voltToSensor(float inputvolt) |
joran | 7:8b3aef52aa7b | 76 | { |
joran | 7:8b3aef52aa7b | 77 | float slope = 0.7931723; |
joran | 7:8b3aef52aa7b | 78 | float intercept = 0.0050561; |
joran | 7:8b3aef52aa7b | 79 | |
joran | 7:8b3aef52aa7b | 80 | float offset = (slope * inputvolt) - intercept; |
joran | 7:8b3aef52aa7b | 81 | |
joran | 7:8b3aef52aa7b | 82 | return inputvolt + offset; |
joran | 7:8b3aef52aa7b | 83 | } |
joran | 7:8b3aef52aa7b | 84 | |
joran | 7:8b3aef52aa7b | 85 | float SalinityController::sensorToPPT(float inputvolt) |
joran | 7:8b3aef52aa7b | 86 | { |
joran | 7:8b3aef52aa7b | 87 | float minimum = 0.03778499; |
joran | 7:8b3aef52aa7b | 88 | if (inputvolt <= minimum) return 0.00; |
joran | 7:8b3aef52aa7b | 89 | |
joran | 7:8b3aef52aa7b | 90 | float slope = 11.53368; |
joran | 7:8b3aef52aa7b | 91 | float intercept = 0.43580; |
joran | 7:8b3aef52aa7b | 92 | |
joran | 7:8b3aef52aa7b | 93 | float ppt = (slope * inputvolt) - intercept; |
joran | 7:8b3aef52aa7b | 94 | |
joran | 7:8b3aef52aa7b | 95 | return ppt; |
joran | 7:8b3aef52aa7b | 96 | } |
joran | 7:8b3aef52aa7b | 97 | |
joran | 7:8b3aef52aa7b | 98 | float SalinityController::getAdjustedPPT() |
joran | 7:8b3aef52aa7b | 99 | { |
joran | 7:8b3aef52aa7b | 100 | return sensorToPPT(voltToSensor(getVoltage())); |
joran | 7:8b3aef52aa7b | 101 | } |
6366295 | 0:dab140a197e0 | 102 | // Function prints salinity sensor value in ppt |
6366295 | 0:dab140a197e0 | 103 | void SalinityController::displayPPT() |
6366295 | 0:dab140a197e0 | 104 | { |
6366295 | 0:dab140a197e0 | 105 | float ppt = 0; |
6366295 | 0:dab140a197e0 | 106 | |
6366295 | 0:dab140a197e0 | 107 | ppt = readSensor(); |
6366295 | 0:dab140a197e0 | 108 | |
6366295 | 0:dab140a197e0 | 109 | printf("Salinity: %f ppt\t", ppt); |
6366295 | 0:dab140a197e0 | 110 | } |