WPILibC++  2020.3.2-60-g3011ebe
RobotDrive.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <memory>
11 
12 #include <wpi/deprecated.h>
13 #include <wpi/raw_ostream.h>
14 
15 #include "frc/ErrorBase.h"
16 #include "frc/MotorSafety.h"
17 
18 namespace frc {
19 
20 class SpeedController;
21 class GenericHID;
22 
37 class RobotDrive : public MotorSafety {
38  public:
39  enum MotorType {
40  kFrontLeftMotor = 0,
41  kFrontRightMotor = 1,
42  kRearLeftMotor = 2,
43  kRearRightMotor = 3
44  };
45 
58  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
59  RobotDrive(int leftMotorChannel, int rightMotorChannel);
60 
77  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
78  RobotDrive(int frontLeftMotorChannel, int rearLeftMotorChannel,
79  int frontRightMotorChannel, int rearRightMotorChannel);
80 
93  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
94  RobotDrive(SpeedController* leftMotor, SpeedController* rightMotor);
95 
96  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
97  RobotDrive(SpeedController& leftMotor, SpeedController& rightMotor);
98 
99  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
100  RobotDrive(std::shared_ptr<SpeedController> leftMotor,
101  std::shared_ptr<SpeedController> rightMotor);
102 
118  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
119  RobotDrive(SpeedController* frontLeftMotor, SpeedController* rearLeftMotor,
120  SpeedController* frontRightMotor, SpeedController* rearRightMotor);
121 
122  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
123  RobotDrive(SpeedController& frontLeftMotor, SpeedController& rearLeftMotor,
124  SpeedController& frontRightMotor, SpeedController& rearRightMotor);
125 
126  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
127  RobotDrive(std::shared_ptr<SpeedController> frontLeftMotor,
128  std::shared_ptr<SpeedController> rearLeftMotor,
129  std::shared_ptr<SpeedController> frontRightMotor,
130  std::shared_ptr<SpeedController> rearRightMotor);
131 
132  virtual ~RobotDrive() = default;
133 
134  RobotDrive(RobotDrive&&) = default;
135  RobotDrive& operator=(RobotDrive&&) = default;
136 
160  void Drive(double outputMagnitude, double curve);
161 
173  void TankDrive(GenericHID* leftStick, GenericHID* rightStick,
174  bool squaredInputs = true);
175 
187  void TankDrive(GenericHID& leftStick, GenericHID& rightStick,
188  bool squaredInputs = true);
189 
205  void TankDrive(GenericHID* leftStick, int leftAxis, GenericHID* rightStick,
206  int rightAxis, bool squaredInputs = true);
207 
208  void TankDrive(GenericHID& leftStick, int leftAxis, GenericHID& rightStick,
209  int rightAxis, bool squaredInputs = true);
210 
221  void TankDrive(double leftValue, double rightValue,
222  bool squaredInputs = true);
223 
237  void ArcadeDrive(GenericHID* stick, bool squaredInputs = true);
238 
252  void ArcadeDrive(GenericHID& stick, bool squaredInputs = true);
253 
270  void ArcadeDrive(GenericHID* moveStick, int moveChannel,
271  GenericHID* rotateStick, int rotateChannel,
272  bool squaredInputs = true);
273 
290  void ArcadeDrive(GenericHID& moveStick, int moveChannel,
291  GenericHID& rotateStick, int rotateChannel,
292  bool squaredInputs = true);
293 
303  void ArcadeDrive(double moveValue, double rotateValue,
304  bool squaredInputs = true);
305 
327  void MecanumDrive_Cartesian(double x, double y, double rotation,
328  double gyroAngle = 0.0);
329 
347  void MecanumDrive_Polar(double magnitude, double direction, double rotation);
348 
361  void HolonomicDrive(double magnitude, double direction, double rotation);
362 
373  virtual void SetLeftRightMotorOutputs(double leftOutput, double rightOutput);
374 
375  /*
376  * Invert a motor direction.
377  *
378  * This is used when a motor should run in the opposite direction as the drive
379  * code would normally run it. Motors that are direct drive would be inverted,
380  * the Drive code assumes that the motors are geared with one reversal.
381  *
382  * @param motor The motor index to invert.
383  * @param isInverted True if the motor should be inverted when operated.
384  */
385  void SetInvertedMotor(MotorType motor, bool isInverted);
386 
395  void SetSensitivity(double sensitivity);
396 
404  void SetMaxOutput(double maxOutput);
405 
406  void StopMotor() override;
407  void GetDescription(wpi::raw_ostream& desc) const override;
408 
409  protected:
417  void InitRobotDrive();
418 
422  double Limit(double number);
423 
428  void Normalize(double* wheelSpeeds);
429 
433  void RotateVector(double& x, double& y, double angle);
434 
435  static constexpr int kMaxNumberOfMotors = 4;
436 
437  double m_sensitivity = 0.5;
438  double m_maxOutput = 1.0;
439 
440  std::shared_ptr<SpeedController> m_frontLeftMotor;
441  std::shared_ptr<SpeedController> m_frontRightMotor;
442  std::shared_ptr<SpeedController> m_rearLeftMotor;
443  std::shared_ptr<SpeedController> m_rearRightMotor;
444 
445  private:
446  int GetNumMotors() {
447  int motors = 0;
448  if (m_frontLeftMotor) motors++;
449  if (m_frontRightMotor) motors++;
450  if (m_rearLeftMotor) motors++;
451  if (m_rearRightMotor) motors++;
452  return motors;
453  }
454 };
455 
456 } // namespace frc
frc::RobotDrive::Normalize
void Normalize(double *wheelSpeeds)
Normalize all wheel speeds if the magnitude of any wheel is greater than 1.0.
frc::RobotDrive::Limit
double Limit(double number)
Limit motor values to the -1.0 to +1.0 range.
frc::MotorSafety
This base class runs a watchdog timer and calls the subclass's StopMotor() function if the timeout ex...
Definition: MotorSafety.h:24
frc::RobotDrive::RotateVector
void RotateVector(double &x, double &y, double angle)
Rotate a vector in Cartesian space.
frc::RobotDrive::MecanumDrive_Polar
void MecanumDrive_Polar(double magnitude, double direction, double rotation)
Drive method for Mecanum wheeled robots.
frc::RobotDrive::SetLeftRightMotorOutputs
virtual void SetLeftRightMotorOutputs(double leftOutput, double rightOutput)
Set the speed of the right and left motors.
frc::RobotDrive::ArcadeDrive
void ArcadeDrive(GenericHID *stick, bool squaredInputs=true)
Arcade drive implements single stick driving.
frc::RobotDrive
Utility class for handling Robot drive based on a definition of the motor configuration.
Definition: RobotDrive.h:37
frc::RobotDrive::SetSensitivity
void SetSensitivity(double sensitivity)
Set the turning sensitivity.
frc::RobotDrive::InitRobotDrive
void InitRobotDrive()
Common function to initialize all the robot drive constructors.
frc::RobotDrive::Drive
void Drive(double outputMagnitude, double curve)
Drive the motors at "outputMagnitude" and "curve".
frc::RobotDrive::HolonomicDrive
void HolonomicDrive(double magnitude, double direction, double rotation)
Holonomic Drive method for Mecanum wheeled robots.
frc::RobotDrive::TankDrive
void TankDrive(GenericHID *leftStick, GenericHID *rightStick, bool squaredInputs=true)
Provide tank steering using the stored robot configuration.
frc::RobotDrive::RobotDrive
RobotDrive(int leftMotorChannel, int rightMotorChannel)
Constructor for RobotDrive with 2 motors specified with channel numbers.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: PDPSim.h:16
frc::RobotDrive::MecanumDrive_Cartesian
void MecanumDrive_Cartesian(double x, double y, double rotation, double gyroAngle=0.0)
Drive method for Mecanum wheeled robots.
frc::SpeedController
Interface for speed controlling devices.
Definition: SpeedController.h:19
frc::RobotDrive::SetMaxOutput
void SetMaxOutput(double maxOutput)
Configure the scaling factor for using RobotDrive with motor controllers in a mode other than Percent...
wpi::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:47
frc::GenericHID
GenericHID Interface.
Definition: GenericHID.h:23