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@58:b5f0c0f305ff, 2016-06-19 (annotated)
- Committer:
- sbouber1
- Date:
- Sun Jun 19 22:39:16 2016 +0000
- Revision:
- 58:b5f0c0f305ff
- Parent:
- 42:e7c726f9c6ed
- Child:
- 66:133398875949
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 | |
sbouber1 | 58:b5f0c0f305ff | 3 | static AnalogIn salinity_sensor(p19); |
6366295 | 0:dab140a197e0 | 4 | |
joran | 42:e7c726f9c6ed | 5 | //Read the sensor, update the local variable. |
sbouber1 | 10:fd4670ec0806 | 6 | void SalinityController::update() { |
sbouber1 | 10:fd4670ec0806 | 7 | this->salinity = SalinityController::getAdjustedPPT(); |
sbouber1 | 10:fd4670ec0806 | 8 | } |
sbouber1 | 10:fd4670ec0806 | 9 | |
joran | 42:e7c726f9c6ed | 10 | //Return the value, this function does not update the value. |
sbouber1 | 10:fd4670ec0806 | 11 | float SalinityController::getValue() { |
sbouber1 | 10:fd4670ec0806 | 12 | return this->salinity; |
6366295 | 0:dab140a197e0 | 13 | } |
6366295 | 0:dab140a197e0 | 14 | |
sbouber1 | 58:b5f0c0f305ff | 15 | //Read the voltage, get an average over NUM_MEASUREMENTS values defined in settings.h |
sbouber1 | 58:b5f0c0f305ff | 16 | float SalinityController::getVoltage() { |
joran | 42:e7c726f9c6ed | 17 | float voltage = 0; |
6366295 | 0:dab140a197e0 | 18 | float analogin_value = 0; |
6366295 | 0:dab140a197e0 | 19 | |
6366295 | 0:dab140a197e0 | 20 | // Read 0-1.0 value |
sbouber1 | 58:b5f0c0f305ff | 21 | for(int i = 0; i < NUM_MEASUREMENTS; i++) { |
6366295 | 0:dab140a197e0 | 22 | analogin_value += salinity_sensor.read(); |
sbouber1 | 58:b5f0c0f305ff | 23 | Thread::wait(MEASUREMENT_DELAY); |
6366295 | 0:dab140a197e0 | 24 | } |
6366295 | 0:dab140a197e0 | 25 | |
sbouber1 | 58:b5f0c0f305ff | 26 | analogin_value /= (float) NUM_MEASUREMENTS; |
joran | 6:067e999b9c6e | 27 | voltage = analogin_value * 3.3f * (5.0f/3.0f); |
joran | 6:067e999b9c6e | 28 | |
joran | 6:067e999b9c6e | 29 | return voltage; |
joran | 6:067e999b9c6e | 30 | } |
joran | 6:067e999b9c6e | 31 | |
joran | 42:e7c726f9c6ed | 32 | //Convert inputvolt to corrected sensor value |
sbouber1 | 58:b5f0c0f305ff | 33 | float SalinityController::voltToSensor(float inputvolt) { |
joran | 7:8b3aef52aa7b | 34 | float slope = 0.7931723; |
joran | 7:8b3aef52aa7b | 35 | float intercept = 0.0050561; |
joran | 7:8b3aef52aa7b | 36 | |
joran | 7:8b3aef52aa7b | 37 | float offset = (slope * inputvolt) - intercept; |
joran | 7:8b3aef52aa7b | 38 | |
joran | 7:8b3aef52aa7b | 39 | return inputvolt + offset; |
joran | 7:8b3aef52aa7b | 40 | } |
joran | 7:8b3aef52aa7b | 41 | |
joran | 42:e7c726f9c6ed | 42 | //Convert corrected sensor value to actual PPT |
sbouber1 | 58:b5f0c0f305ff | 43 | float SalinityController::sensorToPPT(float inputvolt) { |
joran | 7:8b3aef52aa7b | 44 | float slope = 11.53368; |
sbouber1 | 58:b5f0c0f305ff | 45 | float intercept = 0.43580; |
sbouber1 | 58:b5f0c0f305ff | 46 | float minimum = 0.03778499; |
joran | 7:8b3aef52aa7b | 47 | |
sbouber1 | 58:b5f0c0f305ff | 48 | if (inputvolt <= minimum) |
sbouber1 | 58:b5f0c0f305ff | 49 | return 0.00; |
joran | 7:8b3aef52aa7b | 50 | |
sbouber1 | 58:b5f0c0f305ff | 51 | return (slope * inputvolt) - intercept; |
joran | 7:8b3aef52aa7b | 52 | } |
joran | 7:8b3aef52aa7b | 53 | |
joran | 42:e7c726f9c6ed | 54 | //Chain the needed functions to get the proper PPT |
sbouber1 | 58:b5f0c0f305ff | 55 | float SalinityController::getAdjustedPPT() { |
joran | 7:8b3aef52aa7b | 56 | return sensorToPPT(voltToSensor(getVoltage())); |
joran | 7:8b3aef52aa7b | 57 | } |
joran | 42:e7c726f9c6ed | 58 | |
sbouber1 | 10:fd4670ec0806 | 59 | |
sbouber1 | 58:b5f0c0f305ff | 60 | std::string SalinityController::getName() { |
sbouber1 | 10:fd4670ec0806 | 61 | return "SalinityController"; |
joran | 26:966bad4c6365 | 62 | } |