Mario Simaremare
/
g3_waterplay
Salinity and temperature sensors are implemented in classes.
Tank.cpp@12:7a48b90b493e, 2016-06-22 (annotated)
- Committer:
- mariosimaremare
- Date:
- Wed Jun 22 12:34:37 2016 +0000
- Revision:
- 12:7a48b90b493e
- Parent:
- 11:dcaaf1bb21ce
- Child:
- 13:142a142a7ac5
Add procedure.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mariosimaremare | 3:7c648d1d8802 | 1 | /* |
mariosimaremare | 3:7c648d1d8802 | 2 | * G3: WATERPLAY |
mariosimaremare | 3:7c648d1d8802 | 3 | */ |
mariosimaremare | 3:7c648d1d8802 | 4 | |
mariosimaremare | 4:d8914369bf82 | 5 | #include "Printer.h" |
mariosimaremare | 3:7c648d1d8802 | 6 | #include "Tank.h" |
mariosimaremare | 3:7c648d1d8802 | 7 | #include "mbed.h" |
mariosimaremare | 3:7c648d1d8802 | 8 | |
mariosimaremare | 3:7c648d1d8802 | 9 | Tank::Tank( |
mariosimaremare | 4:d8914369bf82 | 10 | Printer &printer, |
mariosimaremare | 4:d8914369bf82 | 11 | DRV8825 &salinitySyringe, |
mariosimaremare | 7:46e65aeb4df2 | 12 | DRV8825 &waterSyringe, |
mariosimaremare | 11:dcaaf1bb21ce | 13 | Ping &pinger, |
mariosimaremare | 11:dcaaf1bb21ce | 14 | Flasher &alarm |
mariosimaremare | 3:7c648d1d8802 | 15 | ): |
mariosimaremare | 4:d8914369bf82 | 16 | _printer(printer), |
mariosimaremare | 4:d8914369bf82 | 17 | _salinitySyringe(salinitySyringe), |
mariosimaremare | 4:d8914369bf82 | 18 | _waterSyringe(waterSyringe), |
mariosimaremare | 7:46e65aeb4df2 | 19 | _pinger(pinger), |
mariosimaremare | 11:dcaaf1bb21ce | 20 | _alarm(alarm), |
mariosimaremare | 4:d8914369bf82 | 21 | _previous_direction_salt(-1), |
mariosimaremare | 4:d8914369bf82 | 22 | _previous_direction_pure(-1), |
mariosimaremare | 4:d8914369bf82 | 23 | _position_salt(14), |
mariosimaremare | 11:dcaaf1bb21ce | 24 | _position_pure(27), |
mariosimaremare | 11:dcaaf1bb21ce | 25 | _added_liquid(0.0) |
mariosimaremare | 3:7c648d1d8802 | 26 | { |
mariosimaremare | 3:7c648d1d8802 | 27 | } |
mariosimaremare | 7:46e65aeb4df2 | 28 | |
mariosimaremare | 11:dcaaf1bb21ce | 29 | char* Tank::getStrStatus() |
mariosimaremare | 11:dcaaf1bb21ce | 30 | { |
mariosimaremare | 11:dcaaf1bb21ce | 31 | return (this->_strStatus); |
mariosimaremare | 11:dcaaf1bb21ce | 32 | } |
mariosimaremare | 11:dcaaf1bb21ce | 33 | |
mariosimaremare | 7:46e65aeb4df2 | 34 | bool Tank::is_proximity_ok() |
mariosimaremare | 7:46e65aeb4df2 | 35 | { |
mariosimaremare | 7:46e65aeb4df2 | 36 | int range = 0; |
mariosimaremare | 7:46e65aeb4df2 | 37 | char buffer[32]; |
mariosimaremare | 7:46e65aeb4df2 | 38 | |
mariosimaremare | 7:46e65aeb4df2 | 39 | _pinger.Send(); |
mariosimaremare | 7:46e65aeb4df2 | 40 | wait(0.1); |
mariosimaremare | 7:46e65aeb4df2 | 41 | range = _pinger.Read_cm(); |
mariosimaremare | 7:46e65aeb4df2 | 42 | |
mariosimaremare | 7:46e65aeb4df2 | 43 | range = range/2; |
mariosimaremare | 7:46e65aeb4df2 | 44 | sprintf( |
mariosimaremare | 7:46e65aeb4df2 | 45 | buffer, |
mariosimaremare | 7:46e65aeb4df2 | 46 | "Range in cm: %d", |
mariosimaremare | 7:46e65aeb4df2 | 47 | range |
mariosimaremare | 7:46e65aeb4df2 | 48 | ); |
mariosimaremare | 12:7a48b90b493e | 49 | _printer.toBothln(buffer); |
mariosimaremare | 7:46e65aeb4df2 | 50 | |
mariosimaremare | 7:46e65aeb4df2 | 51 | if (range >= 12) { |
mariosimaremare | 7:46e65aeb4df2 | 52 | _printer.toBoth("Proximity ok"); |
mariosimaremare | 7:46e65aeb4df2 | 53 | return true; |
mariosimaremare | 7:46e65aeb4df2 | 54 | } |
mariosimaremare | 7:46e65aeb4df2 | 55 | return false; |
mariosimaremare | 7:46e65aeb4df2 | 56 | } |
mariosimaremare | 7:46e65aeb4df2 | 57 | |
mariosimaremare | 4:d8914369bf82 | 58 | void Tank::add(int type, int mililiters, int direction) |
mariosimaremare | 3:7c648d1d8802 | 59 | { |
mariosimaremare | 7:46e65aeb4df2 | 60 | for (int i = 25; i < MAX_SPEED; i+=20) { |
mariosimaremare | 7:46e65aeb4df2 | 61 | if (type == TYPE_SALT) |
mariosimaremare | 7:46e65aeb4df2 | 62 | _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, i); |
mariosimaremare | 7:46e65aeb4df2 | 63 | else |
mariosimaremare | 7:46e65aeb4df2 | 64 | _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, i); |
mariosimaremare | 7:46e65aeb4df2 | 65 | } |
mariosimaremare | 6:931a7fe8fa52 | 66 | |
mariosimaremare | 7:46e65aeb4df2 | 67 | //fix the changing of directions for pure |
mariosimaremare | 7:46e65aeb4df2 | 68 | if (type == TYPE_PURE) { |
mariosimaremare | 7:46e65aeb4df2 | 69 | //mililiters = mililiters * 2.0; |
mariosimaremare | 7:46e65aeb4df2 | 70 | if (_previous_direction_pure != direction) |
mariosimaremare | 7:46e65aeb4df2 | 71 | mililiters = mililiters + 1.0; |
mariosimaremare | 7:46e65aeb4df2 | 72 | } |
mariosimaremare | 6:931a7fe8fa52 | 73 | |
mariosimaremare | 7:46e65aeb4df2 | 74 | //fix the changing of directions for salt |
mariosimaremare | 7:46e65aeb4df2 | 75 | if (type == TYPE_SALT) { |
mariosimaremare | 7:46e65aeb4df2 | 76 | //mililiters = mililiters * 2.0; |
mariosimaremare | 7:46e65aeb4df2 | 77 | if (_previous_direction_salt != direction) |
mariosimaremare | 7:46e65aeb4df2 | 78 | mililiters = mililiters + 1.0; |
mariosimaremare | 7:46e65aeb4df2 | 79 | //if (_previous_direction_salt == DIRECTION_PULL) |
mariosimaremare | 7:46e65aeb4df2 | 80 | //mililiters = mililiters + 0.5; |
mariosimaremare | 7:46e65aeb4df2 | 81 | } |
mariosimaremare | 6:931a7fe8fa52 | 82 | |
mariosimaremare | 7:46e65aeb4df2 | 83 | //move with constant speed |
mariosimaremare | 7:46e65aeb4df2 | 84 | for (int i = 1; i < (MOVEMENT + (int)floor(((mililiters - 1.0)*STEP_EXTRA))); i+=1) { |
mariosimaremare | 7:46e65aeb4df2 | 85 | if (type == TYPE_SALT) |
mariosimaremare | 7:46e65aeb4df2 | 86 | _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, MAX_SPEED); |
mariosimaremare | 7:46e65aeb4df2 | 87 | else |
mariosimaremare | 7:46e65aeb4df2 | 88 | _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, MAX_SPEED); |
mariosimaremare | 6:931a7fe8fa52 | 89 | |
mariosimaremare | 7:46e65aeb4df2 | 90 | //if ((i - MOVEMENT) % STEP_EXTRA == 0 && !proximity_ok()) |
mariosimaremare | 7:46e65aeb4df2 | 91 | // break; |
mariosimaremare | 7:46e65aeb4df2 | 92 | } |
mariosimaremare | 6:931a7fe8fa52 | 93 | |
mariosimaremare | 7:46e65aeb4df2 | 94 | //de-accelerate |
mariosimaremare | 7:46e65aeb4df2 | 95 | for (int i = MAX_SPEED; i > 0; i-=20) { |
mariosimaremare | 7:46e65aeb4df2 | 96 | if (type == TYPE_SALT) |
mariosimaremare | 7:46e65aeb4df2 | 97 | _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, i); |
mariosimaremare | 7:46e65aeb4df2 | 98 | else |
mariosimaremare | 7:46e65aeb4df2 | 99 | _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, i); |
mariosimaremare | 7:46e65aeb4df2 | 100 | } |
mariosimaremare | 6:931a7fe8fa52 | 101 | |
mariosimaremare | 7:46e65aeb4df2 | 102 | //remember previous direction |
mariosimaremare | 7:46e65aeb4df2 | 103 | if (type == TYPE_SALT) { |
mariosimaremare | 7:46e65aeb4df2 | 104 | _previous_direction_salt = direction; |
mariosimaremare | 7:46e65aeb4df2 | 105 | } else { |
mariosimaremare | 7:46e65aeb4df2 | 106 | _previous_direction_pure = direction; |
mariosimaremare | 6:931a7fe8fa52 | 107 | } |
mariosimaremare | 3:7c648d1d8802 | 108 | } |
mariosimaremare | 6:931a7fe8fa52 | 109 | void Tank::react(double salinity) |
mariosimaremare | 6:931a7fe8fa52 | 110 | { |
mariosimaremare | 11:dcaaf1bb21ce | 111 | bool is_allowed = true; |
mariosimaremare | 11:dcaaf1bb21ce | 112 | |
mariosimaremare | 11:dcaaf1bb21ce | 113 | if( |
mariosimaremare | 11:dcaaf1bb21ce | 114 | (this->_added_liquid > 0.0) |
mariosimaremare | 11:dcaaf1bb21ce | 115 | && (fmod(this->_added_liquid, PROXIMITY_CHECK_POINT) == 0.0) |
mariosimaremare | 11:dcaaf1bb21ce | 116 | ) { |
mariosimaremare | 11:dcaaf1bb21ce | 117 | is_allowed = is_proximity_ok(); |
mariosimaremare | 12:7a48b90b493e | 118 | if(is_allowed){ |
mariosimaremare | 12:7a48b90b493e | 119 | this->_added_liquid = 0.0; |
mariosimaremare | 12:7a48b90b493e | 120 | } |
mariosimaremare | 11:dcaaf1bb21ce | 121 | } |
mariosimaremare | 10:b0fd262a0679 | 122 | |
mariosimaremare | 11:dcaaf1bb21ce | 123 | float added_liquid = 0.0; |
mariosimaremare | 11:dcaaf1bb21ce | 124 | if(is_allowed) { |
mariosimaremare | 11:dcaaf1bb21ce | 125 | if(salinity < 0.0) { |
mariosimaremare | 11:dcaaf1bb21ce | 126 | _strStatus = "Add salt"; |
mariosimaremare | 12:7a48b90b493e | 127 | _printer.toBothln("set salt valve to the tank"); |
mariosimaremare | 11:dcaaf1bb21ce | 128 | _alarm.flash(2, 0.2, 3.0); |
mariosimaremare | 11:dcaaf1bb21ce | 129 | this->add(TYPE_SALT, ADDED_SALT, DIRECTION_PUSH); |
mariosimaremare | 12:7a48b90b493e | 130 | _printer.toBothln("set salt valve to the reservoir"); |
mariosimaremare | 11:dcaaf1bb21ce | 131 | _alarm.flash(2, 0.2, 3.0); |
mariosimaremare | 11:dcaaf1bb21ce | 132 | this->add(TYPE_SALT, ADDED_SALT, DIRECTION_PULL); |
mariosimaremare | 11:dcaaf1bb21ce | 133 | added_liquid = ADDED_SALT; |
mariosimaremare | 11:dcaaf1bb21ce | 134 | } else if(salinity > 0.0) { |
mariosimaremare | 11:dcaaf1bb21ce | 135 | _strStatus = "Add water"; |
mariosimaremare | 12:7a48b90b493e | 136 | _printer.toBothln("set water valve to the tank"); |
mariosimaremare | 11:dcaaf1bb21ce | 137 | _alarm.flash(1, 0.2, 3.0); |
mariosimaremare | 11:dcaaf1bb21ce | 138 | this->add(TYPE_PURE, ADDED_WATER, DIRECTION_PUSH); |
mariosimaremare | 12:7a48b90b493e | 139 | _printer.toBothln("set water valve to reservoir"); |
mariosimaremare | 11:dcaaf1bb21ce | 140 | _alarm.flash(1, 0.2, 3.0); |
mariosimaremare | 11:dcaaf1bb21ce | 141 | this->add(TYPE_PURE, ADDED_WATER, DIRECTION_PULL); |
mariosimaremare | 11:dcaaf1bb21ce | 142 | added_liquid = ADDED_WATER; |
mariosimaremare | 11:dcaaf1bb21ce | 143 | } else { |
mariosimaremare | 11:dcaaf1bb21ce | 144 | _strStatus = "Add nothing"; |
mariosimaremare | 11:dcaaf1bb21ce | 145 | } |
mariosimaremare | 11:dcaaf1bb21ce | 146 | this->_added_liquid += added_liquid; |
mariosimaremare | 11:dcaaf1bb21ce | 147 | } else { |
mariosimaremare | 12:7a48b90b493e | 148 | _strStatus = "Tank is overflowing"; |
mariosimaremare | 11:dcaaf1bb21ce | 149 | _alarm.danger_flash(); |
mariosimaremare | 11:dcaaf1bb21ce | 150 | } |
mariosimaremare | 11:dcaaf1bb21ce | 151 | } |
mariosimaremare | 11:dcaaf1bb21ce | 152 | |
mariosimaremare | 11:dcaaf1bb21ce | 153 | void Tank::initialize() |
mariosimaremare | 11:dcaaf1bb21ce | 154 | { |
mariosimaremare | 12:7a48b90b493e | 155 | _printer.toBoth("initializing..."); |
mariosimaremare | 11:dcaaf1bb21ce | 156 | //initialize |
mariosimaremare | 12:7a48b90b493e | 157 | _alarm.flash(1, 0.2, 3.0); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 158 | this->add(TYPE_PURE, 20.0, DIRECTION_PULL);//20 |
mariosimaremare | 12:7a48b90b493e | 159 | _alarm.flash(1, 0.2, 3.0); //turn the thing to pushing to glass |
mariosimaremare | 11:dcaaf1bb21ce | 160 | this->add(TYPE_PURE, 20.0, DIRECTION_PUSH);//19.5 |
mariosimaremare | 12:7a48b90b493e | 161 | _alarm.flash(1, 0.2, 3.0); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 162 | this->add(TYPE_PURE, 20.0, DIRECTION_PULL); //20 |
mariosimaremare | 12:7a48b90b493e | 163 | _alarm.flash(1, 0.2, 3.0); //turn the thing to pushing to glass |
mariosimaremare | 11:dcaaf1bb21ce | 164 | this->add(TYPE_PURE, 20.0, DIRECTION_PUSH); |
mariosimaremare | 12:7a48b90b493e | 165 | _alarm.flash(1, 0.2, 3.0); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 166 | this->add(TYPE_PURE, 25.0, DIRECTION_PULL); //20 |
mariosimaremare | 11:dcaaf1bb21ce | 167 | //done |
mariosimaremare | 11:dcaaf1bb21ce | 168 | |
mariosimaremare | 11:dcaaf1bb21ce | 169 | //initialize |
mariosimaremare | 12:7a48b90b493e | 170 | _alarm.flash(2, 0.2, 3.0); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 171 | this->add(TYPE_SALT, 20.0, DIRECTION_PULL);//20 |
mariosimaremare | 12:7a48b90b493e | 172 | _alarm.flash(2, 0.2, 3.0); //turn the thing to pushing to glass |
mariosimaremare | 11:dcaaf1bb21ce | 173 | this->add(TYPE_SALT, 20.0, DIRECTION_PUSH);//19.5 |
mariosimaremare | 12:7a48b90b493e | 174 | _alarm.flash(2, 0.2, 3.0); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 175 | this->add(TYPE_SALT, 20.0, DIRECTION_PULL); //20 |
mariosimaremare | 12:7a48b90b493e | 176 | _alarm.flash(2, 0.2, 3.0); //turn the thing to pushing to glass |
mariosimaremare | 11:dcaaf1bb21ce | 177 | this->add(TYPE_SALT, 20.0, DIRECTION_PUSH); |
mariosimaremare | 12:7a48b90b493e | 178 | _alarm.flash(2, 0.2, 3.0); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 179 | this->add(TYPE_SALT, 25.0, DIRECTION_PULL); //20 |
mariosimaremare | 6:931a7fe8fa52 | 180 | } |