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