Water control
/
Water_pump
start
general_control.cpp@2:5dd057d67746, 2017-06-27 (annotated)
- Committer:
- BillyGrande
- Date:
- Tue Jun 27 21:13:09 2017 +0000
- Revision:
- 2:5dd057d67746
- Parent:
- 1:924bead61d21
start
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BillyGrande | 1:924bead61d21 | 1 | #include "TextLCD.h" |
BillyGrande | 1:924bead61d21 | 2 | #include "general_control.h" |
BillyGrande | 1:924bead61d21 | 3 | #include "temp.h" |
BillyGrande | 1:924bead61d21 | 4 | |
BillyGrande | 1:924bead61d21 | 5 | Serial pc(USBTX, USBRX); // tx, rx |
BillyGrande | 1:924bead61d21 | 6 | TextLCD lcd(p25, p26, p24, p23, p22, p21); // rs, e, A4-A7 |
BillyGrande | 1:924bead61d21 | 7 | InterruptIn rfl_swt(p6), rsm_htr(p5); //Refill switch, resume heater |
BillyGrande | 1:924bead61d21 | 8 | DigitalOut led1(p28), led2(p29), buzzer(p30); //led1 (Green-normal operation), led2 (under/overflow), buzzer |
BillyGrande | 1:924bead61d21 | 9 | |
BillyGrande | 1:924bead61d21 | 10 | |
BillyGrande | 1:924bead61d21 | 11 | int bC, pw_csv, sw_csv, pwTC, pwTV, swTC, swTV; //Beaker Capacity, Current syringe pure water volume (ml), Current syringe salted water volume (ml), |
BillyGrande | 1:924bead61d21 | 12 | //Pure water tank capacity (ml), Pure water tank volume (ml), Salted water tank capacity (ml), Salted water tank volume (ml) |
BillyGrande | 1:924bead61d21 | 13 | float cbV, air_temp; //Current beaker volume (ml), air temperature (celcius) |
BillyGrande | 1:924bead61d21 | 14 | bool rfl_flag, htr_suspension, vol_state, sal_suspension; //Refill flag, heater suspension flag, state of mixture's volume |
BillyGrande | 1:924bead61d21 | 15 | |
BillyGrande | 1:924bead61d21 | 16 | //Interrupt handlers |
BillyGrande | 1:924bead61d21 | 17 | void refill() { |
BillyGrande | 1:924bead61d21 | 18 | rfl_flag = true; |
BillyGrande | 1:924bead61d21 | 19 | } |
BillyGrande | 1:924bead61d21 | 20 | |
BillyGrande | 1:924bead61d21 | 21 | void resume_operations() { |
BillyGrande | 1:924bead61d21 | 22 | htr_suspension = false; |
BillyGrande | 1:924bead61d21 | 23 | sal_suspension = false; |
BillyGrande | 1:924bead61d21 | 24 | } |
BillyGrande | 1:924bead61d21 | 25 | |
BillyGrande | 1:924bead61d21 | 26 | void change_volume() { |
BillyGrande | 1:924bead61d21 | 27 | char buf[6]; |
BillyGrande | 1:924bead61d21 | 28 | pc.gets(buf, 6); //Get the extra/less volume |
BillyGrande | 1:924bead61d21 | 29 | cbV += (float) atoi(buf); //Apply the change in volume |
BillyGrande | 1:924bead61d21 | 30 | pc.printf("I recieved %s at the change volume function\n", buf); |
BillyGrande | 1:924bead61d21 | 31 | capacity_control(); //Check for over/underflow |
BillyGrande | 1:924bead61d21 | 32 | } |
BillyGrande | 1:924bead61d21 | 33 | |
BillyGrande | 1:924bead61d21 | 34 | |
BillyGrande | 1:924bead61d21 | 35 | //Initialization Functions |
BillyGrande | 1:924bead61d21 | 36 | void set_bC() { |
BillyGrande | 1:924bead61d21 | 37 | char str[6]; |
BillyGrande | 1:924bead61d21 | 38 | pc.gets(str, 6); //Get the extra/less volume |
BillyGrande | 1:924bead61d21 | 39 | bC = atoi(str); |
BillyGrande | 1:924bead61d21 | 40 | pc.printf("Mbed received beaker's capacity: %d ml\n", bC); |
BillyGrande | 1:924bead61d21 | 41 | } |
BillyGrande | 1:924bead61d21 | 42 | |
BillyGrande | 1:924bead61d21 | 43 | void set_cbV() { |
BillyGrande | 1:924bead61d21 | 44 | char str[6]; |
BillyGrande | 1:924bead61d21 | 45 | pc.gets(str, 6); |
BillyGrande | 1:924bead61d21 | 46 | cbV = atof(str); |
BillyGrande | 1:924bead61d21 | 47 | pc.printf("Mbed received current beaker's volume: %.3f ml\n", cbV); |
BillyGrande | 1:924bead61d21 | 48 | } |
BillyGrande | 1:924bead61d21 | 49 | |
BillyGrande | 1:924bead61d21 | 50 | void set_pwTC() { |
BillyGrande | 1:924bead61d21 | 51 | char str[6]; |
BillyGrande | 1:924bead61d21 | 52 | pc.gets(str, 6); |
BillyGrande | 1:924bead61d21 | 53 | pwTC = atoi(str); |
BillyGrande | 1:924bead61d21 | 54 | pc.printf("Mbed received pure water tank's capacity: %d ml\n", pwTC); |
BillyGrande | 1:924bead61d21 | 55 | } |
BillyGrande | 1:924bead61d21 | 56 | |
BillyGrande | 1:924bead61d21 | 57 | void set_pwTV() { //Set pure water tank volume |
BillyGrande | 1:924bead61d21 | 58 | char str[6]; |
BillyGrande | 1:924bead61d21 | 59 | pc.gets(str, 6); |
BillyGrande | 1:924bead61d21 | 60 | pwTV = atoi(str); |
BillyGrande | 1:924bead61d21 | 61 | pc.printf("Mbed received pure water tank's volume: %d ml\n", pwTV); |
BillyGrande | 1:924bead61d21 | 62 | |
BillyGrande | 1:924bead61d21 | 63 | } |
BillyGrande | 1:924bead61d21 | 64 | |
BillyGrande | 1:924bead61d21 | 65 | void set_swTC() { |
BillyGrande | 1:924bead61d21 | 66 | char str[6]; |
BillyGrande | 1:924bead61d21 | 67 | pc.gets(str, 6); |
BillyGrande | 1:924bead61d21 | 68 | swTC = atoi(str); |
BillyGrande | 1:924bead61d21 | 69 | pc.printf("Mbed received pure water tank's capacity: %d ml\n", swTC); |
BillyGrande | 1:924bead61d21 | 70 | } |
BillyGrande | 1:924bead61d21 | 71 | |
BillyGrande | 1:924bead61d21 | 72 | void set_swTV() { |
BillyGrande | 1:924bead61d21 | 73 | char str[6]; |
BillyGrande | 1:924bead61d21 | 74 | pc.gets(str, 6); |
BillyGrande | 1:924bead61d21 | 75 | swTV = atoi(str); |
BillyGrande | 1:924bead61d21 | 76 | pc.printf("Mbed received pure water tank's volume: %d ml\n", swTV); |
BillyGrande | 1:924bead61d21 | 77 | } |
BillyGrande | 1:924bead61d21 | 78 | |
BillyGrande | 1:924bead61d21 | 79 | |
BillyGrande | 1:924bead61d21 | 80 | void initialization() { |
BillyGrande | 1:924bead61d21 | 81 | buzzer = 0; //Silence buzzer |
BillyGrande | 1:924bead61d21 | 82 | pw_csv = SDC; //Syringes start with 25ml deploying water capacity |
BillyGrande | 1:924bead61d21 | 83 | sw_csv = SDC; |
BillyGrande | 1:924bead61d21 | 84 | vol_state=true; //Assume normal starting state |
BillyGrande | 1:924bead61d21 | 85 | rfl_flag = false; //Not refilling atm |
BillyGrande | 1:924bead61d21 | 86 | htr_suspension = false; //No heater suspension |
BillyGrande | 1:924bead61d21 | 87 | sal_suspension = false; //No salinity operations suspended |
BillyGrande | 1:924bead61d21 | 88 | rfl_swt.rise(&refill); //Attach refill switch to respective function |
BillyGrande | 1:924bead61d21 | 89 | rsm_htr.rise(&resume_operations); //Attach resume operations |
BillyGrande | 1:924bead61d21 | 90 | set_bC(); //Set the beaker capacity |
BillyGrande | 1:924bead61d21 | 91 | set_cbV(); //Set current beaker volume |
BillyGrande | 1:924bead61d21 | 92 | set_pwTC(); //Set pure water tank capacity |
BillyGrande | 1:924bead61d21 | 93 | set_pwTV(); //Set pure water tank volume |
BillyGrande | 1:924bead61d21 | 94 | set_swTC(); //Set salted water tank capacity |
BillyGrande | 1:924bead61d21 | 95 | set_swTV(); //Set salted water tank volume |
BillyGrande | 1:924bead61d21 | 96 | air_temp = take_air_temp(); |
BillyGrande | 1:924bead61d21 | 97 | pc.printf("air temp = %.3f\n", air_temp); |
BillyGrande | 1:924bead61d21 | 98 | pc.attach(change_volume); //extra/less volume in the beaker |
BillyGrande | 1:924bead61d21 | 99 | pc.printf("Initialization completed!\n"); |
BillyGrande | 1:924bead61d21 | 100 | } |
BillyGrande | 1:924bead61d21 | 101 | |
BillyGrande | 1:924bead61d21 | 102 | void normality_test(bool sal_state, bool temp_state){ |
BillyGrande | 1:924bead61d21 | 103 | if (sal_state && temp_state && vol_state) {led1=1; led2=0; pc.printf("Normal operation\n");} |
BillyGrande | 1:924bead61d21 | 104 | else {led1=0; led2=1;} |
BillyGrande | 1:924bead61d21 | 105 | } |
BillyGrande | 1:924bead61d21 | 106 | |
BillyGrande | 1:924bead61d21 | 107 | void danger() { |
BillyGrande | 1:924bead61d21 | 108 | led2 = 1; //switch the led on, by setting the output to logic 1 |
BillyGrande | 1:924bead61d21 | 109 | wait(0.1); //wait 0.2 seconds |
BillyGrande | 1:924bead61d21 | 110 | led2 = 0; //switch the led off |
BillyGrande | 1:924bead61d21 | 111 | wait(0.1); |
BillyGrande | 1:924bead61d21 | 112 | buzzer = 1; |
BillyGrande | 1:924bead61d21 | 113 | wait(0.1); |
BillyGrande | 1:924bead61d21 | 114 | buzzer = 0; |
BillyGrande | 1:924bead61d21 | 115 | wait(0.1); |
BillyGrande | 1:924bead61d21 | 116 | } |
BillyGrande | 1:924bead61d21 | 117 | |
BillyGrande | 1:924bead61d21 | 118 | void capacity_control() { |
BillyGrande | 1:924bead61d21 | 119 | if (cbV > bC*BHC) { |
BillyGrande | 1:924bead61d21 | 120 | lcd.cls(); |
BillyGrande | 1:924bead61d21 | 121 | lcd.printf("CBV > 80%% Cap."); |
BillyGrande | 1:924bead61d21 | 122 | lcd.printf("\nAbort operation!"); |
BillyGrande | 1:924bead61d21 | 123 | while(1) { |
BillyGrande | 1:924bead61d21 | 124 | pc.printf("Overflow danger! Current Beaker Volume over 80%% capacity!\nABORT OPERATION!"); |
BillyGrande | 1:924bead61d21 | 125 | danger(); |
BillyGrande | 1:924bead61d21 | 126 | } |
BillyGrande | 1:924bead61d21 | 127 | } |
BillyGrande | 1:924bead61d21 | 128 | if (cbV < bC*BLC) { |
BillyGrande | 1:924bead61d21 | 129 | lcd.cls(); |
BillyGrande | 1:924bead61d21 | 130 | lcd.printf("CBV < 20%% Cap."); |
BillyGrande | 1:924bead61d21 | 131 | lcd.printf("\nAbort operation!"); |
BillyGrande | 1:924bead61d21 | 132 | while(1) { |
BillyGrande | 1:924bead61d21 | 133 | pc.printf("Underflow danger! Current Beaker Volume under 20%% capacity!\nABORT OPERATION!"); |
BillyGrande | 1:924bead61d21 | 134 | danger(); |
BillyGrande | 1:924bead61d21 | 135 | } |
BillyGrande | 1:924bead61d21 | 136 | } |
BillyGrande | 1:924bead61d21 | 137 | pc.printf("Everythink OK with capacity control\n"); |
BillyGrande | 1:924bead61d21 | 138 | } |