CTRE Phoenix C++ 5.33.1
WPI_BaseMotorController.h
Go to the documentation of this file.
1/* Copyright (C) Cross The Road Electronics 2024 */
2/**
3 * WPI Compliant motor controller class.
4 * WPILIB's object model requires many interfaces to be implemented to use
5 * the various features.
6 * This includes...
7 * - Software PID loops running in the robot controller
8 * - LiveWindow/Test mode features
9 * - Motor Safety (auto-turn off of motor if Set stops getting called)
10 * - Single Parameter set that assumes a simple motor controller.
11 */
12#pragma once
13
15
16//Need to disable certain warnings for WPI headers.
17#if __GNUC__
18#pragma GCC diagnostic push
19#pragma GCC diagnostic ignored "-Wconversion"
20#elif _MSC_VER
21#pragma warning(push)
22#pragma warning(disable : 4522 4458 4522)
23#endif
24
25#include "wpi/sendable/Sendable.h"
26#include "wpi/sendable/SendableHelper.h"
27#include "frc/motorcontrol/MotorController.h"
28#include "frc/MotorSafety.h"
29#include "wpi/raw_ostream.h"
30#include <mutex>
31#include <hal/SimDevice.h>
32
33//Put the warning settings back to normal
34#if __GNUC__
35#pragma GCC diagnostic pop
36#elif _MSC_VER
37#pragma warning(pop)
38#endif
39
40namespace ctre
41{
42namespace phoenix
43{
44namespace motorcontrol
45{
46namespace can
47{
48
49/**
50 * VEX Victor SPX Motor Controller when used on CAN Bus.
51 */
53 public virtual frc::MotorController,
54 public frc::MotorSafety,
55 public wpi::Sendable,
56 public wpi::SendableHelper<WPI_BaseMotorController>
57{
58 public:
59 /**
60 * Constructor for a WPI_BaseMotorController
61 * @param deviceNumber Device ID of BaseMotorController
62 */
63 WPI_BaseMotorController(int deviceNumber, const char *model);
65
69
70 //----------------------- set/get routines for WPILIB interfaces -------------------//
71 /**
72 * Common interface for setting the speed of a simple speed controller.
73 *
74 * @param speed The speed to set. Value should be between -1.0 and 1.0.
75 * Value is also saved for Get().
76 */
77 virtual void Set(double speed);
78
79 /**
80 * Common interface for getting the current set speed of a speed controller.
81 *
82 * @return The current set speed. Value is between -1.0 and 1.0.
83 */
84 virtual double Get() const;
85
86 //----------------------- Intercept CTRE calls for motor safety -------------------//
87 /**
88 * Sets the appropriate output on the talon, depending on the mode.
89 * @param mode The output mode to apply.
90 * In PercentOutput, the output is between -1.0 and 1.0, with 0.0 as stopped.
91 * In Current mode, output value is in amperes.
92 * In Velocity mode, output value is in position change / 100ms.
93 * In Position mode, output value is in encoder ticks or an analog value,
94 * depending on the sensor.
95 * In Follower mode, the output value is the integer device ID of the talon to
96 * duplicate.
97 *
98 * @param value The setpoint value, as described above.
99 *
100 *
101 * Standard Driving Example:
102 * _talonLeft.set(ControlMode.PercentOutput, leftJoy);
103 * _talonRght.set(ControlMode.PercentOutput, rghtJoy);
104 */
105 virtual void Set(ControlMode mode, double value);
106 /**
107 * @param mode Sets the appropriate output on the talon, depending on the mode.
108 * @param demand0 The output value to apply.
109 * such as advanced feed forward and/or auxiliary close-looping in firmware.
110 * In PercentOutput, the output is between -1.0 and 1.0, with 0.0 as stopped.
111 * In Current mode, output value is in amperes.
112 * In Velocity mode, output value is in position change / 100ms.
113 * In Position mode, output value is in encoder ticks or an analog value,
114 * depending on the sensor. See
115 * In Follower mode, the output value is the integer device ID of the talon to
116 * duplicate.
117 *
118 * @param demand1Type The demand type for demand1.
119 * Neutral: Ignore demand1 and apply no change to the demand0 output.
120 * AuxPID: Use demand1 to set the target for the auxiliary PID 1.
121 * ArbitraryFeedForward: Use demand1 as an arbitrary additive value to the
122 * demand0 output. In PercentOutput the demand0 output is the motor output,
123 * and in closed-loop modes the demand0 output is the output of PID0.
124 * @param demand1 Supplmental output value. Units match the set mode.
125 *
126 *
127 * Arcade Drive Example:
128 * _talonLeft.set(ControlMode.PercentOutput, joyForward, DemandType.ArbitraryFeedForward, +joyTurn);
129 * _talonRght.set(ControlMode.PercentOutput, joyForward, DemandType.ArbitraryFeedForward, -joyTurn);
130 *
131 * Drive Straight Example:
132 * Note: Selected Sensor Configuration is necessary for both PID0 and PID1.
133 * _talonLeft.follow(_talonRght, FollwerType.AuxOutput1);
134 * _talonRght.set(ControlMode.PercentOutput, joyForward, DemandType.AuxPID, desiredRobotHeading);
135 *
136 * Drive Straight to a Distance Example:
137 * Note: Other configurations (sensor selection, PID gains, etc.) need to be set.
138 * _talonLeft.follow(_talonRght, FollwerType.AuxOutput1);
139 * _talonRght.set(ControlMode.MotionMagic, targetDistance, DemandType.AuxPID, desiredRobotHeading);
140 */
141 virtual void Set(ControlMode mode, double demand0, DemandType demand1Type, double demand1);
142
143
144 /**
145 * Sets the voltage output of the SpeedController. Compensates for
146 * the current bus voltage to ensure that the desired voltage is output even
147 * if the battery voltage is below 12V - highly useful when the voltage
148 * outputs are "meaningful" (e.g. they come from a feedforward calculation).
149 *
150 * <p>NOTE: This function *must* be called regularly in order for voltage
151 * compensation to work properly - unlike the ordinary set function, it is not
152 * "set it and forget it."
153 *
154 * @param output The voltage to output.
155 */
156 virtual void SetVoltage(units::volt_t output);
157
158 //----------------------- Invert routines -------------------//
159 /**
160 * Common interface for inverting direction of a speed controller.
161 *
162 * @param isInverted The state of inversion, true is inverted.
163 */
164 virtual void SetInverted(bool isInverted);
165 /**
166 * Common interface for inverting direction of a speed controller.
167 *
168 * @param invertType The invert strategy to use. Follower controllers
169 * that mirror/oppose the master controller should
170 * use this method.
171 */
172 virtual void SetInverted(InvertType invertType);
173 /**
174 * Common interface for returning the inversion state of a speed controller.
175 *
176 * @return isInverted The state of inversion, true is inverted.
177 */
178 virtual bool GetInverted() const;
179 //----------------------- turn-motor-off routines-------------------//
180 /**
181 * Common interface for disabling a motor.
182 */
183 virtual void Disable();
184 /**
185 * Common interface to stop the motor until Set is called again.
186 */
187 virtual void StopMotor() override;
188
189 /**
190 * @return description of controller
191 */
192 std::string GetDescription() const override;
193
194 protected:
195 /**
196 * Initialize sendable
197 * @param builder Base sendable to build on
198 */
199 virtual void InitSendable(wpi::SendableBuilder &builder);
200
201 private:
202 double _speed = 0;
203 std::string _desc;
204};
205
206} // namespace can
207} // namespace motorcontrol
208} // namespace phoenix
209} // namespace ctre
Base motor controller features for all CTRE CAN motor controllers.
Definition: BaseMotorController.h:578
VEX Victor SPX Motor Controller when used on CAN Bus.
Definition: WPI_BaseMotorController.h:57
virtual void Disable()
Common interface for disabling a motor.
virtual void Set(ControlMode mode, double demand0, DemandType demand1Type, double demand1)
virtual bool GetInverted() const
Common interface for returning the inversion state of a speed controller.
WPI_BaseMotorController & operator=(WPI_BaseMotorController const &)=delete
virtual void Set(ControlMode mode, double value)
Sets the appropriate output on the talon, depending on the mode.
virtual double Get() const
Common interface for getting the current set speed of a speed controller.
WPI_BaseMotorController(WPI_BaseMotorController const &)=delete
virtual void SetInverted(InvertType invertType)
Common interface for inverting direction of a speed controller.
virtual void SetVoltage(units::volt_t output)
Sets the voltage output of the SpeedController.
WPI_BaseMotorController(int deviceNumber, const char *model)
Constructor for a WPI_BaseMotorController.
virtual void SetInverted(bool isInverted)
Common interface for inverting direction of a speed controller.
virtual void StopMotor() override
Common interface to stop the motor until Set is called again.
virtual void Set(double speed)
Common interface for setting the speed of a simple speed controller.
virtual void InitSendable(wpi::SendableBuilder &builder)
Initialize sendable.
ControlMode
Choose the control mode for a motor controller.
Definition: ControlMode.h:11
DemandType
How to interpret a demand value.
Definition: DemandType.h:10
InvertType
Choose the invert type of the motor controller.
Definition: InvertType.h:14
namespace ctre
Definition: paramEnum.h:5