CTRE Phoenix 6 C++ 26.0.0-beta-1
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#if defined(_WIN32) || defined(_WIN64)
25#pragma warning(push)
26#pragma warning(disable : 4250)
27#endif
28
29/**
30 * Class description for the Talon FX integrated motor controller.
31 */
33 public wpi::Sendable,
34 public wpi::SendableHelper<TalonFX>
35{
36public:
37 /**
38 * \brief The default motor safety timeout IF calling
39 * application enables the feature.
40 */
41 static constexpr auto kDefaultSafetyExpiration = 100_ms;
42
43private:
44 std::string m_description;
45
46 /* Motor Safety */
47 mutable std::unique_ptr<wpiutils::MotorSafetyImplem> m_motorSafety{};
48 units::second_t m_motSafeExpiration{kDefaultSafetyExpiration};
49 mutable std::recursive_mutex m_motorSafetyLock;
50
51 /*
52 * The StatusSignal getters are copies so that calls
53 * to the WPI interface do not update any references
54 *
55 * These are also mutable so the const getter methods are
56 * properly managed.
57 */
59
60 controls::DutyCycleOut m_setterControl{0.0};
61 controls::NeutralOut m_brakeRef{};
62 controls::VoltageOut m_voltageControl{0_V};
63
64 hal::SimDevice m_simMotor;
65 hal::SimDouble m_simSupplyVoltage;
66 hal::SimDouble m_simDutyCycle;
67 hal::SimDouble m_simMotorVoltage;
68 hal::SimDouble m_simTorqueCurrent;
69 hal::SimDouble m_simSupplyCurrent;
70
71 hal::SimDevice m_simForwardLimit;
72 hal::SimBoolean m_simForwardLimitValue;
73
74 hal::SimDevice m_simReverseLimit;
75 hal::SimBoolean m_simReverseLimitValue;
76
77 hal::SimDevice m_simRotor;
78 hal::SimDouble m_simRotorPos;
79 hal::SimDouble m_simRotorRawPos;
80 hal::SimDouble m_simRotorVel;
81 hal::SimDouble m_simRotorAccel;
82
83 int32_t m_simPeriodicUid{-1};
84 std::vector<int32_t> m_simValueChangedUids;
85
86 static void OnValueChanged(
87 const char* name, void *param, HAL_SimValueHandle handle,
88 HAL_Bool readonly, const struct HAL_Value* value
89 );
90 static void OnPeriodic(void* param);
91
92 /** caller must lock appropriately */
93 wpiutils::MotorSafetyImplem &GetMotorSafety() const;
94
95public:
96 /**
97 * Constructs a new Talon FX motor controller object.
98 *
99 * \param deviceId ID of the device, as configured in Phoenix Tuner
100 * \param canbus The CAN bus this device is on
101 */
102 TalonFX(int deviceId, CANBus canbus = {});
103 /**
104 * Constructs a new Talon FX motor controller object.
105 *
106 * \param deviceId ID of the device, as configured in Phoenix Tuner
107 * \param canbus Name of the CAN bus this device is on. Possible CAN bus strings are:
108 * - "rio" for the native roboRIO CAN bus
109 * - CANivore name or serial number
110 * - SocketCAN interface (non-FRC Linux only)
111 * - "*" for any CANivore seen by the program
112 * - empty string (default) to select the default for the system:
113 * - "rio" on roboRIO
114 * - "can0" on Linux
115 * - "*" on Windows
116 *
117 * \deprecated Constructing devices with a CAN bus string is deprecated for removal
118 * in the 2027 season. Construct devices using a CANBus instance instead.
119 */
120 [[deprecated(
121 "Constructing devices with a CAN bus string is deprecated for removal "
122 "in the 2027 season. Construct devices using a CANBus instance instead."
123 )]]
124 TalonFX(int deviceId, std::string canbus);
125
127
128 /**
129 * \brief Common interface for setting the speed of a motor controller.
130 *
131 * \param speed The speed to set. Value should be between -1.0 and 1.0.
132 */
133 void Set(double speed);
134 /**
135 * \brief Common interface for seting the direct voltage output of a motor controller.
136 *
137 * \param volts The voltage to output.
138 */
139 void SetVoltage(units::volt_t volts);
140 /**
141 * \brief Common interface for getting the current set speed of a motor controller.
142 *
143 * \returns The current set speed. Value is between -1.0 and 1.0.
144 */
145 double Get() const;
146 /**
147 * \brief Common interface for disabling a motor controller.
148 */
149 void Disable();
150 /**
151 * \brief Common interface to stop motor movement until Set is called again.
152 */
153 void StopMotor();
154
155 /**
156 * \brief Sets the mode of operation when output is neutral or disabled.
157 * This is equivalent to setting the configs#MotorOutputConfigs#NeutralMode
158 * when applying a configs#TalonFXConfiguration to the motor.
159 *
160 * Since neutral mode is a config, this API is blocking. We recommend
161 * that users avoid calling this API periodically.
162 *
163 * \param neutralMode The state of the motor controller bridge when output is neutral or disabled
164 * \param timeoutSeconds Maximum amount of time to wait when performing configuration
165 * \returns Status of refreshing and applying the neutral mode config
166 */
167 ctre::phoenix::StatusCode SetNeutralMode(signals::NeutralModeValue neutralMode, units::second_t timeoutSeconds = 100_ms);
168
169 /**
170 * \returns Description of motor controller
171 */
172 virtual std::string GetDescription() const;
173 void InitSendable(wpi::SendableBuilder &builder) override;
174
175 /* ----- Motor Safety ----- */
176 /**
177 * \brief Feed the motor safety object.
178 *
179 * Resets the timer on this object that is used to do the timeouts.
180 */
181 void Feed();
182 /**
183 * \brief Set the expiration time for the corresponding motor safety object.
184 *
185 * \param expirationTime The timeout value.
186 */
187 void SetExpiration(units::second_t expirationTime);
188 /**
189 * \brief Retrieve the timeout value for the corresponding motor safety object.
190 *
191 * \returns the timeout value.
192 */
193 units::second_t GetExpiration() const;
194 /**
195 * \brief Determine of the motor is still operating or has timed out.
196 *
197 * \returns true if the motor is still operating normally and hasn't timed out.
198 */
199 bool IsAlive() const;
200 /**
201 * \brief Enable/disable motor safety for this device.
202 *
203 * Turn on and off the motor safety option for this object.
204 *
205 * \param enabled True if motor safety is enforced for this object.
206 */
207 void SetSafetyEnabled(bool enabled);
208 /**
209 * \brief Return the state of the motor safety enabled flag.
210 *
211 * Return if the motor safety is currently enabled for this device.
212 *
213 * \returns True if motor safety is enforced for this device
214 */
215 bool IsSafetyEnabled() const;
216
217protected:
218 //------------- Intercept CTRE calls for motor safety ------------//
220};
221
222#if defined(_WIN32) || defined(_WIN64)
223#pragma warning(pop)
224#endif
225
226}
227}
228}
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:474
Common interface implemented by all control requests.
Definition ControlRequest.hpp:27
Request a specified motor duty cycle.
Definition DutyCycleOut.hpp:23
Class description for the Talon FX integrated motor controller.
Definition TalonFX.hpp:35
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:41
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
void InitSendable(wpi::SendableBuilder &builder) override
virtual std::string GetDescription() const
void Disable()
Common interface for disabling a motor controller.
TalonFX(int deviceId, CANBus canbus={})
Constructs a new Talon FX motor controller object.
bool IsSafetyEnabled() const
Return the state of the motor safety enabled flag.
TalonFX(int deviceId, std::string canbus)
Constructs a new Talon FX motor controller object.
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.
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.
void SetSafetyEnabled(bool enabled)
Enable/disable motor safety for this device.
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:3113
StatusSignal< units::dimensionless::scalar_t > & GetDutyCycle(bool refresh=true) final
The applied motor duty cycle.
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:28
Definition motor_constants.h:14
The state of the motor controller bridge when output is neutral or disabled.
Definition SpnEnums.hpp:1603