CTRE Phoenix 6 C++ 25.0.0-beta-4
Loading...
Searching...
No Matches
SwerveModuleImpl.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
12#include "units/force.h"
13#include "units/torque.h"
14#include <array>
15
16namespace ctre {
17namespace phoenix6 {
18namespace swerve {
19namespace impl {
20
21/**
22 * \brief Swerve Module class that encapsulates a swerve module powered by
23 * CTR Electronics devices.
24 *
25 * This class handles the hardware devices but does not configure them for
26 * swerve module operation using the Phoenix 6 API. Users should create a
27 * high-level SwerveModule instead of using this directly.
28 */
30public:
31 /**
32 * \brief All possible control requests for the module steer motor.
33 */
34 enum class SteerRequestType {
35 /**
36 * \brief Control the drive motor using a Motion Magic® Expo request.
37 * The control output type is determined by SwerveModuleConstants#SteerMotorClosedLoopOutput.
38 */
40 /**
41 * \brief Control the drive motor using an unprofiled position request.
42 * The control output type is determined by SwerveModuleConstants#SteerMotorClosedLoopOutput.
43 */
44 Position = 1,
45 };
46
47 /**
48 * \brief All possible control requests for the module drive motor.
49 */
50 enum class DriveRequestType {
51 /**
52 * \brief Control the drive motor using an open-loop voltage request.
53 */
55 /**
56 * \brief Control the drive motor using a velocity closed-loop request.
57 * The control output type is determined by SwerveModuleConstants#DriveMotorClosedLoopOutput.
58 */
59 Velocity = 1,
60 };
61
62 /**
63 * \brief Contains everything the swerve module needs to apply a request.
64 */
66 /**
67 * \brief Unoptimized speed and direction the module should target.
68 */
69 SwerveModuleState State{};
70
71 /**
72 * \brief Robot-centric wheel force feedforward to apply in the
73 * X direction. X is defined as forward according to WPILib
74 * convention, so this determines the forward force to apply.
75 *
76 * This force should include friction applied to the ground.
77 */
78 units::newton_t WheelForceFeedforwardX = 0_N;
79 /**
80 * \brief Robot-centric wheel force feedforward to apply in the
81 * Y direction. Y is defined as to the left according to WPILib
82 * convention, so this determines the force to apply to the left.
83 *
84 * This force should include friction applied to the ground.
85 */
86 units::newton_t WheelForceFeedforwardY = 0_N;
87
88 /**
89 * \brief The type of control request to use for the drive motor.
90 */
92 /**
93 * \brief The type of control request to use for the steer motor.
94 */
96
97 /**
98 * \brief The update period of the module request. Setting this to
99 * a non-zero value adds a velocity feedforward to the steer motor.
100 */
101 units::second_t UpdatePeriod = 0_s;
102
103 /**
104 * \brief When using Voltage-based control, set to true (default) to use FOC
105 * commutation (requires Phoenix Pro), which increases peak power by ~15%. Set to
106 * false to use trapezoidal commutation. This is ignored when using Torque-based
107 * control, which always uses FOC.
108 *
109 * FOC improves motor performance by leveraging torque (current) control.
110 * However, this may be inconvenient for applications that require specifying
111 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
112 * combines the performances gains of FOC while still allowing applications to
113 * provide duty cycle or voltage demand. This not to be confused with simple
114 * sinusoidal control or phase voltage control which lacks the performance
115 * gains.
116 */
117 bool EnableFOC = true;
118
119 /**
120 * \brief Modifies the State parameter and returns itself.
121 *
122 * Unoptimized speed and direction the module should target.
123 *
124 * \param newState Parameter to modify
125 * \returns Itself
126 */
127 ModuleRequest &WithState(SwerveModuleState newState)
128 {
129 this->State = std::move(newState);
130 return *this;
131 }
132
133 /**
134 * \brief Modifies the WheelForceFeedforwardX parameter and returns itself.
135 *
136 * Robot-centric wheel force feedforward to apply in the
137 * X direction. X is defined as forward according to WPILib
138 * convention, so this determines the forward force to apply.
139 *
140 * This force should include friction.
141 *
142 * \param newWheelForceFeedforwardX Parameter to modify
143 * \returns Itself
144 */
145 ModuleRequest &WithWheelForceFeedforwardX(units::newton_t newWheelForceFeedforwardX)
146 {
147 this->WheelForceFeedforwardX = newWheelForceFeedforwardX;
148 return *this;
149 }
150 /**
151 * \brief Modifies the WheelForceFeedforwardY parameter and returns itself.
152 *
153 * Robot-centric wheel force feedforward to apply in the
154 * Y direction. Y is defined as to the left according to WPILib
155 * convention, so this determines the force to apply to the left.
156 *
157 * This force should include friction.
158 *
159 * \param newWheelForceFeedforwardY Parameter to modify
160 * \returns Itself
161 */
162 ModuleRequest &WithWheelForceFeedforwardY(units::newton_t newWheelForceFeedforwardY)
163 {
164 this->WheelForceFeedforwardY = newWheelForceFeedforwardY;
165 return *this;
166 }
167
168 /**
169 * \brief Modifies the DriveRequest parameter and returns itself.
170 *
171 * The type of control request to use for the drive motor.
172 *
173 * \param newDriveRequest Parameter to modify
174 * \returns Itself
175 */
177 {
178 this->DriveRequest = newDriveRequest;
179 return *this;
180 }
181 /**
182 * \brief Modifies the SteerRequest parameter and returns itself.
183 *
184 * The type of control request to use for the steer motor.
185 *
186 * \param newSteerRequest Parameter to modify
187 * \returns Itself
188 */
190 {
191 this->SteerRequest = newSteerRequest;
192 return *this;
193 }
194
195 /**
196 * \brief Modifies the UpdatePeriod parameter and returns itself.
197 *
198 * The update period of the module request. Setting this to a
199 * non-zero value adds a velocity feedforward to the steer motor.
200 *
201 * \param newUpdatePeriod Parameter to modify
202 * \returns Itself
203 */
204 ModuleRequest &WithUpdatePeriod(units::second_t newUpdatePeriod)
205 {
206 this->UpdatePeriod = newUpdatePeriod;
207 return *this;
208 }
209
210 /**
211 * \brief Modifies the EnableFOC parameter and returns itself.
212 *
213 * When using Voltage-based control, set to true (default) to use FOC commutation
214 * (requires Phoenix Pro), which increases peak power by ~15%. Set to false to
215 * use trapezoidal commutation. This is ignored when using Torque-based control,
216 * which always uses FOC.
217 *
218 * FOC improves motor performance by leveraging torque (current) control.
219 * However, this may be inconvenient for applications that require specifying
220 * duty cycle or voltage. CTR-Electronics has developed a hybrid method that
221 * combines the performances gains of FOC while still allowing applications to
222 * provide duty cycle or voltage demand. This not to be confused with simple
223 * sinusoidal control or phase voltage control which lacks the performance
224 * gains.
225 *
226 * \param newEnableFOC Parameter to modify
227 * \returns Itself
228 */
229 ModuleRequest &WithEnableFOC(bool newEnableFOC)
230 {
231 this->EnableFOC = newEnableFOC;
232 return *this;
233 }
234 };
235
236private:
237 hardware::core::CoreTalonFX _driveMotor;
238 hardware::core::CoreTalonFX _steerMotor;
240
241 StatusSignal<units::turn_t> _drivePosition;
243 StatusSignal<units::turn_t> _steerPosition;
245
247 mutable StatusSignal<units::ampere_t> _driveMotorStallCurrent;
248
249 ClosedLoopOutputType _driveClosedLoopOutput;
250 ClosedLoopOutputType _steerClosedLoopOutput;
251
252 using turns_per_meter = units::compound_unit<units::turns, units::inverse<units::meters>>;
253 using turns_per_meter_t = units::unit_t<turns_per_meter>;
254
255 turns_per_meter_t kDriveRotationsPerMeter;
256 units::meter_t kDriveNmPerWheelN;
257 units::scalar_t kCouplingRatioDriveRotorToCANcoder;
258 units::meters_per_second_t kSpeedAt12Volts;
259
260 bool _isOnCANFD;
261
262 SwerveModulePosition _currentPosition;
263 SwerveModuleState _targetState;
264
265public:
266 /**
267 * \brief Construct a SwerveModuleImpl with the specified constants.
268 *
269 * \param constants Constants used to construct the module
270 * \param canbus The CAN bus this module is on
271 */
273
274 /**
275 * \brief Applies the desired ModuleRequest to this module.
276 *
277 * \param moduleRequest The request to apply to this module
278 */
279 void Apply(ModuleRequest const &moduleRequest);
280
281 /**
282 * \brief Controls this module using the specified drive and steer control requests.
283 *
284 * This is intended only to be used for characterization of the robot; do not use this for normal use.
285 *
286 * \param driveRequest The control request to apply to the drive motor
287 * \param steerRequest The control request to apply to the steer motor
288 */
289 template <typename DriveReq, typename SteerReq>
290 void Apply(DriveReq &&driveRequest, SteerReq &&steerRequest)
291 {
292 _driveMotor.SetControl(driveRequest.WithUpdateFreqHz(0_Hz));
293 _steerMotor.SetControl(steerRequest.WithUpdateFreqHz(0_Hz));
294 }
295
296 /**
297 * \brief Configures the neutral mode to use for the module's drive motor.
298 *
299 * \param neutralMode The drive motor neutral mode
300 * \param timeoutSeconds Maximum amount of time to wait when performing configuration
301 * \returns Status code response of the request
302 */
303 ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode, units::second_t timeoutSeconds = 0.100_s);
304
305 /**
306 * \brief Gets the state of this module and passes it back as a
307 * SwerveModulePosition object with latency compensated values.
308 *
309 * This function is blocking when it performs a refresh.
310 *
311 * \param refresh True if the signals should be refreshed
312 * \returns SwerveModulePosition containing this module's state.
313 */
314 SwerveModulePosition GetPosition(bool refresh);
315
316 /**
317 * \brief Gets the last cached swerve module position.
318 * This differs from #GetPosition in that it will not
319 * perform any latency compensation or refresh the signals.
320 *
321 * \returns Last cached SwerveModulePosition
322 */
323 SwerveModulePosition GetCachedPosition() const { return _currentPosition; }
324
325 /**
326 * \brief Get the current state of the module.
327 *
328 * This is typically used for telemetry, as the SwerveModulePosition
329 * is used for odometry.
330 *
331 * \returns Current state of the module
332 */
333 SwerveModuleState GetCurrentState() const
334 {
335 return SwerveModuleState{_driveVelocity.GetValue() / kDriveRotationsPerMeter, {_steerPosition.GetValue()}};
336 }
337
338 /**
339 * \brief Get the target state of the module.
340 *
341 * This is typically used for telemetry.
342 *
343 * \returns Target state of the module
344 */
345 SwerveModuleState GetTargetState() const { return _targetState; }
346
347 /**
348 * \brief Resets this module's drive motor position to 0 rotations.
349 */
351 {
352 /* Only touch drive pos, not steer */
353 _driveMotor.SetPosition(0_tr);
354 }
355
356 /**
357 * \brief Gets this module's Drive Motor TalonFX reference.
358 *
359 * This should be used only to access signals and change configurations that the
360 * swerve drivetrain does not configure itself.
361 *
362 * \returns This module's Drive Motor reference
363 */
365
366 /**
367 * \brief Gets this module's Steer Motor TalonFX reference.
368 *
369 * This should be used only to access signals and change configurations that the
370 * swerve drivetrain does not configure itself.
371 *
372 * \returns This module's Steer Motor reference
373 */
375
376 /**
377 * \brief Gets this module's CANcoder reference.
378 *
379 * This should be used only to access signals and change configurations that the
380 * swerve drivetrain does not configure itself.
381 *
382 * \returns This module's CANcoder reference
383 */
385
386private:
387 /**
388 * \brief Collection of all possible torque feedforward outputs that
389 * can be applied to the motor for a given wheel force feedforward.
390 */
391 struct MotorTorqueFeedforwards {
392 units::newton_meter_t torque;
393 units::ampere_t torqueCurrent;
394 units::volt_t voltage;
395 };
396
397 units::turns_per_second_t ApplyVelocityCorrections(units::turns_per_second_t velocity, units::turn_t targetAngle) const;
398 MotorTorqueFeedforwards CalculateMotorTorqueFeedforwards(
399 units::newton_t wheelForceFeedforwardX,
400 units::newton_t wheelForceFeedforwardY
401 ) const;
402
404
405 std::array<BaseStatusSignal *, 4> GetSignals()
406 {
407 return std::array<BaseStatusSignal *, 4>{&_drivePosition, &_driveVelocity, &_steerPosition, &_steerVelocity};
408 }
409};
410
411}
412}
413}
414}
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:657
T GetValue() const
Gets the cached value from this status signal.
Definition StatusSignal.hpp:759
Class for CANcoder, a CAN based magnetic encoder that provides absolute and relative position along w...
Definition CoreCANcoder.hpp:631
Class description for the Talon FX integrated motor controller.
Definition CoreTalonFX.hpp:2932
ctre::phoenix::StatusCode SetControl(const controls::DutyCycleOut &request) override
Request a specified motor duty cycle.
ctre::phoenix::StatusCode SetPosition(units::angle::turn_t newValue, units::time::second_t timeoutSeconds) override
Sets the mechanism position of the device in mechanism rotations.
Definition CoreTalonFX.hpp:7628
The state of the motor controller bridge when output is neutral or disabled.
Definition SpnEnums.hpp:2202
Swerve Drive class utilizing CTR Electronics Phoenix 6 API.
Definition SwerveDrivetrainImpl.hpp:30
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.
Definition SwerveModuleImpl.hpp:29
SwerveModuleState GetCurrentState() const
Get the current state of the module.
Definition SwerveModuleImpl.hpp:333
DriveRequestType
All possible control requests for the module drive motor.
Definition SwerveModuleImpl.hpp:50
@ Velocity
Control the drive motor using a velocity closed-loop request.
@ OpenLoopVoltage
Control the drive motor using an open-loop voltage request.
hardware::core::CoreTalonFX & GetSteerMotor()
Gets this module's Steer Motor TalonFX reference.
Definition SwerveModuleImpl.hpp:374
hardware::core::CoreCANcoder & GetCANcoder()
Gets this module's CANcoder reference.
Definition SwerveModuleImpl.hpp:384
SteerRequestType
All possible control requests for the module steer motor.
Definition SwerveModuleImpl.hpp:34
@ MotionMagicExpo
Control the drive motor using a Motion Magic® Expo request.
@ Position
Control the drive motor using an unprofiled position request.
SwerveModuleImpl(SwerveModuleConstants const &constants, CANBus canbus)
Construct a SwerveModuleImpl with the specified constants.
SwerveModuleState GetTargetState() const
Get the target state of the module.
Definition SwerveModuleImpl.hpp:345
void Apply(ModuleRequest const &moduleRequest)
Applies the desired ModuleRequest to this module.
void Apply(DriveReq &&driveRequest, SteerReq &&steerRequest)
Controls this module using the specified drive and steer control requests.
Definition SwerveModuleImpl.hpp:290
SwerveModulePosition GetPosition(bool refresh)
Gets the state of this module and passes it back as a SwerveModulePosition object with latency compen...
hardware::core::CoreTalonFX & GetDriveMotor()
Gets this module's Drive Motor TalonFX reference.
Definition SwerveModuleImpl.hpp:364
ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode, units::second_t timeoutSeconds=0.100_s)
Configures the neutral mode to use for the module's drive motor.
void ResetPosition()
Resets this module's drive motor position to 0 rotations.
Definition SwerveModuleImpl.hpp:350
SwerveModulePosition GetCachedPosition() const
Gets the last cached swerve module position.
Definition SwerveModuleImpl.hpp:323
Status codes reported by APIs, including OK, warnings, and errors.
Definition StatusCodes.h:27
ClosedLoopOutputType
Supported closed-loop output types.
Definition SwerveModuleConstants.hpp:23
Definition StatusCodes.h:18
All constants for a swerve module.
Definition SwerveModuleConstants.hpp:53
Contains everything the swerve module needs to apply a request.
Definition SwerveModuleImpl.hpp:65
ModuleRequest & WithSteerRequest(SteerRequestType newSteerRequest)
Modifies the SteerRequest parameter and returns itself.
Definition SwerveModuleImpl.hpp:189
DriveRequestType DriveRequest
The type of control request to use for the drive motor.
Definition SwerveModuleImpl.hpp:91
units::newton_t WheelForceFeedforwardY
Robot-centric wheel force feedforward to apply in the Y direction.
Definition SwerveModuleImpl.hpp:86
units::second_t UpdatePeriod
The update period of the module request.
Definition SwerveModuleImpl.hpp:101
ModuleRequest & WithEnableFOC(bool newEnableFOC)
Modifies the EnableFOC parameter and returns itself.
Definition SwerveModuleImpl.hpp:229
ModuleRequest & WithWheelForceFeedforwardY(units::newton_t newWheelForceFeedforwardY)
Modifies the WheelForceFeedforwardY parameter and returns itself.
Definition SwerveModuleImpl.hpp:162
SteerRequestType SteerRequest
The type of control request to use for the steer motor.
Definition SwerveModuleImpl.hpp:95
ModuleRequest & WithDriveRequest(DriveRequestType newDriveRequest)
Modifies the DriveRequest parameter and returns itself.
Definition SwerveModuleImpl.hpp:176
bool EnableFOC
When using Voltage-based control, set to true (default) to use FOC commutation (requires Phoenix Pro)...
Definition SwerveModuleImpl.hpp:117
ModuleRequest & WithState(SwerveModuleState newState)
Modifies the State parameter and returns itself.
Definition SwerveModuleImpl.hpp:127
ModuleRequest & WithWheelForceFeedforwardX(units::newton_t newWheelForceFeedforwardX)
Modifies the WheelForceFeedforwardX parameter and returns itself.
Definition SwerveModuleImpl.hpp:145
units::newton_t WheelForceFeedforwardX
Robot-centric wheel force feedforward to apply in the X direction.
Definition SwerveModuleImpl.hpp:78
SwerveModuleState State
Unoptimized speed and direction the module should target.
Definition SwerveModuleImpl.hpp:69
ModuleRequest & WithUpdatePeriod(units::second_t newUpdatePeriod)
Modifies the UpdatePeriod parameter and returns itself.
Definition SwerveModuleImpl.hpp:204