Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
StateMachine.h@3:4db174c9e441, 2020-03-25 (annotated)
- Committer:
- zimmeer1
- Date:
- Wed Mar 25 16:12:12 2020 +0000
- Revision:
- 3:4db174c9e441
- Parent:
- 1:c5408c1c1b81
works;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
koenithi | 0:d8d297847268 | 1 | /* |
koenithi | 0:d8d297847268 | 2 | * StateMachine.h |
koenithi | 0:d8d297847268 | 3 | * Copyright (c) 2020, ZHAW |
koenithi | 0:d8d297847268 | 4 | * All rights reserved. |
koenithi | 0:d8d297847268 | 5 | */ |
koenithi | 0:d8d297847268 | 6 | |
koenithi | 0:d8d297847268 | 7 | #ifndef STATE_MACHINE_H_ |
koenithi | 0:d8d297847268 | 8 | #define STATE_MACHINE_H_ |
koenithi | 0:d8d297847268 | 9 | |
koenithi | 0:d8d297847268 | 10 | #include <cstdlib> |
koenithi | 0:d8d297847268 | 11 | #include <mbed.h> |
koenithi | 0:d8d297847268 | 12 | #include "Controller.h" |
koenithi | 0:d8d297847268 | 13 | #include "IRSensor.h" |
koenithi | 0:d8d297847268 | 14 | |
koenithi | 1:c5408c1c1b81 | 15 | #include <deque> |
koenithi | 1:c5408c1c1b81 | 16 | #include "Task.h" |
koenithi | 1:c5408c1c1b81 | 17 | #include "TaskWait.h" |
koenithi | 1:c5408c1c1b81 | 18 | #include "TaskMoveTo.h" |
koenithi | 1:c5408c1c1b81 | 19 | |
koenithi | 1:c5408c1c1b81 | 20 | |
koenithi | 0:d8d297847268 | 21 | /** |
koenithi | 0:d8d297847268 | 22 | * This class implements a simple state machine for a mobile robot. |
koenithi | 0:d8d297847268 | 23 | * It allows to move the robot forward, and to turn left or right, |
koenithi | 0:d8d297847268 | 24 | * depending on distance measurements, to avoid collisions with |
koenithi | 0:d8d297847268 | 25 | * obstacles. |
koenithi | 0:d8d297847268 | 26 | */ |
koenithi | 0:d8d297847268 | 27 | class StateMachine { |
koenithi | 0:d8d297847268 | 28 | |
koenithi | 0:d8d297847268 | 29 | public: |
koenithi | 0:d8d297847268 | 30 | |
koenithi | 0:d8d297847268 | 31 | static const int ROBOT_OFF = 0; // discrete states of this state machine |
koenithi | 0:d8d297847268 | 32 | static const int MOVE_FORWARD = 1; |
koenithi | 0:d8d297847268 | 33 | static const int TURN_LEFT = 2; |
koenithi | 0:d8d297847268 | 34 | static const int TURN_RIGHT = 3; |
koenithi | 0:d8d297847268 | 35 | static const int SLOWING_DOWN = 4; |
koenithi | 0:d8d297847268 | 36 | |
koenithi | 0:d8d297847268 | 37 | 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); |
koenithi | 0:d8d297847268 | 38 | virtual ~StateMachine(); |
koenithi | 0:d8d297847268 | 39 | int getState(); |
koenithi | 0:d8d297847268 | 40 | |
koenithi | 0:d8d297847268 | 41 | private: |
koenithi | 0:d8d297847268 | 42 | |
koenithi | 0:d8d297847268 | 43 | static const float PERIOD; // period of task in [s] |
koenithi | 0:d8d297847268 | 44 | static const float DISTANCE_THRESHOLD; // minimum allowed distance to obstacle in [m] |
koenithi | 0:d8d297847268 | 45 | static const float TRANSLATIONAL_VELOCITY; // translational velocity in [m/s] |
koenithi | 0:d8d297847268 | 46 | static const float ROTATIONAL_VELOCITY; // rotational velocity in [rad/s] |
koenithi | 0:d8d297847268 | 47 | |
koenithi | 0:d8d297847268 | 48 | Controller& controller; |
koenithi | 0:d8d297847268 | 49 | DigitalOut& enableMotorDriver; |
koenithi | 0:d8d297847268 | 50 | DigitalOut& led0; |
koenithi | 0:d8d297847268 | 51 | DigitalOut& led1; |
koenithi | 0:d8d297847268 | 52 | DigitalOut& led2; |
koenithi | 0:d8d297847268 | 53 | DigitalOut& led3; |
koenithi | 0:d8d297847268 | 54 | DigitalOut& led4; |
koenithi | 0:d8d297847268 | 55 | DigitalOut& led5; |
koenithi | 0:d8d297847268 | 56 | DigitalIn& button; |
koenithi | 0:d8d297847268 | 57 | IRSensor& irSensor0; |
koenithi | 0:d8d297847268 | 58 | IRSensor& irSensor1; |
koenithi | 0:d8d297847268 | 59 | IRSensor& irSensor2; |
koenithi | 0:d8d297847268 | 60 | IRSensor& irSensor3; |
koenithi | 0:d8d297847268 | 61 | IRSensor& irSensor4; |
koenithi | 0:d8d297847268 | 62 | IRSensor& irSensor5; |
koenithi | 0:d8d297847268 | 63 | int state; |
koenithi | 0:d8d297847268 | 64 | int buttonNow; |
koenithi | 0:d8d297847268 | 65 | int buttonBefore; |
koenithi | 0:d8d297847268 | 66 | Ticker ticker; |
koenithi | 1:c5408c1c1b81 | 67 | int result; |
koenithi | 1:c5408c1c1b81 | 68 | void run(); |
koenithi | 0:d8d297847268 | 69 | |
koenithi | 1:c5408c1c1b81 | 70 | deque<Task*> taskList; |
koenithi | 1:c5408c1c1b81 | 71 | Task* task; |
koenithi | 0:d8d297847268 | 72 | }; |
koenithi | 0:d8d297847268 | 73 | |
koenithi | 0:d8d297847268 | 74 | #endif /* STATE_MACHINE_H_ */ |
koenithi | 0:d8d297847268 | 75 |