12#ifndef DIFFERENTIAL_DRIVE_HPP
13#define DIFFERENTIAL_DRIVE_HPP
15#include "../AutonomyLogging.h"
16#include "../util/NumberOperations.hpp"
57 enum class DifferentialControlMethod
76 double dLeftDrivePower;
77 double dRightDrivePower;
94 dLeftSpeed = std::clamp(dLeftSpeed, -1.0, 1.0);
95 dRightSpeed = std::clamp(dRightSpeed, -1.0, 1.0);
101 dLeftSpeed = std::copysign(dLeftSpeed * dLeftSpeed, dLeftSpeed);
102 dRightSpeed = std::copysign(dRightSpeed * dRightSpeed, dRightSpeed);
106 return {dLeftSpeed, dRightSpeed};
123 dSpeed = std::clamp(dSpeed, -1.0, 1.0);
124 dRotation = std::clamp(dRotation, -1.0, 1.0);
130 dSpeed = std::copysign(dSpeed * dSpeed, dSpeed);
131 dRotation = std::copysign(dRotation * dRotation, dRotation);
135 double dGreaterInput = std::max(std::abs(dSpeed), std::abs(dRotation));
136 double dLesserInput = std::min(std::abs(dSpeed), std::abs(dRotation));
138 if (dGreaterInput == 0.0)
145 double dLeftSpeed = dSpeed + dRotation;
146 double dRightSpeed = dSpeed - dRotation;
148 double dSaturatedInput = (dGreaterInput + dLesserInput) / dGreaterInput;
149 dLeftSpeed /= dSaturatedInput;
150 dRightSpeed /= dSaturatedInput;
153 return {dLeftSpeed, dRightSpeed};
173 double dLeftSpeed = 0.0;
174 double dRightSpeed = 0.0;
177 dSpeed = std::clamp(dSpeed, -1.0, 1.0);
178 dRotation = std::clamp(dRotation, -1.0, 1.0);
184 dSpeed = std::copysign(dSpeed * dSpeed, dSpeed);
185 dRotation = std::copysign(dRotation * dRotation, dRotation);
189 if (bAllowTurnInPlace && dSpeed <= 0.01)
192 dLeftSpeed = dSpeed + dRotation;
193 dRightSpeed = dSpeed - dRotation;
198 dLeftSpeed = dSpeed + std::abs(dSpeed) * dRotation;
199 dRightSpeed = dSpeed - std::abs(dSpeed) * dRotation;
203 double dMaxMagnitude = std::max(std::abs(dLeftSpeed), std::abs(dRightSpeed));
204 if (dMaxMagnitude > 1.0)
206 dLeftSpeed /= dMaxMagnitude;
207 dRightSpeed /= dMaxMagnitude;
211 return {dLeftSpeed, dRightSpeed};
237 double dActualHeading,
238 const DifferentialControlMethod eDriveMethod,
240 const bool bSquareControlInput =
false,
241 const bool bCurvatureDriveAllowTurningWhileStopped =
true)
247 double dTurnOutput = PID.
Calculate(dActualHeading, dGoalHeading);
250 switch (eDriveMethod)
252 case DifferentialControlMethod::eArcadeDrive:
255 dGoalSpeed *= 1.0 - std::pow(dTurnOutput, 2);
260 case DifferentialControlMethod::eCurvatureDrive:
263 dGoalSpeed *= 1.0 - std::pow(dTurnOutput, 2);
265 stOutputPowers =
CalculateCurvatureDrive(dGoalSpeed, dTurnOutput, bCurvatureDriveAllowTurningWhileStopped, bSquareControlInput);
271 LOG_ERROR(logging::g_qSharedLogger,
"eTankDrive is not supported for the CalculateMotorPowerFromHeading() method!");
277 return stOutputPowers;
Defines the PIDController class.
This class encapsulates the fundamental principles of Proportional-Integral-Derivative (PID) control ...
Definition PIDController.h:50
double Calculate(const double dActual, const double dSetpoint)
Calculate the next control output given the current actual and a setpoint.
Definition PIDController.cpp:74
Namespace containing algorithms related to calculating drive powers, odometry, trajectories,...
Definition DifferentialDrive.hpp:50
DrivePowers CalculateArcadeDrive(double dSpeed, double dRotation, const bool bSquareInputs=false)
Arcade drive inverse kinematics for differential drive robots.
Definition DifferentialDrive.hpp:120
DrivePowers CalculateCurvatureDrive(double dSpeed, double dRotation, const bool bAllowTurnInPlace, const bool bSquareInputs=false)
Curvature drive inverse kinematics for differential drive robots. The rotation parameter controls the...
Definition DifferentialDrive.hpp:170
DrivePowers CalculateTankDrive(double dLeftSpeed, double dRightSpeed, bool bSquareInputs=false)
Tank drive inverse kinematics for differential drive robots.
Definition DifferentialDrive.hpp:91
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
This struct is used to store the left and right drive powers for the robot. Storing these values in a...
Definition DifferentialDrive.hpp:73