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