WPILibC++  2020.2.2-33-g65eab93
RobotDrive.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 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 
34 class RobotDrive : public MotorSafety {
35  public:
36  enum MotorType {
37  kFrontLeftMotor = 0,
38  kFrontRightMotor = 1,
39  kRearLeftMotor = 2,
40  kRearRightMotor = 3
41  };
42 
55  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
56  RobotDrive(int leftMotorChannel, int rightMotorChannel);
57 
74  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
75  RobotDrive(int frontLeftMotorChannel, int rearLeftMotorChannel,
76  int frontRightMotorChannel, int rearRightMotorChannel);
77 
90  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
91  RobotDrive(SpeedController* leftMotor, SpeedController* rightMotor);
92 
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(std::shared_ptr<SpeedController> leftMotor,
98  std::shared_ptr<SpeedController> rightMotor);
99 
115  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
116  RobotDrive(SpeedController* frontLeftMotor, SpeedController* rearLeftMotor,
117  SpeedController* frontRightMotor, SpeedController* rearRightMotor);
118 
119  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
120  RobotDrive(SpeedController& frontLeftMotor, SpeedController& rearLeftMotor,
121  SpeedController& frontRightMotor, SpeedController& rearRightMotor);
122 
123  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
124  RobotDrive(std::shared_ptr<SpeedController> frontLeftMotor,
125  std::shared_ptr<SpeedController> rearLeftMotor,
126  std::shared_ptr<SpeedController> frontRightMotor,
127  std::shared_ptr<SpeedController> rearRightMotor);
128 
129  virtual ~RobotDrive() = default;
130 
131  RobotDrive(RobotDrive&&) = default;
132  RobotDrive& operator=(RobotDrive&&) = default;
133 
157  void Drive(double outputMagnitude, double curve);
158 
170  void TankDrive(GenericHID* leftStick, GenericHID* rightStick,
171  bool squaredInputs = true);
172 
184  void TankDrive(GenericHID& leftStick, GenericHID& rightStick,
185  bool squaredInputs = true);
186 
202  void TankDrive(GenericHID* leftStick, int leftAxis, GenericHID* rightStick,
203  int rightAxis, bool squaredInputs = true);
204 
205  void TankDrive(GenericHID& leftStick, int leftAxis, GenericHID& rightStick,
206  int rightAxis, bool squaredInputs = true);
207 
218  void TankDrive(double leftValue, double rightValue,
219  bool squaredInputs = true);
220 
234  void ArcadeDrive(GenericHID* stick, bool squaredInputs = true);
235 
249  void ArcadeDrive(GenericHID& stick, bool squaredInputs = true);
250 
267  void ArcadeDrive(GenericHID* moveStick, int moveChannel,
268  GenericHID* rotateStick, int rotateChannel,
269  bool squaredInputs = true);
270 
287  void ArcadeDrive(GenericHID& moveStick, int moveChannel,
288  GenericHID& rotateStick, int rotateChannel,
289  bool squaredInputs = true);
290 
300  void ArcadeDrive(double moveValue, double rotateValue,
301  bool squaredInputs = true);
302 
324  void MecanumDrive_Cartesian(double x, double y, double rotation,
325  double gyroAngle = 0.0);
326 
344  void MecanumDrive_Polar(double magnitude, double direction, double rotation);
345 
358  void HolonomicDrive(double magnitude, double direction, double rotation);
359 
370  virtual void SetLeftRightMotorOutputs(double leftOutput, double rightOutput);
371 
372  /*
373  * Invert a motor direction.
374  *
375  * This is used when a motor should run in the opposite direction as the drive
376  * code would normally run it. Motors that are direct drive would be inverted,
377  * the Drive code assumes that the motors are geared with one reversal.
378  *
379  * @param motor The motor index to invert.
380  * @param isInverted True if the motor should be inverted when operated.
381  */
382  void SetInvertedMotor(MotorType motor, bool isInverted);
383 
392  void SetSensitivity(double sensitivity);
393 
401  void SetMaxOutput(double maxOutput);
402 
403  void StopMotor() override;
404  void GetDescription(wpi::raw_ostream& desc) const override;
405 
406  protected:
414  void InitRobotDrive();
415 
419  double Limit(double number);
420 
425  void Normalize(double* wheelSpeeds);
426 
430  void RotateVector(double& x, double& y, double angle);
431 
432  static constexpr int kMaxNumberOfMotors = 4;
433 
434  double m_sensitivity = 0.5;
435  double m_maxOutput = 1.0;
436 
437  std::shared_ptr<SpeedController> m_frontLeftMotor;
438  std::shared_ptr<SpeedController> m_frontRightMotor;
439  std::shared_ptr<SpeedController> m_rearLeftMotor;
440  std::shared_ptr<SpeedController> m_rearRightMotor;
441 
442  private:
443  int GetNumMotors() {
444  int motors = 0;
445  if (m_frontLeftMotor) motors++;
446  if (m_frontRightMotor) motors++;
447  if (m_rearLeftMotor) motors++;
448  if (m_rearRightMotor) motors++;
449  return motors;
450  }
451 };
452 
453 } // 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:34
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
WPILib FRC namespace.
Definition: AnalogOutSim.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