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 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 00064 00065 // implementation of the state machine 00066 00067 switch (state) { 00068 00069 case ROBOT_OFF: 00070 00071 while(taskList.size() > 0){ 00072 delete taskList.front(); 00073 taskList.pop_front(); 00074 } 00075 00076 buttonNow = button; 00077 00078 if (buttonNow && !buttonBefore) { // detect button rising edge 00079 00080 enableMotorDriver = 1; 00081 00082 // controller.setTranslationalVelocity(TRANSLATIONAL_VELOCITY); 00083 // controller.setRotationalVelocity(0.0f); 00084 taskList.push_back(new TaskMoveTo(controller, 0.0f, 1.0f, 0.0f)); 00085 //taskList.push_back(new TaskWait(controller,2.0f)); 00086 taskList.push_back(new TaskMoveTo(controller, 0.1f, 0.9f, 0.0f)); 00087 taskList.push_back(new TaskMoveTo(controller, 0.0f, 0.0f, 0.0f)); 00088 state = MOVE_FORWARD; 00089 } 00090 00091 buttonBefore = buttonNow; 00092 00093 break; 00094 00095 case MOVE_FORWARD: 00096 00097 buttonNow = button; 00098 00099 if (buttonNow && !buttonBefore) { // detect button rising edge 00100 00101 controller.setTranslationalVelocity(0.0f); 00102 controller.setRotationalVelocity(0.0f); 00103 00104 state = SLOWING_DOWN; 00105 00106 } else if ((irSensor3 < DISTANCE_THRESHOLD) || (irSensor4 < DISTANCE_THRESHOLD)) { 00107 00108 controller.setTranslationalVelocity(0.0f); 00109 controller.setRotationalVelocity(ROTATIONAL_VELOCITY); 00110 00111 state = TURN_LEFT; 00112 00113 } else if (irSensor2 < DISTANCE_THRESHOLD) { 00114 00115 controller.setTranslationalVelocity(0.0f); 00116 controller.setRotationalVelocity(-ROTATIONAL_VELOCITY); 00117 00118 state = TURN_RIGHT; 00119 00120 } else { 00121 00122 if(taskList.size() > 0){ 00123 00124 Task* task = taskList.front(); 00125 int result = task->run(PERIOD); 00126 00127 if(result == Task::DONE){ 00128 taskList.pop_front(); 00129 delete task; 00130 } 00131 } 00132 } 00133 00134 buttonBefore = buttonNow; 00135 00136 break; 00137 00138 case TURN_LEFT: 00139 00140 buttonNow = button; 00141 00142 if (buttonNow && !buttonBefore) { // detect button rising edge 00143 00144 controller.setRotationalVelocity(0.0f); 00145 00146 state = SLOWING_DOWN; 00147 00148 } else if ((irSensor2 > DISTANCE_THRESHOLD) && (irSensor3 > DISTANCE_THRESHOLD) && (irSensor4 > DISTANCE_THRESHOLD)) { 00149 00150 controller.setTranslationalVelocity(TRANSLATIONAL_VELOCITY); 00151 controller.setRotationalVelocity(0.0f); 00152 00153 state = MOVE_FORWARD; 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.setRotationalVelocity(0.0f); 00167 00168 state = SLOWING_DOWN; 00169 00170 } else if ((irSensor2 > DISTANCE_THRESHOLD) && (irSensor3 > DISTANCE_THRESHOLD) && (irSensor4 > DISTANCE_THRESHOLD)) { 00171 00172 controller.setTranslationalVelocity(TRANSLATIONAL_VELOCITY); 00173 controller.setRotationalVelocity(0.0f); 00174 00175 state = MOVE_FORWARD; 00176 } 00177 00178 buttonBefore = buttonNow; 00179 00180 break; 00181 00182 case SLOWING_DOWN: 00183 00184 if ((fabs(controller.getActualTranslationalVelocity()) < 0.01f) && (fabs(controller.getActualRotationalVelocity()) < 0.01f)) { 00185 00186 enableMotorDriver = 0; 00187 00188 state = ROBOT_OFF; 00189 } 00190 00191 break; 00192 00193 default: 00194 00195 state = ROBOT_OFF; 00196 } 00197 } 00198
Generated on Wed Jul 20 2022 03:42:12 by
