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

Committer:
joran
Date:
Mon Jun 13 13:57:38 2016 +0000
Revision:
26:966bad4c6365
Parent:
11:1a0a8fd74bc0
Child:
32:1e4919a44196
Salinity controller has a function to calculate milliliter needed to get to 7 ppt.

Who changed what in which revision?

UserRevisionLine numberNew 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
sbouber1 10:fd4670ec0806 5 void SalinityController::update() {
sbouber1 10:fd4670ec0806 6 this->salinity = SalinityController::getAdjustedPPT();
sbouber1 10:fd4670ec0806 7 }
sbouber1 10:fd4670ec0806 8
sbouber1 10:fd4670ec0806 9 float SalinityController::getValue() {
sbouber1 10:fd4670ec0806 10 return this->salinity;
6366295 0:dab140a197e0 11 }
6366295 0:dab140a197e0 12
6366295 0:dab140a197e0 13 // Function reads sensor values and averages N sensor values
6366295 0:dab140a197e0 14 float SalinityController::readSensor()
6366295 0:dab140a197e0 15 {
6366295 0:dab140a197e0 16 float analogin_value = 0;
6366295 0:dab140a197e0 17 float ppt = 0;
6366295 0:dab140a197e0 18
6366295 0:dab140a197e0 19 // Read 0-1.0 value
6366295 0:dab140a197e0 20 for(int i = 0; i < N; i++)
6366295 0:dab140a197e0 21 {
6366295 0:dab140a197e0 22 analogin_value += salinity_sensor.read();
6366295 0:dab140a197e0 23 }
6366295 0:dab140a197e0 24 // Average 10 sensor values
6366295 0:dab140a197e0 25 analogin_value /= (float) N;
6366295 0:dab140a197e0 26
6366295 0:dab140a197e0 27 ppt = analoginToPPT(analogin_value);
6366295 0:dab140a197e0 28
6366295 0:dab140a197e0 29 return ppt;
6366295 0:dab140a197e0 30 }
6366295 0:dab140a197e0 31
6366295 0:dab140a197e0 32 // Function converts average sensor value to ppt
6366295 0:dab140a197e0 33 float SalinityController::analoginToPPT(float analogin_value)
6366295 0:dab140a197e0 34 {
6366295 0:dab140a197e0 35 float voltage = 0;
6366295 0:dab140a197e0 36 float ppt = 0;
6366295 0:dab140a197e0 37
6366295 0:dab140a197e0 38 // Convert average sensor value to value with range 0-5.0v
6366295 0:dab140a197e0 39 voltage = analogin_value * 3.3f * (5.0f/3.0f);
6366295 0:dab140a197e0 40
joran 6:067e999b9c6e 41 // printf("Sensor Voltage: %f \t", voltage);
6366295 1:eb527bc93b62 42
6366295 0:dab140a197e0 43 // Convert voltage to ppt
6366295 4:4011a1562a77 44 // ppt = voltage * 16.3;
6366295 4:4011a1562a77 45 ppt = voltage * 15.1747217178 - 2.89491343498;
6366295 0:dab140a197e0 46
6366295 0:dab140a197e0 47 return ppt;
6366295 0:dab140a197e0 48 }
6366295 0:dab140a197e0 49
joran 6:067e999b9c6e 50 float SalinityController::getPPT()
joran 6:067e999b9c6e 51 {
joran 6:067e999b9c6e 52 float ppt = 0;
joran 6:067e999b9c6e 53
joran 6:067e999b9c6e 54 ppt = readSensor();
joran 6:067e999b9c6e 55
joran 6:067e999b9c6e 56 return ppt;
joran 6:067e999b9c6e 57 }
joran 6:067e999b9c6e 58
joran 6:067e999b9c6e 59 float SalinityController::getVoltage()
joran 6:067e999b9c6e 60 {
joran 6:067e999b9c6e 61 float voltage = 0;
joran 6:067e999b9c6e 62
joran 6:067e999b9c6e 63 float analogin_value = 0;
joran 7:8b3aef52aa7b 64
joran 6:067e999b9c6e 65
joran 6:067e999b9c6e 66 // Read 0-1.0 value
joran 6:067e999b9c6e 67 for(int i = 0; i < N; i++)
joran 6:067e999b9c6e 68 {
joran 6:067e999b9c6e 69 analogin_value += salinity_sensor.read();
joran 6:067e999b9c6e 70 }
joran 6:067e999b9c6e 71 // Average 10 sensor values
joran 6:067e999b9c6e 72 analogin_value /= (float) N;
joran 6:067e999b9c6e 73 voltage = analogin_value * 3.3f * (5.0f/3.0f);
joran 6:067e999b9c6e 74
joran 6:067e999b9c6e 75 return voltage;
joran 6:067e999b9c6e 76 }
joran 6:067e999b9c6e 77
joran 7:8b3aef52aa7b 78 float SalinityController::voltToSensor(float inputvolt)
joran 7:8b3aef52aa7b 79 {
joran 7:8b3aef52aa7b 80 float slope = 0.7931723;
joran 7:8b3aef52aa7b 81 float intercept = 0.0050561;
joran 7:8b3aef52aa7b 82
joran 7:8b3aef52aa7b 83 float offset = (slope * inputvolt) - intercept;
joran 7:8b3aef52aa7b 84
joran 7:8b3aef52aa7b 85 return inputvolt + offset;
joran 7:8b3aef52aa7b 86 }
joran 7:8b3aef52aa7b 87
joran 7:8b3aef52aa7b 88 float SalinityController::sensorToPPT(float inputvolt)
joran 7:8b3aef52aa7b 89 {
joran 7:8b3aef52aa7b 90 float minimum = 0.03778499;
joran 7:8b3aef52aa7b 91 if (inputvolt <= minimum) return 0.00;
joran 7:8b3aef52aa7b 92
joran 7:8b3aef52aa7b 93 float slope = 11.53368;
joran 7:8b3aef52aa7b 94 float intercept = 0.43580;
joran 7:8b3aef52aa7b 95
joran 7:8b3aef52aa7b 96 float ppt = (slope * inputvolt) - intercept;
joran 7:8b3aef52aa7b 97
joran 7:8b3aef52aa7b 98 return ppt;
joran 7:8b3aef52aa7b 99 }
joran 7:8b3aef52aa7b 100
joran 7:8b3aef52aa7b 101 float SalinityController::getAdjustedPPT()
joran 7:8b3aef52aa7b 102 {
joran 7:8b3aef52aa7b 103 return sensorToPPT(voltToSensor(getVoltage()));
joran 7:8b3aef52aa7b 104 }
6366295 0:dab140a197e0 105 // Function prints salinity sensor value in ppt
6366295 0:dab140a197e0 106 void SalinityController::displayPPT()
6366295 0:dab140a197e0 107 {
6366295 0:dab140a197e0 108 float ppt = 0;
6366295 0:dab140a197e0 109
6366295 0:dab140a197e0 110 ppt = readSensor();
6366295 0:dab140a197e0 111
6366295 0:dab140a197e0 112 printf("Salinity: %f ppt\t", ppt);
sbouber1 10:fd4670ec0806 113 }
sbouber1 10:fd4670ec0806 114
sbouber1 11:1a0a8fd74bc0 115 std::string SalinityController::get_name() {
sbouber1 10:fd4670ec0806 116 return "SalinityController";
joran 26:966bad4c6365 117 }
joran 26:966bad4c6365 118
joran 26:966bad4c6365 119 float SalinityController::getMl(float gramsinbody, float volumeinbody)
joran 26:966bad4c6365 120 {
joran 26:966bad4c6365 121
joran 26:966bad4c6365 122 float solvolume = 10; // 10ml
joran 26:966bad4c6365 123
joran 26:966bad4c6365 124 float idealpptconstant = 0.007f; //7 ppt / 1000
joran 26:966bad4c6365 125 float x1 = volumeinbody * idealpptconstant;
joran 26:966bad4c6365 126 float x2 = solvolume * idealpptconstant;
joran 26:966bad4c6365 127
joran 26:966bad4c6365 128 x1 = x1 - gramsinbody;
joran 26:966bad4c6365 129 x2 = 1 - x2;
joran 26:966bad4c6365 130
joran 26:966bad4c6365 131 float outputml = (x1 / x2 * solvolume);
joran 26:966bad4c6365 132
joran 26:966bad4c6365 133 return outputml; // amount in ml to get 7 ppt.
joran 26:966bad4c6365 134
6366295 0:dab140a197e0 135 }