Zürcher Eliteeinheit / Mbed 2 deprecated ROME2_P6

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StateMachine.cpp Source File

StateMachine.cpp

00001 /*
00002  * StateMachine.cpp
00003  * Copyright (c) 2018, ZHAW
00004  * All rights reserved.
00005  */
00006 
00007 #include <cmath>
00008 #include "StateMachine.h"
00009 
00010 using namespace std;
00011 
00012 const float StateMachine::PERIOD = 0.01f;                   // period of task in [s]
00013 const float StateMachine::DISTANCE_THRESHOLD = 0.2f;        // minimum allowed distance to obstacle in [m]
00014 const float StateMachine::TRANSLATIONAL_VELOCITY = 0.3f;    // translational velocity in [m/s]
00015 const float StateMachine::ROTATIONAL_VELOCITY = 1.0f;       // rotational velocity in [rad/s]
00016 
00017 /**
00018  * Creates and initializes a state machine object.
00019  */
00020 StateMachine::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) : controller(controller), enableMotorDriver(enableMotorDriver), led0(led0), led1(led1), led2(led2), led3(led3), led4(led4), led5(led5), button(button), irSensor0(irSensor0), irSensor1(irSensor1), irSensor2(irSensor2), irSensor3(irSensor3), irSensor4(irSensor4), irSensor5(irSensor5) {
00021     
00022     enableMotorDriver = 0;
00023     state = ROBOT_OFF;
00024     buttonNow = button;
00025     buttonBefore = buttonNow;
00026     taskList.clear();
00027     
00028     ticker.attach(callback(this, &StateMachine::run), PERIOD);
00029 }
00030 
00031 /**
00032  * Deletes the state machine object and releases all allocated resources.
00033  */
00034 StateMachine::~StateMachine() {
00035     
00036     ticker.detach();
00037 }
00038 
00039 /**
00040  * Gets the actual state of this state machine.
00041  * @return the actual state as an int constant.
00042  */
00043 int StateMachine::getState() {
00044     
00045     return state;
00046 }
00047 
00048 /**
00049  * This method is called periodically by the ticker object and implements the
00050  * logic of the state machine.
00051  */
00052 void StateMachine::run() {
00053     
00054     // set the leds based on distance measurements
00055     /*
00056     led0 = irSensor0 < DISTANCE_THRESHOLD;
00057     led1 = irSensor1 < DISTANCE_THRESHOLD;
00058     led2 = irSensor2 < DISTANCE_THRESHOLD;
00059     led3 = irSensor3 < DISTANCE_THRESHOLD;
00060     led4 = irSensor4 < DISTANCE_THRESHOLD;
00061     led5 = irSensor5 < DISTANCE_THRESHOLD;
00062     */
00063     // implementation of the state machine
00064     
00065     switch (state) {
00066         
00067         case ROBOT_OFF:
00068             
00069             buttonNow = button;
00070             
00071             if (buttonNow && !buttonBefore) {   // detect button rising edge
00072                 
00073                 enableMotorDriver = 1;
00074                 
00075                 taskList.push_back(new TaskWait(controller, 0.5f));
00076                 
00077                 
00078                    // move to waypoints
00079                 taskList.push_back(new TaskMoveToWaypoint(controller,2,-0.3,0.2f));
00080                 taskList.push_back(new TaskMoveToWaypoint(controller,2.4,0.3,0.2f));
00081                 taskList.push_back(new TaskMoveToWaypoint(controller,3.2,0.25,0.2f));
00082                 taskList.push_back(new TaskMoveToWaypoint(controller,2,0.8,0.2f));
00083                 taskList.push_back(new TaskMoveToWaypoint(controller,0.8,1.2,0.2f));
00084                 taskList.push_back(new TaskMoveToWaypoint(controller,0.8,1.2,0.2f));
00085                 taskList.push_back(new TaskMoveTo(controller, 0.4, 0.5f, -3.14f, 0.2f, 0.1f));
00086                 
00087                 state = PROCESSING_TASKS;
00088             }
00089             
00090             buttonBefore = buttonNow;
00091             
00092             break;
00093             
00094         case PROCESSING_TASKS:
00095             
00096             buttonNow = button;
00097             
00098             if (buttonNow && !buttonBefore) {   // detect button rising edge
00099                 
00100                 controller.setTranslationalVelocity(0.0f);
00101                 controller.setRotationalVelocity(0.0f);
00102                 
00103                 state = SLOWING_DOWN;
00104                 /*
00105             } else if ((irSensor0 < DISTANCE_THRESHOLD) || (irSensor1 < DISTANCE_THRESHOLD)) {
00106                 
00107                 controller.setTranslationalVelocity(0.0f);
00108                 controller.setRotationalVelocity(ROTATIONAL_VELOCITY);
00109                 
00110                 state = TURN_LEFT;
00111                 
00112             } else if (irSensor5 < DISTANCE_THRESHOLD) {
00113                 
00114                 controller.setTranslationalVelocity(0.0f);
00115                 controller.setRotationalVelocity(-ROTATIONAL_VELOCITY);
00116                 
00117                 state = TURN_RIGHT;
00118                 */
00119             } else if (taskList.size() > 0) {
00120                 
00121                 Task* task = taskList.front();
00122                 int result = task->run(PERIOD);
00123                 if (result == Task::DONE) {
00124                     taskList.pop_front();
00125                     delete task;
00126                 }
00127                 
00128             } else {
00129                 
00130                 controller.setTranslationalVelocity(0.0f);
00131                 controller.setRotationalVelocity(0.0f);
00132                 
00133                 state = SLOWING_DOWN;
00134             }
00135             
00136             buttonBefore = buttonNow;
00137             
00138             break;
00139             
00140         case TURN_LEFT:
00141             
00142             buttonNow = button;
00143             
00144             if (buttonNow && !buttonBefore) {   // detect button rising edge
00145                 
00146                 controller.setTranslationalVelocity(0.0f);
00147                 controller.setRotationalVelocity(0.0f);
00148                 
00149                 state = SLOWING_DOWN;
00150                 
00151             } else if ((irSensor0 > DISTANCE_THRESHOLD) && (irSensor1 > DISTANCE_THRESHOLD) && (irSensor5 > DISTANCE_THRESHOLD)) {
00152                 
00153                 state = PROCESSING_TASKS;
00154             }
00155             
00156             buttonBefore = buttonNow;
00157             
00158             break;
00159             
00160         case TURN_RIGHT:
00161             
00162             buttonNow = button;
00163             
00164             if (buttonNow && !buttonBefore) {   // detect button rising edge
00165                 
00166                 controller.setTranslationalVelocity(0.0f);
00167                 controller.setRotationalVelocity(0.0f);
00168                 
00169                 state = SLOWING_DOWN;
00170                 
00171             } else if ((irSensor0 > DISTANCE_THRESHOLD) && (irSensor1 > DISTANCE_THRESHOLD) && (irSensor5 > DISTANCE_THRESHOLD)) {
00172                 
00173                 state = PROCESSING_TASKS;
00174             }
00175             
00176             buttonBefore = buttonNow;
00177             
00178             break;
00179             
00180         case SLOWING_DOWN:
00181             
00182             if ((fabs(controller.getActualTranslationalVelocity()) < 0.01f) && (fabs(controller.getActualRotationalVelocity()) < 0.01f)) {
00183                 
00184                 enableMotorDriver = 0;
00185                 
00186                 while (taskList.size() > 0) {
00187                     delete taskList.front();
00188                     taskList.pop_front();
00189                 }
00190                 
00191                 state = ROBOT_OFF;
00192             }
00193             
00194             break;
00195             
00196         default:
00197             
00198             state = ROBOT_OFF;
00199     }
00200 }
00201