Mario Simaremare
/
g3_waterplay
Salinity and temperature sensors are implemented in classes.
Tank.cpp@11:dcaaf1bb21ce, 2016-06-22 (annotated)
- Committer:
- mariosimaremare
- Date:
- Wed Jun 22 00:16:33 2016 +0000
- Revision:
- 11:dcaaf1bb21ce
- Parent:
- 10:b0fd262a0679
- Child:
- 12:7a48b90b493e
Added alarm.
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 | 7:46e65aeb4df2 | 49 | _printer.toBoth(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 | 11:dcaaf1bb21ce | 118 | } |
mariosimaremare | 10:b0fd262a0679 | 119 | |
mariosimaremare | 11:dcaaf1bb21ce | 120 | float added_liquid = 0.0; |
mariosimaremare | 11:dcaaf1bb21ce | 121 | if(is_allowed) { |
mariosimaremare | 11:dcaaf1bb21ce | 122 | if(salinity < 0.0) { |
mariosimaremare | 11:dcaaf1bb21ce | 123 | _strStatus = "Add salt"; |
mariosimaremare | 11:dcaaf1bb21ce | 124 | _alarm.flash(2, 0.2, 3.0); |
mariosimaremare | 11:dcaaf1bb21ce | 125 | this->add(TYPE_SALT, ADDED_SALT, DIRECTION_PUSH); |
mariosimaremare | 11:dcaaf1bb21ce | 126 | _alarm.flash(2, 0.2, 3.0); |
mariosimaremare | 11:dcaaf1bb21ce | 127 | this->add(TYPE_SALT, ADDED_SALT, DIRECTION_PULL); |
mariosimaremare | 11:dcaaf1bb21ce | 128 | added_liquid = ADDED_SALT; |
mariosimaremare | 11:dcaaf1bb21ce | 129 | } else if(salinity > 0.0) { |
mariosimaremare | 11:dcaaf1bb21ce | 130 | _strStatus = "Add water"; |
mariosimaremare | 11:dcaaf1bb21ce | 131 | _alarm.flash(1, 0.2, 3.0); |
mariosimaremare | 11:dcaaf1bb21ce | 132 | this->add(TYPE_PURE, ADDED_WATER, DIRECTION_PUSH); |
mariosimaremare | 11:dcaaf1bb21ce | 133 | _alarm.flash(1, 0.2, 3.0); |
mariosimaremare | 11:dcaaf1bb21ce | 134 | this->add(TYPE_PURE, ADDED_WATER, DIRECTION_PULL); |
mariosimaremare | 11:dcaaf1bb21ce | 135 | added_liquid = ADDED_WATER; |
mariosimaremare | 11:dcaaf1bb21ce | 136 | } else { |
mariosimaremare | 11:dcaaf1bb21ce | 137 | _strStatus = "Add nothing"; |
mariosimaremare | 11:dcaaf1bb21ce | 138 | } |
mariosimaremare | 11:dcaaf1bb21ce | 139 | this->_added_liquid += added_liquid; |
mariosimaremare | 11:dcaaf1bb21ce | 140 | } else { |
mariosimaremare | 11:dcaaf1bb21ce | 141 | _strStatus = "tank is overflow"; |
mariosimaremare | 11:dcaaf1bb21ce | 142 | _alarm.danger_flash(); |
mariosimaremare | 11:dcaaf1bb21ce | 143 | } |
mariosimaremare | 11:dcaaf1bb21ce | 144 | } |
mariosimaremare | 11:dcaaf1bb21ce | 145 | |
mariosimaremare | 11:dcaaf1bb21ce | 146 | void Tank::initialize() |
mariosimaremare | 11:dcaaf1bb21ce | 147 | { |
mariosimaremare | 11:dcaaf1bb21ce | 148 | //initialize |
mariosimaremare | 11:dcaaf1bb21ce | 149 | wait(3); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 150 | this->add(TYPE_PURE, 20.0, DIRECTION_PULL);//20 |
mariosimaremare | 11:dcaaf1bb21ce | 151 | wait(3); //turn the thing to pushing to glass |
mariosimaremare | 11:dcaaf1bb21ce | 152 | this->add(TYPE_PURE, 20.0, DIRECTION_PUSH);//19.5 |
mariosimaremare | 11:dcaaf1bb21ce | 153 | wait(3); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 154 | this->add(TYPE_PURE, 20.0, DIRECTION_PULL); //20 |
mariosimaremare | 11:dcaaf1bb21ce | 155 | wait(3); //turn the thing to pushing to glass |
mariosimaremare | 11:dcaaf1bb21ce | 156 | this->add(TYPE_PURE, 20.0, DIRECTION_PUSH); |
mariosimaremare | 11:dcaaf1bb21ce | 157 | wait(3); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 158 | this->add(TYPE_PURE, 25.0, DIRECTION_PULL); //20 |
mariosimaremare | 11:dcaaf1bb21ce | 159 | //done |
mariosimaremare | 11:dcaaf1bb21ce | 160 | |
mariosimaremare | 11:dcaaf1bb21ce | 161 | |
mariosimaremare | 11:dcaaf1bb21ce | 162 | //initialize |
mariosimaremare | 11:dcaaf1bb21ce | 163 | wait(3); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 164 | this->add(TYPE_SALT, 20.0, DIRECTION_PULL);//20 |
mariosimaremare | 11:dcaaf1bb21ce | 165 | wait(3); //turn the thing to pushing to glass |
mariosimaremare | 11:dcaaf1bb21ce | 166 | this->add(TYPE_SALT, 20.0, DIRECTION_PUSH);//19.5 |
mariosimaremare | 11:dcaaf1bb21ce | 167 | wait(3); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 168 | this->add(TYPE_SALT, 20.0, DIRECTION_PULL); //20 |
mariosimaremare | 11:dcaaf1bb21ce | 169 | wait(3); //turn the thing to pushing to glass |
mariosimaremare | 11:dcaaf1bb21ce | 170 | this->add(TYPE_SALT, 20.0, DIRECTION_PUSH); |
mariosimaremare | 11:dcaaf1bb21ce | 171 | wait(3); //turn the thing to pulling from reservoir |
mariosimaremare | 11:dcaaf1bb21ce | 172 | this->add(TYPE_SALT, 25.0, DIRECTION_PULL); //20 |
mariosimaremare | 6:931a7fe8fa52 | 173 | } |