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
DriveBoard Class Reference

This class handles communication with the drive board on the rover by sending RoveComm packets over the network. More...

#include <DriveBoard.h>

Collaboration diagram for DriveBoard:

Public Member Functions

 DriveBoard ()
 Construct a new Drive Board::DriveBoard object.
 
 ~DriveBoard ()
 Destroy the Drive Board::DriveBoard object.
 
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.
 
void SendDrive (diffdrive::DrivePowers &stDrivePowers)
 Sets the left and right drive powers of the drive board.
 
void SendStop ()
 Stop the drivetrain of the Rover.
 
void SetMaxDriveEffort (const float fMaxDriveEffortMultiplier)
 Set the max power limits of the drive.
 
diffdrive::DrivePowers GetDrivePowers () const
 Accessor for the current drive powers of the robot.
 

Private Attributes

diffdrive::DrivePowers m_stDrivePowers
 
controllers::PIDControllerm_pPID
 
float m_fMinDriveEffort
 
float m_fMaxDriveEffort
 
std::shared_mutex m_muDriveEffortMutex
 
const std::function< void(const rovecomm::RoveCommPacket< float > &, const sockaddr_in &)> SetMaxSpeedCallback
 Callback function that is called whenever RoveComm receives a new SETMAXSPEED packet.
 

Detailed Description

This class handles communication with the drive board on the rover by sending RoveComm packets over the network.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-09-21

Constructor & Destructor Documentation

◆ DriveBoard()

DriveBoard::DriveBoard ( )

Construct a new Drive Board::DriveBoard object.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-09-21
32{
33 // Initialize member variables.
34 m_stDrivePowers.dLeftDrivePower = 0.0;
35 m_stDrivePowers.dRightDrivePower = 0.0;
36 m_fMinDriveEffort = constants::DRIVE_MIN_EFFORT;
37 m_fMaxDriveEffort = constants::DRIVE_MAX_EFFORT;
38
39 // Configure PID controller for heading hold function.
40 m_pPID = new controllers::PIDController(constants::DRIVE_PID_PROPORTIONAL,
41 constants::DRIVE_PID_INTEGRAL,
42 constants::DRIVE_PID_DERIVATIVE,
43 constants::DRIVE_PID_FEEDFORWARD);
44 m_pPID->SetMaxSetpointDifference(constants::DRIVE_PID_MAX_ERROR_PER_ITER);
45 m_pPID->SetMaxIntegralEffort(constants::DRIVE_PID_MAX_INTEGRAL_TERM);
46 m_pPID->SetOutputLimits(1.0); // Autonomy internally always uses -1.0, 1.0 for turning and drive powers.
47 m_pPID->SetOutputRampRate(constants::DRIVE_PID_MAX_RAMP_RATE);
48 m_pPID->SetOutputFilter(constants::DRIVE_PID_OUTPUT_FILTER);
49 m_pPID->SetMaxSetpointDifference(constants::DRIVE_PID_TOLERANCE);
50 m_pPID->SetDirection(constants::DRIVE_PID_OUTPUT_REVERSED);
51 m_pPID->EnableContinuousInput(0, 360);
52
53 // Set RoveComm callbacks.
54 if (network::g_pRoveCommUDPNode)
55 {
56 network::g_pRoveCommUDPNode->AddUDPCallback<float>(SetMaxSpeedCallback, manifest::Autonomy::COMMANDS.find("SETMAXSPEED")->second.DATA_ID);
57 }
58}
const std::function< void(const rovecomm::RoveCommPacket< float > &, const sockaddr_in &)> SetMaxSpeedCallback
Callback function that is called whenever RoveComm receives a new SETMAXSPEED packet.
Definition DriveBoard.h:57
This class encapsulates the fundamental principles of Proportional-Integral-Derivative (PID) control ...
Definition PIDController.h:50
void SetMaxSetpointDifference(const double dMaxSetpointDifference)
Mutator for the maximum allowable distance of the setpoint from the actual value. This doesn't limit ...
Definition PIDController.cpp:435
void SetMaxIntegralEffort(const double dMaxIEffort)
Mutator for the max value of the integral term during PID calculation. This is useful for preventing ...
Definition PIDController.cpp:450
void SetOutputRampRate(const double dOutputRampRate)
Mutator for the maximum rate that the output can change per iteration.
Definition PIDController.cpp:507
void SetOutputFilter(const double dStrength)
Set a filter on the output to reduce sharp oscillations. 0.1 is likely a good starting point.
Definition PIDController.cpp:522
void EnableContinuousInput(const double dMinimumInput, const double dMaximumInput)
Enables continuous wraparound of PID controller input. Use for setpoints that exists on a circle....
Definition PIDController.cpp:238
void SetOutputLimits(const double dMinEffort, const double dMaxEffort)
Mutator for setting the minimum and maximum allowable values of the control output from the controlle...
Definition PIDController.cpp:466
void SetDirection(const bool bReversed=false)
Set the operating direction of the PID controller. Set true to reverse PID output.
Definition PIDController.cpp:541
Here is the call graph for this function:

