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