CTRE Phoenix 6 C++ 24.2.0
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 * This must be constructed after main executes. We recommend constructing inside
95 * the robot class or your subsystem classes. Otherwise WPILib motor safety may
96 * produce erroneous behavior.
97 *
98 * \param deviceId ID of the device, as configured in Phoenix Tuner.
99 * \param canbus Name of the CAN bus this device is on. Possible CAN bus strings are:
100 * - "rio" for the native roboRIO CAN bus
101 * - CANivore name or serial number
102 * - SocketCAN interface (non-FRC Linux only)
103 * - "*" for any CANivore seen by the program
104 * - empty string (default) to select the default for the system:
105 * - "rio" on roboRIO
106 * - "can0" on Linux
107 * - "*" on Windows
108 */
109 TalonFX(int deviceId, std::string canbus = "");
111
112 /**
113 * Common interface for setting the speed of a motor controller.
114 *
115 * \param speed The speed to set. Value should be between -1.0 and 1.0.
116 */
117 void Set(double speed) override;
118 /**
119 * Common interface for seting the direct voltage output of a motor controller.
120 *
121 * \param volts The voltage to output.
122 */
123 void SetVoltage(units::volt_t volts) override;
124 /**
125 * Common interface for getting the current set speed of a motor controller.
126 *
127 * \returns The current set speed. Value is between -1.0 and 1.0.
128 */
129 double Get() const override;
130 /**
131 * Common interface for disabling a motor controller.
132 */
133 void Disable() override;
134 /**
135 * Common interface to stop motor movement until Set is called again.
136 */
137 void StopMotor() override;
138 /**
139 * Common interface for inverting direction of a motor controller.
140 *
141 * Since invert is a config, this API is blocking. We recommend that
142 * users avoid calling this API periodically.
143 *
144 * \param isInverted The state of inversion, true is inverted.
145 */
146 void SetInverted(bool isInverted) override;
147 /**
148 * Common interface for returning the inversion state of a motor controller.
149 *
150 * Since invert is a config, this API is blocking. We recommend that
151 * users avoid calling this API periodically.
152 *
153 * \returns The state of the inversion, true is inverted.
154 */
155 bool GetInverted() const override;
156 /**
157 * Sets the mode of operation when output is neutral or disabled.
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 */
165
166 /**
167 * \returns Description of motor controller
168 */
169 std::string GetDescription() const;
170 void InitSendable(wpi::SendableBuilder &builder) override;
171
172 /* ----- Motor Safety ----- */
173 /**
174 * \brief Feed the motor safety object.
175 *
176 * Resets the timer on this object that is used to do the timeouts.
177 */
178 void Feed();
179 /**
180 * \brief Set the expiration time for the corresponding motor safety object.
181 *
182 * \param expirationTime The timeout value.
183 */
184 void SetExpiration(units::second_t expirationTime);
185 /**
186 * \brief Retrieve the timeout value for the corresponding motor safety object.
187 *
188 * @return the timeout value.
189 */
190 units::second_t GetExpiration() const;
191 /**
192 * \brief Determine of the motor is still operating or has timed out.
193 *
194 * \returns true if the motor is still operating normally and hasn't timed out.
195 */
196 bool IsAlive() const;
197 /**
198 * \brief Enable/disable motor safety for this device.
199 *
200 * Turn on and off the motor safety option for this object.
201 *
202 * \param enabled True if motor safety is enforced for this object.
203 */
204 void SetSafetyEnabled(bool enabled);
205 /**
206 * \brief Return the state of the motor safety enabled flag.
207 *
208 * Return if the motor safety is currently enabled for this device.
209 *
210 * \returns True if motor safety is enforced for this device
211 */
212 bool IsSafetyEnabled() const;
213
214protected:
215 //------------- Intercept CTRE calls for motor safety ------------//
216 ctre::phoenix::StatusCode SetControlPrivate(controls::ControlRequest &request) override;
217};
218
219}
220}
221}
Abstract Control Request class that other control requests extend for use.
Definition: ControlRequest.hpp:28
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.
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.
void SetNeutralMode(signals::NeutralModeValue neutralMode)
Sets the mode of operation when output is neutral or disabled.
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.
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:2624
StatusSignal< units::dimensionless::scalar_t > & GetDutyCycle()
The applied motor duty cycle.
The state of the motor controller bridge when output is neutral or disabled.
Definition: SpnEnums.hpp:1533
Definition: string_util.hpp:15