Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ExploreMission.h"
- #include "SnailRunner.h"
- /* --Initialize State Description. */
- const bool DEBUG_EXP = FALSE;
- const std::map<ExploreStateMachine::State, std::string> ExploreStateMachine::StateDescription = {
- { State::FAILURE, "FAILURE" },
- { State::FOUND, "FOUND" },
- { State::LOST, "LOST" },
- { State::LOOK_LEFT, "LOOK_LEFT" },
- { State::LOOK_RIGHT, "LOOK_RIGHT" },
- { State::STOPPING, "STOPPING" },
- { State::STOPPING_GREY, "STOPPING_GREY" },
- { State::WAIT, "WAIT"},
- { State::HAND_OVER, "HAND_OVER" },
- { State::DELIVERY, "DELIVERY" },
- { State::FINISH, "FINISH" },
- { State::OBSTACLE, "OBSTACLE" }
- /* --INFO: Here you should add new states for debugging purpose. */
- };
- const std::map<ExploreStateMachine::Event, std::string> ExploreStateMachine::EventDescription = {
- { Event::IS_STOPPED, "IS_STOPPED" },
- { Event::ON_TRAIL, "ON_TRAIL" },
- { Event::OFF_TRAIL, "OFF_TRAIL" },
- { Event::MAX_CORNERS, "MAX_CORNERS" },
- { Event::WALL_AHEAD, "WALL_AHEAD" },
- { Event::GET_SIGNAL, "GET_SIGNAL" },
- { Event::RUNNER_CLEAR, "RUNNER_CLEAR" },
- { Event::WHITE_DETECTED, "WHITE_DETECTED" },
- { Event::RUNNER_AHEAD, "RUNNER_AHEAD" }
- /* --INFO: Here you should add new events for debugging purpose. */
- };
- ExploreStateMachine::ExploreStateMachine(SnailRunner* r) :mystate(State::FAILURE), robot(r), count(1) {}
- /* --INFO: Do not touch this method. */
- void ExploreStateMachine::state(State s) {
- /* --Log state chance. Check if description is available. */
- bool isOldStateAvailable = (StateDescription.find(mystate) != StateDescription.end());
- bool isNewStateAvailable = (StateDescription.find(s) != StateDescription.end());
- /* --If program stops here, you have to add a state description to the map at top of this file !!! */
- if (!isOldStateAvailable && !isNewStateAvailable)
- OutputDebugString((LPCTSTR)"*** CHECK YOUR CODE! STATE DESCRIPTION IS MISSING ***");
- /* --Log string. */
- std::string note = std::string("OLD STATE:") + (isOldStateAvailable ? StateDescription.at(mystate) : "*ERROR*")
- + std::string(" --> NEW STATE:") + (isNewStateAvailable ? StateDescription.at(s) : "*ERROR*");
- robot->controller()->annotate(note);
- /* --Set new state.*/
- mystate = s;
- }
- /* --INFO: Do not touch this method. */
- ExploreStateMachine::State ExploreStateMachine::state() const { return mystate; }
- void ExploreStateMachine::start() {
- /* --Log string. */
- robot->controller()->annotate("RE/START EXPLORE STATE MACHINE");
- /* --Set everything to initial values. */
- count = 1;
- /* --Start with initial transition. */
- robot->lampright().off();
- robot->lampfront().off();
- if (!(robot->getObstacleDetected())) {
- if (robot->getStarter() == 1) {
- robot->lampright().on();
- onEnteringFound();
- corners = 0;
- if (DEBUG_EXP) { cout << endl << "STARTLAEUFER EXP" << endl; }
- } else {
- onEnteringDelivery();
- corners = 0;
- robot->lampright().off();
- if (DEBUG_EXP) { cout << endl << "SCHLUSSLAEUFER EXP" << endl; }
- }
- } else {
- if (DEBUG_EXP) { cout << endl << "HINDERNIS WURDE UMFAHREN EXP" << endl; }
- onEnteringFound();
- corners = 2;
- robot->setObstacleDetected(false);
- }
- /* --INFO: Here you can change/add initial values when state machine is started. */
- }
- void ExploreStateMachine::restart() {
- /* --Same as start. */
- start();
- }
- /* --INFO: Do not touch this method. */
- void ExploreStateMachine::handle(Event ev) {
- State old = mystate;
- /* --Logging information. */
- bool isEventAvailable = (EventDescription.find(ev) != EventDescription.end());
- /* --If program stops here, you have to add an event description to the map at top of this file !!! */
- if (!isEventAvailable)
- OutputDebugString((LPCTSTR)"*** CHECK YOUR CODE! EVENT DESCRIPTION IS MISSING ***");
- /* --Log string. */
- std::string note = std::string("EVENT TRIGGER:") + (isEventAvailable ? EventDescription.at(ev) : "*ERROR*");
- robot->controller()->annotate(note);
- /* --Process the event. */
- transition(ev);
- /* --Check if state was changed. Could be an issue with your state machine.*/
- if (old == mystate)
- robot->controller()->annotate(std::string("MAYBE UNHANDLED TRIGGER!"));
- }
- /* --INFO: This is the implemented state machine. Add/Modify/Enhance this method for additional functionality. */
- void ExploreStateMachine::transition(Event ev) {
- /* --State machine as nested switch case. */
- switch (mystate) {
- case State::FOUND:
- switch (ev) {
- case Event::IS_STOPPED: onLeavingFound(); onEnteringFound(); break;
- case Event::ON_TRAIL: break;
- case Event::OFF_TRAIL: onLeavingFound(); onEnteringLost(); break;
- case Event::CLEAR_VIEW: break;
- case Event::WALL_AHEAD: onLeavingFound(); onEnteringObstacle(); break;
- case Event::RUNNER_AHEAD: onLeavingFound(); onEnteringHandOver(); break;
- case Event::GET_SIGNAL: break;
- case Event::WHITE_DETECTED: break;
- case Event::RUNNER_CLEAR: break;
- default: onLeavingFound(); onEnteringFailure();
- }
- break;
- case State::HAND_OVER:
- switch (ev) {
- case Event::IS_STOPPED: break;
- case Event::ON_TRAIL: break;
- case Event::OFF_TRAIL: break;
- case Event::WALL_AHEAD: break;
- case Event::GET_SIGNAL: break;
- case Event::RUNNER_AHEAD: onLeavingHandOver(); onEnteringStoppingGrey(); break;
- case Event::WHITE_DETECTED: onLeavingHandOver(); onEnteringDelivery(); break;
- case Event::RUNNER_CLEAR: break;
- default: onLeavingHandOver(); onEnteringFailure();
- }
- break;
- case State::LOST:
- switch (ev) {
- case Event::IS_STOPPED: onLeavingLost(); onEnteringLookLeft(); break;
- case Event::ON_TRAIL: break;
- case Event::OFF_TRAIL: break;
- case Event::WALL_AHEAD: break;
- case Event::RUNNER_AHEAD: break;
- case Event::GET_SIGNAL: break;
- case Event::WHITE_DETECTED: break;
- case Event::RUNNER_CLEAR: break;
- default: onLeavingLost(); onEnteringFailure();
- }
- break;
- case State::DELIVERY:
- switch (ev) {
- case Event::IS_STOPPED: break;
- case Event::ON_TRAIL: break;
- case Event::OFF_TRAIL: break;
- case Event::WALL_AHEAD: break;
- case Event::RUNNER_AHEAD: break;
- case Event::CLEAR_VIEW: break;
- case Event::WHITE_DETECTED: break;
- case Event::RUNNER_CLEAR: break;
- case Event::GET_SIGNAL: onLeavingDelivery(); onEnteringFound(); break;
- default: onLeavingDelivery(); onEnteringFailure();
- }
- break;
- case State::OBSTACLE:
- switch (ev) {
- case Event::IS_STOPPED: break;
- case Event::ON_TRAIL: break;
- case Event::OFF_TRAIL: break;
- case Event::WALL_AHEAD: break;
- case Event::RUNNER_AHEAD: break;
- case Event::WHITE_DETECTED: break;
- case Event::GET_SIGNAL: break;
- case Event::RUNNER_CLEAR: break;
- //case Event::CLEAR_VIEW: onLeavingObstacle(); onEnteringFound(); break;
- default: onLeavingObstacle(); onEnteringFailure();
- }
- break;
- case State::LOOK_LEFT:
- switch (ev) {
- case Event::IS_STOPPED: onLeavingLookLeft(); onEnteringLookRight(); break;
- case Event::ON_TRAIL: onLeavingLookLeft(); onEnteringStopping(); break;
- case Event::OFF_TRAIL: break;
- case Event::CLEAR_VIEW: break;
- case Event::WALL_AHEAD: break;
- case Event::RUNNER_AHEAD: break;
- case Event::WHITE_DETECTED: break;
- case Event::GET_SIGNAL: break;
- case Event::RUNNER_CLEAR: break;
- default: onLeavingLookLeft(); onEnteringFailure();
- }
- break;
- case State::LOOK_RIGHT:
- switch (ev) {
- case Event::IS_STOPPED: onLeavingLookRight(); onEnteringLookLeft(); break;
- case Event::ON_TRAIL: onLeavingLookRight(); onEnteringStopping(); break;
- case Event::OFF_TRAIL: break;
- case Event::CLEAR_VIEW: break;
- case Event::WALL_AHEAD: break;
- case Event::WHITE_DETECTED: break;
- case Event::RUNNER_AHEAD: break;
- case Event::GET_SIGNAL: break;
- case Event::RUNNER_CLEAR: break;
- default: onLeavingLookRight(); onEnteringFailure();
- }
- break;
- case State::STOPPING:
- switch (ev) {
- case Event::IS_STOPPED: onLeavingStopping(); onEnteringFound(); break;
- case Event::ON_TRAIL: break;
- case Event::OFF_TRAIL: break;
- case Event::CLEAR_VIEW: break;
- case Event::WALL_AHEAD: break;
- case Event::WHITE_DETECTED: break;
- case Event::GET_SIGNAL: break;
- case Event::RUNNER_AHEAD: break;
- case Event::RUNNER_CLEAR: onLeavingStopping(); onEnteringHandOver(); break;
- default: onLeavingStopping(); onEnteringFailure();
- }
- break;
- case State::STOPPING_GREY:
- switch (ev) {
- case Event::IS_STOPPED: break;
- case Event::ON_TRAIL: break;
- case Event::OFF_TRAIL: break;
- case Event::CLEAR_VIEW: break;
- case Event::GET_SIGNAL: break;
- case Event::WALL_AHEAD: break;
- case Event::WHITE_DETECTED: break;
- case Event::RUNNER_AHEAD: break;
- case Event::RUNNER_CLEAR: onLeavingStopping(); onEnteringHandOver(); break;
- default: onLeavingStopping(); onEnteringFailure();
- }
- break;
- case State::WAIT:
- switch (ev) {
- case Event::IS_STOPPED: break;
- case Event::ON_TRAIL: break;
- case Event::OFF_TRAIL: break;
- case Event::WALL_AHEAD: break;
- case Event::GET_SIGNAL: break;
- case Event::WHITE_DETECTED: break;
- case Event::RUNNER_AHEAD: break;
- case Event::RUNNER_CLEAR: break;
- default: onLeavingWait(); onEnteringFailure();
- }
- break;
- case State::FINISH:
- switch (ev) {
- case Event::IS_STOPPED: break;
- case Event::ON_TRAIL: break;
- case Event::OFF_TRAIL: break;
- case Event::WALL_AHEAD: break;
- case Event::GET_SIGNAL: break;
- case Event::WHITE_DETECTED: break;
- case Event::RUNNER_AHEAD: break;
- case Event::RUNNER_CLEAR: break;
- default: onLeavingWait(); onEnteringFailure();
- }
- break;
- default: state(State::FAILURE);
- };
- }
- /* --INFO: These methods can be changed to add/modify the given behavior. Important: The
- first instruction in all onEnteringXYZ() methods should call state(State::XYZ)
- in order to set the new/current state. */
- void ExploreStateMachine::onEnteringFailure() {
- state(State::FAILURE);
- robot->stop();
- robot->lampleft().on();
- }
- void ExploreStateMachine::onEnteringFound() {
- robot->forward(1, METER);
- state(State::FOUND);
- robot->lampright().on();
- robot->setSpeed(270);
- // Pruefen ob eine Ecke erkannt worden ist. Wenn ja dann Event MAX_CORNERS ausloesen.
- if (count >= 30) {
- corners += 1;
- robot->setCorners(corners);
- cout << endl << "ECKE ERKANNT : " << corners << endl;
- if (corners >= robot->getMaxCorners()) {
- this->handle(ExploreStateMachine::Event::MAX_CORNERS);
- corners = 0;
- }
- }
- count = 1;
- }
- void ExploreStateMachine::onEnteringWait() {
- state(State::WAIT);
- if (DEBUG_EXP) { cout << endl << "ENTERING WAIT EXP" << endl; }
- robot->stop();
- }
- void ExploreStateMachine::onEnteringLost() {
- state(State::LOST);
- if (DEBUG_EXP) { cout << endl << "ENTERING LOST EXP" << endl; }
- robot->stop();
- }
- void ExploreStateMachine::onEnteringLookLeft() {
- state(State::LOOK_LEFT);
- if (DEBUG_EXP) { cout << endl << "ENTERING LOOK LEFT EXP" << endl; }
- robot->turn(count * -3
- );
- count += 3;
- if (count > 12) { count += 5; }
- if (count >= 9) { robot->setSpeed(400); }
- }
- void ExploreStateMachine::onEnteringLookRight() {
- state(State::LOOK_RIGHT);
- if (DEBUG_EXP) { cout << endl << "ENTERING LOOK RIGHT EXP" << endl; }
- robot->turn(count * 3);
- count += 3;
- cout << count << endl;
- if (count > 12) { count += 5; }
- if (count >= 9) { robot->setSpeed(400); }
- }
- void ExploreStateMachine::onEnteringStopping() {
- state(State::STOPPING);
- robot->stop();
- if (DEBUG_EXP) { cout << endl << "ENTERING STOPPING EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingFailure() {
- robot->lampright().off();
- if (DEBUG_EXP) { cout << endl << "ENTERING FAILURE EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingFound() {
- if (DEBUG_EXP) { cout << endl << "ENCODER LINKS " << robot->left().encoder().value() << endl; }
- if (DEBUG_EXP) { cout << endl << "ENCODER RECHTS " << robot->right().encoder().value() << endl; }
- robot->addDriveDistance(robot->left().encoder().value());
- cout << "FAHRSTRECKE FOUND" << endl;
- if (DEBUG_EXP) { cout << endl << "LEAVING FOUND EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingLost() {
- if (DEBUG_EXP) { cout << endl << "LEAVING LOST EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingStopping() {
- robot->setSpeed(270);
- if (DEBUG_EXP) { cout << endl << "LEAVING STOPPING EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingLookLeft() {
- if (DEBUG_EXP) { cout << endl << "LEAVING LOOK LEFT EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingLookRight() {
- if (DEBUG_EXP) { cout << endl << "LEAVING LOOK RIGHT EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingWait() {
- if (DEBUG_EXP) { cout << endl << "LEAVING WAIT EXP" << endl; }
- }
- void ExploreStateMachine::onEnteringHandOver() {
- state(State::HAND_OVER);
- robot->stopTime();
- robot->setRounds(robot->getRounds() + 1);
- robot->forward(0.5, METER);
- robot->lampfront().on();
- robot->lampright().off();
- if (DEBUG_EXP) { cout << endl << "ENTERING HANDOVER EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingHandOver() {
- if (DEBUG_EXP) { cout << endl << "LEAVING HANDOVER EXP" << endl; }
- }
- void ExploreStateMachine::onEnteringDelivery() {
- if (DEBUG_EXP) { cout << endl << "ENTERING DELIVERY EXP" << endl; }
- state(State::DELIVERY);
- robot->stop();
- robot->setCorners(0);
- robot->saveRound();
- robot->resetDriveDistance();
- if (robot->getRounds() == robot->getMaxRounds()) {
- cout << endl << "STAFFELLAUF BEENDET !!!!!!!!!!!!!!!!!!!!!!" << endl;
- state(State::FINISH);
- robot->WriteLogFile();
- robot->lampfront().off();
- robot->lampright().off();
- robot->lampcontroller().off();
- }
- }
- void ExploreStateMachine::onLeavingDelivery() {
- robot->activate(SnailRunner::EXPLORE_MISSION);
- robot->startTime();
- robot->onStart();
- robot->lampfront().off();
- if (DEBUG_EXP) { cout << endl << "LEAVING DELIVERY EXP" << endl; }
- }
- void ExploreStateMachine::onEnteringObstacle() {
- state(State::OBSTACLE);
- robot->setObstacleDetected(true);
- robot->activate(SnailRunner::OBSTACLE_MISSION);
- robot->onStart();
- if (DEBUG_EXP) { cout << endl << "ENTERING OBSTACLE EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingObstacle() {
- robot->lampfront().off();
- if (DEBUG_EXP) { cout << endl << "LEAVING OBSTACLE EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingStoppingGrey() {
- if (DEBUG_EXP) { cout << endl << "LEAVING STOPPING GREY EXP" << endl; }
- }
- void ExploreStateMachine::onEnteringStoppingGrey() {
- state(State::STOPPING_GREY);
- robot->stop();
- if (DEBUG_EXP) { cout << endl << "ENTERING STOPPING GREY EXP" << endl; }
- }
- void ExploreStateMachine::onLeavingFinish() {
- if (DEBUG_EXP) { cout << endl << "LEAVING FINISH EXP" << endl; }
- }
- void ExploreStateMachine::onEnteringFinish() {
- state(State::FINISH);
- cout << "STAFFELLAUF BEENDET !!! - ZUSTAND FNIISH" << endl;
- robot->stop();
- if (DEBUG_EXP) { cout << endl << "ENTERING FINISH EXP" << endl; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement