WPILibC++  unspecified
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 <llvm/raw_ostream.h>
13 #include <support/deprecated.h>
14 
15 #include "ErrorBase.h"
16 #include "MotorSafety.h"
17 #include "MotorSafetyHelper.h"
18 
19 namespace frc {
20 
21 class SpeedController;
22 class GenericHID;
23 
35 class RobotDrive : public MotorSafety, public ErrorBase {
36  public:
37  enum MotorType {
38  kFrontLeftMotor = 0,
39  kFrontRightMotor = 1,
40  kRearLeftMotor = 2,
41  kRearRightMotor = 3
42  };
43 
44  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
45  RobotDrive(int leftMotorChannel, int rightMotorChannel);
46  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
47  RobotDrive(int frontLeftMotorChannel, int rearLeftMotorChannel,
48  int frontRightMotorChannel, int rearRightMotorChannel);
49  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
50  RobotDrive(SpeedController* leftMotor, SpeedController* rightMotor);
51  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
52  RobotDrive(SpeedController& leftMotor, SpeedController& rightMotor);
53  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
54  RobotDrive(std::shared_ptr<SpeedController> leftMotor,
55  std::shared_ptr<SpeedController> rightMotor);
56  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
57  RobotDrive(SpeedController* frontLeftMotor, SpeedController* rearLeftMotor,
58  SpeedController* frontRightMotor, SpeedController* rearRightMotor);
59  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
60  RobotDrive(SpeedController& frontLeftMotor, SpeedController& rearLeftMotor,
61  SpeedController& frontRightMotor, SpeedController& rearRightMotor);
62  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
63  RobotDrive(std::shared_ptr<SpeedController> frontLeftMotor,
64  std::shared_ptr<SpeedController> rearLeftMotor,
65  std::shared_ptr<SpeedController> frontRightMotor,
66  std::shared_ptr<SpeedController> rearRightMotor);
67  virtual ~RobotDrive() = default;
68 
69  RobotDrive(const RobotDrive&) = delete;
70  RobotDrive& operator=(const RobotDrive&) = delete;
71 
72  void Drive(double outputMagnitude, double curve);
73  void TankDrive(GenericHID* leftStick, GenericHID* rightStick,
74  bool squaredInputs = true);
75  void TankDrive(GenericHID& leftStick, GenericHID& rightStick,
76  bool squaredInputs = true);
77  void TankDrive(GenericHID* leftStick, int leftAxis, GenericHID* rightStick,
78  int rightAxis, bool squaredInputs = true);
79  void TankDrive(GenericHID& leftStick, int leftAxis, GenericHID& rightStick,
80  int rightAxis, bool squaredInputs = true);
81  void TankDrive(double leftValue, double rightValue,
82  bool squaredInputs = true);
83  void ArcadeDrive(GenericHID* stick, bool squaredInputs = true);
84  void ArcadeDrive(GenericHID& stick, bool squaredInputs = true);
85  void ArcadeDrive(GenericHID* moveStick, int moveChannel,
86  GenericHID* rotateStick, int rotateChannel,
87  bool squaredInputs = true);
88  void ArcadeDrive(GenericHID& moveStick, int moveChannel,
89  GenericHID& rotateStick, int rotateChannel,
90  bool squaredInputs = true);
91  void ArcadeDrive(double moveValue, double rotateValue,
92  bool squaredInputs = true);
93  void MecanumDrive_Cartesian(double x, double y, double rotation,
94  double gyroAngle = 0.0);
95  void MecanumDrive_Polar(double magnitude, double direction, double rotation);
96  void HolonomicDrive(double magnitude, double direction, double rotation);
97  virtual void SetLeftRightMotorOutputs(double leftOutput, double rightOutput);
98  void SetInvertedMotor(MotorType motor, bool isInverted);
99  void SetSensitivity(double sensitivity);
100  void SetMaxOutput(double maxOutput);
101 
102  void SetExpiration(double timeout) override;
103  double GetExpiration() const override;
104  bool IsAlive() const override;
105  void StopMotor() override;
106  bool IsSafetyEnabled() const override;
107  void SetSafetyEnabled(bool enabled) override;
108  void GetDescription(llvm::raw_ostream& desc) const override;
109 
110  protected:
111  void InitRobotDrive();
112  double Limit(double number);
113  void Normalize(double* wheelSpeeds);
114  void RotateVector(double& x, double& y, double angle);
115 
116  static constexpr int kMaxNumberOfMotors = 4;
117 
118  double m_sensitivity = 0.5;
119  double m_maxOutput = 1.0;
120 
121  std::shared_ptr<SpeedController> m_frontLeftMotor;
122  std::shared_ptr<SpeedController> m_frontRightMotor;
123  std::shared_ptr<SpeedController> m_rearLeftMotor;
124  std::shared_ptr<SpeedController> m_rearRightMotor;
125  std::unique_ptr<MotorSafetyHelper> m_safetyHelper;
126 
127  private:
128  int GetNumMotors() {
129  int motors = 0;
130  if (m_frontLeftMotor) motors++;
131  if (m_frontRightMotor) motors++;
132  if (m_rearLeftMotor) motors++;
133  if (m_rearRightMotor) motors++;
134  return motors;
135  }
136 };
137 
138 } // namespace frc
Interface for speed controlling devices.
Definition: SpeedController.h:17
void SetMaxOutput(double maxOutput)
Configure the scaling factor for using RobotDrive with motor controllers in a mode other than Percent...
Definition: RobotDrive.cpp:718
Definition: RobotController.cpp:14
Utility class for handling Robot drive based on a definition of the motor configuration.
Definition: RobotDrive.h:35
virtual void SetLeftRightMotorOutputs(double leftOutput, double rightOutput)
Set the speed of the right and left motors.
Definition: RobotDrive.cpp:612
void MecanumDrive_Polar(double magnitude, double direction, double rotation)
Drive method for Mecanum wheeled robots.
Definition: RobotDrive.cpp:553
void Drive(double outputMagnitude, double curve)
Drive the motors at "outputMagnitude" and "curve".
Definition: RobotDrive.cpp:217
Definition: MotorSafety.h:16
void TankDrive(GenericHID *leftStick, GenericHID *rightStick, bool squaredInputs=true)
Provide tank steering using the stored robot configuration.
Definition: RobotDrive.cpp:256
RobotDrive(int leftMotorChannel, int rightMotorChannel)
Constructor for RobotDrive with 2 motors specified with channel numbers.
Definition: RobotDrive.cpp:60
void HolonomicDrive(double magnitude, double direction, double rotation)
Holonomic Drive method for Mecanum wheeled robots.
Definition: RobotDrive.cpp:597
double Limit(double number)
Limit motor values to the -1.0 to +1.0 range.
Definition: RobotDrive.cpp:630
Base class for most objects.
Definition: ErrorBase.h:74
void RotateVector(double &x, double &y, double angle)
Rotate a vector in Cartesian space.
Definition: RobotDrive.cpp:659
void MecanumDrive_Cartesian(double x, double y, double rotation, double gyroAngle=0.0)
Drive method for Mecanum wheeled robots.
Definition: RobotDrive.cpp:504
void Normalize(double *wheelSpeeds)
Normalize all wheel speeds if the magnitude of any wheel is greater than 1.0.
Definition: RobotDrive.cpp:643
void ArcadeDrive(GenericHID *stick, bool squaredInputs=true)
Arcade drive implements single stick driving.
Definition: RobotDrive.cpp:358
GenericHID Interface.
Definition: GenericHID.h:23
void InitRobotDrive()
Common function to initialize all the robot drive constructors.
Definition: RobotDrive.cpp:43
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:33
void SetSensitivity(double sensitivity)
Set the turning sensitivity.
Definition: RobotDrive.cpp:707