Helvijs Kiselis
/
Micromouse
Algorithmus
Motion.cpp@33:836ab2089565, 2018-05-23 (annotated)
- Committer:
- Helvis
- Date:
- Wed May 23 12:30:39 2018 +0000
- Revision:
- 33:836ab2089565
- Parent:
- 32:e984b7959cb0
- Child:
- 34:0587c0943263
Challenge Day
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helvis | 1:2b5f79285a3e | 1 | |
Helvis | 1:2b5f79285a3e | 2 | #include <cmath> |
Helvis | 1:2b5f79285a3e | 3 | #include "Motion.h" |
Helvis | 1:2b5f79285a3e | 4 | |
Helvis | 1:2b5f79285a3e | 5 | using namespace std; |
Helvis | 1:2b5f79285a3e | 6 | |
Helvis | 19:6cd6cc5c8b4c | 7 | const float Motion::LEFT_MID_VAL = 40.73f; //44.73 |
Helvis | 19:6cd6cc5c8b4c | 8 | const float Motion::RIGHT_MID_VAL = 43.03f; //47.03 |
luethale | 26:f964408401fa | 9 | const float Motion::KP = 2.0; |
Helvis | 5:e2c0a4388d85 | 10 | const float Motion::KD = 0.0f; |
Helvis | 30:bdb8c92434a0 | 11 | const int Motion::MOVE_DIST = 1620; |
Helvis | 15:0ae61d3d199f | 12 | const float Motion::MOVE_SPEED = 50.0f; |
luethale | 27:f111ba194412 | 13 | const float Motion::RUN_SPEED = 70.0f; |
Helvis | 13:845e49f20426 | 14 | const float Motion::ROTATE_SPEED = 80.0f; |
Helvis | 30:bdb8c92434a0 | 15 | const float Motion::ACCEL_CONST = 4.0f; //2.212f |
Helvis | 1:2b5f79285a3e | 16 | |
Helvis | 1:2b5f79285a3e | 17 | |
Helvis | 1:2b5f79285a3e | 18 | Motion::Motion(Controller& controller, EncoderCounter& counterLeft, |
Helvis | 1:2b5f79285a3e | 19 | EncoderCounter& counterRight, IRSensor& irSensorL, |
Helvis | 1:2b5f79285a3e | 20 | IRSensor& irSensorC, IRSensor& irSensorR, AnalogIn& lineSensor, |
Helvis | 1:2b5f79285a3e | 21 | DigitalOut& enableMotorDriver) : |
Helvis | 1:2b5f79285a3e | 22 | controller(controller), counterLeft(counterLeft), |
Helvis | 1:2b5f79285a3e | 23 | counterRight(counterRight), irSensorL(irSensorL), |
Helvis | 1:2b5f79285a3e | 24 | irSensorC(irSensorC), irSensorR(irSensorR), lineSensor(lineSensor), |
Helvis | 1:2b5f79285a3e | 25 | enableMotorDriver(enableMotorDriver) {} |
Helvis | 1:2b5f79285a3e | 26 | |
Helvis | 1:2b5f79285a3e | 27 | Motion::~Motion() {} |
Helvis | 1:2b5f79285a3e | 28 | |
Helvis | 1:2b5f79285a3e | 29 | /** |
Helvis | 1:2b5f79285a3e | 30 | * Eine Feldstrecke druchführen |
Helvis | 1:2b5f79285a3e | 31 | */ |
Helvis | 1:2b5f79285a3e | 32 | void Motion::move() { |
Helvis | 1:2b5f79285a3e | 33 | |
Helvis | 1:2b5f79285a3e | 34 | countsLOld = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 35 | countsROld = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 36 | countsL = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 37 | countsR = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 38 | |
Helvis | 16:c5b864804632 | 39 | avgSpeed = ( abs(controller.getSpeedLeft()) + abs(controller.getSpeedRight()) ) * 0.5f; |
Helvis | 16:c5b864804632 | 40 | |
Helvis | 13:845e49f20426 | 41 | t.start(); |
Helvis | 13:845e49f20426 | 42 | |
Helvis | 6:4868f789c223 | 43 | while ((countsL - countsLOld) < MOVE_DIST || (countsR - countsROld) > -MOVE_DIST) { |
Helvis | 1:2b5f79285a3e | 44 | |
Helvis | 1:2b5f79285a3e | 45 | countsL = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 46 | countsR = counterRight.read(); |
Helvis | 5:e2c0a4388d85 | 47 | distanceC = irSensorC.readC(); |
Helvis | 12:75d0291a9785 | 48 | distanceL = irSensorL.readL(); |
Helvis | 12:75d0291a9785 | 49 | distanceR = irSensorR.readR(); |
Helvis | 1:2b5f79285a3e | 50 | |
Helvis | 1:2b5f79285a3e | 51 | if (enableMotorDriver == 0) {enableMotorDriver = 1;} |
Helvis | 16:c5b864804632 | 52 | |
Helvis | 16:c5b864804632 | 53 | avgSpeed = ( abs(controller.getSpeedLeft()) + abs(controller.getSpeedRight()) ) * 0.5f; |
Helvis | 16:c5b864804632 | 54 | |
luethale | 27:f111ba194412 | 55 | if (speedRun == true) accel(RUN_SPEED); |
luethale | 27:f111ba194412 | 56 | else accel(MOVE_SPEED); |
Helvis | 12:75d0291a9785 | 57 | control(); |
Helvis | 5:e2c0a4388d85 | 58 | |
Helvis | 30:bdb8c92434a0 | 59 | //Stop at certainn distance before wall |
Helvis | 28:b7ce1e3bf08b | 60 | if ((distanceC) < 40.0f && speedRun == false) { |
Helvis | 5:e2c0a4388d85 | 61 | countsLOld = countsL; |
Helvis | 5:e2c0a4388d85 | 62 | countsROld = countsR; |
Helvis | 19:6cd6cc5c8b4c | 63 | |
Helvis | 5:e2c0a4388d85 | 64 | while ((countsL - countsLOld) < 70 || (countsR - countsROld) > -70) { |
Helvis | 5:e2c0a4388d85 | 65 | countsL = counterLeft.read(); |
Helvis | 19:6cd6cc5c8b4c | 66 | countsR = counterRight.read(); |
Helvis | 19:6cd6cc5c8b4c | 67 | distanceC = irSensorC.readC(); |
Helvis | 30:bdb8c92434a0 | 68 | |
Helvis | 30:bdb8c92434a0 | 69 | if (speedRun == true) accel(RUN_SPEED); |
Helvis | 30:bdb8c92434a0 | 70 | else accel(MOVE_SPEED); |
Helvis | 30:bdb8c92434a0 | 71 | control(); |
Helvis | 30:bdb8c92434a0 | 72 | |
Helvis | 19:6cd6cc5c8b4c | 73 | if (distanceC > 40.0f) { |
Helvis | 19:6cd6cc5c8b4c | 74 | stop(); |
Helvis | 19:6cd6cc5c8b4c | 75 | break; |
Helvis | 19:6cd6cc5c8b4c | 76 | } |
Helvis | 5:e2c0a4388d85 | 77 | } |
Helvis | 5:e2c0a4388d85 | 78 | stop(); |
Helvis | 5:e2c0a4388d85 | 79 | break; |
Helvis | 30:bdb8c92434a0 | 80 | |
Helvis | 30:bdb8c92434a0 | 81 | //Stop at certain distance in speed run |
Helvis | 30:bdb8c92434a0 | 82 | }else if (distanceC < 160.0f && speedRun == true && lastMove == true) { |
Helvis | 28:b7ce1e3bf08b | 83 | stop(); |
Helvis | 28:b7ce1e3bf08b | 84 | break; |
Helvis | 30:bdb8c92434a0 | 85 | |
Helvis | 30:bdb8c92434a0 | 86 | //Correct distance from wall |
Helvis | 19:6cd6cc5c8b4c | 87 | }else if ( ((countsL - countsLOld) >= MOVE_DIST || (countsR - countsROld) <= -MOVE_DIST) && (distanceC <= 130.0f) && (distanceC > 40.0f)) { |
Helvis | 31:2c54f8304ef5 | 88 | countsLOld += 1000; |
Helvis | 31:2c54f8304ef5 | 89 | countsROld -= 1000; |
Helvis | 19:6cd6cc5c8b4c | 90 | |
Helvis | 30:bdb8c92434a0 | 91 | //Stop after certain distance if side wall and front wall missing |
Helvis | 31:2c54f8304ef5 | 92 | }else if ( deceleration == true && speedRun == false && ( distanceL > 80.0f || distanceR > 80.0f ) && distanceC > 200.0f ) { |
Helvis | 31:2c54f8304ef5 | 93 | |
Helvis | 31:2c54f8304ef5 | 94 | controller.counterReset(); |
Helvis | 19:6cd6cc5c8b4c | 95 | countsLOld = counterLeft.read(); |
Helvis | 19:6cd6cc5c8b4c | 96 | countsROld = counterRight.read(); |
Helvis | 31:2c54f8304ef5 | 97 | countsL = counterLeft.read(); |
Helvis | 31:2c54f8304ef5 | 98 | countsR = counterRight.read(); |
Helvis | 19:6cd6cc5c8b4c | 99 | |
Helvis | 31:2c54f8304ef5 | 100 | while ((countsL - countsLOld) < MOVE_DIST*0.5f + 400.0f || (countsR - countsROld) > -0.5f*MOVE_DIST - 400.0f) { |
Helvis | 12:75d0291a9785 | 101 | countsL = counterLeft.read(); |
Helvis | 12:75d0291a9785 | 102 | countsR = counterRight.read(); |
Helvis | 30:bdb8c92434a0 | 103 | |
Helvis | 31:2c54f8304ef5 | 104 | deceleration = true; |
Helvis | 31:2c54f8304ef5 | 105 | acceleration = false; |
Helvis | 31:2c54f8304ef5 | 106 | |
Helvis | 31:2c54f8304ef5 | 107 | accel(MOVE_SPEED); |
Helvis | 19:6cd6cc5c8b4c | 108 | control(); |
Helvis | 12:75d0291a9785 | 109 | } |
Helvis | 12:75d0291a9785 | 110 | stop(); |
Helvis | 12:75d0291a9785 | 111 | break; |
Helvis | 30:bdb8c92434a0 | 112 | } |
Helvis | 23:accd07ca2da7 | 113 | |
Helvis | 23:accd07ca2da7 | 114 | } |
Helvis | 23:accd07ca2da7 | 115 | |
Helvis | 23:accd07ca2da7 | 116 | t.stop(); |
Helvis | 23:accd07ca2da7 | 117 | t.reset(); |
Helvis | 23:accd07ca2da7 | 118 | lastMove = false; |
Helvis | 31:2c54f8304ef5 | 119 | acceleration = false; |
Helvis | 31:2c54f8304ef5 | 120 | deceleration = false; |
Helvis | 23:accd07ca2da7 | 121 | } |
Helvis | 23:accd07ca2da7 | 122 | |
Helvis | 23:accd07ca2da7 | 123 | /** |
Helvis | 23:accd07ca2da7 | 124 | * Eine Feldstrecke druchführen |
Helvis | 23:accd07ca2da7 | 125 | */ |
Helvis | 23:accd07ca2da7 | 126 | void Motion::moveHalf() { |
Helvis | 23:accd07ca2da7 | 127 | |
Helvis | 23:accd07ca2da7 | 128 | countsLOld = counterLeft.read(); |
Helvis | 23:accd07ca2da7 | 129 | countsROld = counterRight.read(); |
Helvis | 23:accd07ca2da7 | 130 | countsL = counterLeft.read(); |
Helvis | 23:accd07ca2da7 | 131 | countsR = counterRight.read(); |
Helvis | 23:accd07ca2da7 | 132 | |
Helvis | 23:accd07ca2da7 | 133 | avgSpeed = ( abs(controller.getSpeedLeft()) + abs(controller.getSpeedRight()) ) * 0.5f; |
Helvis | 23:accd07ca2da7 | 134 | |
Helvis | 23:accd07ca2da7 | 135 | t.start(); |
Helvis | 23:accd07ca2da7 | 136 | |
Helvis | 23:accd07ca2da7 | 137 | while ((countsL - countsLOld) < 824 || (countsR - countsROld) > -824) { |
Helvis | 23:accd07ca2da7 | 138 | |
Helvis | 23:accd07ca2da7 | 139 | countsL = counterLeft.read(); |
Helvis | 23:accd07ca2da7 | 140 | countsR = counterRight.read(); |
Helvis | 23:accd07ca2da7 | 141 | distanceC = irSensorC.readC(); |
Helvis | 23:accd07ca2da7 | 142 | distanceL = irSensorL.readL(); |
Helvis | 23:accd07ca2da7 | 143 | distanceR = irSensorR.readR(); |
Helvis | 23:accd07ca2da7 | 144 | |
Helvis | 23:accd07ca2da7 | 145 | if (enableMotorDriver == 0) {enableMotorDriver = 1;} |
Helvis | 23:accd07ca2da7 | 146 | |
Helvis | 23:accd07ca2da7 | 147 | avgSpeed = ( abs(controller.getSpeedLeft()) + abs(controller.getSpeedRight()) ) * 0.5f; |
Helvis | 23:accd07ca2da7 | 148 | |
luethale | 27:f111ba194412 | 149 | accel(RUN_SPEED); |
Helvis | 23:accd07ca2da7 | 150 | control(); |
Helvis | 23:accd07ca2da7 | 151 | |
Helvis | 30:bdb8c92434a0 | 152 | //Stop ceratin distance before wall |
Helvis | 30:bdb8c92434a0 | 153 | if (distanceC < 100.0f && lastMove == false) { |
Helvis | 23:accd07ca2da7 | 154 | break; |
Helvis | 30:bdb8c92434a0 | 155 | |
Helvis | 33:836ab2089565 | 156 | }else if ( ((countsL - countsLOld) >= 824 || (countsR - countsROld) <= -824) && (distanceC >= 100.0f) && (distanceC < 120.0f) ) { |
Helvis | 33:836ab2089565 | 157 | countsLOld += 100; |
Helvis | 33:836ab2089565 | 158 | countsROld -= 100; |
Helvis | 33:836ab2089565 | 159 | |
Helvis | 30:bdb8c92434a0 | 160 | //Stop after certain distance if side wall missing |
Helvis | 31:2c54f8304ef5 | 161 | }else if ( (distanceL > 80.0f || distanceR > 80.0f) && lastMove == false && deceleration == true && distanceC < 190.0f) { |
Helvis | 31:2c54f8304ef5 | 162 | |
Helvis | 31:2c54f8304ef5 | 163 | controller.counterReset(); |
Helvis | 30:bdb8c92434a0 | 164 | countsLOld = counterLeft.read(); |
Helvis | 30:bdb8c92434a0 | 165 | countsROld = counterRight.read(); |
Helvis | 31:2c54f8304ef5 | 166 | countsL = counterLeft.read(); |
Helvis | 31:2c54f8304ef5 | 167 | countsR = counterRight.read(); |
Helvis | 30:bdb8c92434a0 | 168 | |
Helvis | 33:836ab2089565 | 169 | while ((countsL - countsLOld) < 250.0f || (countsR - countsROld) > -250.0f) { |
Helvis | 30:bdb8c92434a0 | 170 | countsL = counterLeft.read(); |
Helvis | 30:bdb8c92434a0 | 171 | countsR = counterRight.read(); |
Helvis | 30:bdb8c92434a0 | 172 | accel(RUN_SPEED); |
Helvis | 30:bdb8c92434a0 | 173 | control(); |
Helvis | 30:bdb8c92434a0 | 174 | } |
Helvis | 30:bdb8c92434a0 | 175 | stop(); |
Helvis | 30:bdb8c92434a0 | 176 | break; |
Helvis | 30:bdb8c92434a0 | 177 | |
Helvis | 30:bdb8c92434a0 | 178 | //Stop before wall at target field |
Helvis | 24:11c5fb5280eb | 179 | }else if (distanceC < 40.0f && lastMove == true) { |
Helvis | 23:accd07ca2da7 | 180 | stop(); |
Helvis | 23:accd07ca2da7 | 181 | break; |
Helvis | 19:6cd6cc5c8b4c | 182 | } |
Helvis | 1:2b5f79285a3e | 183 | |
Helvis | 1:2b5f79285a3e | 184 | } |
Helvis | 13:845e49f20426 | 185 | |
Helvis | 13:845e49f20426 | 186 | t.stop(); |
Helvis | 13:845e49f20426 | 187 | t.reset(); |
Helvis | 19:6cd6cc5c8b4c | 188 | lastMove = false; |
Helvis | 31:2c54f8304ef5 | 189 | acceleration = false; |
Helvis | 31:2c54f8304ef5 | 190 | deceleration = false; |
Helvis | 1:2b5f79285a3e | 191 | } |
Helvis | 19:6cd6cc5c8b4c | 192 | |
Helvis | 1:2b5f79285a3e | 193 | /** |
Helvis | 1:2b5f79285a3e | 194 | * Eine Feldstrecke mit überprüfung der Ziellinie fahren |
Helvis | 1:2b5f79285a3e | 195 | */ |
Helvis | 1:2b5f79285a3e | 196 | void Motion::scanMove() { |
Helvis | 1:2b5f79285a3e | 197 | |
Helvis | 16:c5b864804632 | 198 | acceleration = false; |
Helvis | 17:8a8758bfe3c5 | 199 | deceleration = false; |
Helvis | 22:91526c8d15ba | 200 | longMove = false; |
Helvis | 16:c5b864804632 | 201 | |
Helvis | 1:2b5f79285a3e | 202 | countsLOld = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 203 | countsROld = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 204 | countsL = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 205 | countsR = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 206 | |
Helvis | 31:2c54f8304ef5 | 207 | bool sideWalls = false; |
Helvis | 31:2c54f8304ef5 | 208 | |
Helvis | 13:845e49f20426 | 209 | t.start(); |
Helvis | 5:e2c0a4388d85 | 210 | |
Helvis | 6:4868f789c223 | 211 | while ((countsL - countsLOld) < MOVE_DIST || (countsR - countsROld) > -MOVE_DIST) { |
Helvis | 1:2b5f79285a3e | 212 | |
Helvis | 1:2b5f79285a3e | 213 | countsL = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 214 | countsR = counterRight.read(); |
Helvis | 3:076dd7ec7eb4 | 215 | distanceC = irSensorC.readC(); |
Helvis | 12:75d0291a9785 | 216 | distanceL = irSensorL.readL(); |
Helvis | 12:75d0291a9785 | 217 | distanceR = irSensorR.readR(); |
Helvis | 1:2b5f79285a3e | 218 | |
Helvis | 31:2c54f8304ef5 | 219 | //for distance correcture with side wall |
Helvis | 31:2c54f8304ef5 | 220 | if (distanceL < 80.0f && distanceR < 80.0f) sideWalls = true; |
Helvis | 31:2c54f8304ef5 | 221 | |
Helvis | 1:2b5f79285a3e | 222 | if (enableMotorDriver == 0) { |
Helvis | 1:2b5f79285a3e | 223 | enableMotorDriver = 1; |
Helvis | 1:2b5f79285a3e | 224 | } |
Helvis | 1:2b5f79285a3e | 225 | |
Helvis | 19:6cd6cc5c8b4c | 226 | if (lineSensor.read() > 0.85f) { |
Helvis | 1:2b5f79285a3e | 227 | line = 1; |
Helvis | 1:2b5f79285a3e | 228 | } |
Helvis | 1:2b5f79285a3e | 229 | |
Helvis | 13:845e49f20426 | 230 | |
luethale | 27:f111ba194412 | 231 | accel(MOVE_SPEED); |
Helvis | 13:845e49f20426 | 232 | control(); |
Helvis | 13:845e49f20426 | 233 | |
Helvis | 5:e2c0a4388d85 | 234 | if ((distanceC) < 40.0f) { |
Helvis | 5:e2c0a4388d85 | 235 | countsLOld = countsL; |
Helvis | 5:e2c0a4388d85 | 236 | countsROld = countsR; |
Helvis | 5:e2c0a4388d85 | 237 | while ((countsL - countsLOld) < 70 || (countsR - countsROld) > -70) { |
Helvis | 5:e2c0a4388d85 | 238 | countsL = counterLeft.read(); |
Helvis | 5:e2c0a4388d85 | 239 | countsR = counterRight.read(); |
Helvis | 19:6cd6cc5c8b4c | 240 | if (distanceC > 40.0f) { |
Helvis | 19:6cd6cc5c8b4c | 241 | stop(); |
Helvis | 19:6cd6cc5c8b4c | 242 | break; |
Helvis | 19:6cd6cc5c8b4c | 243 | } |
Helvis | 5:e2c0a4388d85 | 244 | } |
Helvis | 23:accd07ca2da7 | 245 | |
Helvis | 3:076dd7ec7eb4 | 246 | stop(); |
Helvis | 23:accd07ca2da7 | 247 | break; |
Helvis | 23:accd07ca2da7 | 248 | |
Helvis | 6:4868f789c223 | 249 | }else if ( ((countsL - countsLOld) >= MOVE_DIST || (countsR - countsROld) <= -MOVE_DIST) && (distanceC < 100.0f) && (distanceC > 40.0f)) { |
Helvis | 5:e2c0a4388d85 | 250 | countsLOld += 500; |
Helvis | 5:e2c0a4388d85 | 251 | countsROld += 500; |
Helvis | 31:2c54f8304ef5 | 252 | |
Helvis | 31:2c54f8304ef5 | 253 | //Stop after certain distance if side wall and front wall missing |
Helvis | 31:2c54f8304ef5 | 254 | }else if (sideWalls == true && speedRun == false && ( distanceL > 80.0f || distanceR > 80.0f ) && distanceC > 200.0f ) { |
Helvis | 31:2c54f8304ef5 | 255 | countsLOld = counterLeft.read(); |
Helvis | 31:2c54f8304ef5 | 256 | countsROld = counterRight.read(); |
Helvis | 31:2c54f8304ef5 | 257 | |
Helvis | 31:2c54f8304ef5 | 258 | while ((countsL - countsLOld) < MOVE_DIST*0.5f + 320.0f || (countsR - countsROld) > -0.5f*MOVE_DIST - 320.0f) { |
Helvis | 31:2c54f8304ef5 | 259 | countsL = counterLeft.read(); |
Helvis | 31:2c54f8304ef5 | 260 | countsR = counterRight.read(); |
Helvis | 31:2c54f8304ef5 | 261 | |
Helvis | 31:2c54f8304ef5 | 262 | if (speedRun == true) accel(RUN_SPEED); |
Helvis | 31:2c54f8304ef5 | 263 | else accel(MOVE_SPEED); |
Helvis | 31:2c54f8304ef5 | 264 | control(); |
Helvis | 31:2c54f8304ef5 | 265 | } |
Helvis | 31:2c54f8304ef5 | 266 | break; |
Helvis | 23:accd07ca2da7 | 267 | } |
Helvis | 23:accd07ca2da7 | 268 | } |
Helvis | 23:accd07ca2da7 | 269 | |
Helvis | 13:845e49f20426 | 270 | t.stop(); |
Helvis | 13:845e49f20426 | 271 | t.reset(); |
Helvis | 19:6cd6cc5c8b4c | 272 | lastMove = false; |
Helvis | 1:2b5f79285a3e | 273 | } |
Helvis | 1:2b5f79285a3e | 274 | /** |
Helvis | 1:2b5f79285a3e | 275 | * 90° Rotation nach Links |
Helvis | 1:2b5f79285a3e | 276 | */ |
Helvis | 1:2b5f79285a3e | 277 | void Motion::rotateL() { |
Helvis | 1:2b5f79285a3e | 278 | |
Helvis | 1:2b5f79285a3e | 279 | stop(); |
Helvis | 1:2b5f79285a3e | 280 | controller.counterReset(); |
Helvis | 1:2b5f79285a3e | 281 | countsLOld = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 282 | countsROld = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 283 | countsL = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 284 | countsR = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 285 | |
Helvis | 12:75d0291a9785 | 286 | controller.setDesiredSpeedLeft(-ROTATE_SPEED); |
Helvis | 12:75d0291a9785 | 287 | controller.setDesiredSpeedRight(-ROTATE_SPEED); |
Helvis | 1:2b5f79285a3e | 288 | |
Helvis | 13:845e49f20426 | 289 | while ((countsL - countsLOld) > -870 || (countsR - countsROld) > -870) { |
Helvis | 1:2b5f79285a3e | 290 | |
Helvis | 12:75d0291a9785 | 291 | //accel(); |
Helvis | 12:75d0291a9785 | 292 | |
Helvis | 1:2b5f79285a3e | 293 | countsL = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 294 | countsR = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 295 | |
Helvis | 1:2b5f79285a3e | 296 | if (enableMotorDriver == 0) {enableMotorDriver = 1;} |
Helvis | 12:75d0291a9785 | 297 | |
Helvis | 1:2b5f79285a3e | 298 | } |
Helvis | 1:2b5f79285a3e | 299 | |
Helvis | 1:2b5f79285a3e | 300 | stop(); |
Helvis | 1:2b5f79285a3e | 301 | } |
Helvis | 1:2b5f79285a3e | 302 | |
Helvis | 1:2b5f79285a3e | 303 | /** |
Helvis | 1:2b5f79285a3e | 304 | * 90° Rotation nach Rechts |
Helvis | 1:2b5f79285a3e | 305 | */ |
Helvis | 1:2b5f79285a3e | 306 | void Motion::rotateR() { |
Helvis | 1:2b5f79285a3e | 307 | |
Helvis | 1:2b5f79285a3e | 308 | stop(); |
Helvis | 1:2b5f79285a3e | 309 | controller.counterReset(); |
Helvis | 1:2b5f79285a3e | 310 | countsLOld = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 311 | countsROld = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 312 | countsL = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 313 | countsR = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 314 | |
Helvis | 12:75d0291a9785 | 315 | controller.setDesiredSpeedLeft(ROTATE_SPEED); |
Helvis | 12:75d0291a9785 | 316 | controller.setDesiredSpeedRight(ROTATE_SPEED); |
Helvis | 1:2b5f79285a3e | 317 | |
Helvis | 13:845e49f20426 | 318 | while ((countsL - countsLOld) < 870 || (countsR - countsROld) < 870) { |
Helvis | 1:2b5f79285a3e | 319 | |
Helvis | 12:75d0291a9785 | 320 | //accel(); |
Helvis | 12:75d0291a9785 | 321 | |
Helvis | 1:2b5f79285a3e | 322 | countsL = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 323 | countsR = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 324 | |
Helvis | 1:2b5f79285a3e | 325 | if (enableMotorDriver == 0) {enableMotorDriver = 1;} |
Helvis | 12:75d0291a9785 | 326 | |
Helvis | 1:2b5f79285a3e | 327 | } |
Helvis | 1:2b5f79285a3e | 328 | |
Helvis | 1:2b5f79285a3e | 329 | stop(); |
Helvis | 1:2b5f79285a3e | 330 | } |
Helvis | 2:f898adf2d817 | 331 | /** |
Helvis | 2:f898adf2d817 | 332 | * Links abbiegen |
Helvis | 2:f898adf2d817 | 333 | */ |
Helvis | 3:076dd7ec7eb4 | 334 | void Motion::turnL() { |
Helvis | 2:f898adf2d817 | 335 | |
Helvis | 2:f898adf2d817 | 336 | controller.counterReset(); |
Helvis | 2:f898adf2d817 | 337 | countsLOld = counterLeft.read(); |
Helvis | 2:f898adf2d817 | 338 | countsROld = counterRight.read(); |
Helvis | 2:f898adf2d817 | 339 | countsL = counterLeft.read(); |
Helvis | 2:f898adf2d817 | 340 | countsR = counterRight.read(); |
Helvis | 2:f898adf2d817 | 341 | |
luethale | 27:f111ba194412 | 342 | controller.setDesiredSpeedLeft(24.15f); |
luethale | 27:f111ba194412 | 343 | controller.setDesiredSpeedRight(-115.85f); |
Helvis | 2:f898adf2d817 | 344 | |
Helvis | 28:b7ce1e3bf08b | 345 | /* Velocity Settings: |
Helvis | 28:b7ce1e3bf08b | 346 | 50rpm -> 17.25 : 82.75 |
Helvis | 28:b7ce1e3bf08b | 347 | 60rpm -> 20.7 : 99.3 |
Helvis | 28:b7ce1e3bf08b | 348 | 70rpm -> 24.15 : 115.85 |
Helvis | 28:b7ce1e3bf08b | 349 | 80rpm -> 27.6 : 132.4 |
Helvis | 28:b7ce1e3bf08b | 350 | 90rpm -> 31.05 : 148.95 |
Helvis | 28:b7ce1e3bf08b | 351 | 100rpm -> 34.5 : 165.5 |
Helvis | 28:b7ce1e3bf08b | 352 | */ |
Helvis | 28:b7ce1e3bf08b | 353 | |
Helvis | 28:b7ce1e3bf08b | 354 | while ((countsL - countsLOld) < 446 || (countsR - countsROld) > -2141) { |
Helvis | 2:f898adf2d817 | 355 | |
Helvis | 2:f898adf2d817 | 356 | countsL = counterLeft.read(); |
Helvis | 2:f898adf2d817 | 357 | countsR = counterRight.read(); |
Helvis | 2:f898adf2d817 | 358 | |
Helvis | 2:f898adf2d817 | 359 | if (enableMotorDriver == 0) {enableMotorDriver = 1;} |
Helvis | 2:f898adf2d817 | 360 | } |
Helvis | 2:f898adf2d817 | 361 | } |
Helvis | 2:f898adf2d817 | 362 | /** |
Helvis | 2:f898adf2d817 | 363 | * Rechts abbiegen |
Helvis | 2:f898adf2d817 | 364 | */ |
Helvis | 3:076dd7ec7eb4 | 365 | void Motion::turnR() { |
Helvis | 2:f898adf2d817 | 366 | |
Helvis | 2:f898adf2d817 | 367 | controller.counterReset(); |
Helvis | 2:f898adf2d817 | 368 | countsLOld = counterLeft.read(); |
Helvis | 2:f898adf2d817 | 369 | countsROld = counterRight.read(); |
Helvis | 2:f898adf2d817 | 370 | countsL = counterLeft.read(); |
Helvis | 2:f898adf2d817 | 371 | countsR = counterRight.read(); |
Helvis | 2:f898adf2d817 | 372 | |
Helvis | 28:b7ce1e3bf08b | 373 | controller.setDesiredSpeedLeft(115.85f); |
luethale | 27:f111ba194412 | 374 | controller.setDesiredSpeedRight(-24.15f); |
Helvis | 2:f898adf2d817 | 375 | |
Helvis | 28:b7ce1e3bf08b | 376 | while ((countsL - countsLOld) < 2141 || (countsR - countsROld) > -446) { |
Helvis | 2:f898adf2d817 | 377 | |
Helvis | 2:f898adf2d817 | 378 | countsL = counterLeft.read(); |
Helvis | 2:f898adf2d817 | 379 | countsR = counterRight.read(); |
Helvis | 2:f898adf2d817 | 380 | |
Helvis | 2:f898adf2d817 | 381 | if (enableMotorDriver == 0) {enableMotorDriver = 1;} |
Helvis | 3:076dd7ec7eb4 | 382 | } |
Helvis | 2:f898adf2d817 | 383 | } |
Helvis | 1:2b5f79285a3e | 384 | /** |
Helvis | 1:2b5f79285a3e | 385 | * Motor Stop |
Helvis | 1:2b5f79285a3e | 386 | */ |
Helvis | 1:2b5f79285a3e | 387 | void Motion::stop() { |
Helvis | 1:2b5f79285a3e | 388 | |
Helvis | 1:2b5f79285a3e | 389 | controller.setDesiredSpeedLeft(0.0f); |
Helvis | 1:2b5f79285a3e | 390 | controller.setDesiredSpeedRight(0.0f); |
Helvis | 16:c5b864804632 | 391 | actSpeed = 0.0f; |
Helvis | 1:2b5f79285a3e | 392 | |
Helvis | 1:2b5f79285a3e | 393 | float sL = controller.getSpeedLeft(); |
Helvis | 1:2b5f79285a3e | 394 | float ticks = 0.08f*sL; |
Helvis | 1:2b5f79285a3e | 395 | |
Helvis | 1:2b5f79285a3e | 396 | waitStop = 0; |
Helvis | 1:2b5f79285a3e | 397 | while( waitStop < ticks) { |
Helvis | 16:c5b864804632 | 398 | controller.setDesiredSpeedLeft(0.0f); |
Helvis | 16:c5b864804632 | 399 | controller.setDesiredSpeedRight(0.0f); |
Helvis | 1:2b5f79285a3e | 400 | waitStop+= 1; |
Helvis | 1:2b5f79285a3e | 401 | } |
Helvis | 1:2b5f79285a3e | 402 | } |
Helvis | 1:2b5f79285a3e | 403 | /** |
Helvis | 1:2b5f79285a3e | 404 | * 180° Rotation |
Helvis | 1:2b5f79285a3e | 405 | */ |
Helvis | 1:2b5f79285a3e | 406 | void Motion::rotate180() { |
Helvis | 13:845e49f20426 | 407 | //1723 |
Helvis | 1:2b5f79285a3e | 408 | stop(); |
Helvis | 1:2b5f79285a3e | 409 | controller.counterReset(); |
Helvis | 1:2b5f79285a3e | 410 | countsLOld = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 411 | countsROld = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 412 | countsL = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 413 | countsR = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 414 | |
Helvis | 13:845e49f20426 | 415 | //controller.setDesiredSpeedLeft(-ROTATE_SPEED); |
Helvis | 13:845e49f20426 | 416 | //controller.setDesiredSpeedRight(-ROTATE_SPEED); |
Helvis | 13:845e49f20426 | 417 | |
Helvis | 13:845e49f20426 | 418 | t.start(); |
Helvis | 1:2b5f79285a3e | 419 | |
Helvis | 13:845e49f20426 | 420 | while ((countsL - countsLOld) > -900 || (countsR - countsROld) > -900) { |
Helvis | 1:2b5f79285a3e | 421 | |
Helvis | 13:845e49f20426 | 422 | actSpeed = 3.5f * t.read()*60.0f; |
Helvis | 13:845e49f20426 | 423 | |
Helvis | 13:845e49f20426 | 424 | controller.setDesiredSpeedLeft(-actSpeed); |
Helvis | 13:845e49f20426 | 425 | controller.setDesiredSpeedRight(-actSpeed); |
Helvis | 12:75d0291a9785 | 426 | |
Helvis | 1:2b5f79285a3e | 427 | countsL = counterLeft.read(); |
Helvis | 1:2b5f79285a3e | 428 | countsR = counterRight.read(); |
Helvis | 1:2b5f79285a3e | 429 | |
Helvis | 1:2b5f79285a3e | 430 | if (enableMotorDriver == 0) {enableMotorDriver = 1;} |
Helvis | 1:2b5f79285a3e | 431 | } |
Helvis | 1:2b5f79285a3e | 432 | |
Helvis | 13:845e49f20426 | 433 | t.reset(); |
Helvis | 13:845e49f20426 | 434 | |
Helvis | 13:845e49f20426 | 435 | avgSpeed = ( abs(controller.getSpeedLeft()) + abs(controller.getSpeedRight()) ) * 0.5f; |
Helvis | 13:845e49f20426 | 436 | |
Helvis | 13:845e49f20426 | 437 | while ((countsL - countsLOld) > -1720 || (countsR - countsROld) > -1720) { |
Helvis | 13:845e49f20426 | 438 | |
Helvis | 13:845e49f20426 | 439 | actSpeed = avgSpeed + (-3.5f * t.read()*60.0f); |
Helvis | 13:845e49f20426 | 440 | |
Helvis | 13:845e49f20426 | 441 | controller.setDesiredSpeedLeft(-actSpeed); |
Helvis | 13:845e49f20426 | 442 | controller.setDesiredSpeedRight(-actSpeed); |
Helvis | 13:845e49f20426 | 443 | |
Helvis | 13:845e49f20426 | 444 | countsL = counterLeft.read(); |
Helvis | 13:845e49f20426 | 445 | countsR = counterRight.read(); |
Helvis | 13:845e49f20426 | 446 | |
Helvis | 13:845e49f20426 | 447 | if (enableMotorDriver == 0) {enableMotorDriver = 1;} |
Helvis | 13:845e49f20426 | 448 | } |
Helvis | 13:845e49f20426 | 449 | t.stop(); |
Helvis | 13:845e49f20426 | 450 | t.reset(); |
Helvis | 1:2b5f79285a3e | 451 | stop(); |
Helvis | 1:2b5f79285a3e | 452 | } |
Helvis | 1:2b5f79285a3e | 453 | |
Helvis | 1:2b5f79285a3e | 454 | |
Helvis | 1:2b5f79285a3e | 455 | void Motion::control() { |
Helvis | 1:2b5f79285a3e | 456 | |
Helvis | 24:11c5fb5280eb | 457 | float wallLeft = 47.0f; //48.73 |
luethale | 26:f964408401fa | 458 | float wallRight = 44.0f; //51.03f; |
Helvis | 1:2b5f79285a3e | 459 | |
Helvis | 12:75d0291a9785 | 460 | distanceL = irSensorL.readL(); |
Helvis | 12:75d0291a9785 | 461 | distanceR = irSensorR.readR(); |
Helvis | 12:75d0291a9785 | 462 | |
Helvis | 6:4868f789c223 | 463 | |
luethale | 26:f964408401fa | 464 | /*if (distanceL < wallLeft-1.0f && distanceR < wallRight-1.0f) { |
Helvis | 12:75d0291a9785 | 465 | |
Helvis | 24:11c5fb5280eb | 466 | errorP = distanceL - distanceR - 3.0f; |
Helvis | 1:2b5f79285a3e | 467 | |
luethale | 26:f964408401fa | 468 | }else if (distanceL < LEFT_MID_VAL && distanceR > RIGHT_MID_VAL) { |
Helvis | 12:75d0291a9785 | 469 | |
Helvis | 12:75d0291a9785 | 470 | errorP = distanceL - LEFT_MID_VAL; |
Helvis | 24:11c5fb5280eb | 471 | |
Helvis | 12:75d0291a9785 | 472 | }else if (distanceL > LEFT_MID_VAL && distanceR < RIGHT_MID_VAL) { |
Helvis | 12:75d0291a9785 | 473 | |
Helvis | 12:75d0291a9785 | 474 | errorP = RIGHT_MID_VAL - distanceR; |
Helvis | 12:75d0291a9785 | 475 | |
Helvis | 30:bdb8c92434a0 | 476 | }*/ |
Helvis | 30:bdb8c92434a0 | 477 | |
Helvis | 30:bdb8c92434a0 | 478 | if (distanceL < wallLeft && distanceR > wallRight) { |
Helvis | 24:11c5fb5280eb | 479 | |
Helvis | 24:11c5fb5280eb | 480 | errorP = distanceL - wallLeft; |
Helvis | 24:11c5fb5280eb | 481 | |
Helvis | 24:11c5fb5280eb | 482 | }else if (distanceL > wallLeft && distanceR < wallRight) { |
Helvis | 24:11c5fb5280eb | 483 | |
Helvis | 24:11c5fb5280eb | 484 | errorP = wallRight - distanceR; |
Helvis | 24:11c5fb5280eb | 485 | |
luethale | 26:f964408401fa | 486 | }else if (distanceL < wallLeft+10.0f && distanceL > wallLeft && distanceR > wallRight) { |
luethale | 26:f964408401fa | 487 | |
luethale | 26:f964408401fa | 488 | errorP = distanceL - wallLeft; |
luethale | 26:f964408401fa | 489 | |
luethale | 26:f964408401fa | 490 | }else if (distanceR < wallRight+10.0f && distanceL > wallLeft && distanceR > wallRight) { |
Helvis | 24:11c5fb5280eb | 491 | |
Helvis | 24:11c5fb5280eb | 492 | errorP = wallRight - distanceR; |
Helvis | 24:11c5fb5280eb | 493 | |
Helvis | 12:75d0291a9785 | 494 | }else{ |
Helvis | 12:75d0291a9785 | 495 | |
Helvis | 12:75d0291a9785 | 496 | errorP = 0; |
Helvis | 12:75d0291a9785 | 497 | errorD = 0; |
Helvis | 12:75d0291a9785 | 498 | } |
Helvis | 12:75d0291a9785 | 499 | |
Helvis | 12:75d0291a9785 | 500 | errorD = errorP - oldErrorP; |
Helvis | 12:75d0291a9785 | 501 | |
Helvis | 12:75d0291a9785 | 502 | oldErrorP = errorP; |
Helvis | 12:75d0291a9785 | 503 | |
luethale | 26:f964408401fa | 504 | if (abs(errorP) < 80.0f) { |
Helvis | 12:75d0291a9785 | 505 | totalError = KP*errorP + KD*errorD; |
Helvis | 21:41997651337a | 506 | }/*else{ |
Helvis | 12:75d0291a9785 | 507 | totalError = 0; |
Helvis | 21:41997651337a | 508 | }*/ |
Helvis | 12:75d0291a9785 | 509 | |
Helvis | 13:845e49f20426 | 510 | controller.setDesiredSpeedLeft(actSpeed - totalError); |
Helvis | 13:845e49f20426 | 511 | controller.setDesiredSpeedRight(-actSpeed - totalError); |
Helvis | 1:2b5f79285a3e | 512 | } |
Helvis | 1:2b5f79285a3e | 513 | |
Helvis | 18:3309329d5f42 | 514 | void Motion::runTask(int path[],int task, bool reverse, int junction) { |
Helvis | 30:bdb8c92434a0 | 515 | |
Helvis | 30:bdb8c92434a0 | 516 | //reverse happens only in search run |
Helvis | 28:b7ce1e3bf08b | 517 | if (reverse == false) speedRun = true; |
Helvis | 28:b7ce1e3bf08b | 518 | else speedRun = false; |
luethale | 27:f111ba194412 | 519 | |
Helvis | 12:75d0291a9785 | 520 | switch(path[task]) { |
Helvis | 12:75d0291a9785 | 521 | |
Helvis | 12:75d0291a9785 | 522 | case 1: |
Helvis | 23:accd07ca2da7 | 523 | |
Helvis | 23:accd07ca2da7 | 524 | //Acceleration |
Helvis | 31:2c54f8304ef5 | 525 | if ( reverse == true && path[task-1] == path[task] && path[task+1] != path[task] && task != junction && task-1 != junction) { //if next move() and previous no move() and step no junction |
Helvis | 22:91526c8d15ba | 526 | |
Helvis | 17:8a8758bfe3c5 | 527 | acceleration = true; |
Helvis | 21:41997651337a | 528 | longMove = true; |
Helvis | 17:8a8758bfe3c5 | 529 | deceleration = false; |
Helvis | 22:91526c8d15ba | 530 | |
Helvis | 33:836ab2089565 | 531 | }else if (reverse == false && path[task+1] == path[task] && ( path[task-1] != path[task] || task == 0 || path[task-1] != 4) ) { //same as above, also if start field |
Helvis | 22:91526c8d15ba | 532 | |
Helvis | 12:75d0291a9785 | 533 | acceleration = true; |
Helvis | 21:41997651337a | 534 | longMove = true; |
Helvis | 15:0ae61d3d199f | 535 | deceleration = false; |
Helvis | 22:91526c8d15ba | 536 | |
Helvis | 12:75d0291a9785 | 537 | }else{ |
Helvis | 17:8a8758bfe3c5 | 538 | acceleration = false; |
Helvis | 17:8a8758bfe3c5 | 539 | avgSpeed = ( abs(controller.getSpeedLeft()) + abs(controller.getSpeedRight()) ) * 0.5f; |
Helvis | 12:75d0291a9785 | 540 | } |
Helvis | 23:accd07ca2da7 | 541 | |
Helvis | 23:accd07ca2da7 | 542 | //Deceleration |
Helvis | 30:bdb8c92434a0 | 543 | if (reverse == true && ( path[task-1] != path[task] || task == junction ) && avgSpeed > 2.4f*MOVE_SPEED) { //next step no move() or junction and speed above 120rpm |
Helvis | 22:91526c8d15ba | 544 | |
Helvis | 17:8a8758bfe3c5 | 545 | deceleration = true; |
Helvis | 17:8a8758bfe3c5 | 546 | acceleration = false; |
Helvis | 19:6cd6cc5c8b4c | 547 | lastMove = true; |
Helvis | 21:41997651337a | 548 | longMove = false; |
Helvis | 22:91526c8d15ba | 549 | |
Helvis | 30:bdb8c92434a0 | 550 | }else if (reverse == false && path[task+1] != path[task] && path[task+1] != 4 && avgSpeed > 2.4f*MOVE_SPEED) { //next step no move() and no moveHalf() and speed above 120rpm |
Helvis | 22:91526c8d15ba | 551 | |
Helvis | 12:75d0291a9785 | 552 | deceleration = true; |
Helvis | 15:0ae61d3d199f | 553 | acceleration = false; |
Helvis | 19:6cd6cc5c8b4c | 554 | lastMove = true; |
Helvis | 21:41997651337a | 555 | longMove = false; |
Helvis | 22:91526c8d15ba | 556 | |
Helvis | 30:bdb8c92434a0 | 557 | }else if (reverse == false && path[task+1] != path[task] && path[task+1] == 4 && avgSpeed > 2.4f*MOVE_SPEED) { |
Helvis | 30:bdb8c92434a0 | 558 | |
Helvis | 30:bdb8c92434a0 | 559 | lastMove = true; |
Helvis | 30:bdb8c92434a0 | 560 | |
Helvis | 12:75d0291a9785 | 561 | }else{ |
Helvis | 12:75d0291a9785 | 562 | deceleration = false; |
Helvis | 12:75d0291a9785 | 563 | } |
Helvis | 12:75d0291a9785 | 564 | |
Helvis | 17:8a8758bfe3c5 | 565 | //printf("\nSchritt: %d Befehl: %d Reverse: %d acceleration: %d deceleration: %d\n", task, path[task], reverse, acceleration, deceleration); |
Helvis | 17:8a8758bfe3c5 | 566 | //printf("\nVor: %d Nach: %d Speed: %f\n\n", path[task+1], path[task-1], avgSpeed); |
Helvis | 17:8a8758bfe3c5 | 567 | |
Helvis | 12:75d0291a9785 | 568 | move(); |
Helvis | 12:75d0291a9785 | 569 | break; |
Helvis | 12:75d0291a9785 | 570 | case 2: |
Helvis | 12:75d0291a9785 | 571 | rotateL(); |
Helvis | 12:75d0291a9785 | 572 | break; |
Helvis | 12:75d0291a9785 | 573 | case 3: |
Helvis | 12:75d0291a9785 | 574 | rotateR(); |
Helvis | 12:75d0291a9785 | 575 | break; |
Helvis | 12:75d0291a9785 | 576 | case 4: |
Helvis | 23:accd07ca2da7 | 577 | |
Helvis | 30:bdb8c92434a0 | 578 | if (reverse == false && path[task] == 4 && path[task+1] == 1) { //accelerate if next step is move() |
Helvis | 23:accd07ca2da7 | 579 | |
Helvis | 23:accd07ca2da7 | 580 | acceleration = true; |
Helvis | 23:accd07ca2da7 | 581 | longMove = true; |
Helvis | 23:accd07ca2da7 | 582 | deceleration = false; |
Helvis | 23:accd07ca2da7 | 583 | |
Helvis | 23:accd07ca2da7 | 584 | }else{ |
Helvis | 23:accd07ca2da7 | 585 | acceleration = false; |
Helvis | 23:accd07ca2da7 | 586 | avgSpeed = ( abs(controller.getSpeedLeft()) + abs(controller.getSpeedRight()) ) * 0.5f; |
Helvis | 23:accd07ca2da7 | 587 | } |
Helvis | 23:accd07ca2da7 | 588 | |
Helvis | 30:bdb8c92434a0 | 589 | if (reverse == false && path[task-1] == 1 && path[task] == 4 && path[task+1] != 0) { //decelerate if previous step was move() |
Helvis | 23:accd07ca2da7 | 590 | |
Helvis | 23:accd07ca2da7 | 591 | deceleration = true; |
Helvis | 23:accd07ca2da7 | 592 | acceleration = false; |
Helvis | 23:accd07ca2da7 | 593 | longMove = false; |
Helvis | 23:accd07ca2da7 | 594 | |
Helvis | 23:accd07ca2da7 | 595 | }else{ |
Helvis | 23:accd07ca2da7 | 596 | deceleration = false; |
Helvis | 23:accd07ca2da7 | 597 | } |
Helvis | 23:accd07ca2da7 | 598 | |
Helvis | 23:accd07ca2da7 | 599 | if (reverse == false && path[task+1] == 0) { |
Helvis | 23:accd07ca2da7 | 600 | |
Helvis | 23:accd07ca2da7 | 601 | lastMove = true; |
Helvis | 23:accd07ca2da7 | 602 | } |
Helvis | 23:accd07ca2da7 | 603 | |
Helvis | 23:accd07ca2da7 | 604 | moveHalf(); |
Helvis | 23:accd07ca2da7 | 605 | break; |
Helvis | 23:accd07ca2da7 | 606 | case 5: |
Helvis | 23:accd07ca2da7 | 607 | turnL(); |
Helvis | 23:accd07ca2da7 | 608 | break; |
Helvis | 23:accd07ca2da7 | 609 | case 6: |
Helvis | 23:accd07ca2da7 | 610 | turnR(); |
Helvis | 23:accd07ca2da7 | 611 | break; |
Helvis | 23:accd07ca2da7 | 612 | case 7: |
Helvis | 23:accd07ca2da7 | 613 | break; |
Helvis | 12:75d0291a9785 | 614 | } |
Helvis | 1:2b5f79285a3e | 615 | } |
Helvis | 1:2b5f79285a3e | 616 | |
Helvis | 1:2b5f79285a3e | 617 | int Motion::finish() { |
Helvis | 12:75d0291a9785 | 618 | |
Helvis | 12:75d0291a9785 | 619 | return line; |
Helvis | 1:2b5f79285a3e | 620 | } |
Helvis | 11:2960fc540616 | 621 | |
Helvis | 12:75d0291a9785 | 622 | |
Helvis | 13:845e49f20426 | 623 | void Motion::accel(float targetSpeed) { |
Helvis | 13:845e49f20426 | 624 | |
luethale | 27:f111ba194412 | 625 | float fastSpeed; |
luethale | 27:f111ba194412 | 626 | |
Helvis | 28:b7ce1e3bf08b | 627 | //Acclerated Target Speed |
luethale | 27:f111ba194412 | 628 | if (speedRun == true) fastSpeed = targetSpeed*2.0f; |
luethale | 27:f111ba194412 | 629 | else fastSpeed = targetSpeed*2.8f; |
Helvis | 13:845e49f20426 | 630 | |
Helvis | 12:75d0291a9785 | 631 | avgSpeed = ( abs(controller.getSpeedLeft()) + abs(controller.getSpeedRight()) ) * 0.5f; |
Helvis | 28:b7ce1e3bf08b | 632 | |
Helvis | 28:b7ce1e3bf08b | 633 | //Acceleration logic |
Helvis | 28:b7ce1e3bf08b | 634 | |
Helvis | 28:b7ce1e3bf08b | 635 | //Short distance |
Helvis | 28:b7ce1e3bf08b | 636 | if (avgSpeed < targetSpeed && deceleration == false && acceleration == false && longMove == false) { |
Helvis | 12:75d0291a9785 | 637 | |
Helvis | 28:b7ce1e3bf08b | 638 | actSpeed = ACCEL_CONST * t.read()*60.0f; //Acceleration equation |
Helvis | 19:6cd6cc5c8b4c | 639 | |
Helvis | 21:41997651337a | 640 | }else if(avgSpeed > targetSpeed+5.0f && deceleration == false && acceleration == false && longMove == false) { |
Helvis | 21:41997651337a | 641 | |
Helvis | 28:b7ce1e3bf08b | 642 | actSpeed = targetSpeed+5.0f; //Keep Speed steady in case of overshooting |
Helvis | 21:41997651337a | 643 | |
Helvis | 28:b7ce1e3bf08b | 644 | //Long distance |
Helvis | 28:b7ce1e3bf08b | 645 | }else if (avgSpeed < fastSpeed && acceleration == true && deceleration == false) { |
Helvis | 12:75d0291a9785 | 646 | |
Helvis | 28:b7ce1e3bf08b | 647 | actSpeed = ACCEL_CONST * t.read()*60.0f; |
Helvis | 16:c5b864804632 | 648 | |
Helvis | 28:b7ce1e3bf08b | 649 | }else if (avgSpeed > targetSpeed+5.0f && acceleration == false && deceleration == true) { |
Helvis | 11:2960fc540616 | 650 | |
Helvis | 28:b7ce1e3bf08b | 651 | actSpeed = fastSpeed - ACCEL_CONST * t.read()*60.0f; //Deceleration equation |
Helvis | 24:11c5fb5280eb | 652 | |
Helvis | 28:b7ce1e3bf08b | 653 | }else if (avgSpeed < targetSpeed && acceleration == false && deceleration == true) { |
Helvis | 24:11c5fb5280eb | 654 | |
Helvis | 28:b7ce1e3bf08b | 655 | actSpeed = targetSpeed+5.0f; //Limit deceleration in case of overshooting |
Helvis | 24:11c5fb5280eb | 656 | |
Helvis | 24:11c5fb5280eb | 657 | } |
Helvis | 12:75d0291a9785 | 658 | } |
Helvis | 12:75d0291a9785 | 659 |