2025-Robot
Robot code for 2025 FRC season by Argos, FRC team #1756
Loading...
Searching...
No Matches
talonsrx_config.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <ctre/phoenix/motorcontrol/can/TalonSRX.h>
8#include <units/current.h>
9#include <units/time.h>
10#include <units/voltage.h>
11
14#include "status_frame_config.h"
15
16namespace argos_lib {
17 namespace talonsrx_config {
18 HAS_MEMBER(inverted)
19 HAS_MEMBER(neutralMode)
20 HAS_MEMBER(pid0_allowableError)
21 HAS_MEMBER(pid0_iZone)
22 HAS_MEMBER(pid0_kD)
23 HAS_MEMBER(pid0_kF)
24 HAS_MEMBER(pid0_kI)
25 HAS_MEMBER(pid0_kP)
26 HAS_MEMBER(pid0_selectedSensor)
27 HAS_MEMBER(remoteFilter0_addr)
28 HAS_MEMBER(remoteFilter0_type)
29 HAS_MEMBER(sensorPhase)
30 HAS_MEMBER(voltCompSat)
31 HAS_MEMBER(statusFrameMotorMode)
32 HAS_MEMBER(peakCurrentLimit)
33 HAS_MEMBER(peakCurrentDuration)
34 HAS_MEMBER(continuousCurrentLimit)
35 HAS_MEMBER(peakOutputForward)
36 HAS_MEMBER(peakOutputReverse)
37 HAS_MEMBER(forwardLimitSwitchSource)
38 HAS_MEMBER(reverseLimitSwitchSource)
39 HAS_MEMBER(forwardLimitSwitchNormal)
40 HAS_MEMBER(reverseLimitSwitchNormal)
74 template <typename T>
75 bool TalonSRXConfig(ctre::phoenix::motorcontrol::can::TalonSRX& motorController,
76 units::millisecond_t configTimeout) {
77 ctre::phoenix::motorcontrol::can::TalonSRXConfiguration config;
78 auto timeout = configTimeout.to<int>();
79
80 if constexpr (has_inverted<T>{}) {
81 motorController.SetInverted(T::inverted);
82 }
83 if constexpr (has_sensorPhase<T>{}) {
84 motorController.SetSensorPhase(T::sensorPhase);
85 }
86 if constexpr (has_neutralMode<T>{}) {
87 motorController.SetNeutralMode(T::neutralMode);
88 }
89 if constexpr (has_voltCompSat<T>{}) {
90 constexpr units::volt_t voltage = T::voltCompSat;
91 config.voltageCompSaturation = voltage.to<double>();
92 motorController.EnableVoltageCompensation(true);
93 } else {
94 motorController.EnableVoltageCompensation(false);
95 }
97 ctre::phoenix::motorcontrol::can::FilterConfiguration filterConfig;
98 filterConfig.remoteSensorDeviceID = T::remoteFilter0_addr.address;
99 filterConfig.remoteSensorSource = T::remoteFilter0_type;
100 config.remoteFilter0 = filterConfig;
101 }
102 if constexpr (has_pid0_selectedSensor<T>{}) {
103 config.primaryPID.selectedFeedbackSensor = T::pid0_selectedSensor;
104 }
105 if constexpr (has_pid0_kP<T>{}) {
106 config.slot0.kP = T::pid0_kP;
107 }
108 if constexpr (has_pid0_kI<T>{}) {
109 config.slot0.kI = T::pid0_kI;
110 }
111 if constexpr (has_pid0_kD<T>{}) {
112 config.slot0.kD = T::pid0_kD;
113 }
114 if constexpr (has_pid0_kF<T>{}) {
115 config.slot0.kF = T::pid0_kF;
116 }
117 if constexpr (has_pid0_iZone<T>{}) {
118 config.slot0.integralZone = T::pid0_iZone;
119 }
120 if constexpr (has_pid0_allowableError<T>{}) {
121 config.slot0.allowableClosedloopError = T::pid0_allowableError;
122 }
123 if constexpr (has_peakCurrentLimit<T>()) {
124 constexpr units::ampere_t currentLimit = T::peakCurrentLimit;
125 static_assert(currentLimit.to<double>() > 0, "Current limit must be positive");
126 config.peakCurrentLimit = std::round(currentLimit.to<double>());
127 }
128 if constexpr (has_peakCurrentDuration<T>()) {
129 constexpr units::millisecond_t currentDuration = T::peakCurrentDuration;
130 static_assert(currentDuration.to<double>() > 0, "Current duration must be positive");
131 config.peakCurrentDuration = std::round(currentDuration.to<double>());
132 }
133 if constexpr (has_continuousCurrentLimit<T>()) {
134 constexpr units::ampere_t currentLimit = T::continuousCurrentLimit;
135 static_assert(currentLimit.to<double>() > 0, "Current limit must be positive");
136 config.continuousCurrentLimit = std::round(currentLimit.to<double>());
137 }
138 if constexpr (has_peakOutputForward<T>()) {
139 config.peakOutputForward = T::peakOutputForward;
140 }
141 if constexpr (has_peakOutputReverse<T>()) {
142 config.peakOutputReverse = T::peakOutputReverse;
143 }
144
145 if constexpr (has_statusFrameMotorMode<T>()) {
146 argos_lib::status_frame_config::SetMotorStatusFrameRates(motorController, T::statusFrameMotorMode);
147 }
148
149 if constexpr (has_forwardLimitSwitchSource<T>()) {
150 config.forwardLimitSwitchSource = T::forwardLimitSwitchSource;
151 }
152
153 if constexpr (has_reverseLimitSwitchSource<T>()) {
154 config.reverseLimitSwitchSource = T::reverseLimitSwitchSource;
155 }
156
157 if constexpr (has_forwardLimitSwitchNormal<T>()) {
158 config.forwardLimitSwitchNormal = T::forwardLimitSwitchNormal;
159 }
160
161 if constexpr (has_reverseLimitSwitchNormal<T>()) {
162 config.reverseLimitSwitchNormal = T::reverseLimitSwitchNormal;
163 }
164
165 // enable current limiting if any current limiting option is set, disable if none are
167 motorController.EnableCurrentLimit(true);
168 } else {
169 motorController.EnableCurrentLimit(false);
170 }
171
172 // ensure using single threshold if peakCurrentLimit is not set but continuousCurrentLimit is
174 config.peakCurrentLimit = 0;
175 }
176
177 return 0 != motorController.ConfigAllSettings(config, timeout);
178 }
179
191 template <typename CompetitionConfig, typename PracticeConfig>
192 bool TalonSRXConfig(ctre::phoenix::motorcontrol::can::TalonSRX& motorController,
193 units::millisecond_t configTimeout,
194 argos_lib::RobotInstance instance) {
195 switch (instance) {
197 return TalonSRXConfig<CompetitionConfig>(motorController, configTimeout);
198 break;
200 return TalonSRXConfig<PracticeConfig>(motorController, configTimeout);
201 break;
202 }
203 return false;
204 }
205
206 } // namespace talonsrx_config
207} // namespace argos_lib
#define HAS_MEMBER(X)
Helper function generator to detect if a namespace has a member defined.
Definition compile_time_member_check.h:14
void SetMotorStatusFrameRates(BaseTalon &motor, MotorPresetMode motorMode)
Set motor controller status frame update periods based on the motor preset.
Definition status_frame_config.cpp:9
bool TalonSRXConfig(ctre::phoenix::motorcontrol::can::TalonSRX &motorController, units::millisecond_t configTimeout)
Configures a CTRE TalonSRX with only the fields provided. All other fields are given the factory defa...
Definition talonsrx_config.h:75
Definition swap_controllers_command.h:12
RobotInstance
Differentiates between practice robot and competition robot.
Definition config_types.h:13
@ Competition
Competition robot.
@ Practice
Practice robot.
Definition custom_units.h:11
Definition talonsrx_config.h:18
Definition talonsrx_config.h:19
Definition talonsrx_config.h:21
Definition talonsrx_config.h:22
Definition talonsrx_config.h:23
Definition talonsrx_config.h:24
Definition talonsrx_config.h:25
Definition talonsrx_config.h:29
Definition talonsrx_config.h:30