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_stPlanner
 
controllers::StanleyController m_stController
 
geoops::RoverPose m_stPose
 
geoops::UTMCoordinate m_stStart
 
geoops::UTMCoordinate m_stGoal
 
geoops::Waypoint m_stGoalWaypoint
 
std::vector< geoops::UTMCoordinatem_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
93 : State(States::eAvoidance)
94 {
95 LOG_INFO(logging::g_qConsoleLogger, "Entering State: {}", ToString());
96
97 m_bInitialized = false;
98 m_stStuckChecker = statemachine::TimeIntervalBasedStuckDetector(constants::STUCK_CHECK_ATTEMPTS,
99 constants::STUCK_CHECK_INTERVAL,
100 constants::STUCK_CHECK_VEL_THRESH,
101 constants::STUCK_CHECK_ROT_THRESH);
102
103 if (!m_bInitialized)
104 {
105 Start();
106 m_bInitialized = true;
107 }
108 }
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 Stanley Controller:
40 m_stController = controllers::StanleyController(constants::STANLEY_STEER_CONTROL_GAIN, constants::STANLEY_DIST_TO_FRONT_AXLE, constants::STANLEY_YAW_TOLERANCE);
41
42 // Initialize ASTAR Pathfinder:
43 // TODO: Poll zedCam / object detector for seen obstacles to pass to AStar.
44 // Determine start and goal (peek waypoint for goal).
45 m_stPose = globals::g_pWaypointHandler->SmartRetrieveRoverPose();
46 m_stStart = m_stPose.GetUTMCoordinate();
47 m_stGoalWaypoint = globals::g_pWaypointHandler->PeekNextWaypoint();
48
49 // Plan avoidance route using AStar.
50 // TODO: Add obstacles to params.
51 m_vPlannedRoute = m_stPlanner.PlanAvoidancePath(m_stStart, m_stGoalWaypoint.GetUTMCoordinate());
52
53 // Check for AStar failure.
54 if (!m_vPlannedRoute.empty())
55 {
56 m_stGoal = m_vPlannedRoute.back();
57 m_stController.SetPath(m_vPlannedRoute);
58 }
59 // Exit Obstacle Avoidance if AStar fails to generate a path.
60 else
61 {
62 globals::g_pStateMachineHandler->HandleEvent(Event::eEndObstacleAvoidance, false);
63 }
64 }
statemachine::States GetPreviousState() const
Accessor for the Previous State private member.
Definition StateMachineHandler.cpp:407
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:337
const geoops::Waypoint PeekNextWaypoint()
Returns an immutable reference to the geoops::Waypoint struct at the front of the list without removi...
Definition WaypointHandler.cpp:539
geoops::RoverPose SmartRetrieveRoverPose(bool bVIOTracking=false)
Retrieve the rover's current position and heading. Automatically picks between getting the position/h...
Definition WaypointHandler.cpp:738
Provides an implementation of a lightweight lateral StanleyController. This algorithm is used to prec...
Definition StanleyController.h:50
void SetPath(std::vector< geoops::UTMCoordinate > &vUTMPath)
Setter for path.
Definition StanleyController.cpp:276
std::vector< geoops::UTMCoordinate > PlanAvoidancePath(const geoops::UTMCoordinate &stStartCoordinate, const geoops::UTMCoordinate &stGoalCoordinate, const std::vector< sl::ObjectData > &vObstacles=std::vector< sl::ObjectData >())
Called in the obstacle avoidance state to plan a path around obstacles blocking our path.
Definition AStar.cpp:411
const geoops::UTMCoordinate & GetUTMCoordinate() const
Accessor for the geoops::UTMCoordinate member variable.
Definition GeospatialOperations.hpp:733
const geoops::UTMCoordinate & GetUTMCoordinate() const
Accessor for the geoops::UTMCoordinate member variable.
Definition GeospatialOperations.hpp:634
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.

75 {
76 // Clean up the state before exiting
77 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Exiting state.");
78
79 // Clear ASTAR Pathfinder
80 m_stPlanner.ClearObstacleData();
81
82 // Clear Stanley Controller
83 m_stController.ClearPath();
84 }
void ClearObstacleData()
Helper function to destroy objects from m_vObstacles.
Definition AStar.cpp:61
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.

117 {
118 LOG_DEBUG(logging::g_qSharedLogger, "AvoidanceState: Running state-specific behavior.");
119
120 // TODO: build in obstacle detection and refresh of astar path when a new object is detected
121 // This can be done by calling PlanAvoidanceRoute() with an updated obstacle vector.
122
123 // A route has already been plotted by the planner and passed to the controller.
124 // Navigate by issuing drive commands from the controller.
125 // Check if we are at the goal waypoint.
126 geoops::RoverPose stCurrentPose = globals::g_pWaypointHandler->SmartRetrieveRoverPose();
127 geoops::GeoMeasurement stGoalWaypointMeasurement = geoops::CalculateGeoMeasurement(stCurrentPose.GetUTMCoordinate(), m_stGoalWaypoint.GetUTMCoordinate());
128
129 // Check to see if rover velocity is below stuck threshold (scaled to avoidance speed).
130 if (m_stStuckChecker.CheckIfStuck(globals::g_pWaypointHandler->SmartRetrieveVelocity(), globals::g_pWaypointHandler->SmartRetrieveAngularVelocity()))
131 {
132 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Rover has become stuck");
133 globals::g_pStateMachineHandler->HandleEvent(Event::eStuck, true);
134 }
135
136 // Goal has not been reached yet:
137 else if (stGoalWaypointMeasurement.dDistanceMeters > constants::NAVIGATING_REACHED_GOAL_RADIUS)
138 {
139 // Request objects:
140 // Check for any new objects:
141 // Re-plan route (call planPath again):
142
143 // Update pose for drive calculation.
144 m_stPose = globals::g_pWaypointHandler->SmartRetrieveRoverPose();
145 const double dVelocity = globals::g_pWaypointHandler->SmartRetrieveVelocity();
146 // Calculate drive move/powers at the speed multiplier.
147 diffdrive::DrivePowers stDriveSpeeds =
148 globals::g_pDriveBoard->CalculateMove(constants::AVOIDANCE_STATE_MOTOR_POWER,
149 m_stController.Calculate(m_stPose.GetUTMCoordinate(), dVelocity, m_stPose.GetCompassHeading()),
150 stCurrentPose.GetCompassHeading(),
151 diffdrive::DifferentialControlMethod::eArcadeDrive);
152 // Send drive command to drive board.
153 globals::g_pDriveBoard->SendDrive(stDriveSpeeds);
154 }
155
156 // Local goal is reached, end obstacle avoidance:
157 else
158 {
159 globals::g_pDriveBoard->SendStop();
160 globals::g_pStateMachineHandler->HandleEvent(Event::eEndObstacleAvoidance, false);
161 }
162 }
diffdrive::DrivePowers CalculateMove(const double dGoalSpeed, const double dGoalHeading, const double dActualHeading, const diffdrive::DifferentialControlMethod eKinematicsMethod)
This method determines drive powers to make the Rover drive towards a given heading at a given speed.
Definition DriveBoard.cpp:92
void SendStop()
Stop the drivetrain of the Rover.
Definition DriveBoard.cpp:162
void SendDrive(diffdrive::DrivePowers &stDrivePowers)
Sets the left and right drive powers of the drive board.
Definition DriveBoard.cpp:120
double SmartRetrieveVelocity()
Retrieve the rover's current velocity. Currently there is no easy way to get the velocity of the ZEDC...
Definition WaypointHandler.cpp:883
double Calculate(const geoops::UTMCoordinate &stUTMCurrPos, const double dVelocity, const double dBearing)
Calculate the steering control adjustment for an agent using the Stanley method.
Definition StanleyController.cpp:150
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:445
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:674
double GetCompassHeading() const
Accessor for the Compass Heading private member.
Definition GeospatialOperations.hpp:743
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.

174 {
175 // Create instance variables.
176 States eNextState = States::eAvoidance;
177 bool bCompleteStateExit = true;
178
179 switch (eEvent)
180 {
181 case Event::eStart:
182 {
183 // Submit logger message.
184 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Handling Start event.");
185 // Send multimedia command to update state display.
186 globals::g_pMultimediaBoard->SendLightingState(MultimediaBoard::MultimediaBoardLightingState::eAutonomy);
187 break;
188 }
189 case Event::eAbort:
190 {
191 // Submit logger message.
192 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Handling Abort event.");
193 // Send multimedia command to update state display.
194 globals::g_pMultimediaBoard->SendLightingState(MultimediaBoard::MultimediaBoardLightingState::eAutonomy);
195 // Change state.
196 eNextState = States::eIdle;
197 break;
198 }
199 case Event::eEndObstacleAvoidance:
200 {
201 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Handling EndObstacleAvoidance event.");
202 eNextState = m_eTriggeringState;
203 break;
204 }
205 case Event::eStuck:
206 {
207 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Handling Stuck event.");
208 eNextState = States::eStuck;
209 break;
210 }
211 default:
212 {
213 LOG_WARNING(logging::g_qSharedLogger, "AvoidanceState: Handling unknown event.");
214 eNextState = States::eIdle;
215 break;
216 }
217 }
218
219 if (eNextState != States::eAvoidance)
220 {
221 LOG_INFO(logging::g_qSharedLogger, "AvoidanceState: Transitioning to {} State.", StateToString(eNextState));
222
223 // Exit the current state
224 if (bCompleteStateExit)
225 {
226 Exit();
227 }
228 }
229
230 return eNextState;
231 }
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:74
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: