WPILibC++  2018.4.1-20180921151738-1194-gf89274f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 #include "frc/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 
56  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
57  RobotDrive(int leftMotorChannel, int rightMotorChannel);
58 
75  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
76  RobotDrive(int frontLeftMotorChannel, int rearLeftMotorChannel,
77  int frontRightMotorChannel, int rearRightMotorChannel);
78 
91  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
92  RobotDrive(SpeedController* leftMotor, SpeedController* rightMotor);
93 
94  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
95  RobotDrive(SpeedController& leftMotor, SpeedController& rightMotor);
96 
97  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
98  RobotDrive(std::shared_ptr<SpeedController> leftMotor,
99  std::shared_ptr<SpeedController> rightMotor);
100 
116  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
117  RobotDrive(SpeedController* frontLeftMotor, SpeedController* rearLeftMotor,
118  SpeedController* frontRightMotor, SpeedController* rearRightMotor);
119 
120  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
121  RobotDrive(SpeedController& frontLeftMotor, SpeedController& rearLeftMotor,
122  SpeedController& frontRightMotor, SpeedController& rearRightMotor);
123 
124  WPI_DEPRECATED("Use DifferentialDrive or MecanumDrive classes instead.")
125  RobotDrive(std::shared_ptr<SpeedController> frontLeftMotor,
126  std::shared_ptr<SpeedController> rearLeftMotor,
127  std::shared_ptr<SpeedController> frontRightMotor,
128  std::shared_ptr<SpeedController> rearRightMotor);
129 
130  virtual ~RobotDrive() = default;
131 
132  RobotDrive(const RobotDrive&) = delete;
133  RobotDrive& operator=(const RobotDrive&) = delete;
134 
158  void Drive(double outputMagnitude, double curve);
159 
171  void TankDrive(GenericHID* leftStick, GenericHID* rightStick,
172  bool squaredInputs = true);
173 
185  void TankDrive(GenericHID& leftStick, GenericHID& rightStick,
186  bool squaredInputs = true);
187 
203  void TankDrive(GenericHID* leftStick, int leftAxis, GenericHID* rightStick,
204  int rightAxis, bool squaredInputs = true);
205 
206  void TankDrive(GenericHID& leftStick, int leftAxis, GenericHID& rightStick,
207  int rightAxis, bool squaredInputs = true);
208 
219  void TankDrive(double leftValue, double rightValue,
220  bool squaredInputs = true);
221 
235  void ArcadeDrive(GenericHID* stick, bool squaredInputs = true);
236 
250  void ArcadeDrive(GenericHID& stick, bool squaredInputs = true);
251 
268  void ArcadeDrive(GenericHID* moveStick, int moveChannel,
269  GenericHID* rotateStick, int rotateChannel,
270  bool squaredInputs = true);
271 
288  void ArcadeDrive(GenericHID& moveStick, int moveChannel,
289  GenericHID& rotateStick, int rotateChannel,
290  bool squaredInputs = true);
291 
301  void ArcadeDrive(double moveValue, double rotateValue,
302  bool squaredInputs = true);
303 
325  void MecanumDrive_Cartesian(double x, double y, double rotation,
326  double gyroAngle = 0.0);
327 
345  void MecanumDrive_Polar(double magnitude, double direction, double rotation);
346 
359  void HolonomicDrive(double magnitude, double direction, double rotation);
360 
371  virtual void SetLeftRightMotorOutputs(double leftOutput, double rightOutput);
372 
373  /*
374  * Invert a motor direction.
375  *
376  * This is used when a motor should run in the opposite direction as the drive
377  * code would normally run it. Motors that are direct drive would be inverted,
378  * the Drive code assumes that the motors are geared with one reversal.
379  *
380  * @param motor The motor index to invert.
381  * @param isInverted True if the motor should be inverted when operated.
382  */
383  void SetInvertedMotor(MotorType motor, bool isInverted);
384 
393  void SetSensitivity(double sensitivity);
394 
402  void SetMaxOutput(double maxOutput);
403 
404  void SetExpiration(double timeout) override;
405  double GetExpiration() const override;
406  bool IsAlive() const override;
407  void StopMotor() override;
408  bool IsSafetyEnabled() const override;
409  void SetSafetyEnabled(bool enabled) override;
410  void GetDescription(wpi::raw_ostream& desc) const override;
411 
412  protected:
420  void InitRobotDrive();
421 
425  double Limit(double number);
426 
431  void Normalize(double* wheelSpeeds);
432 
436  void RotateVector(double& x, double& y, double angle);
437 
438  static constexpr int kMaxNumberOfMotors = 4;
439 
440  double m_sensitivity = 0.5;
441  double m_maxOutput = 1.0;
442 
443  std::shared_ptr<SpeedController> m_frontLeftMotor;
444  std::shared_ptr<SpeedController> m_frontRightMotor;
445  std::shared_ptr<SpeedController> m_rearLeftMotor;
446  std::shared_ptr<SpeedController> m_rearRightMotor;
447  std::unique_ptr<MotorSafetyHelper> m_safetyHelper;
448 
449  private:
450  int GetNumMotors() {
451  int motors = 0;
452  if (m_frontLeftMotor) motors++;
453  if (m_frontRightMotor) motors++;
454  if (m_rearLeftMotor) motors++;
455  if (m_rearRightMotor) motors++;
456  return motors;
457  }
458 };
459 
460 } // namespace frc
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Interface for speed controlling devices.
Definition: SpeedController.h:17
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
Utility class for handling Robot drive based on a definition of the motor configuration.
Definition: RobotDrive.h:35
double Limit(double number)
Limit motor values to the -1.0 to +1.0 range.
void ArcadeDrive(GenericHID *stick, bool squaredInputs=true)
Arcade drive implements single stick driving.
void InitRobotDrive()
Common function to initialize all the robot drive constructors.
void RotateVector(double &x, double &y, double angle)
Rotate a vector in Cartesian space.
void MecanumDrive_Polar(double magnitude, double direction, double rotation)
Drive method for Mecanum wheeled robots.
Definition: MotorSafety.h:16
void SetSensitivity(double sensitivity)
Set the turning sensitivity.
virtual void SetLeftRightMotorOutputs(double leftOutput, double rightOutput)
Set the speed of the right and left motors.
void HolonomicDrive(double magnitude, double direction, double rotation)
Holonomic Drive method for Mecanum wheeled robots.
void TankDrive(GenericHID *leftStick, GenericHID *rightStick, bool squaredInputs=true)
Provide tank steering using the stored robot configuration.
void Drive(double outputMagnitude, double curve)
Drive the motors at "outputMagnitude" and "curve".
Base class for most objects.
Definition: ErrorBase.h:74
GenericHID Interface.
Definition: GenericHID.h:23
void SetMaxOutput(double maxOutput)
Configure the scaling factor for using RobotDrive with motor controllers in a mode other than Percent...
RobotDrive(int leftMotorChannel, int rightMotorChannel)
Constructor for RobotDrive with 2 motors specified with channel numbers.
void Normalize(double *wheelSpeeds)
Normalize all wheel speeds if the magnitude of any wheel is greater than 1.0.
void MecanumDrive_Cartesian(double x, double y, double rotation, double gyroAngle=0.0)
Drive method for Mecanum wheeled robots.