CTRE Phoenix 6 C++ 26.70.0-alpha-2
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 <wpi/hal/SimDevice.hpp>
13#include <wpi/telemetry/TelemetryLoggable.hpp>
14
15namespace ctre {
16namespace phoenix6 {
17namespace hardware {
18
19#if defined(_WIN32) || defined(_WIN64)
20#pragma warning(push)
21#pragma warning(disable : 4250)
22#endif
23
24/**
25 * Class description for the Talon FX integrated motor controller.
26 */
27class TalonFX : public core::CoreTalonFX, public wpi::telemetry::TelemetryLoggable {
28public:
29 /**
30 * \brief The default motor safety timeout IF calling
31 * application enables the feature.
32 */
33 static constexpr auto DEFAULT_SAFETY_EXPIRATION = 100_ms;
34
35private:
36 std::string m_description;
37
38 /* Motor Safety */
39 mutable std::unique_ptr<wpiutils::MotorSafetyImplem> m_motorSafety{};
40 wpi::units::second_t m_motSafeExpiration{DEFAULT_SAFETY_EXPIRATION};
41 mutable std::recursive_mutex m_motorSafetyLock;
42
43 /*
44 * The StatusSignal getters are copies so that calls
45 * to the WPI interface do not update any references
46 *
47 * These are also mutable so the const getter methods are
48 * properly managed.
49 */
50 mutable StatusSignal<wpi::units::scalar_t> m_dutyCycle = GetDutyCycle(false);
51 mutable StatusSignal<wpi::units::turn_t> m_position = GetPosition(false);
53
54 controls::DutyCycleOut m_setterControl{0.0};
55 controls::NeutralOut m_neutralControl{};
56 controls::VoltageOut m_voltageControl{0_V};
57
58 wpi::hal::SimDevice m_simMotor;
59 wpi::hal::SimDouble m_simSupplyVoltage;
60 wpi::hal::SimDouble m_simDutyCycle;
61 wpi::hal::SimDouble m_simMotorVoltage;
62 wpi::hal::SimDouble m_simTorqueCurrent;
63 wpi::hal::SimDouble m_simSupplyCurrent;
64
65 wpi::hal::SimDevice m_simForwardLimit;
66 wpi::hal::SimBoolean m_simForwardLimitValue;
67
68 wpi::hal::SimDevice m_simReverseLimit;
69 wpi::hal::SimBoolean m_simReverseLimitValue;
70
71 wpi::hal::SimDevice m_simRotor;
72 wpi::hal::SimDouble m_simRotorPos;
73 wpi::hal::SimDouble m_simRotorRawPos;
74 wpi::hal::SimDouble m_simRotorVel;
75 wpi::hal::SimDouble m_simRotorAccel;
76
77 int32_t m_simPeriodicUid{-1};
78 std::vector<int32_t> m_simValueChangedUids;
79
80 static void OnValueChanged(
81 const char* name, void *param, HAL_SimValueHandle handle,
82 HAL_Bool readonly, const struct HAL_Value* value
83 );
84 static void OnPeriodic(void* param);
85
86 /** caller must lock appropriately */
87 wpiutils::MotorSafetyImplem &GetMotorSafety() const;
88
89public:
90 /**
91 * Constructs a new Talon FX motor controller object.
92 *
93 * \param deviceId ID of the device, as configured in Phoenix Tuner
94 * \param canbus The CAN bus this device is on
95 */
96 TalonFX(int deviceId, CANBus canbus);
97
99
100 /**
101 * \brief Constructs a stubbed-out TalonFX, where all status signals, controls,
102 * configs, etc. perform no action and immediately return OK. This can be used to
103 * silence error messages for devices that have been completely removed from the robot.
104 *
105 * \returns Stubbed-out TalonFX
106 */
107 static TalonFX None()
108 {
109 return TalonFX{-1, CANBus{}};
110 }
111
112 /**
113 * \brief Sets the throttle of the motor controller.
114 *
115 * \param throttle The throttle where -1 indicates full reverse and 1
116 * indicates full forward.
117 */
118 void SetThrottle(double throttle)
119 {
120 SetControl(m_setterControl.WithOutput(throttle));
121 }
122 /**
123 * \brief Sets the direct voltage output of the motor controller.
124 *
125 * Compensates for the current bus voltage to ensure that the desired voltage
126 * is output even if the battery voltage is below 12V - highly useful when the
127 * voltage outputs are "meaningful" (e.g. they come from a feedforward
128 * calculation).
129 *
130 * \param voltage The voltage.
131 */
132 void SetVoltage(wpi::units::volt_t voltage)
133 {
134 SetControl(m_voltageControl.WithOutput(voltage));
135 }
136 /**
137 * \brief Gets the throttle of the motor controller.
138 *
139 * \returns The throttle where -1 represents full reverse and 1 represents full
140 * forward.
141 */
142 double GetThrottle() const
143 {
144 return m_dutyCycle.Refresh().GetValue();
145 }
146 /**
147 * \brief Disables the motor controller.
148 */
149 void Disable()
150 {
151 SetControl(m_neutralControl);
152 }
153
154 /**
155 * \brief Sets the mode of operation when output is neutral or disabled.
156 * This is equivalent to setting the configs#MotorOutputConfigs#NeutralMode
157 * when applying a configs#TalonFXConfiguration to the motor.
158 *
159 * Since neutral mode is a config, this API is blocking. We recommend
160 * that users avoid calling this API periodically.
161 *
162 * \param neutralMode The state of the motor controller bridge when output is neutral or disabled
163 * \param timeoutSeconds Maximum amount of time to wait when performing configuration
164 * \returns Status of refreshing and applying the neutral mode config
165 */
166 ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode, wpi::units::second_t timeoutSeconds = 100_ms);
167
168 /* ----- Motor Safety ----- */
169 /**
170 * \brief Feed the motor safety object.
171 *
172 * Resets the timer on this object that is used to do the timeouts.
173 */
174 void Feed();
175 /**
176 * \brief Set the expiration time for the corresponding motor safety object.
177 *
178 * \param expirationTime The timeout value.
179 */
180 void SetExpiration(wpi::units::second_t expirationTime);
181 /**
182 * \brief Retrieve the timeout value for the corresponding motor safety object.
183 *
184 * \returns the timeout value.
185 */
186 wpi::units::second_t GetExpiration() const;
187 /**
188 * \brief Determine of the motor is still operating or has timed out.
189 *
190 * \returns true if the motor is still operating normally and hasn't timed out.
191 */
192 bool IsAlive() const;
193 /**
194 * \brief Enable/disable motor safety for this device.
195 *
196 * Turn on and off the motor safety option for this object.
197 *
198 * \param enabled True if motor safety is enforced for this object.
199 */
200 void SetSafetyEnabled(bool enabled);
201 /**
202 * \brief Return the state of the motor safety enabled flag.
203 *
204 * Return if the motor safety is currently enabled for this device.
205 *
206 * \returns True if motor safety is enforced for this device
207 */
208 bool IsSafetyEnabled() const;
209 /**
210 * \brief Called to stop the motor when the timeout expires.
211 */
212 void StopMotor() { Disable(); }
213 /**
214 * \returns Description of the motor controller
215 */
216 virtual std::string GetDescription() const { return m_description; }
217
218 /**
219 * \brief Logs the object to a wpi::telemetry::TelemetryTable.
220 *
221 * \param table Telemetry table
222 */
223 void LogTo(wpi::telemetry::TelemetryTable &table) const override;
224
225 /**
226 * \brief Gets the telemetry table type.
227 *
228 * \returns Table type
229 */
230 std::string_view GetTelemetryType() const override { return "Motor Controller"; }
231
232protected:
233 //------------- Intercept CTRE calls for motor safety ------------//
235 {
236 Feed();
237 return CoreTalonFX::SetControlPrivate(request);
238 }
239};
240
241#if defined(_WIN32) || defined(_WIN64)
242#pragma warning(pop)
243#endif
244
245}
246}
247}
Class for getting information about an available CAN bus.
Definition CANBus.hpp:142
Represents a status signal with data of type T, and operations available to retrieve information abou...
Definition StatusSignal.hpp:523
Common interface implemented by all control requests.
Definition ControlRequest.hpp:27
Request a specified motor duty cycle.
Definition DutyCycleOut.hpp:23
TalonFX(int deviceId, CANBus canbus)
Constructs a new Talon FX motor controller object.
void Feed()
Feed the motor safety object.
bool IsAlive() const
Determine of the motor is still operating or has timed out.
ctre::phoenix::StatusCode SetControlPrivate(controls::ControlRequest const &request) override
Definition TalonFX.hpp:234
void SetVoltage(wpi::units::volt_t voltage)
Sets the direct voltage output of the motor controller.
Definition TalonFX.hpp:132
wpi::units::second_t GetExpiration() const
Retrieve the timeout value for the corresponding motor safety object.
ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode, wpi::units::second_t timeoutSeconds=100_ms)
Sets the mode of operation when output is neutral or disabled.
virtual std::string GetDescription() const
Definition TalonFX.hpp:216
void SetExpiration(wpi::units::second_t expirationTime)
Set the expiration time for the corresponding motor safety object.
double GetThrottle() const
Gets the throttle of the motor controller.
Definition TalonFX.hpp:142
std::string_view GetTelemetryType() const override
Gets the telemetry table type.
Definition TalonFX.hpp:230
void Disable()
Disables the motor controller.
Definition TalonFX.hpp:149
void LogTo(wpi::telemetry::TelemetryTable &table) const override
Logs the object to a wpi::telemetry::TelemetryTable.
bool IsSafetyEnabled() const
Return the state of the motor safety enabled flag.
void StopMotor()
Called to stop the motor when the timeout expires.
Definition TalonFX.hpp:212
static constexpr auto DEFAULT_SAFETY_EXPIRATION
The default motor safety timeout IF calling application enables the feature.
Definition TalonFX.hpp:33
static TalonFX None()
Constructs a stubbed-out TalonFX, where all status signals, controls, configs, etc.
Definition TalonFX.hpp:107
void SetSafetyEnabled(bool enabled)
Enable/disable motor safety for this device.
void SetThrottle(double throttle)
Sets the throttle of the motor controller.
Definition TalonFX.hpp:118
Class for the Talon FX integrated motor controller.
Definition CoreTalonFX.hpp:3182
StatusSignal< wpi::units::scalar_t > & GetDutyCycle(bool refresh=true) final
The applied motor duty cycle.
ctre::phoenix::StatusCode SetControl(controls::DutyCycleOut const &request) final
Request a specified motor duty cycle.
StatusSignal< wpi::units::turn_t > & GetPosition(bool refresh=true) final
Position of the device in mechanism rotations.
StatusSignal< wpi::units::turns_per_second_t > & GetVelocity(bool refresh=true) final
Velocity of the device in mechanism rotations per second.
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
Definition ExternalFeedbackConfigs.hpp:17
Definition ExternalFeedbackConfigs.hpp:16
Definition motor_constants.h:14
The state of the motor controller bridge when output is neutral or disabled.
Definition SpnEnums.hpp:1499