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
BicycleModel.hpp
Go to the documentation of this file.
1
14#ifndef BICYCLE_MODEL_H
15#define BICYCLE_MODEL_H
16
17#include "../../util/NumberOperations.hpp"
18
20#include <chrono>
21#include <cmath>
22#include <vector>
23
25
26
35{
36 public:
38 // Declare public structs for this class.
40
41
49 {
50 public:
51 double dXPosition;
52 double dYPosition;
53 double dTheta;
54 };
55
57 // Declare public class methods.
59
60
68 {
69 // Initialize member variables.
70 m_dWheelbase = 0.0;
71 m_dXPosition = 0.0;
72 m_dYPosition = 0.0;
73 m_dTheta = 0.0;
74 m_dVelocity = -1.0;
75 m_dSteeringAngle = 0.0;
76 m_tmLastUpdateTime = std::chrono::system_clock::now();
77 }
78
79
90 BicycleModel(const double dWheelbase, const double dXPosition, const double dYPosition, const double dTheta)
91 {
92 // Initialize member variables.
93 m_dWheelbase = dWheelbase;
94 m_dXPosition = dXPosition;
95 m_dYPosition = dYPosition;
96 m_dTheta = dTheta;
97 m_dVelocity = -1.0;
98 m_dSteeringAngle = 0.0;
99 m_tmLastUpdateTime = std::chrono::system_clock::now();
100 }
101
102
112 void ResetState(const double dXPosition, const double dYPosition, const double dTheta)
113 {
114 // Update member variables.
115 m_dXPosition = dXPosition;
116 m_dYPosition = dYPosition;
117 m_dTheta = dTheta;
118 m_dSteeringAngle = 0.0;
119 m_dVelocity = -1.0;
120 m_tmLastUpdateTime = std::chrono::system_clock::now();
121 }
122
123
131 {
132 // Update member variables.
133 m_dXPosition = 0.0;
134 m_dYPosition = 0.0;
135 m_dTheta = 0.0;
136 m_dSteeringAngle = 0.0;
137 m_dVelocity = -1.0;
138 m_tmLastUpdateTime = std::chrono::system_clock::now();
139 }
140
141
154 void UpdateState(const double dXPosition, const double dYPosition, const double dTheta)
155 {
156 // Check if this is our first update.
157 if (m_dVelocity == -1.0)
158 {
159 // Set the velocity to zero.
160 m_dVelocity = 0.0;
161 }
162 // Calculate the velocity of the rover as long as the new position is different from the current position.
163 else if (dXPosition != m_dXPosition || dYPosition != m_dYPosition)
164 {
165 // Calculate the velocity of the rover.
166 std::chrono::system_clock::time_point tmCurrentTime = std::chrono::system_clock::now();
167 double dTimeElapsed = std::chrono::duration_cast<std::chrono::milliseconds>(tmCurrentTime - m_tmLastUpdateTime).count() / 1000.0;
168 m_dVelocity = std::sqrt(std::pow(dXPosition - m_dXPosition, 2) + std::pow(dYPosition - m_dYPosition, 2)) / dTimeElapsed;
169
170 // Update the last update time.
171 m_tmLastUpdateTime = tmCurrentTime;
172 }
173
174 // Calculate the steering angle of the rover.
175 m_dSteeringAngle = (std::atan2(dYPosition - m_dYPosition, dXPosition - m_dXPosition) * 180.0 / M_PI) - m_dTheta;
176 // Ensure the steering angle stays within 0-360 degrees.
177 m_dSteeringAngle = numops::InputAngleModulus(m_dSteeringAngle, 0.0, 360.0);
178
179 // Update member variables.
180 m_dXPosition = dXPosition;
181 m_dYPosition = dYPosition;
182 m_dTheta = dTheta;
183 }
184
185
195 void Predict(const double dTimeStep, const int nNumPredictions, std::vector<Prediction>& vPredictions)
196 {
197 // Start from the current state.
198 double dXPredicted = m_dXPosition;
199 double dYPredicted = m_dYPosition;
200 double dThetaPredicted = m_dTheta;
201
202 // Perform prediction for a specified number of time steps.
203 for (int nIter = 0; nIter < nNumPredictions; ++nIter)
204 {
205 // Convert theta from degrees to radians for calculation.
206 double dThetaRad = dThetaPredicted * M_PI / 180.0;
207 double dSteeringAngleRad = m_dSteeringAngle * M_PI / 180.0;
208
209 // Calculate the new state.
210 dXPredicted += m_dVelocity * std::sin(dThetaRad) * dTimeStep;
211 dYPredicted += m_dVelocity * std::cos(dThetaRad) * dTimeStep;
212 dThetaPredicted += (m_dVelocity / m_dWheelbase) * std::tan(dSteeringAngleRad - M_PI_2) * dTimeStep;
213
214 // Ensure theta stays within 0-360 degrees.
215 dThetaPredicted = numops::InputAngleModulus(dThetaPredicted, 0.0, 360.0);
216
217 // Store the new state.
218 Prediction stPrediction{dXPredicted, dYPredicted, dThetaPredicted};
219 vPredictions.push_back(stPrediction);
220 }
221 }
222
224 // Setters.
226
227
235 void SetWheelbase(const double dWheelbase) { m_dWheelbase = dWheelbase; }
236
237
245 void SetXPosition(const double dXPosition) { m_dXPosition = dXPosition; }
246
247
255 void SetYPosition(const double dYPosition) { m_dYPosition = dYPosition; }
256
257
265 void SetTheta(const double dTheta) { m_dTheta = dTheta; }
266
267
275 void SetSteeringAngle(const double dSteeringAngle) { m_dSteeringAngle = dSteeringAngle; }
276
278 // Getters.
280
281
289 double GetWheelbase() const { return m_dWheelbase; }
290
291
299 double GetXPosition() const { return m_dXPosition; }
300
301
309 double GetYPosition() const { return m_dYPosition; }
310
311
319 double GetTheta() const { return m_dTheta; }
320
321
329 double GetVelocity() const { return m_dVelocity; }
330
331
339 double GetSteeringAngle() const { return m_dSteeringAngle; }
340
341 private:
343 // Declare private member variables.
345
346 double m_dWheelbase;
347 double m_dXPosition;
348 double m_dYPosition;
349 double m_dTheta;
350 double m_dVelocity;
351 double m_dSteeringAngle;
352 std::chrono::system_clock::time_point m_tmLastUpdateTime;
353};
354#endif
This class implements the Bicycle Model. This model is used to predict the future state of the rover ...
Definition BicycleModel.hpp:35
BicycleModel()
Construct a new Bicycle Model object.
Definition BicycleModel.hpp:67
double GetXPosition() const
Accessor for the XPosition private member.
Definition BicycleModel.hpp:299
double GetSteeringAngle() const
Accessor for the Steering Angle private member.
Definition BicycleModel.hpp:339
void ResetState(const double dXPosition, const double dYPosition, const double dTheta)
Resets the state of the model to a new position and heading.
Definition BicycleModel.hpp:112
void SetXPosition(const double dXPosition)
Mutator for the XPosition private member.
Definition BicycleModel.hpp:245
double GetVelocity() const
Accessor for the Velocity private member.
Definition BicycleModel.hpp:329
double GetTheta() const
Accessor for the Theta private member.
Definition BicycleModel.hpp:319
void Predict(const double dTimeStep, const int nNumPredictions, std::vector< Prediction > &vPredictions)
Accessor for the State private member.
Definition BicycleModel.hpp:195
void SetWheelbase(const double dWheelbase)
Mutator for the Wheelbase private member.
Definition BicycleModel.hpp:235
double GetWheelbase() const
Accessor for the Wheelbase private member.
Definition BicycleModel.hpp:289
void SetTheta(const double dTheta)
Mutator for the Theta private member.
Definition BicycleModel.hpp:265
void ResetState()
Resets the state of the model to a default state.
Definition BicycleModel.hpp:130
void UpdateState(const double dXPosition, const double dYPosition, const double dTheta)
Update the state of the model, given a new position and heading. This method will automatically calcu...
Definition BicycleModel.hpp:154
void SetYPosition(const double dYPosition)
Mutator for the YPosition private member.
Definition BicycleModel.hpp:255
BicycleModel(const double dWheelbase, const double dXPosition, const double dYPosition, const double dTheta)
Construct a new Bicycle Model object.
Definition BicycleModel.hpp:90
void SetSteeringAngle(const double dSteeringAngle)
Mutator for the Steering Angle private member.
Definition BicycleModel.hpp:275
double GetYPosition() const
Accessor for the YPosition private member.
Definition BicycleModel.hpp:309
constexpr T InputAngleModulus(T tValue, T tMinValue, T tMaxValue)
Calculates the modulus of an input angle.
Definition NumberOperations.hpp:165
This struct is used to store the predicted state of the bicycle.
Definition BicycleModel.hpp:49