CTRE Phoenix 6 C++ 25.0.0-beta-4
Loading...
Searching...
No Matches
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
39 /**
40 * \brief Contains everything the swerve module needs to apply a request.
41 */
43
44protected:
45 /** \brief Number of times to attempt config applies. */
46 static constexpr int kNumConfigAttempts = 2;
47
48 /** \brief The underlying swerve module instance. */
50
51private:
52 hardware::TalonFX _driveMotor;
53 hardware::TalonFX _steerMotor;
54 hardware::CANcoder _cancoder;
55
56public:
57 /**
58 * \brief Construct a SwerveModule with the specified constants.
59 *
60 * \param constants Constants used to construct the module
61 * \param canbusName The name of the CAN bus this module is on
62 * \param module The impl#SwerveModuleImpl to use
63 */
64 SwerveModule(SwerveModuleConstants const &constants, std::string_view canbusName, impl::SwerveModuleImpl &module);
65
66 virtual ~SwerveModule() = default;
67
68 /**
69 * \brief Applies the desired ModuleRequest to this module.
70 *
71 * \param moduleRequest The request to apply to this module
72 */
73 virtual void Apply(ModuleRequest const &moduleRequest)
74 {
75 return _module->Apply(moduleRequest);
76 }
77
78 /**
79 * \brief Controls this module using the specified drive and steer control requests.
80 *
81 * This is intended only to be used for characterization of the robot; do not use this for normal use.
82 *
83 * \param driveRequest The control request to apply to the drive motor
84 * \param steerRequest The control request to apply to the steer motor
85 */
86 template <typename DriveReq, typename SteerReq>
87 void Apply(DriveReq &&driveRequest, SteerReq &&steerRequest)
88 {
89 return _module->Apply(std::forward<DriveReq>(driveRequest), std::forward<SteerReq>(steerRequest));
90 }
91
92 /**
93 * \brief Gets the state of this module and passes it back as a
94 * SwerveModulePosition object with latency compensated values.
95 *
96 * This function is blocking when it performs a refresh.
97 *
98 * \param refresh True if the signals should be refreshed
99 * \returns SwerveModulePosition containing this module's state.
100 */
101 SwerveModulePosition GetPosition(bool refresh)
102 {
103 return _module->GetPosition(refresh);
104 }
105
106 /**
107 * \brief Gets the last cached swerve module position.
108 * This differs from #GetPosition in that it will not
109 * perform any latency compensation or refresh the signals.
110 *
111 * \returns Last cached SwerveModulePosition
112 */
113 SwerveModulePosition GetCachedPosition() const
114 {
115 return _module->GetCachedPosition();
116 }
117
118 /**
119 * \brief Get the current state of the module.
120 *
121 * This is typically used for telemetry, as the SwerveModulePosition
122 * is used for odometry.
123 *
124 * \returns Current state of the module
125 */
126 SwerveModuleState GetCurrentState() const
127 {
128 return _module->GetCurrentState();
129 }
130
131 /**
132 * \brief Get the target state of the module.
133 *
134 * This is typically used for telemetry.
135 *
136 * \returns Target state of the module
137 */
138 SwerveModuleState GetTargetState() const
139 {
140 return _module->GetTargetState();
141 }
142
143 /**
144 * \brief Resets this module's drive motor position to 0 rotations.
145 */
146 virtual void ResetPosition()
147 {
148 return _module->ResetPosition();
149 }
150
151 /**
152 * \brief Gets this module's Drive Motor TalonFX reference.
153 *
154 * This should be used only to access signals and change configurations that the
155 * swerve drivetrain does not configure itself.
156 *
157 * \returns This module's Drive Motor reference
158 */
160 {
161 return _driveMotor;
162 }
163
164 /**
165 * \brief Gets this module's Steer Motor TalonFX reference.
166 *
167 * This should be used only to access signals and change configurations that the
168 * swerve drivetrain does not configure itself.
169 *
170 * \returns This module's Steer Motor reference
171 */
173 {
174 return _steerMotor;
175 }
176
177 /**
178 * \brief Gets this module's CANcoder reference.
179 *
180 * This should be used only to access signals and change configurations that the
181 * swerve drivetrain does not configure itself.
182 *
183 * \returns This module's CANcoder reference
184 */
186 {
187 return _cancoder;
188 }
189};
190
191}
192}
193}
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:30
Swerve Module class that encapsulates a swerve module powered by CTR Electronics devices.
Definition SwerveModule.hpp:28
SwerveModulePosition GetCachedPosition() const
Gets the last cached swerve module position.
Definition SwerveModule.hpp:113
virtual void Apply(ModuleRequest const &moduleRequest)
Applies the desired ModuleRequest to this module.
Definition SwerveModule.hpp:73
void Apply(DriveReq &&driveRequest, SteerReq &&steerRequest)
Controls this module using the specified drive and steer control requests.
Definition SwerveModule.hpp:87
SwerveModulePosition GetPosition(bool refresh)
Gets the state of this module and passes it back as a SwerveModulePosition object with latency compen...
Definition SwerveModule.hpp:101
SwerveModuleState GetCurrentState() const
Get the current state of the module.
Definition SwerveModule.hpp:126
SwerveModuleState GetTargetState() const
Get the target state of the module.
Definition SwerveModule.hpp:138
SwerveModule(SwerveModuleConstants const &constants, std::string_view canbusName, impl::SwerveModuleImpl &module)
Construct a SwerveModule with the specified constants.
hardware::TalonFX & GetSteerMotor()
Gets this module's Steer Motor TalonFX reference.
Definition SwerveModule.hpp:172
hardware::TalonFX & GetDriveMotor()
Gets this module's Drive Motor TalonFX reference.
Definition SwerveModule.hpp:159
impl::SwerveModuleImpl * _module
The underlying swerve module instance.
Definition SwerveModule.hpp:49
virtual void ResetPosition()
Resets this module's drive motor position to 0 rotations.
Definition SwerveModule.hpp:146
static constexpr int kNumConfigAttempts
Number of times to attempt config applies.
Definition SwerveModule.hpp:46
hardware::CANcoder & GetCANcoder()
Gets this module's CANcoder reference.
Definition SwerveModule.hpp:185
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
SteerRequestType
All possible control requests for the module steer motor.
Definition SwerveModuleImpl.hpp:34
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.
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:350
SwerveModulePosition GetCachedPosition() const
Gets the last cached swerve module position.
Definition SwerveModuleImpl.hpp:323
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