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 bVIOHeading=true, bool bVIOTracking=false)
Retrieve the rover's current position and heading. Automatically picks between getting the position/h...
Definition WaypointHandler.cpp:742
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::milliseconds>(tmCurrentTime - m_tmTimeSinceLastMeter).count() / 1000.0;
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 }
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
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
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
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
const geoops::GPSCoordinate & GetGPSCoordinate() const
Accessor for the geoops::GPSCoordinate member variable.
Definition GeospatialOperations.hpp:725
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::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 // Send multimedia command to update state display.
187 globals::g_pMultimediaBoard->SendLightingState(MultimediaBoard::MultimediaBoardLightingState::eAutonomy);
188 break;
189 }
190 case Event::eAbort:
191 {
192 // Submit logger message.
193 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Handling Abort event.");
194 // Send multimedia command to update state display.
195 globals::g_pMultimediaBoard->SendLightingState(MultimediaBoard::MultimediaBoardLightingState::eOff);
196 // Change states.
197 eNextState = States::eIdle;
198 break;
199 }
200 case Event::eReverseComplete:
201 {
202 // Submit logger message.
203 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Handling ReverseComplete event.");
204 // Send multimedia command to update state display.
205 globals::g_pMultimediaBoard->SendLightingState(MultimediaBoard::MultimediaBoardLightingState::eAutonomy);
206 // Transition back to state that triggered reversing.
207 eNextState = globals::g_pStateMachineHandler->GetPreviousState();
208 break;
209 }
210 case Event::eStuck:
211 {
212 // Submit logger message.
213 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Handling Stuck event.");
214 // Send multimedia command to update state display.
215 globals::g_pMultimediaBoard->SendLightingState(MultimediaBoard::MultimediaBoardLightingState::eAutonomy);
216 // Change states.
217 eNextState = States::eStuck;
218 break;
219 }
220 default:
221 {
222 // Submit logger message.
223 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Handling unknown event.");
224 // Change states.
225 eNextState = States::eIdle;
226 break;
227 }
228 }
229
230 if (eNextState != States::eReversing)
231 {
232 LOG_INFO(logging::g_qSharedLogger, "ReversingState: Transitioning to {} State.", StateToString(eNextState));
233
234 // Exit the current state
235 if (bCompleteStateExit)
236 {
237 Exit();
238 }
239 }
240
241 return eNextState;
242 }
void SendLightingState(MultimediaBoardLightingState eState)
Sends a predetermined color pattern to board.
Definition MultimediaBoard.cpp:55
statemachine::States GetPreviousState() const
Accessor for the Previous State private member.
Definition StateMachineHandler.cpp:423
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: