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