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::ReversingState Class Reference

The ReversingState class implements the Reversing state for the Autonomy State Machine. More...

#include <ReversingState.h>

Inheritance diagram for statemachine::ReversingState:
Collaboration diagram for statemachine::ReversingState:

Public Member Functions

 ReversingState ()
 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

geoops::RoverPose m_stStartRoverPose
 
std::chrono::system_clock::time_point m_tmStartReversingTime
 
std::chrono::system_clock::time_point m_tmTimeSinceLastMeter
 
bool m_bInitialized
 

Detailed Description

The ReversingState class implements the Reversing 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

◆ ReversingState()

statemachine::ReversingState::ReversingState ( )

Construct a new State object.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-01-17
65 : State(States::eReversing)
66 {
67 // Submit logger message.
68 LOG_INFO(logging::g_qConsoleLogger, "Entering State: {}", ToString());
69
70 m_bInitialized = false;
71
72 if (!m_bInitialized)
73 {
74 Start();
75 m_bInitialized = true;
76 }
77 }
void Start() override
This method is called when the state is first started. It is used to initialize the state.
Definition ReversingState.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
Here is the call graph for this function:

Member Function Documentation

◆ Start()

void statemachine::ReversingState::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, "ReversingState: Scheduling next run of state logic.");
35
36 // Store the starting position and heading of rover when it entered this state.
37 m_stStartRoverPose = globals::g_pWaypointHandler->SmartRetrieveRoverPose();
38
39 // Store state start time.
40 m_tmStartReversingTime = std::chrono::high_resolution_clock::now();
41 m_tmTimeSinceLastMeter = m_tmStartReversingTime;
42 }
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Exit()

void statemachine::ReversingState::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.

53 {
54 // Clean up the state before exiting
55 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Exiting state.");
56 }
Here is the caller graph for this function:

◆ Run()

void statemachine::ReversingState::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.

86 {
87 // Submit logger message.
88 LOG_DEBUG(logging::g_qSharedLogger, "ReversingState: Running state-specific behavior.");
89
90 // Create instance variables.
91 static bool bTimeSinceLastMeterAlreadySet = false;
92
93 // Get current position and heading.
94 geoops::RoverPose stCurrentRoverPose = globals::g_pWaypointHandler->SmartRetrieveRoverPose();
95 // Get the current time.
96 std::chrono::system_clock::time_point tmCurrentTime = std::chrono::high_resolution_clock::now();
97 // Calculate current distance from start point.
98 geoops::GeoMeasurement stMeasurement = geoops::CalculateGeoMeasurement(stCurrentRoverPose.GetGPSCoordinate(), m_stStartRoverPose.GetGPSCoordinate());
99
100 // Calculate time elapsed.
101 double dTotalTimeElapsed = std::chrono::duration_cast<std::chrono::seconds>(tmCurrentTime - m_tmStartReversingTime).count();
102 double dTimeElapsedSinceLastMeter = std::chrono::duration_cast<std::chrono::seconds>(tmCurrentTime - m_tmTimeSinceLastMeter).count();
103 // Check if rover has reversed the desired distance or timeout has been reached.
104 if (stMeasurement.dDistanceMeters >= constants::REVERSE_DISTANCE)
105 {
106 // Submit logger message.
107 LOG_NOTICE(logging::g_qSharedLogger, "ReversingState: Successfully reversed {} meters in {} seconds.", stMeasurement.dDistanceMeters, dTotalTimeElapsed);
108 // Stop reversing.
109 globals::g_pDriveBoard->SendStop();
110 // Handle reversing complete event.
111 globals::g_pStateMachineHandler->HandleEvent(Event::eReverseComplete);
112 // Exit method without running other code below.
113 return;
114 }
115 // Check if we aren't covering enough distance within the timeout limit.
116 else if (dTimeElapsedSinceLastMeter >= constants::REVERSE_TIMEOUT_PER_METER)
117 {
118 // Submit logger message.
119 LOG_NOTICE(logging::g_qSharedLogger,
120 "ReversingState: Reversed {} meters in {} seconds before timeout was reached. Goal was {} meters, so rover must be running into something...",
121 stMeasurement.dDistanceMeters,
122 dTotalTimeElapsed,
123 constants::REVERSE_DISTANCE);
124 // Stop reversing.
125 globals::g_pDriveBoard->SendStop();
126 // Handle reversing complete event.
127 globals::g_pStateMachineHandler->HandleEvent(Event::eStuck);
128 // Exit method without running other code below.
129 return;
130 }
131 // Reset the timestamp since last meter every other meter reversed.
132 else if (int(stMeasurement.dDistanceMeters) % 2 == 0 && !bTimeSinceLastMeterAlreadySet)
133 {
134 // Update timestamp.
135 m_tmTimeSinceLastMeter = std::chrono::high_resolution_clock::now();
136 // Set toggle.
137 bTimeSinceLastMeterAlreadySet = true;
138 }
139 else if (int(stMeasurement.dDistanceMeters) % 2 != 0 && bTimeSinceLastMeterAlreadySet)
140 {
141 // Reset toggle.
142 bTimeSinceLastMeterAlreadySet = false;
143 }
144
145 // Check if we should try to maintain heading while reversing.
146 if (constants::REVERSE_MAINTAIN_HEADING)
147 {
148 // Reverse straight backwards.
149 diffdrive::DrivePowers stReverse = globals::g_pDriveBoard->CalculateMove(-std::fabs(constants::REVERSE_MOTOR_POWER),
150 m_stStartRoverPose.GetCompassHeading(),
151 stCurrentRoverPose.GetCompassHeading(),
152 diffdrive::DifferentialControlMethod::eArcadeDrive);
153 // Send drive powers.
154 globals::g_pDriveBoard->SendDrive(stReverse);
155 }
156 else
157 {
158 // Just set reverse drive powers manually.
159 diffdrive::DrivePowers stMotorPowers{-std::fabs(constants::REVERSE_MOTOR_POWER), -std::fabs(constants::REVERSE_MOTOR_POWER)};
160 // Send drive powers.
161 globals::g_pDriveBoard->SendDrive(stMotorPowers);
162 }
163 }
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
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
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
const geoops::GPSCoordinate & GetGPSCoordinate() const
Accessor for the geoops::GPSCoordinate member variable.
Definition GeospatialOperations.hpp:722
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::ReversingState::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.

175 {
176 // Initialize member variables.
177 States eNextState = States::eReversing;
178 bool bCompleteStateExit = true;
179
180 switch (eEvent)
181 {
182 case Event::eStart:
183 {
184 // Submit logger message.
185 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Handling Start event.");
186 break;
187 }
188 case Event::eAbort:
189 {
190 // Submit logger message.
191 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Handling Abort event.");
192 // Change states.
193 eNextState = States::eIdle;
194 break;
195 }
196 case Event::eReverseComplete:
197 {
198 // Submit logger message.
199 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Handling ReverseComplete event.");
200 // Transition back to state that triggered reversing.
201 eNextState = globals::g_pStateMachineHandler->GetPreviousState();
202 break;
203 }
204 case Event::eStuck:
205 {
206 // Submit logger message.
207 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Handling Stuck event.");
208 // Change states.
209 eNextState = States::eStuck;
210 break;
211 }
212 default:
213 {
214 // Submit logger message.
215 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Handling unknown event.");
216 // Change states.
217 eNextState = States::eIdle;
218 break;
219 }
220 }
221
222 if (eNextState != States::eReversing)
223 {
224 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Transitioning to {} State.", StateToString(eNextState));
225
226 // Exit the current state
227 if (bCompleteStateExit)
228 {
229 Exit();
230 }
231 }
232
233 return eNextState;
234 }
statemachine::States GetPreviousState() const
Accessor for the Previous State private member.
Definition StateMachineHandler.cpp:407
void Exit() override
This method is called when the state is exited. It is used to clean up the state.
Definition ReversingState.cpp:52
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: