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=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.
 
void SendDrive (const 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
 
std::unique_ptr< 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 = std::make_unique<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);
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->SetTolerance(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

◆ ~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}
void SendStop()
Stop the drivetrain of the Rover.
Definition DriveBoard.cpp:163
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 = 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.

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
bAlwaysProgressForward- If true, the rover will always move forward or backward. Point turns will not be allowed.
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
92{
93 // Calculate the drive powers from the current heading, goal heading, and goal speed.
94 m_stDrivePowers = diffdrive::CalculateMotorPowerFromHeading(dGoalSpeed,
95 dGoalHeading,
96 dActualHeading,
97 eKinematicsMethod,
98 *m_pPID,
99 bAlwaysProgressForward,
100 constants::DRIVE_SQUARE_CONTROL_INPUTS,
101 constants::DRIVE_CURVATURE_KINEMATICS_ALLOW_TURN_WHILE_STOPPED);
102
103 return m_stDrivePowers;
104}
DrivePowers CalculateMotorPowerFromHeading(double dGoalSpeed, double dGoalHeading, double dActualHeading, const DifferentialControlMethod eDriveMethod, controllers::PIDController &PID, const bool bAlwaysProgressForward=false, 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:237
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendDrive()

void DriveBoard::SendDrive ( const 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
118{
119 // Create instance variables.
120 float fDriveBoardLeftPower = 0.0;
121 float fDriveBoardRightPower = 0.0;
122
123 // If the min and max drive effort have been set to 0, then just send zero powers.
124 if (m_fMinDriveEffort != 0.0 || m_fMaxDriveEffort != 0.0)
125 {
126 // Limit input values.
127 double dLeftSpeed = std::clamp(stDrivePowers.dLeftDrivePower, -1.0, 1.0);
128 double dRightSpeed = std::clamp(stDrivePowers.dRightDrivePower, -1.0, 1.0);
129 // Limit the power to max and min effort defined in constants.
130 fDriveBoardLeftPower = std::clamp(float(dLeftSpeed), constants::DRIVE_MIN_EFFORT, constants::DRIVE_MAX_EFFORT);
131 fDriveBoardRightPower = std::clamp(float(dRightSpeed), constants::DRIVE_MIN_EFFORT, constants::DRIVE_MAX_EFFORT);
132 // Update member variables with new target speeds.
133 m_stDrivePowers.dLeftDrivePower = fDriveBoardLeftPower;
134 m_stDrivePowers.dRightDrivePower = fDriveBoardRightPower;
135 }
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 // Send drive command over RoveComm to drive board.
145 if (network::g_pRoveCommUDPNode)
146 {
147 // Check if we should send packets to the SIM or board.
148 const char* cIPAddress = constants::MODE_SIM ? constants::SIM_IP_ADDRESS.c_str() : manifest::Core::IP_ADDRESS.IP_STR.c_str();
149 // Send packet.
150 network::g_pRoveCommUDPNode->SendUDPPacket(stPacket, cIPAddress, constants::ROVECOMM_OUTGOING_UDP_PORT);
151 }
152 // Submit logger message.
153 LOG_DEBUG(logging::g_qSharedLogger, "Driving at: ({}, {})", fDriveBoardLeftPower, fDriveBoardRightPower);
154}
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
164{
165 // Update member variables with new target speeds.
166 m_stDrivePowers.dLeftDrivePower = 0.0;
167 m_stDrivePowers.dRightDrivePower = 0.0;
168
169 // Construct a RoveComm packet with the drive data.
170 rovecomm::RoveCommPacket<float> stPacket;
171 stPacket.unDataId = manifest::Core::COMMANDS.find("DRIVELEFTRIGHT")->second.DATA_ID;
172 stPacket.unDataCount = manifest::Core::COMMANDS.find("DRIVELEFTRIGHT")->second.DATA_COUNT;
173 stPacket.eDataType = manifest::Core::COMMANDS.find("DRIVELEFTRIGHT")->second.DATA_TYPE;
174 stPacket.vData.emplace_back(m_stDrivePowers.dLeftDrivePower);
175 stPacket.vData.emplace_back(m_stDrivePowers.dRightDrivePower);
176 // Check if we should send packets to the SIM or board.
177 const char* cIPAddress = constants::MODE_SIM ? constants::SIM_IP_ADDRESS.c_str() : manifest::Core::IP_ADDRESS.IP_STR.c_str();
178 // Send drive command over RoveComm to drive board.
179 if (network::g_pRoveCommUDPNode)
180 {
181 network::g_pRoveCommUDPNode->SendUDPPacket(stPacket, cIPAddress, constants::ROVECOMM_OUTGOING_UDP_PORT);
182 }
183 // Submit logger message.
184 LOG_DEBUG(logging::g_qSharedLogger, "Sent stop powers to drivetrain");
185}
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
197{
198 // Acquire write lock for writing to max effort member variables.
199 std::unique_lock<std::shared_mutex> lkDriveEffortLock(m_muDriveEffortMutex);
200
201 // Clamp the multiplier to the range [0, 1].
202 float fClampedMaxDriveEffortMultiplier = std::clamp(fMaxDriveEffortMultiplier, 0.0f, constants::DRIVE_MAX_POWER);
203
204 // Update member variables.
205 m_fMinDriveEffort = constants::DRIVE_MIN_POWER * fClampedMaxDriveEffortMultiplier;
206 m_fMaxDriveEffort = constants::DRIVE_MAX_POWER * fClampedMaxDriveEffortMultiplier;
207}

◆ 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
218{
219 // Return the current drive powers.
220 return m_stDrivePowers;
221}

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:196

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: