start

Dependencies:   DRV88255 mbed

Committer:
BillyGrande
Date:
Tue Jun 27 21:13:09 2017 +0000
Revision:
2:5dd057d67746
Parent:
1:924bead61d21
start

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BillyGrande 1:924bead61d21 1 #include "salinity.h"
BillyGrande 1:924bead61d21 2 #include "general_control.h"
BillyGrande 1:924bead61d21 3 #include "TextLCD.h"
BillyGrande 1:924bead61d21 4 #include "motors.h"
BillyGrande 1:924bead61d21 5
BillyGrande 1:924bead61d21 6 AnalogIn sal(p20);
BillyGrande 1:924bead61d21 7 extern Serial pc;
BillyGrande 1:924bead61d21 8 extern float cbV; extern int bC; extern int pw_csv; extern int sw_csv;
BillyGrande 1:924bead61d21 9 extern int pwTC; extern int pwTV; extern int swTC; extern int swTV;
BillyGrande 1:924bead61d21 10 extern bool rfl_flag, sal_suspension;
BillyGrande 1:924bead61d21 11 extern TextLCD lcd;
BillyGrande 1:924bead61d21 12
BillyGrande 1:924bead61d21 13
BillyGrande 1:924bead61d21 14 //Salinity calculation
BillyGrande 1:924bead61d21 15 float calc_sal(float voltage) {
BillyGrande 1:924bead61d21 16 return 15.1747217178 * voltage - 2.89491343498;
BillyGrande 1:924bead61d21 17 }
BillyGrande 1:924bead61d21 18
BillyGrande 1:924bead61d21 19 float calc_sal2(float voltage) {
BillyGrande 1:924bead61d21 20 return 16.3 * voltage;
BillyGrande 1:924bead61d21 21 }
BillyGrande 1:924bead61d21 22
BillyGrande 1:924bead61d21 23 float calc_finalsal(float s1, float s2) {
BillyGrande 1:924bead61d21 24 return 14.259 + 6.795*s1 -5.148*s2 -1.3;
BillyGrande 1:924bead61d21 25 }
BillyGrande 1:924bead61d21 26
BillyGrande 1:924bead61d21 27 float calc_salinity(){
BillyGrande 1:924bead61d21 28 float s1, s2, sfinal, sensor_volt = 1.6 * 3.3 * sal.read();;
BillyGrande 1:924bead61d21 29 s1=calc_sal(sensor_volt);
BillyGrande 1:924bead61d21 30 s2=calc_sal2(sensor_volt);
BillyGrande 1:924bead61d21 31 sfinal=calc_finalsal(s1,s2);
BillyGrande 1:924bead61d21 32 return sfinal;
BillyGrande 1:924bead61d21 33 }
BillyGrande 1:924bead61d21 34
BillyGrande 1:924bead61d21 35 void print_water_limits() {
BillyGrande 1:924bead61d21 36 pc.printf("MAX volume of water that can be added: %.3f ml\n", bC-cbV);
BillyGrande 1:924bead61d21 37 pc.printf("MAX volume of water that can be extracted: %.3f ml\n", cbV);
BillyGrande 1:924bead61d21 38 }
BillyGrande 1:924bead61d21 39
BillyGrande 1:924bead61d21 40 void syringe_capacity_control(char motor, int target) {
BillyGrande 1:924bead61d21 41 if (motor=='a' && pw_csv<target) { //Pure water syringe does not currently have target ml of water
BillyGrande 1:924bead61d21 42 while (!rfl_flag) {wait(0.1);} //Refill needed - Wait untill rfl_flag is raised
BillyGrande 1:924bead61d21 43 rfl_flag = false;
BillyGrande 1:924bead61d21 44 move_motors(motor, "left", SDC - pw_csv); //Refill the syringe with 25ml with water
BillyGrande 1:924bead61d21 45 }
BillyGrande 1:924bead61d21 46 if (motor=='b' && sw_csv<target) { //Pure water syringe does not currently have target ml of water
BillyGrande 1:924bead61d21 47 while (!rfl_flag) {wait(0.1);} //Refill needed - Wait untill rfl_flag is raised
BillyGrande 1:924bead61d21 48 rfl_flag = false;
BillyGrande 1:924bead61d21 49 move_motors(motor, "left", SDC - sw_csv); //Refill the syringe with water
BillyGrande 1:924bead61d21 50 }
BillyGrande 1:924bead61d21 51 }
BillyGrande 1:924bead61d21 52
BillyGrande 1:924bead61d21 53 void valve_warning_messages(int vol, bool pure, int pos, bool refill) {
BillyGrande 1:924bead61d21 54 if(refill) pc.printf("Refill of the %s water syringe is needed\n", pure ? "pure":"salted");
BillyGrande 1:924bead61d21 55 else pc.printf("Going to add %d ml of %s water to the beaker\n", SDC, pure ? "pure":"salted");
BillyGrande 1:924bead61d21 56 pc.printf("Make sure the %s water valve is at designated position %d!!\n", pure ? "pure":"salted", pos);
BillyGrande 1:924bead61d21 57 pc.printf("Operation has been suspended. Press switch 3 to resume\n");
BillyGrande 1:924bead61d21 58 }
BillyGrande 1:924bead61d21 59
BillyGrande 1:924bead61d21 60 void add_water(int vol, bool pure) {
BillyGrande 1:924bead61d21 61 int i;
BillyGrande 1:924bead61d21 62 char motor = pure ? 'a':'b'; //select the proper motor
BillyGrande 1:924bead61d21 63 syringe_capacity_control(motor, SDC); //make sure that the syringe has enough water
BillyGrande 1:924bead61d21 64 if (vol>SDC) { //if more than SDC ml are needed, refill will be required
BillyGrande 1:924bead61d21 65 for (i=0; i<vol/SDC; i++) { //vol div SDC refills needed
BillyGrande 1:924bead61d21 66 valve_warning_messages(SDC, pure, 1, false); //print valve warning messages
BillyGrande 1:924bead61d21 67 pc.printf("rfl_flag in add_water = %s\n", rfl_flag ? "true":"false");
BillyGrande 1:924bead61d21 68 while (!rfl_flag) {wait(0.1);} //Wait until switch 3 has been pressed
BillyGrande 1:924bead61d21 69 pc.printf("Operation has been resumed\n");
BillyGrande 1:924bead61d21 70 rfl_flag = false;
BillyGrande 1:924bead61d21 71 move_motors(motor, "right", SDC); //add SDC ml of water to the beaker
BillyGrande 1:924bead61d21 72 cbV += SDC; //Update current beaker volume
BillyGrande 1:924bead61d21 73 pc.printf("New beaker volume: %.3f\n",cbV);
BillyGrande 1:924bead61d21 74 print_water_limits(); //Print the new water limits
BillyGrande 1:924bead61d21 75 valve_warning_messages(SDC, pure, 1, true); //print valve warning messages
BillyGrande 1:924bead61d21 76 while (!rfl_flag) {wait(0.1);} //Wait untill switch 3 has been pressed
BillyGrande 1:924bead61d21 77 pc.printf("Operation has been resumed\n");
BillyGrande 1:924bead61d21 78 rfl_flag = false; //Reset flag
BillyGrande 1:924bead61d21 79 move_motors(motor, "left", SDC); //Refill the syringe with 25ml with water
BillyGrande 1:924bead61d21 80 capacity_control(); //Capacity control
BillyGrande 1:924bead61d21 81 }
BillyGrande 1:924bead61d21 82 move_motors(motor, "right", vol % SDC); //Add the remaining vol modulo SDC ml with water - no syringe capacity control needed because already full
BillyGrande 1:924bead61d21 83 cbV += vol % SDC; //Update current beaker volume
BillyGrande 1:924bead61d21 84 print_water_limits(); //Print the water limits
BillyGrande 1:924bead61d21 85 capacity_control(); //Capacity control
BillyGrande 1:924bead61d21 86 }
BillyGrande 1:924bead61d21 87 else {
BillyGrande 1:924bead61d21 88 move_motors(motor, "right", vol); //Add the needed water (no refill needed)
BillyGrande 1:924bead61d21 89 cbV += vol; //Update current beaker volume
BillyGrande 1:924bead61d21 90 print_water_limits(); //Print the water limits
BillyGrande 1:924bead61d21 91 capacity_control(); //Capacity control
BillyGrande 1:924bead61d21 92 }
BillyGrande 1:924bead61d21 93 }
BillyGrande 1:924bead61d21 94
BillyGrande 1:924bead61d21 95 void print_salinity(float sal) {
BillyGrande 1:924bead61d21 96 pc.printf("Salinity = %.3f\n", sal);
BillyGrande 1:924bead61d21 97 lcd.cls();
BillyGrande 1:924bead61d21 98 lcd.printf("Sal: %.3f\n", sal);
BillyGrande 1:924bead61d21 99 }
BillyGrande 1:924bead61d21 100
BillyGrande 1:924bead61d21 101 void print_system_status() {
BillyGrande 1:924bead61d21 102 pc.printf("bC = %d\n", bC);
BillyGrande 1:924bead61d21 103 pc.printf("cbV = %.3f\n", cbV);
BillyGrande 1:924bead61d21 104 pc.printf("pwTC = %d\n", pwTC);
BillyGrande 1:924bead61d21 105 pc.printf("pwTV = %d\n", pwTV);
BillyGrande 1:924bead61d21 106 pc.printf("swTC = %d\n", swTC);
BillyGrande 1:924bead61d21 107 pc.printf("swTV = %d\n", swTV);
BillyGrande 1:924bead61d21 108 }
BillyGrande 1:924bead61d21 109
BillyGrande 1:924bead61d21 110 bool sal_sensor_out_of_water_check(float sl) {
BillyGrande 1:924bead61d21 111 if(sl<SSOOW) {
BillyGrande 1:924bead61d21 112 pc.printf("Salinity extremely low. Highly likely that the sensor is out of the water\n");
BillyGrande 1:924bead61d21 113 pc.printf("Please check the salinity sensor position\n");
BillyGrande 1:924bead61d21 114 sal_suspension = true; //Suspend heater
BillyGrande 1:924bead61d21 115 pc.printf("For safety reasons, system operation has been suspended. If everything is OK, resume operation by pressing switch 4\n");
BillyGrande 1:924bead61d21 116 while(sal_suspension) {wait(0.1);} //Wait until switch 4 is pressed
BillyGrande 1:924bead61d21 117 return false;
BillyGrande 1:924bead61d21 118 }
BillyGrande 1:924bead61d21 119 return true;
BillyGrande 1:924bead61d21 120 }
BillyGrande 1:924bead61d21 121
BillyGrande 1:924bead61d21 122 bool sal_connection_check(float sl) {
BillyGrande 1:924bead61d21 123 if(SCLLL<sl && sl<SCLUL) {
BillyGrande 1:924bead61d21 124 pc.printf("Salinity really low! Highly likely that the salinity sensor connection is lost\n");
BillyGrande 1:924bead61d21 125 pc.printf("Please check the salinity sensor connection\n");
BillyGrande 1:924bead61d21 126 sal_suspension = true; //Suspend heater
BillyGrande 1:924bead61d21 127 pc.printf("For safety reasons, system operation has been suspended. If everything is OK, resume operation by pressing switch 4\n");
BillyGrande 1:924bead61d21 128 while(sal_suspension) {wait(0.1);} //Wait until switch 4 is pressed
BillyGrande 1:924bead61d21 129 return false;
BillyGrande 1:924bead61d21 130 }
BillyGrande 1:924bead61d21 131 return true;
BillyGrande 1:924bead61d21 132 }
BillyGrande 1:924bead61d21 133
BillyGrande 1:924bead61d21 134 /*bool sal_malfunction_check(float sl) {
BillyGrande 1:924bead61d21 135 ////
BillyGrande 1:924bead61d21 136 return true;
BillyGrande 1:924bead61d21 137 }*/
BillyGrande 1:924bead61d21 138
BillyGrande 1:924bead61d21 139 bool salinity_control() {
BillyGrande 1:924bead61d21 140 bool sal_state = true;
BillyGrande 1:924bead61d21 141 float pwv = 0, swv = 0, salt; //pwv=Pure Water Volume (ml), swv = Salted Water Volume (ml), salt (gr)
BillyGrande 1:924bead61d21 142 float cbs = calc_salinity(); //Current salinity
BillyGrande 1:924bead61d21 143 print_salinity(cbs);
BillyGrande 1:924bead61d21 144 sal_state = sal_connection_check(cbs); //connection check
BillyGrande 1:924bead61d21 145 //sal_state = sal_malfunction_check(cbs); //Malfunction check
BillyGrande 1:924bead61d21 146 sal_state = sal_sensor_out_of_water_check(cbs); //Sensor out of water check
BillyGrande 1:924bead61d21 147 cbs = calc_salinity(); //Everything ok, take the value of salinity now
BillyGrande 1:924bead61d21 148
BillyGrande 1:924bead61d21 149 if (cbs>=SSUL) { //pw case
BillyGrande 1:924bead61d21 150 sal_state = false;
BillyGrande 1:924bead61d21 151 pwv = cbs*cbV/DS - cbV; //how many ml?
BillyGrande 1:924bead61d21 152 pc.printf("%.3f ml of pure water need to be added to the beaker\n", pwv);
BillyGrande 1:924bead61d21 153 pc.printf("rfl_flag right before adding water = %s\n", rfl_flag ? "true":"false");
BillyGrande 1:924bead61d21 154 add_water(pwv,true); //add pure water
BillyGrande 1:924bead61d21 155 }
BillyGrande 1:924bead61d21 156 else if (cbs<=SSLL) { //sw case
BillyGrande 1:924bead61d21 157 sal_state = false;
BillyGrande 1:924bead61d21 158 salt = cbV*cbs/1000;
BillyGrande 1:924bead61d21 159 swv = cbV-50*salt/4.8; ///how many ml? (Applicable ONLY for DS=20)
BillyGrande 1:924bead61d21 160 pc.printf("%.3f ml of salted water need to be added to the beaker\n", swv);
BillyGrande 1:924bead61d21 161 add_water(swv,false); //add salted water
BillyGrande 1:924bead61d21 162 }
BillyGrande 1:924bead61d21 163 pc.printf("\n");
BillyGrande 1:924bead61d21 164 return sal_state;
BillyGrande 1:924bead61d21 165 }