Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
salinity.cpp
00001 #include "salinity.h" 00002 #include "general_control.h" 00003 #include "TextLCD.h" 00004 #include "motors.h" 00005 00006 AnalogIn sal(p20); 00007 extern Serial pc; 00008 extern float cbV; extern int bC; extern int pw_csv; extern int sw_csv; 00009 extern int pwTC; extern int pwTV; extern int swTC; extern int swTV; 00010 extern bool rfl_flag, sal_suspension; 00011 extern TextLCD lcd; 00012 00013 00014 //Salinity calculation 00015 float calc_sal(float voltage) { 00016 return 15.1747217178 * voltage - 2.89491343498; 00017 } 00018 00019 float calc_sal2(float voltage) { 00020 return 16.3 * voltage; 00021 } 00022 00023 float calc_finalsal(float s1, float s2) { 00024 return 14.259 + 6.795*s1 -5.148*s2 -1.3; 00025 } 00026 00027 float calc_salinity(){ 00028 float s1, s2, sfinal, sensor_volt = 1.6 * 3.3 * sal.read();; 00029 s1=calc_sal(sensor_volt); 00030 s2=calc_sal2(sensor_volt); 00031 sfinal=calc_finalsal(s1,s2); 00032 return sfinal; 00033 } 00034 00035 void print_water_limits() { 00036 pc.printf("MAX volume of water that can be added: %.3f ml\n", bC-cbV); 00037 pc.printf("MAX volume of water that can be extracted: %.3f ml\n", cbV); 00038 } 00039 00040 void syringe_capacity_control(char motor, int target) { 00041 if (motor=='a' && pw_csv<target) { //Pure water syringe does not currently have target ml of water 00042 while (!rfl_flag) {wait(0.1);} //Refill needed - Wait untill rfl_flag is raised 00043 rfl_flag = false; 00044 move_motors(motor, "left", SDC - pw_csv); //Refill the syringe with 25ml with water 00045 } 00046 if (motor=='b' && sw_csv<target) { //Pure water syringe does not currently have target ml of water 00047 while (!rfl_flag) {wait(0.1);} //Refill needed - Wait untill rfl_flag is raised 00048 rfl_flag = false; 00049 move_motors(motor, "left", SDC - sw_csv); //Refill the syringe with water 00050 } 00051 } 00052 00053 void valve_warning_messages(int vol, bool pure, int pos, bool refill) { 00054 if(refill) pc.printf("Refill of the %s water syringe is needed\n", pure ? "pure":"salted"); 00055 else pc.printf("Going to add %d ml of %s water to the beaker\n", SDC, pure ? "pure":"salted"); 00056 pc.printf("Make sure the %s water valve is at designated position %d!!\n", pure ? "pure":"salted", pos); 00057 pc.printf("Operation has been suspended. Press switch 3 to resume\n"); 00058 } 00059 00060 void add_water(int vol, bool pure) { 00061 int i; 00062 char motor = pure ? 'a':'b'; //select the proper motor 00063 syringe_capacity_control(motor, SDC); //make sure that the syringe has enough water 00064 if (vol>SDC) { //if more than SDC ml are needed, refill will be required 00065 for (i=0; i<vol/SDC; i++) { //vol div SDC refills needed 00066 valve_warning_messages(SDC, pure, 1, false); //print valve warning messages 00067 pc.printf("rfl_flag in add_water = %s\n", rfl_flag ? "true":"false"); 00068 while (!rfl_flag) {wait(0.1);} //Wait until switch 3 has been pressed 00069 pc.printf("Operation has been resumed\n"); 00070 rfl_flag = false; 00071 move_motors(motor, "right", SDC); //add SDC ml of water to the beaker 00072 cbV += SDC; //Update current beaker volume 00073 pc.printf("New beaker volume: %.3f\n",cbV); 00074 print_water_limits(); //Print the new water limits 00075 valve_warning_messages(SDC, pure, 1, true); //print valve warning messages 00076 while (!rfl_flag) {wait(0.1);} //Wait untill switch 3 has been pressed 00077 pc.printf("Operation has been resumed\n"); 00078 rfl_flag = false; //Reset flag 00079 move_motors(motor, "left", SDC); //Refill the syringe with 25ml with water 00080 capacity_control(); //Capacity control 00081 } 00082 move_motors(motor, "right", vol % SDC); //Add the remaining vol modulo SDC ml with water - no syringe capacity control needed because already full 00083 cbV += vol % SDC; //Update current beaker volume 00084 print_water_limits(); //Print the water limits 00085 capacity_control(); //Capacity control 00086 } 00087 else { 00088 move_motors(motor, "right", vol); //Add the needed water (no refill needed) 00089 cbV += vol; //Update current beaker volume 00090 print_water_limits(); //Print the water limits 00091 capacity_control(); //Capacity control 00092 } 00093 } 00094 00095 void print_salinity(float sal) { 00096 pc.printf("Salinity = %.3f\n", sal); 00097 lcd.cls(); 00098 lcd.printf("Sal: %.3f\n", sal); 00099 } 00100 00101 void print_system_status() { 00102 pc.printf("bC = %d\n", bC); 00103 pc.printf("cbV = %.3f\n", cbV); 00104 pc.printf("pwTC = %d\n", pwTC); 00105 pc.printf("pwTV = %d\n", pwTV); 00106 pc.printf("swTC = %d\n", swTC); 00107 pc.printf("swTV = %d\n", swTV); 00108 } 00109 00110 bool sal_sensor_out_of_water_check(float sl) { 00111 if(sl<SSOOW) { 00112 pc.printf("Salinity extremely low. Highly likely that the sensor is out of the water\n"); 00113 pc.printf("Please check the salinity sensor position\n"); 00114 sal_suspension = true; //Suspend heater 00115 pc.printf("For safety reasons, system operation has been suspended. If everything is OK, resume operation by pressing switch 4\n"); 00116 while(sal_suspension) {wait(0.1);} //Wait until switch 4 is pressed 00117 return false; 00118 } 00119 return true; 00120 } 00121 00122 bool sal_connection_check(float sl) { 00123 if(SCLLL<sl && sl<SCLUL) { 00124 pc.printf("Salinity really low! Highly likely that the salinity sensor connection is lost\n"); 00125 pc.printf("Please check the salinity sensor connection\n"); 00126 sal_suspension = true; //Suspend heater 00127 pc.printf("For safety reasons, system operation has been suspended. If everything is OK, resume operation by pressing switch 4\n"); 00128 while(sal_suspension) {wait(0.1);} //Wait until switch 4 is pressed 00129 return false; 00130 } 00131 return true; 00132 } 00133 00134 /*bool sal_malfunction_check(float sl) { 00135 //// 00136 return true; 00137 }*/ 00138 00139 bool salinity_control() { 00140 bool sal_state = true; 00141 float pwv = 0, swv = 0, salt; //pwv=Pure Water Volume (ml), swv = Salted Water Volume (ml), salt (gr) 00142 float cbs = calc_salinity(); //Current salinity 00143 print_salinity(cbs); 00144 sal_state = sal_connection_check(cbs); //connection check 00145 //sal_state = sal_malfunction_check(cbs); //Malfunction check 00146 sal_state = sal_sensor_out_of_water_check(cbs); //Sensor out of water check 00147 cbs = calc_salinity(); //Everything ok, take the value of salinity now 00148 00149 if (cbs>=SSUL) { //pw case 00150 sal_state = false; 00151 pwv = cbs*cbV/DS - cbV; //how many ml? 00152 pc.printf("%.3f ml of pure water need to be added to the beaker\n", pwv); 00153 pc.printf("rfl_flag right before adding water = %s\n", rfl_flag ? "true":"false"); 00154 add_water(pwv,true); //add pure water 00155 } 00156 else if (cbs<=SSLL) { //sw case 00157 sal_state = false; 00158 salt = cbV*cbs/1000; 00159 swv = cbV-50*salt/4.8; ///how many ml? (Applicable ONLY for DS=20) 00160 pc.printf("%.3f ml of salted water need to be added to the beaker\n", swv); 00161 add_water(swv,false); //add salted water 00162 } 00163 pc.printf("\n"); 00164 return sal_state; 00165 }
Generated on Wed Jul 13 2022 10:07:54 by
1.7.2