CTRE Phoenix 6 C++ 24.3.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};
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 TalonFX(TalonFX &&) = default;
113 TalonFX &operator=(TalonFX &&) = default;
114
115 /**
116 * Common interface for setting the speed of a motor controller.
117 *
118 * \param speed The speed to set. Value should be between -1.0 and 1.0.
119 */
120 void Set(double speed) override;
121 /**
122 * Common interface for seting the direct voltage output of a motor controller.
123 *
124 * \param volts The voltage to output.
125 */
126 void SetVoltage(units::volt_t volts) override;
127 /**
128 * Common interface for getting the current set speed of a motor controller.
129 *
130 * \returns The current set speed. Value is between -1.0 and 1.0.
131 */
132 double Get() const override;
133 /**
134 * Common interface for disabling a motor controller.
135 */
136 void Disable() override;
137 /**
138 * Common interface to stop motor movement until Set is called again.
139 */
140 void StopMotor() override;
141 /**
142 * Common interface for inverting direction of a motor controller.
143 *
144 * Since invert is a config, this API is blocking. We recommend that
145 * users avoid calling this API periodically.
146 *
147 * \param isInverted The state of inversion, true is inverted.
148 */
149 void SetInverted(bool isInverted) override;
150 /**
151 * Common interface for returning the inversion state of a motor controller.
152 *
153 * Since invert is a config, this API is blocking. We recommend that
154 * users avoid calling this API periodically.
155 *
156 * \returns The state of the inversion, true is inverted.
157 */
158 bool GetInverted() const override;
159 /**
160 * Sets the mode of operation when output is neutral or disabled.
161 *
162 * Since neutral mode is a config, this API is blocking. We recommend
163 * that users avoid calling this API periodically.
164 *
165 * \param neutralMode The state of the motor controller bridge when output is neutral or disabled
166 */
168
169 /**
170 * \returns Description of motor controller
171 */
172 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 * @return 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 ------------//
219 ctre::phoenix::StatusCode SetControlPrivate(controls::ControlRequest &request) override;
220};
221
222}
223}
224}
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.
TalonFX & operator=(TalonFX &&)=default
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:2734
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:1537
Definition: string_util.hpp:15