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
controllers::StanleyController Class Reference

Provides an implementation of a lightweight lateral StanleyController. This algorithm is used to precisely control a different drive robot to follow a given path. More...

#include <StanleyController.h>

Public Member Functions

 StanleyController ()
 Default constructor for Stanley Controller.
 
 StanleyController (const double dKp, const double dDistToFrontAxle, const double dYawTolerance)
 Construct a new Stanley Contoller:: Stanley Contoller object.
 
 StanleyController (const std::vector< geoops::UTMCoordinate > &vUTMPath, const double dKp, const double dDistToFrontAxle, const double dYawTolerance)
 Construct a new Stanley Contoller:: Stanley Contoller object.
 
 StanleyController (const std::vector< geoops::GPSCoordinate > &vGPSPath, const double dKp, const double dDistToFrontAxle, const double dYawTolerance)
 Construct a new Stanley Contoller:: Stanley Contoller object.
 
 ~StanleyController ()
 Destroy the Stanley Contoller:: Stanley Contoller object.
 
double Calculate (const geoops::UTMCoordinate &stUTMCurrPos, const double dVelocity, const double dBearing)
 Calculate the steering control adjustment for an agent using the Stanley method.
 
double Calculate (const geoops::GPSCoordinate &stGPSCurrPos, const double dVelocity, const double dBearing)
 Calculate the steering control adjustment for an agent using the Stanley method.
 
void ResetProgress ()
 Resets the progress on the current path.
 
void SetSteeringControlGain (const double dKpp)
 Setter for steering control gain.
 
void SetDistanceToFrontAxle (const double dDistToFrontAxle)
 Setter for distance to front axle.
 
void SetYawTolerance (const double dYawTolerance)
 Setter for yaw tolerance.
 
void SetPath (std::vector< geoops::UTMCoordinate > &vUTMPath)
 Setter for path.
 
void SetPath (std::vector< geoops::GPSCoordinate > &vGPSPath)
 Setter for path.
 
void ClearPath ()
 
double GetSteeringControlGain () const
 Getter for steering control gain.
 
double GetDistanceToFrontAxle () const
 Getter for distance to the front axle.
 
double GetYawTolerance () const
 Getter for yaw tolerance.
 
unsigned int GetLastTargetIdx () const
 Getter for the index of the last target point on the path.
 
std::vector< geoops::UTMCoordinateGetPathUTM () const
 Getter for path.
 
std::vector< geoops::GPSCoordinateGetPathGPS () const
 Getter for path.
 

Private Member Functions

geoops::UTMCoordinate CalculateFrontAxleCoordinate (const geoops::UTMCoordinate &stUTMCurrPos, const double dBearing) const
 Calculate the UTM coordinate of the center of the agent's front axle.
 
unsigned int IdentifyTargetIdx (const geoops::UTMCoordinate &stUTMFrontAxlePos) const
 Identifies the closest point to the center of the agent's front axle on the path.
 
double CalculateTargetBearing (const unsigned int unTargetIdx) const
 Calculate the required bearing to navigate from the current target point to the subsequent point on the path.
 
double CalculateCrossTrackError (const geoops::UTMCoordinate &stUTMFrontAxlePos, const unsigned int unTargetIdx, const double dBearing) const
 Calculate the cross track error. This error expresses how far off the agent is from the path (lateral distance).
 

Private Attributes

double m_dKp
 
double m_dDistToFrontAxle
 
double m_dYawTolerance
 
unsigned int m_unLastTargetIdx
 
std::vector< geoops::UTMCoordinatem_vUTMPath
 
std::vector< geoops::GPSCoordinatem_vGPSPath
 

Detailed Description

Provides an implementation of a lightweight lateral StanleyController. This algorithm is used to precisely control a different drive robot to follow a given path.

Note
In this class heading/bearing refers to the absolute orientation of a rover (N,E,S,W). While yaw refers to an angle relative to the agent's current orientation.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-01

Constructor & Destructor Documentation

◆ StanleyController() [1/4]

controllers::StanleyController::StanleyController ( )

Default constructor for Stanley Controller.

Author
Kai Shafe (kasq5.nosp@m.m@um.nosp@m.syste.nosp@m.m.ed.nosp@m.u)
Date
2024-04-28
36 {
37 // Nothing to do.
38 }

◆ StanleyController() [2/4]

controllers::StanleyController::StanleyController ( const double  dKp,
const double  dDistToFrontAxle,
const double  dYawTolerance 
)

Construct a new Stanley Contoller:: Stanley Contoller object.

Parameters
dKp- Steering control gain.
dDistToFrontAxle- Distance between the position sensor (GPS) and the front axle.
dYawTolerance- Minimum yaw change threshold for execution.
Note
The higher the steering control gain the more reactive the rover will be to changes in yaw.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-03
53 {
54 // Initialize member variables
55 m_dKp = dKp;
56 m_dDistToFrontAxle = dDistToFrontAxle;
57 m_dYawTolerance = dYawTolerance;
58 m_unLastTargetIdx = 0;
59 }

◆ StanleyController() [3/4]

controllers::StanleyController::StanleyController ( const std::vector< geoops::UTMCoordinate > &  vUTMPath,
const double  dKp,
const double  dDistToFrontAxle,
const double  dYawTolerance 
)

Construct a new Stanley Contoller:: Stanley Contoller object.

Parameters
vUTMPath- Vector of UTM coordinates describing path to follow.
dKp- Steering control gain.
dDistToFrontAxle- Distance between the position sensor (GPS) and the front axle.
dYawTolerance- Minimum yaw change threshold for execution.
Note
The higher the steering control gain the more reactive the rover will be to changes in yaw.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-03
75 {
76 // Initialize member variables
77 m_dKp = dKp;
78 m_dDistToFrontAxle = dDistToFrontAxle;
79 m_dYawTolerance = dYawTolerance;
80 m_unLastTargetIdx = 0;
81 m_vUTMPath = vUTMPath;
82
83 // Convert the UTM path to a GPS path and save it.
84 m_vGPSPath.clear();
85 std::vector<geoops::UTMCoordinate>::const_iterator itrUTM = vUTMPath.begin();
86 while (itrUTM != vUTMPath.end())
87 {
88 m_vGPSPath.push_back(geoops::ConvertUTMToGPS(*itrUTM));
89 ++itrUTM;
90 }
91 }
GPSCoordinate ConvertUTMToGPS(const UTMCoordinate &stUTMCoord)
Given a UTM coordinate, convert to GPS and create a new GPSCoordinate object.
Definition GeospatialOperations.hpp:396
Here is the call graph for this function:

◆ StanleyController() [4/4]

controllers::StanleyController::StanleyController ( const std::vector< geoops::GPSCoordinate > &  vGPSPath,
const double  dKp,
const double  dDistToFrontAxle,
const double  dYawTolerance 
)

Construct a new Stanley Contoller:: Stanley Contoller object.

Parameters
vGPSPath- Vector of GPS coordinates describing path to follow.
dKp- Steering control gain.
dDistToFrontAxle- Distance between the position sensor (GPS) and the front axle.
dYawTolerance- Minimum yaw change threshold for execution.
Note
The higher the steering control gain the more reactive the rover will be to changes in yaw.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-17-03
107 {
108 // Initialize member variables
109 m_dKp = dKp;
110 m_dDistToFrontAxle = dDistToFrontAxle;
111 m_dYawTolerance = dYawTolerance;
112 m_unLastTargetIdx = 0;
113
114 // For each GPS coordinate convert it to UTM and save it to the path.
115 std::vector<geoops::GPSCoordinate>::const_iterator itrGPS = vGPSPath.begin();
116 while (itrGPS != vGPSPath.end())
117 {
118 m_vUTMPath.push_back(geoops::ConvertGPSToUTM(*itrGPS));
119 ++itrGPS;
120 }
121 m_vGPSPath = vGPSPath;
122 }
UTMCoordinate ConvertGPSToUTM(const GPSCoordinate &stGPSCoord)
Given a GPS coordinate, convert to UTM and create a new UTMCoordinate object.
Definition GeospatialOperations.hpp:351
Here is the call graph for this function:

◆ ~StanleyController()

controllers::StanleyController::~StanleyController ( )

Destroy the Stanley Contoller:: Stanley Contoller object.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-01
132 {
133 // Nothing to destroy yet.
134 }

Member Function Documentation

◆ Calculate() [1/2]

double controllers::StanleyController::Calculate ( const geoops::UTMCoordinate stUTMCurrPos,
const double  dVelocity,
const double  dBearing 
)

