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