CTRE Phoenix 6 C++ 24.50.0-alpha-2
SwerveModule.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
13namespace ctre {
14namespace phoenix6 {
15namespace swerve {
16
17/**
18 * \brief Swerve Module class that encapsulates a swerve module powered by
19 * CTR Electronics devices.
20 *
21 * This class handles the hardware devices and configures them for
22 * swerve module operation using the Phoenix 6 API.
23 *
24 * This class constructs hardware devices internally, so the user
25 * only specifies the constants (IDs, PID gains, gear ratios, etc).
26 * Getters for these hardware devices are available.
27 */
29public:
30 /**
31 * \brief All possible control requests for the module steer motor.
32 */
34 /**
35 * \brief All possible control requests for the module drive motor.
36 */
38
39private:
41
42 hardware::TalonFX _driveMotor;
43 hardware::TalonFX _steerMotor;
44 hardware::CANcoder _cancoder;
45
46public:
47 /**
48 * \brief Construct a SwerveModule with the specified constants.
49 *
50 * \param constants Constants used to construct the module
51 * \param canbusName The name of the CAN bus this module is on
52 * \param module The impl#SwerveModuleImpl to use
53 */
54 SwerveModule(SwerveModuleConstants const &constants, std::string_view canbusName, impl::SwerveModuleImpl &module);
55
56 virtual ~SwerveModule() = default;
57
58 /**
59 * \brief Applies the desired SwerveModuleState to this module.
60 *
61 * \param state Speed and direction the module should target
62 * \param driveRequestType The #DriveRequestType to apply
63 * \param steerRequestType The #SteerRequestType to apply; defaults to SteerRequestType#MotionMagicExpo
64 */
66 {
67 return _module->Apply(state, driveRequestType, steerRequestType);
68 }
69
70 /**
71 * \brief Controls this module using the specified drive and steer control requests.
72 *
73 * This is intended only to be used for characterization of the robot; do not use this for normal use.
74 *
75 * \param driveRequest The control request to apply to the drive motor
76 * \param steerRequest The control request to apply to the steer motor
77 */
78 template <typename DriveReq, typename SteerReq>
79 void Apply(DriveReq &&driveRequest, SteerReq &&steerRequest)
80 {
81 return _module->Apply(std::forward<DriveReq>(driveRequest), std::forward<SteerReq>(steerRequest));
82 }
83
84 /**
85 * \brief Gets the state of this module and passes it back as a
86 * SwerveModulePosition object with latency compensated values.
87 *
88 * \param refresh True if the signals should be refreshed
89 * \returns SwerveModulePosition containing this module's state.
90 */
92 {
93 return _module->GetPosition(refresh);
94 }
95
96 /**
97 * \brief Gets the last cached swerve module position.
98 * This differs from #GetPosition in that it will not
99 * perform any latency compensation or refresh the signals.
100 *
101 * \returns Last cached SwerveModulePosition
102 */
104 {
105 return _module->GetCachedPosition();
106 }
107
108 /**
109 * \brief Get the current state of the module.
110 *
111 * This is typically used for telemetry, as the SwerveModulePosition
112 * is used for odometry.
113 *
114 * \returns Current state of the module
115 */
117 {
118 return _module->GetCurrentState();
119 }
120
121 /**
122 * \brief Get the target state of the module.
123 *
124 * This is typically used for telemetry.
125 *
126 * \returns Target state of the module
127 */
129 {
130 return _module->GetTargetState();
131 }
132
133 /**
134 * Resets this module's drive motor position to 0 rotations.
135 */
137 {
138 return _module->ResetPosition();
139 }
140
141 /**
142 * Gets this module's Drive Motor TalonFX reference.
143 *
144 * This should be used only to access signals and change configurations that the
145 * swerve drivetrain does not configure itself.
146 *
147 * \returns This module's Drive Motor reference
148 */
150 {
151 return _driveMotor;
152 }
153
154 /**
155 * Gets this module's Steer Motor TalonFX reference.
156 *
157 * This should be used only to access signals and change configurations that the
158 * swerve drivetrain does not configure itself.
159 *
160 * \returns This module's Steer Motor reference
161 */
163 {
164 return _steerMotor;
165 }
166
167 /**
168 * Gets this module's CANcoder reference.
169 *
170 * This should be used only to access signals and change configurations that the
171 * swerve drivetrain does not configure itself.
172 *
173 * \returns This module's CANcoder reference
174 */
176 {
177 return _cancoder;
178 }
179};
180
181}
182}
183}
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 CANcoder, a CAN based magnetic encoder that provides absolute and relative position along w...
Definition: CANcoder.hpp:27
Class description for the Talon FX integrated motor controller.
Definition: TalonFX.hpp:32
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.
Definition: SwerveModule.hpp:28
void Apply(impl::SwerveModuleState const &state, DriveRequestType driveRequestType, SteerRequestType steerRequestType=SteerRequestType::MotionMagicExpo)
Applies the desired SwerveModuleState to this module.
Definition: SwerveModule.hpp:65
void Apply(DriveReq &&driveRequest, SteerReq &&steerRequest)
Controls this module using the specified drive and steer control requests.
Definition: SwerveModule.hpp:79
impl::SwerveModuleState GetCurrentState() const
Get the current state of the module.
Definition: SwerveModule.hpp:116
SwerveModule(SwerveModuleConstants const &constants, std::string_view canbusName, impl::SwerveModuleImpl &module)
Construct a SwerveModule with the specified constants.
impl::SwerveModuleState GetTargetState() const
Get the target state of the module.
Definition: SwerveModule.hpp:128
void ResetPosition()
Resets this module's drive motor position to 0 rotations.
Definition: SwerveModule.hpp:136
hardware::TalonFX & GetSteerMotor()
Gets this module's Steer Motor TalonFX reference.
Definition: SwerveModule.hpp:162
impl::SwerveModulePosition GetCachedPosition() const
Gets the last cached swerve module position.
Definition: SwerveModule.hpp:103
hardware::TalonFX & GetDriveMotor()
Gets this module's Drive Motor TalonFX reference.
Definition: SwerveModule.hpp:149
hardware::CANcoder & GetCANcoder()
Gets this module's CANcoder reference.
Definition: SwerveModule.hpp:175
impl::SwerveModulePosition GetPosition(bool refresh)
Gets the state of this module and passes it back as a SwerveModulePosition object with latency compen...
Definition: SwerveModule.hpp:91
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
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.
SwerveModuleState GetTargetState() const
Get the target state of the module.
Definition: SwerveModuleImpl.hpp:165
SwerveModulePosition GetPosition(bool refresh)
Gets the state of this module and passes it back as a SwerveModulePosition object with latency compen...
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.
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