Marco Oehler
/
Lab3
ROME2 Lab3
StateMachine.h@2:fc9e2aebf9d5, 2020-03-25 (annotated)
- Committer:
- oehlemar
- Date:
- Wed Mar 25 14:15:52 2020 +0000
- Revision:
- 2:fc9e2aebf9d5
- Parent:
- 1:ff05970bb9b0
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oehlemar | 0:6a4d3264c067 | 1 | /* |
oehlemar | 0:6a4d3264c067 | 2 | * StateMachine.h |
oehlemar | 0:6a4d3264c067 | 3 | * Copyright (c) 2020, ZHAW |
oehlemar | 0:6a4d3264c067 | 4 | * All rights reserved. |
oehlemar | 0:6a4d3264c067 | 5 | */ |
oehlemar | 0:6a4d3264c067 | 6 | |
oehlemar | 0:6a4d3264c067 | 7 | #ifndef STATE_MACHINE_H_ |
oehlemar | 0:6a4d3264c067 | 8 | #define STATE_MACHINE_H_ |
oehlemar | 0:6a4d3264c067 | 9 | |
oehlemar | 1:ff05970bb9b0 | 10 | #include <deque> |
oehlemar | 1:ff05970bb9b0 | 11 | #include "Task.h" |
oehlemar | 1:ff05970bb9b0 | 12 | #include "TaskWait.h" |
oehlemar | 1:ff05970bb9b0 | 13 | #include "TaskMoveTo.h" |
oehlemar | 0:6a4d3264c067 | 14 | #include <cstdlib> |
oehlemar | 0:6a4d3264c067 | 15 | #include <mbed.h> |
oehlemar | 0:6a4d3264c067 | 16 | #include "Controller.h" |
oehlemar | 0:6a4d3264c067 | 17 | #include "IRSensor.h" |
oehlemar | 0:6a4d3264c067 | 18 | |
oehlemar | 0:6a4d3264c067 | 19 | /** |
oehlemar | 0:6a4d3264c067 | 20 | * This class implements a simple state machine for a mobile robot. |
oehlemar | 0:6a4d3264c067 | 21 | * It allows to move the robot forward, and to turn left or right, |
oehlemar | 0:6a4d3264c067 | 22 | * depending on distance measurements, to avoid collisions with |
oehlemar | 0:6a4d3264c067 | 23 | * obstacles. |
oehlemar | 0:6a4d3264c067 | 24 | */ |
oehlemar | 0:6a4d3264c067 | 25 | class StateMachine { |
oehlemar | 0:6a4d3264c067 | 26 | |
oehlemar | 0:6a4d3264c067 | 27 | public: |
oehlemar | 0:6a4d3264c067 | 28 | |
oehlemar | 0:6a4d3264c067 | 29 | static const int ROBOT_OFF = 0; // discrete states of this state machine |
oehlemar | 0:6a4d3264c067 | 30 | static const int MOVE_FORWARD = 1; |
oehlemar | 0:6a4d3264c067 | 31 | static const int TURN_LEFT = 2; |
oehlemar | 0:6a4d3264c067 | 32 | static const int TURN_RIGHT = 3; |
oehlemar | 0:6a4d3264c067 | 33 | static const int SLOWING_DOWN = 4; |
oehlemar | 0:6a4d3264c067 | 34 | |
oehlemar | 0:6a4d3264c067 | 35 | StateMachine(Controller& controller, DigitalOut& enableMotorDriver, DigitalOut& led0, DigitalOut& led1, DigitalOut& led2, DigitalOut& led3, DigitalOut& led4, DigitalOut& led5, DigitalIn& button, IRSensor& irSensor0, IRSensor& irSensor1, IRSensor& irSensor2, IRSensor& irSensor3, IRSensor& irSensor4, IRSensor& irSensor5); |
oehlemar | 0:6a4d3264c067 | 36 | virtual ~StateMachine(); |
oehlemar | 0:6a4d3264c067 | 37 | int getState(); |
oehlemar | 0:6a4d3264c067 | 38 | |
oehlemar | 0:6a4d3264c067 | 39 | private: |
oehlemar | 0:6a4d3264c067 | 40 | |
oehlemar | 0:6a4d3264c067 | 41 | static const float PERIOD; // period of task in [s] |
oehlemar | 0:6a4d3264c067 | 42 | static const float DISTANCE_THRESHOLD; // minimum allowed distance to obstacle in [m] |
oehlemar | 0:6a4d3264c067 | 43 | static const float TRANSLATIONAL_VELOCITY; // translational velocity in [m/s] |
oehlemar | 0:6a4d3264c067 | 44 | static const float ROTATIONAL_VELOCITY; // rotational velocity in [rad/s] |
oehlemar | 0:6a4d3264c067 | 45 | |
oehlemar | 0:6a4d3264c067 | 46 | Controller& controller; |
oehlemar | 0:6a4d3264c067 | 47 | DigitalOut& enableMotorDriver; |
oehlemar | 0:6a4d3264c067 | 48 | DigitalOut& led0; |
oehlemar | 0:6a4d3264c067 | 49 | DigitalOut& led1; |
oehlemar | 0:6a4d3264c067 | 50 | DigitalOut& led2; |
oehlemar | 0:6a4d3264c067 | 51 | DigitalOut& led3; |
oehlemar | 0:6a4d3264c067 | 52 | DigitalOut& led4; |
oehlemar | 0:6a4d3264c067 | 53 | DigitalOut& led5; |
oehlemar | 0:6a4d3264c067 | 54 | DigitalIn& button; |
oehlemar | 0:6a4d3264c067 | 55 | IRSensor& irSensor0; |
oehlemar | 0:6a4d3264c067 | 56 | IRSensor& irSensor1; |
oehlemar | 0:6a4d3264c067 | 57 | IRSensor& irSensor2; |
oehlemar | 0:6a4d3264c067 | 58 | IRSensor& irSensor3; |
oehlemar | 0:6a4d3264c067 | 59 | IRSensor& irSensor4; |
oehlemar | 0:6a4d3264c067 | 60 | IRSensor& irSensor5; |
oehlemar | 0:6a4d3264c067 | 61 | int state; |
oehlemar | 0:6a4d3264c067 | 62 | int buttonNow; |
oehlemar | 0:6a4d3264c067 | 63 | int buttonBefore; |
oehlemar | 0:6a4d3264c067 | 64 | Ticker ticker; |
oehlemar | 0:6a4d3264c067 | 65 | |
oehlemar | 0:6a4d3264c067 | 66 | void run(); |
oehlemar | 1:ff05970bb9b0 | 67 | |
oehlemar | 1:ff05970bb9b0 | 68 | deque<Task*> taskList; |
oehlemar | 0:6a4d3264c067 | 69 | }; |
oehlemar | 0:6a4d3264c067 | 70 | |
oehlemar | 0:6a4d3264c067 | 71 | #endif /* STATE_MACHINE_H_ */ |
oehlemar | 0:6a4d3264c067 | 72 |