CTRE Phoenix 6 C++ 24.50.0-alpha-2
TalonFX.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
11
12#include "frc/MotorSafety.h"
13#include "frc/motorcontrol/MotorController.h"
14#include "wpi/sendable/Sendable.h"
15#include "wpi/sendable/SendableBuilder.h"
16#include "wpi/sendable/SendableHelper.h"
17#include <hal/SimDevice.h>
18
19#include <string>
20
21namespace ctre {
22namespace phoenix6 {
23namespace hardware {
24
25/**
26 * Class description for the Talon FX integrated motor controller.
27 */
29 public frc::MotorController,
30 public wpi::Sendable,
31 public wpi::SendableHelper<TalonFX>
32{
33public:
34 /**
35 * \brief The default motor safety timeout IF calling
36 * application enables the feature.
37 */
38 static constexpr auto kDefaultSafetyExpiration = 100_ms;
39
40private:
41 std::string m_description;
42
43 /* Motor Safety */
44 mutable std::unique_ptr<wpiutils::MotorSafetyImplem> m_motorSafety{};
45 units::second_t m_motSafeExpiration{kDefaultSafetyExpiration};
46 mutable std::recursive_mutex m_motorSafetyLock;
47
48 /**
49 * The StatusSignal getters are copies so that calls
50 * to the WPI interface do not update any references
51 *
52 * These are also mutable so the const getter methods are
53 * properly managed.
54 */
56
57 controls::DutyCycleOut m_setterControl{0.0};
58 controls::NeutralOut m_brakeRef{};
59 controls::VoltageOut m_voltageControl{0_V};
60
61 hal::SimDevice m_simMotor;
62 hal::SimDouble m_simSupplyVoltage;
63 hal::SimDouble m_simDutyCycle;
64 hal::SimDouble m_simMotorVoltage;
65 hal::SimDouble m_simTorqueCurrent;
66 hal::SimDouble m_simSupplyCurrent;
67
68 hal::SimDevice m_simForwardLimit;
69 hal::SimBoolean m_simForwardLimitValue;
70
71 hal::SimDevice m_simReverseLimit;
72 hal::SimBoolean m_simReverseLimitValue;
73
74 hal::SimDevice m_simRotor;
75 hal::SimDouble m_simRotorPos;
76 hal::SimDouble m_simRotorRawPos;
77 hal::SimDouble m_simRotorVel;
78 hal::SimDouble m_simRotorAccel;
79
80 int32_t m_simPeriodicUid{-1};
81 std::vector<int32_t> m_simValueChangedUids;
82
83 static void OnValueChanged(const char* name, void *param, HAL_SimValueHandle handle,
84 HAL_Bool readonly, const struct HAL_Value* value);
85 static void OnPeriodic(void* param);
86
87 /** caller must lock appropriately */
88 wpiutils::MotorSafetyImplem &GetMotorSafety() const;
89
90public:
91 /**
92 * Constructs a new Talon FX motor controller object.
93 *
94 * \param deviceId ID of the device, as configured in Phoenix Tuner.
95 * \param canbus Name of the CAN bus this device is on. Possible CAN bus strings are:
96 * - "rio" for the native roboRIO CAN bus
97 * - CANivore name or serial number
98 * - SocketCAN interface (non-FRC Linux only)
99 * - "*" for any CANivore seen by the program
100 * - empty string (default) to select the default for the system:
101 * - "rio" on roboRIO
102 * - "can0" on Linux
103 * - "*" on Windows
104 */
105 TalonFX(int deviceId, std::string canbus = "");
106 /**
107 * Constructs a new Talon FX motor controller object.
108 *
109 * \param deviceId ID of the device, as configured in Phoenix Tuner.
110 * \param canbus The CAN bus this device is on.
111 */
112 TalonFX(int deviceId, CANBus canbus) :
113 TalonFX{deviceId, std::string{canbus.GetName()}}
114 {}
115
117
118 /**
119 * Common interface for setting the speed of a motor controller.
120 *
121 * \param speed The speed to set. Value should be between -1.0 and 1.0.
122 */
123 void Set(double speed) override;
124 /**
125 * Common interface for seting the direct voltage output of a motor controller.
126 *
127 * \param volts The voltage to output.
128 */
129 void SetVoltage(units::volt_t volts) override;
130 /**
131 * Common interface for getting the current set speed of a motor controller.
132 *
133 * \returns The current set speed. Value is between -1.0 and 1.0.
134 */
135 double Get() const override;
136 /**
137 * Common interface for disabling a motor controller.
138 */
139 void Disable() override;
140 /**
141 * Common interface to stop motor movement until Set is called again.
142 */
143 void StopMotor() override;
144 /**
145 * Common interface for inverting direction of a motor controller.
146 *
147 * Since invert is a config, this API is blocking. We recommend that
148 * users avoid calling this API periodically.
149 *
150 * Since invert affects the reported motor position, this API should
151 * be called before any calls to #SetPosition.
152 *
153 * \deprecated This API is deprecated for removal in 2026. Since invert
154 * is a config, apply the invert setting as part of a full
155 * configs#TalonFXConfiguration object. Invert can be found in the
156 * MotorOutput config group.
157 *
158 * \param isInverted The state of inversion, true is inverted.
159 */
160 [[deprecated("This API is deprecated for removal in 2026."
161 " Since invert is a config, apply the invert setting as part of a full configs::TalonFXConfiguration object."
162 " Invert can be found in the MotorOutput config group.")]]
163 void SetInverted(bool isInverted) override;
164 /**
165 * Common interface for returning the inversion state of a motor controller.
166 *
167 * Since invert is a config, this API is blocking. We recommend that
168 * users avoid calling this API periodically.
169 *
170 * \deprecated This API is deprecated for removal in 2026. Since invert
171 * is a config, read the invert setting as part of a full
172 * configs#TalonFXConfiguration object or using a configs#MotorOutputConfigs
173 * object. Applied invert, which may not match the invert config for followers,
174 * can also be fetched using #GetAppliedRotorPolarity.
175 *
176 * \returns The state of the inversion, true is inverted.
177 */
178 [[deprecated("This API is deprecated for removal in 2026."
179 " Since invert is a config, read the invert setting as part of a full configs::TalonFXConfiguration object"
180 " or using a configs::MotorOutputConfigs object."
181 " Applied invert, which may not match the invert config for followers, can also be fetched using GetAppliedRotorPolarity.")]]
182 bool GetInverted() const override;
183 /**
184 * Sets the mode of operation when output is neutral or disabled.
185 *
186 * Since neutral mode is a config, this API is blocking. We recommend
187 * that users avoid calling this API periodically.
188 *
189 * \param neutralMode The state of the motor controller bridge when output is neutral or disabled
190 * \param timeoutSeconds Maximum amount of time to wait when performing configuration
191 * \returns Status of refreshing and applying the neutral mode config
192 */
193 ctre::phoenix::StatusCode SetNeutralMode(signals::NeutralModeValue neutralMode, units::second_t timeoutSeconds = 100_ms);
194
195 /**
196 * \returns Description of motor controller
197 */
198 std::string GetDescription() const;
199 void InitSendable(wpi::SendableBuilder &builder) override;
200
201 /* ----- Motor Safety ----- */
202 /**
203 * \brief Feed the motor safety object.
204 *
205 * Resets the timer on this object that is used to do the timeouts.
206 */
207 void Feed();
208 /**
209 * \brief Set the expiration time for the corresponding motor safety object.
210 *
211 * \param expirationTime The timeout value.
212 */
213 void SetExpiration(units::second_t expirationTime);
214 /**
215 * \brief Retrieve the timeout value for the corresponding motor safety object.
216 *
217 * @return the timeout value.
218 */
219 units::second_t GetExpiration() const;
220 /**
221 * \brief Determine of the motor is still operating or has timed out.
222 *
223 * \returns true if the motor is still operating normally and hasn't timed out.
224 */
225 bool IsAlive() const;
226 /**
227 * \brief Enable/disable motor safety for this device.
228 *
229 * Turn on and off the motor safety option for this object.
230 *
231 * \param enabled True if motor safety is enforced for this object.
232 */
233 void SetSafetyEnabled(bool enabled);
234 /**
235 * \brief Return the state of the motor safety enabled flag.
236 *
237 * Return if the motor safety is currently enabled for this device.
238 *
239 * \returns True if motor safety is enforced for this device
240 */
241 bool IsSafetyEnabled() const;
242
243protected:
244 //------------- Intercept CTRE calls for motor safety ------------//
246};
247
248}
249}
250}
Class for getting information about an available CAN bus.
Definition: CANBus.hpp:19
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:29
Request a specified motor duty cycle.
Definition: DutyCycleOut.hpp:28
Class description for the Talon FX integrated motor controller.
Definition: TalonFX.hpp:32
ctre::phoenix::StatusCode SetControlPrivate(controls::ControlRequest &request) override
static constexpr auto kDefaultSafetyExpiration
The default motor safety timeout IF calling application enables the feature.
Definition: TalonFX.hpp:38
void SetInverted(bool isInverted) override
Common interface for inverting direction of a motor controller.
TalonFX(int deviceId, CANBus canbus)
Constructs a new Talon FX motor controller object.
Definition: TalonFX.hpp:112
void Feed()
Feed the motor safety object.
void StopMotor() override
Common interface to stop motor movement until Set is called again.
bool IsAlive() const
Determine of the motor is still operating or has timed out.
void Disable() override
Common interface for disabling a motor controller.
void InitSendable(wpi::SendableBuilder &builder) override
TalonFX(int deviceId, std::string canbus="")
Constructs a new Talon FX motor controller object.
bool IsSafetyEnabled() const
Return the state of the motor safety enabled flag.
void SetVoltage(units::volt_t volts) override
Common interface for seting the direct voltage output of a motor controller.
void SetExpiration(units::second_t expirationTime)
Set the expiration time for the corresponding motor safety object.
units::second_t GetExpiration() const
Retrieve the timeout value for the corresponding motor safety object.
bool GetInverted() const override
Common interface for returning the inversion state of a motor controller.
void Set(double speed) override
Common interface for setting the speed of a motor controller.
ctre::phoenix::StatusCode SetNeutralMode(signals::NeutralModeValue neutralMode, units::second_t timeoutSeconds=100_ms)
Sets the mode of operation when output is neutral or disabled.
void SetSafetyEnabled(bool enabled)
Enable/disable motor safety for this device.
std::string GetDescription() const
double Get() const override
Common interface for getting the current set speed of a motor controller.
Class description for the Talon FX integrated motor controller.
Definition: CoreTalonFX.hpp:2813
StatusSignal< units::dimensionless::scalar_t > & GetDutyCycle(bool refresh=true)
The applied motor duty cycle.
The state of the motor controller bridge when output is neutral or disabled.
Definition: SpnEnums.hpp:1698
Status codes reported by APIs, including OK, warnings, and errors.
Definition: StatusCodes.h:27
Represents the state of one swerve module.
Definition: StatusCodes.h:18
Definition: span.hpp:401