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