Water control / Mbed 2 deprecated Water_pump

Dependencies:   DRV88255 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers general_control.cpp Source File

general_control.cpp

00001 #include "TextLCD.h"
00002 #include "general_control.h"
00003 #include "temp.h"
00004 
00005 Serial pc(USBTX, USBRX); // tx, rx
00006 TextLCD lcd(p25, p26, p24, p23, p22, p21); // rs, e, A4-A7
00007 InterruptIn rfl_swt(p6), rsm_htr(p5);                                           //Refill switch, resume heater
00008 DigitalOut led1(p28), led2(p29), buzzer(p30);                                   //led1 (Green-normal operation), led2 (under/overflow), buzzer
00009 
00010 
00011 int bC, pw_csv, sw_csv, pwTC, pwTV, swTC, swTV;                                 //Beaker Capacity, Current syringe pure water volume (ml), Current syringe salted water volume (ml),
00012                                                                                 //Pure water tank capacity (ml), Pure water tank volume (ml), Salted water tank capacity (ml), Salted water tank volume (ml)
00013 float cbV, air_temp;                                                            //Current beaker volume (ml), air temperature (celcius)
00014 bool rfl_flag, htr_suspension, vol_state, sal_suspension;                       //Refill flag, heater suspension flag, state of mixture's volume
00015 
00016 //Interrupt handlers
00017 void refill() {
00018     rfl_flag = true;
00019 }
00020 
00021 void resume_operations() {
00022     htr_suspension = false;
00023     sal_suspension = false;
00024 }
00025 
00026 void change_volume() {
00027     char buf[6];
00028     pc.gets(buf, 6);                                                        //Get the extra/less volume
00029     cbV += (float) atoi(buf);                                                       //Apply the change in volume
00030     pc.printf("I recieved %s at the change volume function\n", buf);
00031     capacity_control();                                                     //Check for over/underflow  
00032 }
00033 
00034 
00035 //Initialization Functions
00036 void set_bC() {
00037     char str[6];
00038     pc.gets(str, 6);                                                            //Get the extra/less volume
00039     bC = atoi(str);
00040     pc.printf("Mbed received beaker's capacity: %d ml\n", bC);
00041 }
00042 
00043 void set_cbV() {
00044     char str[6];
00045     pc.gets(str, 6);
00046     cbV = atof(str);
00047     pc.printf("Mbed received current beaker's volume: %.3f ml\n", cbV);
00048 }
00049  
00050 void set_pwTC() {
00051     char str[6];
00052     pc.gets(str, 6);
00053     pwTC = atoi(str);
00054     pc.printf("Mbed received pure water tank's capacity: %d ml\n", pwTC);
00055 }
00056  
00057 void set_pwTV() {                                                                    //Set pure water tank volume 
00058     char str[6];
00059     pc.gets(str, 6);
00060     pwTV = atoi(str);
00061     pc.printf("Mbed received pure water tank's volume: %d ml\n", pwTV);
00062  
00063 }
00064  
00065 void set_swTC() {
00066     char str[6];
00067     pc.gets(str, 6);
00068     swTC = atoi(str);
00069     pc.printf("Mbed received pure water tank's capacity: %d ml\n", swTC);
00070 }
00071  
00072 void set_swTV() {
00073    char str[6];
00074     pc.gets(str, 6);
00075     swTV = atoi(str);
00076     pc.printf("Mbed received pure water tank's volume: %d ml\n", swTV);
00077 }
00078 
00079 
00080 void initialization() {
00081     buzzer = 0;                                                                 //Silence buzzer
00082     pw_csv = SDC;                                                               //Syringes start with 25ml deploying water capacity
00083     sw_csv = SDC;
00084     vol_state=true;                                                             //Assume normal starting state
00085     rfl_flag = false;                                                           //Not refilling atm
00086     htr_suspension = false;                                                     //No heater suspension
00087     sal_suspension = false;                                                     //No salinity operations suspended
00088     rfl_swt.rise(&refill);                                                      //Attach refill switch to respective function
00089     rsm_htr.rise(&resume_operations);                                           //Attach resume operations
00090     set_bC();                                                                   //Set the beaker capacity
00091     set_cbV();                                                                  //Set current beaker volume
00092     set_pwTC();                                                                 //Set pure water tank capacity
00093     set_pwTV();                                                                 //Set pure water tank volume
00094     set_swTC();                                                                 //Set salted water tank capacity
00095     set_swTV();                                                                 //Set salted water tank volume
00096     air_temp = take_air_temp();
00097     pc.printf("air temp = %.3f\n", air_temp);
00098     pc.attach(change_volume);                                                   //extra/less volume in the beaker
00099     pc.printf("Initialization completed!\n");
00100 }
00101 
00102 void normality_test(bool sal_state, bool temp_state){
00103     if (sal_state && temp_state && vol_state) {led1=1; led2=0; pc.printf("Normal operation\n");}
00104     else {led1=0; led2=1;}     
00105 }
00106 
00107 void danger() {
00108     led2 = 1; //switch the led on, by setting the output to logic 1
00109     wait(0.1); //wait 0.2 seconds
00110     led2 = 0; //switch the led off
00111     wait(0.1);
00112     buzzer = 1;
00113     wait(0.1);
00114     buzzer = 0; 
00115     wait(0.1);
00116 }
00117 
00118 void capacity_control() {
00119     if (cbV > bC*BHC) {
00120         lcd.cls();
00121         lcd.printf("CBV > 80%% Cap.");
00122         lcd.printf("\nAbort operation!");
00123         while(1) {
00124             pc.printf("Overflow danger! Current Beaker Volume over 80%% capacity!\nABORT OPERATION!");
00125             danger();
00126         }
00127     }
00128     if (cbV < bC*BLC) {
00129         lcd.cls();
00130         lcd.printf("CBV < 20%% Cap.");
00131         lcd.printf("\nAbort operation!");
00132         while(1) {
00133             pc.printf("Underflow danger! Current Beaker Volume under 20%% capacity!\nABORT OPERATION!");
00134             danger();   
00135         }     
00136     }
00137     pc.printf("Everythink OK with capacity control\n");
00138 }