◆ ~DriveBoard()

DriveBoard::~DriveBoard ( )

Destroy the Drive Board::DriveBoard object.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-09-21
68{
69 // Stop drivetrain.
70 this->SendStop();
71
72 // Delete dynamically allocated memory.
73 delete m_pPID;
74
75 // Set dangling pointers to null.
76 m_pPID = nullptr;
77}
void SendStop()
Stop the drivetrain of the Rover.
Definition DriveBoard.cpp:162
Here is the call graph for this function:

Member Function Documentation

◆ CalculateMove()

diffdrive::DrivePowers DriveBoard::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.

Parameters
dGoalSpeed- The speed to drive at (-1 to 1)
dGoalHeading- The angle to drive towards. (0 - 360) 0 is North.
dActualHeading- The real angle that the Rover is current facing.
eKinematicsMethod- The kinematics model to use for differential drive control. Enum within DifferentialDrive.hpp
Returns
diffdrive::DrivePowers - A struct containing two values. (left power, right power)
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-09-21
96{
97 // Calculate the drive powers from the current heading, goal heading, and goal speed.
98 m_stDrivePowers = diffdrive::CalculateMotorPowerFromHeading(dGoalSpeed,
99 dGoalHeading,
100 dActualHeading,
101 eKinematicsMethod,
102 *m_pPID,
103 constants::DRIVE_SQUARE_CONTROL_INPUTS,
104 constants::DRIVE_CURVATURE_KINEMATICS_ALLOW_TURN_WHILE_STOPPED);
105
106 return m_stDrivePowers;
107}
DrivePowers CalculateMotorPowerFromHeading(double dGoalSpeed, double dGoalHeading, double dActualHeading, const DifferentialControlMethod eDriveMethod, controllers::PIDController &PID, const bool bSquareControlInput=false, const bool bCurvatureDriveAllowTurningWhileStopped=true)
This function will calculate the drive powers for a given speed and absolute heading....
Definition DifferentialDrive.hpp:235
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendDrive()

void DriveBoard::SendDrive ( diffdrive::DrivePowers stDrivePowers)

Sets the left and right drive powers of the drive board.

Parameters
stDrivePowers- A struct containing info about the desired drive powers. Drive powers are always in between -1.0 and 1.0 no matter what constants or RoveComm say. the -1.0 to 1.0 range is automatically mapped to the correct DriveBoard range in this method.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-09-21
121{
122 // Limit input values.
123 double dLeftSpeed = std::clamp(stDrivePowers.dLeftDrivePower, -1.0, 1.0);
124 double dRightSpeed = std::clamp(stDrivePowers.dRightDrivePower, -1.0, 1.0);
125
126 // Remap -1.0 - 1.0 range to drive power range defined in constants. This is so that the driveboard/rovecomm can understand our input.
127 float fDriveBoardLeftPower = numops::MapRange(float(dLeftSpeed), -1.0f, 1.0f, m_fMinDriveEffort, m_fMaxDriveEffort);
128 float fDriveBoardRightPower = numops::MapRange(float(dRightSpeed), -1.0f, 1.0f, m_fMinDriveEffort, m_fMaxDriveEffort);
129 // Limit the power to max and min effort defined in constants.
130 fDriveBoardLeftPower = std::clamp(float(fDriveBoardLeftPower), constants::DRIVE_MIN_POWER, constants::DRIVE_MAX_POWER);
131 fDriveBoardRightPower = std::clamp(float(fDriveBoardRightPower), constants::DRIVE_MIN_POWER, constants::DRIVE_MAX_POWER);
132
133 // Update member variables with new target speeds.
134 m_stDrivePowers.dLeftDrivePower = fDriveBoardLeftPower;
135 m_stDrivePowers.dRightDrivePower = fDriveBoardRightPower;
136
137 // Construct a RoveComm packet with the drive data.
138 rovecomm::RoveCommPacket<float> stPacket;
139 stPacket.unDataId = manifest::Core::COMMANDS.find("DRIVELEFTRIGHT")->second.DATA_ID;
140 stPacket.unDataCount = manifest::Core::COMMANDS.find("DRIVELEFTRIGHT")->second.DATA_COUNT;
141 stPacket.eDataType = manifest::Core::COMMANDS.find("DRIVELEFTRIGHT")->second.DATA_TYPE;
142 stPacket.vData.emplace_back(fDriveBoardLeftPower);
143 stPacket.vData.emplace_back(fDriveBoardRightPower);
144 // Check if we should send packets to the SIM or board.
145 const char* cIPAddress = constants::MODE_SIM ? constants::SIM_IP_ADDRESS.c_str() : manifest::Core::IP_ADDRESS.IP_STR.c_str();
146 // Send drive command over RoveComm to drive board.
147 if (network::g_pRoveCommUDPNode)
148 {
149 network::g_pRoveCommUDPNode->SendUDPPacket(stPacket, cIPAddress, constants::ROVECOMM_OUTGOING_UDP_PORT);
150 }
151 // Submit logger message.
152 LOG_DEBUG(logging::g_qSharedLogger, "Driving at: ({}, {})", fDriveBoardLeftPower, fDriveBoardRightPower);
153}
constexpr T MapRange(const T tValue, const T tOldMinimum, const T tOldMaximum, const T tNewMinimum, const T tNewMaximum)
Maps a value to a new range given the old range.
Definition NumberOperations.hpp:131
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendStop()

void DriveBoard::SendStop ( )

Stop the drivetrain of the Rover.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2023-06-18
163{
164 // Update member variables with new target speeds.
165 m_stDrivePowers.dLeftDrivePower = 0.0;
166 m_stDrivePowers.dRightDrivePower = 0.0;
167
168 // Construct a RoveComm packet with the drive data.
169 rovecomm::RoveCommPacket<float> stPacket;
170 stPacket.unDataId = manifest::Core::COMMANDS.find("DRIVELEFTRIGHT")->second.DATA_ID;
171 stPacket.unDataCount = manifest::Core::COMMANDS.find("DRIVELEFTRIGHT")->second.DATA_COUNT;
172 stPacket.eDataType = manifest::Core::COMMANDS.find("DRIVELEFTRIGHT")->second.DATA_TYPE;
173 stPacket.vData.emplace_back(m_stDrivePowers.dLeftDrivePower);
174 stPacket.vData.emplace_back(m_stDrivePowers.dRightDrivePower);
175 // Check if we should send packets to the SIM or board.
176 const char* cIPAddress = constants::MODE_SIM ? constants::SIM_IP_ADDRESS.c_str() : manifest::Core::IP_ADDRESS.IP_STR.c_str();
177 // Send drive command over RoveComm to drive board.
178 if (network::g_pRoveCommUDPNode)
179 {
180 network::g_pRoveCommUDPNode->SendUDPPacket(stPacket, cIPAddress, constants::ROVECOMM_OUTGOING_UDP_PORT);
181 }
182 // Submit logger message.
183 LOG_DEBUG(logging::g_qSharedLogger, "Sent stop powers to drivetrain");
184}
Here is the caller graph for this function:

◆ SetMaxDriveEffort()

void DriveBoard::SetMaxDriveEffort ( const float  fMaxDriveEffortMultiplier)

Set the max power limits of the drive.

Parameters
fMinDriveEffort- A multiplier from 0-1 for the max power output of the drive. Multiplier will be applied to constants::DRIVE_MIN_POWER and constants::DRIVE_MAX_POWER.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-03-15
196{
197 // Acquire write lock for writing to max effort member variables.
198 std::unique_lock<std::shared_mutex> lkDriveEffortLock(m_muDriveEffortMutex);
199
200 // Update member variables.
201 m_fMinDriveEffort = constants::DRIVE_MIN_POWER * fMaxDriveEffortMultiplier;
202 m_fMaxDriveEffort = constants::DRIVE_MAX_POWER * fMaxDriveEffortMultiplier;
203}

◆ GetDrivePowers()

diffdrive::DrivePowers DriveBoard::GetDrivePowers ( ) const

Accessor for the current drive powers of the robot.

Returns
diffdrive::DrivePowers - A struct containing the left and right drive power of the drivetrain.
Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-20
214{
215 // Return the current drive powers.
216 return m_stDrivePowers;
217}

Member Data Documentation

◆ SetMaxSpeedCallback

const std::function<void(const rovecomm::RoveCommPacket<float>&, const sockaddr_in&)> DriveBoard::SetMaxSpeedCallback
private
Initial value:
=
[this](const rovecomm::RoveCommPacket<float>& stPacket, const sockaddr_in& stdAddr)
{
(void) stdAddr;
this->SetMaxDriveEffort(stPacket.vData[0]);
LOG_INFO(logging::g_qSharedLogger, "Incoming SETMAXSPEED: {}", stPacket.vData[0]);
}
void SetMaxDriveEffort(const float fMaxDriveEffortMultiplier)
Set the max power limits of the drive.
Definition DriveBoard.cpp:195

Callback function that is called whenever RoveComm receives a new SETMAXSPEED packet.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-03-03
59 {
60 // Not using this.
61 (void) stdAddr;
62
63 // Call class method to update max speed.
64 this->SetMaxDriveEffort(stPacket.vData[0]);
65
66 // Submit logger message.
67 LOG_INFO(logging::g_qSharedLogger, "Incoming SETMAXSPEED: {}", stPacket.vData[0]);
68 };

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