Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
statemachine::AvoidanceState Class Reference

The AvoidanceState class implements the Avoidance state for the Autonomy State Machine. More...

#include <AvoidanceState.h>

Inheritance diagram for statemachine::AvoidanceState:
Collaboration diagram for statemachine::AvoidanceState:

Public Member Functions

 AvoidanceState ()
 Construct a new State object.
 
void Run () override
 Run the state machine. Returns the next state.
 
States TriggerEvent (Event eEvent) override
 Trigger an event in the state machine. Returns the next state.
 
- Public Member Functions inherited from statemachine::State
 State (States eState)
 Construct a new State object.
 
virtual ~State ()=default
 Destroy the State object.
 
States GetState () const
 Accessor for the State private member.
 
virtual std::string ToString () const
 Accessor for the State private member. Returns the state as a string.
 
virtual bool operator== (const State &other) const
 Checks to see if the current state is equal to the passed state.
 
virtual bool operator!= (const State &other) const
 Checks to see if the current state is not equal to the passed state.
 

Protected Member Functions

void Start () override
 This method is called when the state is first started. It is used to initialize the state.
 
void Exit () override
 This method is called when the state is exited. It is used to clean up the state.
 

Private Attributes

statemachine::TimeIntervalBasedStuckDetector m_stStuckChecker
 
pathplanners::AStar m_AStarPlanner
 
controllers::PredictiveStanleyController m_StanleyController
 
geoops::RoverPose m_stPose
 
geoops::UTMCoordinate m_stStart
 
geoops::UTMCoordinate m_stGoal
 
geoops::Waypoint m_stGoalWaypoint
 
std::vector< geoops::Waypointm_vPlannedRoute
 
States m_eTriggeringState
 
bool m_bInitialized
 

Detailed Description

The AvoidanceState class implements the Avoidance state for the Autonomy State Machine.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-01-17

Constructor & Destructor Documentation

◆ AvoidanceState()

statemachine::AvoidanceState::AvoidanceState ( )

Construct a new State object.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-01-17
87 : State(States::eAvoidance)
88 {
89 LOG_INFO(logging::g_qConsoleLogger, "Entering State: {}", ToString());
90
91 m_bInitialized = false;
92 m_stStuckChecker = statemachine::TimeIntervalBasedStuckDetector(constants::STUCK_CHECK_ATTEMPTS,
93 constants::STUCK_CHECK_INTERVAL,
94 constants::STUCK_CHECK_VEL_THRESH,
95 constants::STUCK_CHECK_ROT_THRESH);
96 m_StanleyController = controllers::PredictiveStanleyController(constants::STANLEY_CROSSTRACK_CONTROL_GAIN,
97 constants::STANLEY_STEERING_ANGLE_LIMIT,
98 constants::STANLEY_DIST_TO_FRONT_AXLE,
99 constants::STANLEY_PREDICTION_HORIZON,
100 constants::STANLEY_PREDICTION_TIME_STEP);
101
102 if (!m_bInitialized)
103 {
104 Start();
105 m_bInitialized = true;
106 }
107 }
This class implements the Predictive Stanley Controller. This controller is used to follow a path usi...
Definition PredictiveStanleyController.h:44
void Start() override
This method is called when the state is first started. It is used to initialize the state.
Definition AvoidanceState.cpp:31
virtual std::string ToString() const
Accessor for the State private member. Returns the state as a string.
Definition State.hpp:207
State(States eState)
Construct a new State object.
Definition State.hpp:150
This class should be instantiated within another state to be used for detection of if the rover is st...
Definition StuckDetection.hpp:43
Here is the call graph for this function:

Member Function Documentation

◆ Start()

void statemachine::AvoidanceState::Start ( )
overrideprotectedvirtual

This method is called when the state is first started. It is used to initialize the state.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-01-17

Reimplemented from statemachine::State.

32 {
33 // Schedule the next run of the state's logic
34 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Scheduling next run of state logic.");
35
36 // Store the state that got stuck and triggered a stuck event.
37 m_eTriggeringState = globals::g_pStateMachineHandler->GetPreviousState();
38
39 // Initialize ASTAR Pathfinder:
40 // TODO: Poll zedCam / object detector for seen obstacles to pass to AStar.
41 // Determine start and goal (peek waypoint for goal).
42 m_stPose = globals::g_pWaypointHandler->SmartRetrieveRoverPose();
43 m_stStart = m_stPose.GetUTMCoordinate();
44 m_stGoalWaypoint = globals::g_pWaypointHandler->PeekNextWaypoint();
45
46 // Plan avoidance route using AStar.
47 // TODO: Add obstacles to params.
48 m_vPlannedRoute = m_AStarPlanner.PlanAvoidancePath(m_stStart, m_stGoalWaypoint.GetUTMCoordinate());
49
50 // Check for AStar failure.
51 if (!m_vPlannedRoute.empty())
52 {
53 m_stGoal = m_vPlannedRoute.back().GetUTMCoordinate();
54 m_StanleyController.SetReferencePath(m_vPlannedRoute);
55 }
56 // Exit Obstacle Avoidance if AStar fails to generate a path.
57 else
58 {
59 globals::g_pStateMachineHandler->HandleEvent(Event::eEndObstacleAvoidance, false);
60 }
61 }
statemachine::States GetPreviousState() const
Accessor for the Previous State private member.
Definition StateMachineHandler.cpp:423
void HandleEvent(statemachine::Event eEvent, const bool bSaveCurrentState=false)
This method Handles Events that are passed to the State Machine Handler. It will check the current st...
Definition StateMachineHandler.cpp:350
const geoops::Waypoint PeekNextWaypoint()
Returns an immutable reference to the geoops::Waypoint struct at the front of the list without removi...
Definition WaypointHandler.cpp:540
geoops::RoverPose SmartRetrieveRoverPose(bool bVIOHeading=true, bool bVIOTracking=false)
Retrieve the rover's current position and heading. Automatically picks between getting the position/h...
Definition WaypointHandler.cpp:742
void SetReferencePath(const std::vector< geoops::Waypoint > &vReferencePath)
Sets the path that the controller will follow.
Definition PredictiveStanleyController.cpp:203
std::vector< geoops::Waypoint > PlanAvoidancePath(const geoops::Waypoint &stStartCoordinate, const geoops::Waypoint &stGoalCoordinate)
Called in the obstacle avoidance state to plan a path around obstacles.
Definition AStar.cpp:65
const geoops::UTMCoordinate & GetUTMCoordinate() const
Accessor for the geoops::UTMCoordinate member variable.
Definition GeospatialOperations.hpp:736
const geoops::UTMCoordinate & GetUTMCoordinate() const
Accessor for the geoops::UTMCoordinate member variable.
Definition GeospatialOperations.hpp:477
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Exit()

void statemachine::AvoidanceState::Exit ( )
overrideprotectedvirtual

This method is called when the state is exited. It is used to clean up the state.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-01-17

Reimplemented from statemachine::State.

72 {
73 // Clean up the state before exiting
74 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Exiting state.");
75
76 // Clear ASTAR Pathfinder
77 m_AStarPlanner.ClearObstacleData();
78 }
void ClearObstacleData()
Helper function to destroy objects from m_vObstacles.
Definition AStar.cpp:464
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Run()

void statemachine::AvoidanceState::Run ( )
overridevirtual

Run the state machine. Returns the next state.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-01-17

Implements statemachine::State.

116 {
117 LOG_DEBUG(logging::g_qSharedLogger, "AvoidanceState: Running state-specific behavior.");
118
119 // TODO: build in obstacle detection and refresh of astar path when a new object is detected
120 // This can be done by calling PlanAvoidanceRoute() with an updated obstacle vector.
121
122 // A route has already been plotted by the planner and passed to the controller.
123 // Navigate by issuing drive commands from the controller.
124 // Check if we are at the goal waypoint.
125 geoops::RoverPose stCurrentRoverPose = globals::g_pWaypointHandler->SmartRetrieveRoverPose();
126 geoops::GeoMeasurement stGoalWaypointMeasurement = geoops::CalculateGeoMeasurement(stCurrentRoverPose.GetUTMCoordinate(), m_stGoalWaypoint.GetUTMCoordinate());
127
128 // Check to see if rover velocity is below stuck threshold (scaled to avoidance speed).
129 if (m_stStuckChecker.CheckIfStuck(globals::g_pWaypointHandler->SmartRetrieveVelocity(), globals::g_pWaypointHandler->SmartRetrieveAngularVelocity()))
130 {
131 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Rover has become stuck");
132 globals::g_pStateMachineHandler->HandleEvent(Event::eStuck, true);
133 }
134
135 // Goal has not been reached yet:
136 else if (stGoalWaypointMeasurement.dDistanceMeters > constants::NAVIGATING_REACHED_GOAL_RADIUS)
137 {
138 // Request objects:
139 // Check for any new objects:
140 // Re-plan route (call planPath again):
141 // TEST Stanley CONTROLLER.
142 // Calculate drive move/powers at the speed multiplier.
143 controllers::PredictiveStanleyController::DriveVector stDriveVector = m_StanleyController.Calculate(stCurrentRoverPose);
144
145 diffdrive::DrivePowers stDriveSpeeds = globals::g_pDriveBoard->CalculateMove(stDriveVector.dVelocity,
146 stDriveVector.dThetaHeading,
147 stCurrentRoverPose.GetCompassHeading(),
148 diffdrive::DifferentialControlMethod::eArcadeDrive);
149
150 // Send drive powers over RoveComm.
151 globals::g_pDriveBoard->SendDrive(stDriveSpeeds);
152 }
153
154 // Local goal is reached, end obstacle avoidance:
155 else
156 {
157 globals::g_pDriveBoard->SendStop();
158 globals::g_pStateMachineHandler->HandleEvent(Event::eEndObstacleAvoidance, false);
159 }
160 }
void SendDrive(const diffdrive::DrivePowers &stDrivePowers)
Sets the left and right drive powers of the drive board.
Definition DriveBoard.cpp:117
void SendStop()
Stop the drivetrain of the Rover.
Definition DriveBoard.cpp:163
diffdrive::DrivePowers CalculateMove(const double dGoalSpeed, const double dGoalHeading, const double dActualHeading, const diffdrive::DifferentialControlMethod eKinematicsMethod=diffdrive::DifferentialControlMethod::eArcadeDrive, const bool bAlwaysProgressForward=false)
This method determines drive powers to make the Rover drive towards a given heading at a given speed.
Definition DriveBoard.cpp:87
DriveVector Calculate(const geoops::RoverPose &stCurrentPose, const double dMaxSpeed=constants::NAVIGATING_MOTOR_POWER)
Calculate an updated steering angle for the rover based on the current pose using the predictive stan...
Definition PredictiveStanleyController.cpp:100
bool CheckIfStuck(double dCurrentVelocity, double dCurrentAngularVelocity)
Checks if the rover meets stuck criteria based in the given parameters.
Definition StuckDetection.hpp:105
GeoMeasurement CalculateGeoMeasurement(const GPSCoordinate &stCoord1, const GPSCoordinate &stCoord2)
The shortest path between two points on an ellipsoid at (lat1, lon1) and (lat2, lon2) is called the g...
Definition GeospatialOperations.hpp:522
Definition PredictiveStanleyController.h:50
This struct is used to store the left and right drive powers for the robot. Storing these values in a...
Definition DifferentialDrive.hpp:73
This struct is used to store the distance, arc length, and relative bearing for a calculated geodesic...
Definition GeospatialOperations.hpp:82
This struct is used by the WaypointHandler to provide an easy way to store all pose data about the ro...
Definition GeospatialOperations.hpp:677
double GetCompassHeading() const
Accessor for the Compass Heading private member.
Definition GeospatialOperations.hpp:756
Here is the call graph for this function:

◆ TriggerEvent()

States statemachine::AvoidanceState::TriggerEvent ( Event  eEvent)
overridevirtual

Trigger an event in the state machine. Returns the next state.

Parameters
eEvent- The event to trigger.
Returns
std::shared_ptr<State> - The next state.
Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-01-17

Implements statemachine::State.

172 {
173 // Create instance variables.
174 States eNextState = States::eAvoidance;
175 bool bCompleteStateExit = true;
176
177 switch (eEvent)
178 {
179 case Event::eStart:
180 {
181 // Submit logger message.
182 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Handling Start event.");
183 // Send multimedia command to update state display.
184 globals::g_pMultimediaBoard->SendLightingState(MultimediaBoard::MultimediaBoardLightingState::eAutonomy);
185 break;
186 }
187 case Event::eAbort:
188 {
189 // Submit logger message.
190 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Handling Abort event.");
191 // Send multimedia command to update state display.
192 globals::g_pMultimediaBoard->SendLightingState(MultimediaBoard::MultimediaBoardLightingState::eOff);
193 // Change state.
194 eNextState = States::eIdle;
195 break;
196 }
197 case Event::eEndObstacleAvoidance:
198 {
199 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Handling EndObstacleAvoidance event.");
200 eNextState = m_eTriggeringState;
201 break;
202 }
203 case Event::eStuck:
204 {
205 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Handling Stuck event.");
206 eNextState = States::eStuck;
207 break;
208 }
209 default:
210 {
211 LOG_WARNING(logging::g_qSharedLogger, "AvoidanceState: Handling unknown event.");
212 eNextState = States::eIdle;
213 break;
214 }
215 }
216
217 if (eNextState != States::eAvoidance)
218 {
219 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Transitioning to {} State.", StateToString(eNextState));
220
221 // Exit the current state
222 if (bCompleteStateExit)
223 {
224 Exit();
225 }
226 }
227
228 return eNextState;
229 }
void SendLightingState(MultimediaBoardLightingState eState)
Sends a predetermined color pattern to board.
Definition MultimediaBoard.cpp:55
void Exit() override
This method is called when the state is exited. It is used to clean up the state.
Definition AvoidanceState.cpp:71
std::string StateToString(States eState)
Converts a state object to a string.
Definition State.hpp:89
States
The states that the state machine can be in.
Definition State.hpp:31
Here is the call graph for this function:

The documentation for this class was generated from the following files: