Mario Simaremare
/
g3_waterplay
Salinity and temperature sensors are implemented in classes.
Tank.cpp@7:46e65aeb4df2, 2016-06-19 (annotated)
- Committer:
- mariosimaremare
- Date:
- Sun Jun 19 23:48:07 2016 +0000
- Revision:
- 7:46e65aeb4df2
- Parent:
- 6:931a7fe8fa52
- Child:
- 8:6be230b22314
Update the pouring water.
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 | 7:46e65aeb4df2 | 13 | Ping &pinger |
mariosimaremare | 3:7c648d1d8802 | 14 | ): |
mariosimaremare | 4:d8914369bf82 | 15 | _printer(printer), |
mariosimaremare | 4:d8914369bf82 | 16 | _salinitySyringe(salinitySyringe), |
mariosimaremare | 4:d8914369bf82 | 17 | _waterSyringe(waterSyringe), |
mariosimaremare | 7:46e65aeb4df2 | 18 | _pinger(pinger), |
mariosimaremare | 4:d8914369bf82 | 19 | _previous_direction_salt(-1), |
mariosimaremare | 4:d8914369bf82 | 20 | _previous_direction_pure(-1), |
mariosimaremare | 4:d8914369bf82 | 21 | _position_salt(14), |
mariosimaremare | 4:d8914369bf82 | 22 | _position_pure(27) |
mariosimaremare | 3:7c648d1d8802 | 23 | { |
mariosimaremare | 3:7c648d1d8802 | 24 | } |
mariosimaremare | 7:46e65aeb4df2 | 25 | |
mariosimaremare | 7:46e65aeb4df2 | 26 | bool Tank::is_proximity_ok() |
mariosimaremare | 7:46e65aeb4df2 | 27 | { |
mariosimaremare | 7:46e65aeb4df2 | 28 | int range = 0; |
mariosimaremare | 7:46e65aeb4df2 | 29 | char buffer[32]; |
mariosimaremare | 7:46e65aeb4df2 | 30 | |
mariosimaremare | 7:46e65aeb4df2 | 31 | _pinger.Send(); |
mariosimaremare | 7:46e65aeb4df2 | 32 | wait(0.1); |
mariosimaremare | 7:46e65aeb4df2 | 33 | range = _pinger.Read_cm(); |
mariosimaremare | 7:46e65aeb4df2 | 34 | |
mariosimaremare | 7:46e65aeb4df2 | 35 | range = range/2; |
mariosimaremare | 7:46e65aeb4df2 | 36 | sprintf( |
mariosimaremare | 7:46e65aeb4df2 | 37 | buffer, |
mariosimaremare | 7:46e65aeb4df2 | 38 | "Range in cm: %d", |
mariosimaremare | 7:46e65aeb4df2 | 39 | range |
mariosimaremare | 7:46e65aeb4df2 | 40 | ); |
mariosimaremare | 7:46e65aeb4df2 | 41 | _printer.toBoth(buffer); |
mariosimaremare | 7:46e65aeb4df2 | 42 | |
mariosimaremare | 7:46e65aeb4df2 | 43 | if (range >= 12) { |
mariosimaremare | 7:46e65aeb4df2 | 44 | _printer.toBoth("Proximity ok"); |
mariosimaremare | 7:46e65aeb4df2 | 45 | return true; |
mariosimaremare | 7:46e65aeb4df2 | 46 | } |
mariosimaremare | 7:46e65aeb4df2 | 47 | return false; |
mariosimaremare | 7:46e65aeb4df2 | 48 | } |
mariosimaremare | 7:46e65aeb4df2 | 49 | |
mariosimaremare | 4:d8914369bf82 | 50 | void Tank::add(int type, int mililiters, int direction) |
mariosimaremare | 3:7c648d1d8802 | 51 | { |
mariosimaremare | 7:46e65aeb4df2 | 52 | for (int i = 25; i < MAX_SPEED; i+=20) { |
mariosimaremare | 7:46e65aeb4df2 | 53 | if (type == TYPE_SALT) |
mariosimaremare | 7:46e65aeb4df2 | 54 | _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, i); |
mariosimaremare | 7:46e65aeb4df2 | 55 | else |
mariosimaremare | 7:46e65aeb4df2 | 56 | _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, i); |
mariosimaremare | 7:46e65aeb4df2 | 57 | } |
mariosimaremare | 6:931a7fe8fa52 | 58 | |
mariosimaremare | 7:46e65aeb4df2 | 59 | //fix the changing of directions for pure |
mariosimaremare | 7:46e65aeb4df2 | 60 | if (type == TYPE_PURE) { |
mariosimaremare | 7:46e65aeb4df2 | 61 | //mililiters = mililiters * 2.0; |
mariosimaremare | 7:46e65aeb4df2 | 62 | if (_previous_direction_pure != direction) |
mariosimaremare | 7:46e65aeb4df2 | 63 | mililiters = mililiters + 1.0; |
mariosimaremare | 7:46e65aeb4df2 | 64 | } |
mariosimaremare | 6:931a7fe8fa52 | 65 | |
mariosimaremare | 7:46e65aeb4df2 | 66 | //fix the changing of directions for salt |
mariosimaremare | 7:46e65aeb4df2 | 67 | if (type == TYPE_SALT) { |
mariosimaremare | 7:46e65aeb4df2 | 68 | //mililiters = mililiters * 2.0; |
mariosimaremare | 7:46e65aeb4df2 | 69 | if (_previous_direction_salt != direction) |
mariosimaremare | 7:46e65aeb4df2 | 70 | mililiters = mililiters + 1.0; |
mariosimaremare | 7:46e65aeb4df2 | 71 | //if (_previous_direction_salt == DIRECTION_PULL) |
mariosimaremare | 7:46e65aeb4df2 | 72 | //mililiters = mililiters + 0.5; |
mariosimaremare | 7:46e65aeb4df2 | 73 | } |
mariosimaremare | 6:931a7fe8fa52 | 74 | |
mariosimaremare | 7:46e65aeb4df2 | 75 | //move with constant speed |
mariosimaremare | 7:46e65aeb4df2 | 76 | for (int i = 1; i < (MOVEMENT + (int)floor(((mililiters - 1.0)*STEP_EXTRA))); i+=1) { |
mariosimaremare | 7:46e65aeb4df2 | 77 | if (type == TYPE_SALT) |
mariosimaremare | 7:46e65aeb4df2 | 78 | _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, MAX_SPEED); |
mariosimaremare | 7:46e65aeb4df2 | 79 | else |
mariosimaremare | 7:46e65aeb4df2 | 80 | _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, MAX_SPEED); |
mariosimaremare | 6:931a7fe8fa52 | 81 | |
mariosimaremare | 7:46e65aeb4df2 | 82 | //if ((i - MOVEMENT) % STEP_EXTRA == 0 && !proximity_ok()) |
mariosimaremare | 7:46e65aeb4df2 | 83 | // break; |
mariosimaremare | 7:46e65aeb4df2 | 84 | } |
mariosimaremare | 6:931a7fe8fa52 | 85 | |
mariosimaremare | 7:46e65aeb4df2 | 86 | //de-accelerate |
mariosimaremare | 7:46e65aeb4df2 | 87 | for (int i = MAX_SPEED; i > 0; i-=20) { |
mariosimaremare | 7:46e65aeb4df2 | 88 | if (type == TYPE_SALT) |
mariosimaremare | 7:46e65aeb4df2 | 89 | _salinitySyringe.settings(1/MICROSTEPS_PER_STEP, direction, i); |
mariosimaremare | 7:46e65aeb4df2 | 90 | else |
mariosimaremare | 7:46e65aeb4df2 | 91 | _waterSyringe.settings(1/MICROSTEPS_PER_STEP, direction, i); |
mariosimaremare | 7:46e65aeb4df2 | 92 | } |
mariosimaremare | 6:931a7fe8fa52 | 93 | |
mariosimaremare | 7:46e65aeb4df2 | 94 | //remember previous direction |
mariosimaremare | 7:46e65aeb4df2 | 95 | if (type == TYPE_SALT) { |
mariosimaremare | 7:46e65aeb4df2 | 96 | _previous_direction_salt = direction; |
mariosimaremare | 7:46e65aeb4df2 | 97 | } else { |
mariosimaremare | 7:46e65aeb4df2 | 98 | _previous_direction_pure = direction; |
mariosimaremare | 6:931a7fe8fa52 | 99 | } |
mariosimaremare | 3:7c648d1d8802 | 100 | } |
mariosimaremare | 6:931a7fe8fa52 | 101 | void Tank::react(double salinity) |
mariosimaremare | 6:931a7fe8fa52 | 102 | { |
mariosimaremare | 6:931a7fe8fa52 | 103 | if(salinity < 0.0) { |
mariosimaremare | 6:931a7fe8fa52 | 104 | // add more pure water |
mariosimaremare | 6:931a7fe8fa52 | 105 | add(TYPE_SALT, 1, DIRECTION_PULL); |
mariosimaremare | 6:931a7fe8fa52 | 106 | add(TYPE_SALT, 1, DIRECTION_PUSH); |
mariosimaremare | 6:931a7fe8fa52 | 107 | add(TYPE_SALT, 2, DIRECTION_PULL); |
mariosimaremare | 6:931a7fe8fa52 | 108 | add(TYPE_SALT, 2, DIRECTION_PUSH); |
mariosimaremare | 6:931a7fe8fa52 | 109 | add(TYPE_SALT, 3, DIRECTION_PULL); |
mariosimaremare | 6:931a7fe8fa52 | 110 | add(TYPE_SALT, 3, DIRECTION_PUSH); |
mariosimaremare | 6:931a7fe8fa52 | 111 | } else { |
mariosimaremare | 6:931a7fe8fa52 | 112 | // add more saline solution |
mariosimaremare | 6:931a7fe8fa52 | 113 | add(TYPE_PURE, 1, DIRECTION_PULL); |
mariosimaremare | 6:931a7fe8fa52 | 114 | add(TYPE_PURE, 1, DIRECTION_PUSH); |
mariosimaremare | 6:931a7fe8fa52 | 115 | add(TYPE_PURE, 2, DIRECTION_PULL); |
mariosimaremare | 6:931a7fe8fa52 | 116 | add(TYPE_PURE, 2, DIRECTION_PUSH); |
mariosimaremare | 6:931a7fe8fa52 | 117 | add(TYPE_PURE, 3, DIRECTION_PULL); |
mariosimaremare | 6:931a7fe8fa52 | 118 | add(TYPE_PURE, 3, DIRECTION_PUSH); |
mariosimaremare | 6:931a7fe8fa52 | 119 | } |
mariosimaremare | 6:931a7fe8fa52 | 120 | } |