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.
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 taskList.push_back(new TaskMoveToWaypoint(controller, 1.0f, 0.0f, 0.2f)); 00077 taskList.push_back(new TaskMoveToWaypoint(controller, 1.0f, 1.0f, 0.2f)); 00078 taskList.push_back(new TaskMoveToWaypoint(controller, 0.0f, 1.0f, 0.2f)); 00079 taskList.push_back(new TaskMoveTo(controller, 0.0f, 0.0f, -1.5f, 0.2f, 0.1f)); 00080 00081 state = PROCESSING_TASKS; 00082 } 00083 00084 buttonBefore = buttonNow; 00085 00086 break; 00087 00088 case PROCESSING_TASKS: 00089 00090 buttonNow = button; 00091 00092 if (buttonNow && !buttonBefore) { // detect button rising edge 00093 00094 controller.setTranslationalVelocity(0.0f); 00095 controller.setRotationalVelocity(0.0f); 00096 00097 state = SLOWING_DOWN; 00098 /* 00099 } else if ((irSensor0 < DISTANCE_THRESHOLD) || (irSensor1 < DISTANCE_THRESHOLD)) { 00100 00101 controller.setTranslationalVelocity(0.0f); 00102 controller.setRotationalVelocity(ROTATIONAL_VELOCITY); 00103 00104 state = TURN_LEFT; 00105 00106 } else if (irSensor5 < DISTANCE_THRESHOLD) { 00107 00108 controller.setTranslationalVelocity(0.0f); 00109 controller.setRotationalVelocity(-ROTATIONAL_VELOCITY); 00110 00111 state = TURN_RIGHT; 00112 */ 00113 } else if (taskList.size() > 0) { 00114 00115 Task* task = taskList.front(); 00116 int result = task->run(PERIOD); 00117 if (result == Task::DONE) { 00118 taskList.pop_front(); 00119 delete task; 00120 } 00121 00122 } else { 00123 00124 controller.setTranslationalVelocity(0.0f); 00125 controller.setRotationalVelocity(0.0f); 00126 00127 state = SLOWING_DOWN; 00128 } 00129 00130 buttonBefore = buttonNow; 00131 00132 break; 00133 00134 case TURN_LEFT: 00135 00136 buttonNow = button; 00137 00138 if (buttonNow && !buttonBefore) { // detect button rising edge 00139 00140 controller.setTranslationalVelocity(0.0f); 00141 controller.setRotationalVelocity(0.0f); 00142 00143 state = SLOWING_DOWN; 00144 00145 } else if ((irSensor0 > DISTANCE_THRESHOLD) && (irSensor1 > DISTANCE_THRESHOLD) && (irSensor5 > DISTANCE_THRESHOLD)) { 00146 00147 state = PROCESSING_TASKS; 00148 } 00149 00150 buttonBefore = buttonNow; 00151 00152 break; 00153 00154 case TURN_RIGHT: 00155 00156 buttonNow = button; 00157 00158 if (buttonNow && !buttonBefore) { // detect button rising edge 00159 00160 controller.setTranslationalVelocity(0.0f); 00161 controller.setRotationalVelocity(0.0f); 00162 00163 state = SLOWING_DOWN; 00164 00165 } else if ((irSensor0 > DISTANCE_THRESHOLD) && (irSensor1 > DISTANCE_THRESHOLD) && (irSensor5 > DISTANCE_THRESHOLD)) { 00166 00167 state = PROCESSING_TASKS; 00168 } 00169 00170 buttonBefore = buttonNow; 00171 00172 break; 00173 00174 case SLOWING_DOWN: 00175 00176 if ((fabs(controller.getActualTranslationalVelocity()) < 0.01f) && (fabs(controller.getActualRotationalVelocity()) < 0.01f)) { 00177 00178 enableMotorDriver = 0; 00179 00180 while (taskList.size() > 0) { 00181 delete taskList.front(); 00182 taskList.pop_front(); 00183 } 00184 00185 state = ROBOT_OFF; 00186 } 00187 00188 break; 00189 00190 default: 00191 00192 state = ROBOT_OFF; 00193 } 00194 } 00195
Generated on Wed Jul 27 2022 09:04:32 by
1.7.2