CTRE Phoenix 6 C++ 24.50.0-alpha-2
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 <array>
13
14namespace ctre {
15namespace phoenix6 {
16namespace swerve {
17namespace impl {
18
19/**
20 * \brief Swerve Module class that encapsulates a swerve module powered by
21 * CTR Electronics devices.
22 *
23 * This class handles the hardware devices but does not configure them for
24 * swerve module operation using the Phoenix 6 API. Users should create a
25 * high-level SwerveModule instead of using this directly.
26 */
28public:
29 /**
30 * \brief All possible control requests for the module steer motor.
31 */
32 enum class SteerRequestType {
33 /**
34 * \brief Control the drive motor using a Motion Magic® Expo request.
35 * The control output type is determined by SwerveModuleConstants#SteerMotorClosedLoopOutput.
36 */
37 MotionMagicExpo = 0,
38 /**
39 * \brief Control the drive motor using an unprofiled position request.
40 * The control output type is determined by SwerveModuleConstants#SteerMotorClosedLoopOutput.
41 */
42 Position = 1,
43 };
44
45 /**
46 * \brief All possible control requests for the module drive motor.
47 */
48 enum class DriveRequestType {
49 /**
50 * \brief Control the drive motor using an open-loop voltage request.
51 */
52 OpenLoopVoltage = 0,
53 /**
54 * \brief Control the drive motor using a velocity closed-loop request.
55 * The control output type is determined by SwerveModuleConstants#DriveMotorClosedLoopOutput.
56 */
57 Velocity = 1,
58 };
59
60private:
64
65 StatusSignal<units::turn_t> _drivePosition;
67 StatusSignal<units::turn_t> _steerPosition;
69
70 ClosedLoopOutputType _driveClosedLoopOutput;
71 ClosedLoopOutputType _steerClosedLoopOutput;
72
73 using turns_per_meter = units::compound_unit<units::turns, units::inverse<units::meters>>;
74 using turns_per_meter_t = units::unit_t<turns_per_meter>;
75
76 turns_per_meter_t kDriveRotationsPerMeter;
77 units::scalar_t kCouplingRatioDriveRotorToCANcoder;
78 units::meters_per_second_t kSpeedAt12Volts;
79
80 bool _isOnCANFD;
81
82 SwerveModulePosition _internalState;
83 SwerveModuleState _targetState;
84
85public:
86 /**
87 * \brief Construct a SwerveModuleImpl with the specified constants.
88 *
89 * \param constants Constants used to construct the module
90 * \param canbus The CAN bus this module is on
91 */
93
94 /**
95 * \brief Applies the desired SwerveModuleState to this module.
96 *
97 * \param state Speed and direction the module should target
98 * \param driveRequestType The #DriveRequestType to apply
99 * \param steerRequestType The #SteerRequestType to apply; defaults to SteerRequestType#MotionMagicExpo
100 */
102
103 /**
104 * \brief Controls this module using the specified drive and steer control requests.
105 *
106 * This is intended only to be used for characterization of the robot; do not use this for normal use.
107 *
108 * \param driveRequest The control request to apply to the drive motor
109 * \param steerRequest The control request to apply to the steer motor
110 */
111 template <typename DriveReq, typename SteerReq>
112 void Apply(DriveReq &&driveRequest, SteerReq &&steerRequest)
113 {
114 _driveMotor.SetControl(driveRequest.WithUpdateFreqHz(0_Hz));
115 _steerMotor.SetControl(steerRequest.WithUpdateFreqHz(0_Hz));
116 }
117
118 /**
119 * \brief Configures the neutral mode to use for the module's drive motor.
120 *
121 * \param neutralMode The drive motor neutral mode
122 * \param timeoutSeconds Maximum amount of time to wait when performing configuration
123 * \returns Status code response of the request
124 */
125 ctre::phoenix::StatusCode ConfigNeutralMode(signals::NeutralModeValue neutralMode, units::second_t timeoutSeconds = 0.100_s);
126
127 /**
128 * \brief Gets the state of this module and passes it back as a
129 * SwerveModulePosition object with latency compensated values.
130 *
131 * \param refresh True if the signals should be refreshed
132 * \returns SwerveModulePosition containing this module's state.
133 */
135
136 /**
137 * \brief Gets the last cached swerve module position.
138 * This differs from #GetPosition in that it will not
139 * perform any latency compensation or refresh the signals.
140 *
141 * \returns Last cached SwerveModulePosition
142 */
143 SwerveModulePosition GetCachedPosition() const { return _internalState; }
144
145 /**
146 * \brief Get the current state of the module.
147 *
148 * This is typically used for telemetry, as the SwerveModulePosition
149 * is used for odometry.
150 *
151 * \returns Current state of the module
152 */
154 {
155 return SwerveModuleState{_driveVelocity.GetValue() / kDriveRotationsPerMeter, {_steerPosition.GetValue()}};
156 }
157
158 /**
159 * \brief Get the target state of the module.
160 *
161 * This is typically used for telemetry.
162 *
163 * \returns Target state of the module
164 */
165 SwerveModuleState GetTargetState() const { return _targetState; }
166
167 /**
168 * Resets this module's drive motor position to 0 rotations.
169 */
171 {
172 /* Only touch drive pos, not steer */
173 _driveMotor.SetPosition(0_tr);
174 }
175
176 /**
177 * Gets this module's Drive Motor TalonFX reference.
178 *
179 * This should be used only to access signals and change configurations that the
180 * swerve drivetrain does not configure itself.
181 *
182 * \returns This module's Drive Motor reference
183 */
185
186 /**
187 * Gets this module's Steer Motor TalonFX reference.
188 *
189 * This should be used only to access signals and change configurations that the
190 * swerve drivetrain does not configure itself.
191 *
192 * \returns This module's Steer Motor reference
193 */
195
196 /**
197 * Gets this module's CANcoder reference.
198 *
199 * This should be used only to access signals and change configurations that the
200 * swerve drivetrain does not configure itself.
201 *
202 * \returns This module's CANcoder reference
203 */
205
206private:
208
209 std::array<BaseStatusSignal *, 4> GetSignals()
210 {
211 return std::array<BaseStatusSignal *, 4>{&_drivePosition, &_driveVelocity, &_steerPosition, &_steerVelocity};
212 }
213};
214
215}
216}
217}
218}
its the or e mails sent from or on behalf of the Company are free of trojan timebombs or other harmful components Some jurisdictions do not allow the exclusion of certain types of warranties or limitations on applicable statutory rights of a so some or all of the above exclusions and limitations may not apply to You But in such a case the exclusions and limitations set forth in this section shall be applied to the greatest extent enforceable under applicable law To the extent any warranty exists under law that cannot be the Company shall be solely responsible for such warranty Severability and Waiver Severability If any provision of this Agreement is held to be unenforceable or such provision will be changed and interpreted to accomplish the objectives of such provision to the greatest extent possible under applicable law and the remaining provisions will continue in full force and effect Waiver Except as provided the failure to exercise a right or to require performance of an obligation under this Agreement shall not affect a party s ability to exercise such right or require such performance at any time thereafter nor shall the waiver of a breach constitute waiver of any subsequent breach Product Claims The Company does not make any warranties concerning the Software United States Legal Compliance You represent and warrant or that has been designated by the United States government as a terrorist supporting at its sole to modify or replace this Agreement at any time If a revision is material we will provide at least days notice prior to any new terms taking effect What constitutes a material change will be determined at the sole discretion of the Company By continuing to access or use the Software after any revisions become You agree to be bound by the revised terms If You do not agree to the new You are no longer authorized to use the Software Governing Law The laws of the State of United States of excluding its conflicts of law shall govern this Agreement and your use of the Software Your use of the Software may also be subject to other state
Definition: CTRE_LICENSE.txt:283
Class for getting information about an available CAN bus.
Definition: CANBus.hpp:19
T GetValue() const
Gets the cached value from this status signal.
Definition: StatusSignal.hpp:783
Class for CANcoder, a CAN based magnetic encoder that provides absolute and relative position along w...
Definition: CoreCANcoder.hpp:592
Class description for the Talon FX integrated motor controller.
Definition: CoreTalonFX.hpp:2813
ctre::phoenix::StatusCode SetPosition(units::angle::turn_t newValue, units::time::second_t timeoutSeconds)
Sets the mechanism position of the device in mechanism rotations.
Definition: CoreTalonFX.hpp:10018
ctre::phoenix::StatusCode SetControl(controls::DutyCycleOut &request)
Request a specified motor duty cycle.
The state of the motor controller bridge when output is neutral or disabled.
Definition: SpnEnums.hpp:1698
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:27
SwerveModuleState GetCurrentState() const
Get the current state of the module.
Definition: SwerveModuleImpl.hpp:153
DriveRequestType
All possible control requests for the module drive motor.
Definition: SwerveModuleImpl.hpp:48
hardware::core::CoreTalonFX & GetSteerMotor()
Gets this module's Steer Motor TalonFX reference.
Definition: SwerveModuleImpl.hpp:194
hardware::core::CoreCANcoder & GetCANcoder()
Gets this module's CANcoder reference.
Definition: SwerveModuleImpl.hpp:204
SteerRequestType
All possible control requests for the module steer motor.
Definition: SwerveModuleImpl.hpp:32
@ MotionMagicExpo
Control the drive motor using a Motion Magic® Expo 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:165
void Apply(DriveReq &&driveRequest, SteerReq &&steerRequest)
Controls this module using the specified drive and steer control requests.
Definition: SwerveModuleImpl.hpp:112
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:184
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:170
SwerveModulePosition GetCachedPosition() const
Gets the last cached swerve module position.
Definition: SwerveModuleImpl.hpp:143
void Apply(SwerveModuleState const &state, DriveRequestType driveRequestType, SteerRequestType steerRequestType=SteerRequestType::MotionMagicExpo)
Applies the desired SwerveModuleState to this module.
Status codes reported by APIs, including OK, warnings, and errors.
Definition: StatusCodes.h:27
ClosedLoopOutputType
Supported closed-loop output types.
Definition: SwerveModuleConstants.hpp:23
Represents the state of one swerve module.
Definition: StatusCodes.h:18
All constants for a swerve module.
Definition: SwerveModuleConstants.hpp:53
Represents the position of one swerve module.
Definition: SwerveDriveKinematics.hpp:63
Definition: SwerveDriveKinematics.hpp:23