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