Calculate the steering control adjustment for an agent using the Stanley method.

This function computes the necessary change in yaw (steering angle) to align the agent with the predetermined path. The steering angle is limited by the proportional gain constant to prevent excessively sharp turns.

Parameters
stUTMCurrPos- The agent's current position in the UTM coordinate space.
dVelocity- The agent's current magnitude of velocity.
dBearing- The agent's current yaw angle (heading).
Returns
double - The calculated change in yaw needed to align the agent with the path.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-03
151 {
152 // Verify the given bearing is within 0-360 degrees.
153 if (dBearing < 0 || dBearing > 360)
154 {
155 LOG_ERROR(logging::g_qSharedLogger, "StanleyController::Calculate bearing must be in the interval [0-360]. Received: {}", dBearing);
156 }
157 // Verify a path has been loaded into the Stanley Controller
158 if (m_vUTMPath.empty())
159 {
160 LOG_ERROR(logging::g_qSharedLogger, "StanleyController::Calculate No path has been loaded.");
161 }
162
163 // Calculate the position for the center of the front axle.
164 geoops::UTMCoordinate stUTMFrontAxlePos = CalculateFrontAxleCoordinate(stUTMCurrPos, dBearing);
165
166 // Find the point on the path closest to the front axle center
167 unsigned int unTargetIdx = IdentifyTargetIdx(stUTMFrontAxlePos);
168
169 // Make sure the agent proceeds forward through the path
170 unTargetIdx = std::max(unTargetIdx, m_unLastTargetIdx);
171 m_unLastTargetIdx = unTargetIdx;
172
173 // Calculate the change in yaw needed to go from the target to the next point
174 double dTargetYaw = CalculateTargetBearing(unTargetIdx);
175
176 // Calculate the difference between the rover's yaw and the desired path yaw
177 double dYawError = numops::InputAngleModulus<double>(dTargetYaw - dBearing, -180.0, 180.0);
178
179 // Calculate the change in yaw needed to correct for the cross track error
180 double dCrossTrackError = CalculateCrossTrackError(stUTMFrontAxlePos, unTargetIdx, dBearing);
181 double dDeltaYaw = dYawError + std::atan2(m_dKp * dCrossTrackError, dVelocity);
182
183 // If a rotation is small enough we will just go ahead and skip it
184 if (std::abs(dDeltaYaw) < m_dYawTolerance)
185 {
186 dDeltaYaw = 0;
187 }
188
189 // Here we translate the relative change in yaw to an absolute heading
190 return numops::InputAngleModulus<double>(dBearing + dDeltaYaw, 0, 360);
191 }
double CalculateTargetBearing(const unsigned int unTargetIdx) const
Calculate the required bearing to navigate from the current target point to the subsequent point on t...
Definition StanleyController.cpp:479
unsigned int IdentifyTargetIdx(const geoops::UTMCoordinate &stUTMFrontAxlePos) const
Identifies the closest point to the center of the agent's front axle on the path.
Definition StanleyController.cpp:444
double CalculateCrossTrackError(const geoops::UTMCoordinate &stUTMFrontAxlePos, const unsigned int unTargetIdx, const double dBearing) const
Calculate the cross track error. This error expresses how far off the agent is from the path (lateral...
Definition StanleyController.cpp:532
geoops::UTMCoordinate CalculateFrontAxleCoordinate(const geoops::UTMCoordinate &stUTMCurrPos, const double dBearing) const
Calculate the UTM coordinate of the center of the agent's front axle.
Definition StanleyController.cpp:409
This struct stores/contains information about a UTM coordinate.
Definition GeospatialOperations.hpp:244
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Calculate() [2/2]

double controllers::StanleyController::Calculate ( const geoops::GPSCoordinate stGPSCurrPos,
const double  dVelocity,
const double  dBearing 
)

Calculate the steering control adjustment for an agent using the Stanley method.

This function computes the necessary change in yaw (steering angle) to align the agent with the predetermined path. The steering angle is limited by the proportional gain constant to prevent excessively sharp turns.

Parameters
stGPSCurrPos- The agent's current position in the GPS coordinate space.
dVelocity- The agent's current magnitude of velocity.
dBearing- The agent's current yaw angle (heading).
Returns
double - The calculated change in yaw needed to align the agent with the path.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-17
208 {
209 geoops::UTMCoordinate stUTMCurrPos = geoops::ConvertGPSToUTM(stGPSCurrPos);
210 return Calculate(stUTMCurrPos, dVelocity, dBearing);
211 }
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
Here is the call graph for this function:

◆ ResetProgress()

void controllers::StanleyController::ResetProgress ( )

Resets the progress on the current path.

The stanley control algorithm prevents points that have been pass to be revisited to motivate the agent to go further along the path. Although sometimes it may be necessary to restart an agents progress if it deviates far enough from the path.

Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-16
225 {
226 m_unLastTargetIdx = 0;
227 }
Here is the caller graph for this function:

◆ SetSteeringControlGain()

void controllers::StanleyController::SetSteeringControlGain ( const double  dKp)

Setter for steering control gain.

Parameters
dKp- Steering control gain.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-16
238 {
239 m_dKp = dKp;
240 }

◆ SetDistanceToFrontAxle()

void controllers::StanleyController::SetDistanceToFrontAxle ( const double  dDistToFrontAxle)

Setter for distance to front axle.

Parameters
dDistToFrontAxle- Distance between the position sensor (GPS) and the front axle.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-16
251 {
252 m_dDistToFrontAxle = dDistToFrontAxle;
253 }

◆ SetYawTolerance()

void controllers::StanleyController::SetYawTolerance ( const double  dYawTolerance)

Setter for yaw tolerance.

Parameters
dYawTolerance- Minimum yaw change threshold for execution.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-16
264 {
265 m_dYawTolerance = dYawTolerance;
266 }

◆ SetPath() [1/2]

void controllers::StanleyController::SetPath ( std::vector< geoops::UTMCoordinate > &  vUTMPath)

Setter for path.

Parameters
vUTMPath- Vector of UTM coordinates describing path to follow.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-16
277 {
278 m_vUTMPath = vUTMPath;
279 m_unLastTargetIdx = 0;
280
281 // Convert the UTM path to a GPS path and save it.
282 m_vGPSPath.clear();
283 std::vector<geoops::UTMCoordinate>::const_iterator itrUTM = vUTMPath.begin();
284 while (itrUTM != vUTMPath.end())
285 {
286 m_vGPSPath.push_back(geoops::ConvertUTMToGPS(*itrUTM));
287 ++itrUTM;
288 }
289 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetPath() [2/2]

void controllers::StanleyController::SetPath ( std::vector< geoops::GPSCoordinate > &  vGPSPath)

Setter for path.

Parameters
vGPSPath- Vector of GPS coordinates describing path to follow.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-17
300 {
301 m_vUTMPath.clear();
302
303 // For each GPS coordinate convert it to UTM and save it to the path.
304 std::vector<geoops::GPSCoordinate>::const_iterator itrGPS = vGPSPath.begin();
305 while (itrGPS != vGPSPath.end())
306 {
307 m_vUTMPath.push_back(geoops::ConvertGPSToUTM(*itrGPS));
308 ++itrGPS;
309 }
310 m_vGPSPath = vGPSPath;
311
312 m_unLastTargetIdx = 0;
313 }
Here is the call graph for this function:

◆ ClearPath()

void controllers::StanleyController::ClearPath ( )
394 {
395 m_vUTMPath.clear();
396 m_vGPSPath.clear();
397 }

◆ GetSteeringControlGain()

double controllers::StanleyController::GetSteeringControlGain ( ) const

Getter for steering control gain.

Returns
double - Steering control gain.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-16
324 {
325 return m_dKp;
326 }

◆ GetDistanceToFrontAxle()

double controllers::StanleyController::GetDistanceToFrontAxle ( ) const

Getter for distance to the front axle.

Returns
double - Distance between the position sensor (GPS) and the front axle.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-16
337 {
338 return m_dDistToFrontAxle;
339 }

◆ GetYawTolerance()

double controllers::StanleyController::GetYawTolerance ( ) const

Getter for yaw tolerance.

Returns
double - Minimum yaw change threshold for execution.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-16
350 {
351 return m_dYawTolerance;
352 }

◆ GetLastTargetIdx()

unsigned int controllers::StanleyController::GetLastTargetIdx ( ) const

Getter for the index of the last target point on the path.

Returns
unsigned int - Index of the last target point on the path.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-16
363 {
364 return m_unLastTargetIdx;
365 }
Here is the caller graph for this function:

◆ GetPathUTM()

std::vector< geoops::UTMCoordinate > controllers::StanleyController::GetPathUTM ( ) const

Getter for path.

Returns
std::vector<geoops::UTMCoordinate> - Sequence of UTM coordinates defining the navigational path.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-16
376 {
377 return m_vUTMPath;
378 }

◆ GetPathGPS()

std::vector< geoops::GPSCoordinate > controllers::StanleyController::GetPathGPS ( ) const

Getter for path.

Returns
std::vector<geoops::GPSCoordinate> - Sequence of GPS coordinates defining the navigational path.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-17
389 {
390 return m_vGPSPath;
391 }

◆ CalculateFrontAxleCoordinate()

geoops::UTMCoordinate controllers::StanleyController::CalculateFrontAxleCoordinate ( const geoops::UTMCoordinate stUTMCurrPos,
const double  dBearing 
) const
private

Calculate the UTM coordinate of the center of the agent's front axle.

Parameters
stUTMCurrPos- The agent's current position in the UTM coordinate space.
dBearing- The current bearing of the agent, measured in degrees from 0 to 360.
Returns
UTMCoordinate - The UTM coordinate of the center of the agent's front axle.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-08
410 {
411 // Convert the bearing to a change in degrees of yaw relative to the north axis
412 // Here a positive degree represents a change in yaw towards the East.
413 // Here a negative degree represents a change in yaw towards the West.
414 double dChangeInYawRelToNorth = dBearing <= 180 ? dBearing : dBearing - 360;
415
416 // Convert to radians.
417 dChangeInYawRelToNorth = (dChangeInYawRelToNorth / 180) * M_PI;
418
419 // Calculate the unit vector for the agent's orientation in terms of East and North.
420 double dOrientEast = std::sin(dChangeInYawRelToNorth);
421 double dOrientNorth = std::cos(dChangeInYawRelToNorth);
422
423 // Calculate the UTM coordinate for the center of the agent's front axle.
424 geoops::UTMCoordinate stUTMFrontAxlePos = geoops::UTMCoordinate(stUTMCurrPos);
425 stUTMFrontAxlePos.dEasting = stUTMCurrPos.dEasting + m_dDistToFrontAxle * dOrientEast;
426 stUTMFrontAxlePos.dNorthing = stUTMCurrPos.dNorthing + m_dDistToFrontAxle * dOrientNorth;
427
428 // Return the UTM coordinate of the center of the agent's front axle.
429 return stUTMFrontAxlePos;
430 }
Here is the caller graph for this function:

◆ IdentifyTargetIdx()

unsigned int controllers::StanleyController::IdentifyTargetIdx ( const geoops::UTMCoordinate stUTMFrontAxlePos) const
private

Identifies the closest point to the center of the agent's front axle on the path.

This function scans through a set of path points to find the one that is nearest to the center of the agent's front axle, providing a crucial reference for any path corrections.

Parameters
stUTMFrontAxlePos- The UTM coordinate of the center of the agent's front axle.
Returns
unsigned int - Index of the target point on the path.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-03
445 {
446 // Search for the nearest point in the path
447 unsigned int unTargetIdx = 0;
448 double dMinDistance = std::numeric_limits<double>::max();
449 std::vector<geoops::UTMCoordinate>::const_iterator itPath = this->m_vUTMPath.begin();
450 while (itPath != this->m_vUTMPath.end())
451 {
452 // Find the distance in meters between the center of the front axle and a point on the path.
453 double dDistanceFromPoint = std::hypot(stUTMFrontAxlePos.dNorthing - itPath->dNorthing, stUTMFrontAxlePos.dEasting - itPath->dEasting);
454
455 // Update the closest point to the front axle's center if the current distance is the shortest recorded.
456 // Save both the point's index and the distance.
457 if (dDistanceFromPoint < dMinDistance)
458 {
459 unTargetIdx = std::distance(this->m_vUTMPath.begin(), itPath);
460 dMinDistance = dDistanceFromPoint;
461 }
462
463 // Move the iterator to the next point in the path
464 ++itPath;
465 }
466
467 return unTargetIdx;
468 }
Here is the caller graph for this function:

◆ CalculateTargetBearing()

double controllers::StanleyController::CalculateTargetBearing ( const unsigned int  unTargetIdx) const
private

Calculate the required bearing to navigate from the current target point to the subsequent point on the path.

Parameters
unTargetIdx- Index of the target point on the path.
Returns
double - Bearing required to get from the target point to the subsequent point on the path.
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-03
480 {
481 // Verify the target index is a valid point on the path.
482 if (unTargetIdx >= m_vUTMPath.size())
483 {
484 LOG_ERROR(logging::g_qSharedLogger, "StanleyController::CalculateTargetBearing target {} does not exist.", unTargetIdx);
485 }
486
487 // The yaw is calculated by finding the bearing needed to navigate from the
488 // target point to the next point in the path.
489 geoops::UTMCoordinate stUTMTargetPoint = m_vUTMPath[unTargetIdx];
490 geoops::UTMCoordinate stUTMNextPoint = m_vUTMPath[unTargetIdx + 1];
491
492 // Calculate the displacement from the target point to the next point in the path.
493 double dDisplacementEast = stUTMNextPoint.dEasting - stUTMTargetPoint.dEasting;
494 double dDisplacementNorth = stUTMNextPoint.dNorthing - stUTMTargetPoint.dNorthing;
495
496 // Calculate the magnitude of the displacement.
497 double dDisplacementMagnitude = std::hypot(dDisplacementEast, dDisplacementNorth);
498
499 // Calculate the bearing in degrees required to navigate to the next point on the path.
500 dDisplacementNorth /= dDisplacementMagnitude;
501 double dBearing = (std::acos(dDisplacementNorth) / M_PI) * 180;
502 if (dDisplacementEast < 0)
503 {
504 dBearing = 360 - dBearing;
505 }
506
507 // Return the relative bearing needed to get from the target point to the next point in the path.
508 return dBearing;
509 }
Here is the caller graph for this function:

◆ CalculateCrossTrackError()

double controllers::StanleyController::CalculateCrossTrackError ( const geoops::UTMCoordinate stUTMFrontAxlePos,
const unsigned int  unTargetIdx,
const double  dBearing 
) const
private

Calculate the cross track error. This error expresses how far off the agent is from the path (lateral distance).

Note
Here is what causes a high magnitude for cross track error.
  1. The agent is far from the target point (a large displacement from the path).
  2. The agent is driving parallel to the path. The reason for number 2 is if the agent is far from the path and driving parallel it will continue to stay off course. However, if the agent is on the path and driving parallel the displacement vector will be negligible and the error will likewise be negligible.
Note that the significance of the error is its magnitude. Positive and negative are the same amount of error just in opposing directions.
Parameters
stUTMFrontAxlePos- The UTM coordinate of the center of the agent's front axle.
unTargetIdx- Index of the target point on the path.
dBearing- The current bearing of the agent, measured in degrees from 0 to 360.
Returns
double - The cross track error (CTE).
Author
JSpencerPittman (jspen.nosp@m.cerp.nosp@m.ittma.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-02-09
533 {
534 // Convert the bearing to a change in degrees of yaw relative to the north axis
535 // Here a positive degree represents a change in yaw towards the East.
536 // Here a negative degree represents a change in yaw towards the West.
537 double dChangeInYawRelToNorth = dBearing <= 180 ? dBearing : dBearing - 360;
538 dChangeInYawRelToNorth = (dChangeInYawRelToNorth / 180) * M_PI;
539 dChangeInYawRelToNorth -= M_PI / 2;
540
541 // Calculate the front axle vector.
542 // This vector will point perpendicular to the orientation of the agent (representing the front drive axle).
543 double dFrontDriveAxleX = std::sin(dChangeInYawRelToNorth);
544 double dFrontDriveAxleY = std::cos(dChangeInYawRelToNorth);
545
546 // Find the displacement between the target and the center of the front drive axle.
547 geoops::UTMCoordinate stUTMTargetPos = m_vUTMPath[unTargetIdx];
548 double dDisplacementX = stUTMFrontAxlePos.dEasting - stUTMTargetPos.dEasting;
549 double dDisplacementY = stUTMFrontAxlePos.dNorthing - stUTMTargetPos.dNorthing;
550
551 // Calculate the cross track error as a dot product of our front drive axle and the displacement vector.
552 return (dDisplacementX * dFrontDriveAxleX) + (dDisplacementY * dFrontDriveAxleY);
553 }
Here is the caller graph for this function:

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