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, DigitalOut& led) : 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), led(led) { 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 // implementation of the state machine 00063 00064 switch (state) { 00065 00066 case ROBOT_OFF: 00067 00068 buttonNow = button; 00069 00070 if (buttonNow && !buttonBefore) { // detect button rising edge 00071 00072 enableMotorDriver = 1; 00073 00074 // Now it's time for some tasks 00075 //controller.setTranslationalVelocity(TRANSLATIONAL_VELOCITY); 00076 //controller.setRotationalVelocity(0.0f); 00077 00078 // Plan some tasks 00079 00080 // TASKWAIT FUNKTIONIERT BI MIR NID 00081 taskList.push_back(new TaskMoveTo(controller, 0.0f, 1.0f, 0.0f)); 00082 //taskList.push_back(new TaskWait(controller, 1.0f)); 00083 taskList.push_back(new TaskMoveTo(controller, 1.0f, 1.0f, 0.0f)); 00084 00085 taskList.push_back(new TaskMoveTo(controller, 0.0f, 1.0f, 0.0f)); 00086 taskList.push_back(new TaskMoveTo(controller, 0.0f, 0.0f, 0.0f)); 00087 00088 state = MOVE_FORWARD; 00089 } 00090 00091 buttonBefore = buttonNow; 00092 00093 break; 00094 00095 case MOVE_FORWARD: 00096 led = 1; 00097 if (taskList.size() > 0) { 00098 Task* task = taskList.front(); 00099 if (task->run(0.0f) == Task::DONE) { 00100 led = 0; 00101 taskList.pop_front(); 00102 delete task; 00103 } 00104 } else { 00105 00106 controller.setTranslationalVelocity(0.0f); 00107 controller.setRotationalVelocity(0.0f); 00108 00109 state = SLOWING_DOWN; 00110 } 00111 00112 buttonNow = button; 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 ((irSensor0 < DISTANCE_THRESHOLD) || (irSensor1 < DISTANCE_THRESHOLD)) { 00122 00123 controller.setTranslationalVelocity(0.0f); 00124 controller.setRotationalVelocity(ROTATIONAL_VELOCITY); 00125 00126 state = TURN_LEFT; 00127 00128 } else if (irSensor5 < DISTANCE_THRESHOLD) { 00129 00130 controller.setTranslationalVelocity(0.0f); 00131 controller.setRotationalVelocity(-ROTATIONAL_VELOCITY); 00132 00133 state = TURN_RIGHT; 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.setRotationalVelocity(0.0f); 00147 00148 state = SLOWING_DOWN; 00149 00150 } else if ((irSensor0 > DISTANCE_THRESHOLD) && (irSensor1 > DISTANCE_THRESHOLD) && (irSensor5 > DISTANCE_THRESHOLD)) { 00151 00152 controller.setTranslationalVelocity(TRANSLATIONAL_VELOCITY); 00153 controller.setRotationalVelocity(0.0f); 00154 00155 state = MOVE_FORWARD; 00156 } 00157 00158 buttonBefore = buttonNow; 00159 00160 break; 00161 00162 case TURN_RIGHT: 00163 00164 buttonNow = button; 00165 00166 if (buttonNow && !buttonBefore) { // detect button rising edge 00167 00168 controller.setRotationalVelocity(0.0f); 00169 00170 state = SLOWING_DOWN; 00171 00172 } else if ((irSensor0 > DISTANCE_THRESHOLD) && (irSensor1 > DISTANCE_THRESHOLD) && (irSensor5 > DISTANCE_THRESHOLD)) { 00173 00174 controller.setTranslationalVelocity(TRANSLATIONAL_VELOCITY); 00175 controller.setRotationalVelocity(0.0f); 00176 00177 state = MOVE_FORWARD; 00178 } 00179 00180 buttonBefore = buttonNow; 00181 00182 break; 00183 00184 case SLOWING_DOWN: 00185 00186 if ((fabs(controller.getActualTranslationalVelocity()) < 0.01f) && (fabs(controller.getActualRotationalVelocity()) < 0.01f)) { 00187 00188 enableMotorDriver = 0; 00189 00190 while (taskList.size() > 0) { 00191 delete taskList.front(); 00192 taskList.pop_front(); 00193 } 00194 00195 state = ROBOT_OFF; 00196 } 00197 00198 break; 00199 00200 default: 00201 00202 state = ROBOT_OFF; 00203 } 00204 } 00205
Generated on Thu Jul 14 2022 09:49:36 by
