blob: 6f5908ca5310d953d9114062e07463c5ece8af38 [file] [log] [blame]
Ravago Jones486de802021-05-19 20:47:55 -07001#include <unistd.h>
2
3#include <array>
4#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <cinttypes>
Ravago Jones486de802021-05-19 20:47:55 -07006#include <cmath>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07007#include <cstdio>
8#include <cstring>
Ravago Jones486de802021-05-19 20:47:55 -07009#include <functional>
Tyler Chatowbf0609c2021-07-31 16:13:27 -070010#include <memory>
Ravago Jones486de802021-05-19 20:47:55 -070011#include <mutex>
12#include <thread>
13
14#include "ctre/phoenix/CANifier.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070015
Ravago Jones486de802021-05-19 20:47:55 -070016#include "frc971/wpilib/ahal/AnalogInput.h"
17#include "frc971/wpilib/ahal/Counter.h"
18#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
19#include "frc971/wpilib/ahal/DriverStation.h"
20#include "frc971/wpilib/ahal/Encoder.h"
21#include "frc971/wpilib/ahal/TalonFX.h"
22#include "frc971/wpilib/ahal/VictorSP.h"
23#undef ERROR
24
Maxwell Henderson1c0843c2023-12-22 16:20:59 -080025#include "ctre/phoenix6/TalonFX.hpp"
Philipp Schrader790cb542023-07-05 21:06:52 -070026
Ravago Jones486de802021-05-19 20:47:55 -070027#include "aos/commonmath.h"
28#include "aos/events/event_loop.h"
29#include "aos/events/shm_event_loop.h"
30#include "aos/init.h"
31#include "aos/logging/logging.h"
Ravago Jones486de802021-05-19 20:47:55 -070032#include "aos/realtime.h"
Ravago Jones486de802021-05-19 20:47:55 -070033#include "aos/time/time.h"
34#include "aos/util/log_interval.h"
35#include "aos/util/phased_loop.h"
36#include "aos/util/wrapping_counter.h"
Ravago Jones486de802021-05-19 20:47:55 -070037#include "frc971/autonomous/auto_mode_generated.h"
38#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070039#include "frc971/input/robot_state_generated.h"
Ravago Jones486de802021-05-19 20:47:55 -070040#include "frc971/wpilib/ADIS16448.h"
41#include "frc971/wpilib/buffered_pcm.h"
42#include "frc971/wpilib/buffered_solenoid.h"
43#include "frc971/wpilib/dma.h"
44#include "frc971/wpilib/drivetrain_writer.h"
45#include "frc971/wpilib/encoder_and_potentiometer.h"
46#include "frc971/wpilib/joystick_sender.h"
47#include "frc971/wpilib/logging_generated.h"
48#include "frc971/wpilib/loop_output_handler.h"
49#include "frc971/wpilib/pdp_fetcher.h"
50#include "frc971/wpilib/sensor_reader.h"
51#include "frc971/wpilib/wpilib_robot_base.h"
52#include "y2021_bot3/constants.h"
53#include "y2021_bot3/control_loops/superstructure/superstructure_output_generated.h"
54#include "y2021_bot3/control_loops/superstructure/superstructure_position_generated.h"
55
56using ::aos::monotonic_clock;
57using ::y2021_bot3::constants::Values;
58namespace superstructure = ::y2021_bot3::control_loops::superstructure;
59namespace chrono = ::std::chrono;
Vinay Sivae52a6b32021-07-10 15:19:26 -070060using std::make_unique;
Ravago Jones486de802021-05-19 20:47:55 -070061
62namespace y2021_bot3 {
63namespace wpilib {
64namespace {
65
66constexpr double kMaxBringupPower = 12.0;
67
68// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
69// DMA stuff and then removing the * 2.0 in *_translate.
70// The low bit is direction.
71
72// TODO(brian): Use ::std::max instead once we have C++14 so that can be
73// constexpr.
74template <typename T>
75constexpr T max(T a, T b) {
76 return (a > b) ? a : b;
77}
78
79template <typename T, typename... Rest>
80constexpr T max(T a, T b, T c, Rest... rest) {
81 return max(max(a, b), c, rest...);
82}
83
84double drivetrain_translate(int32_t in) {
85 return ((static_cast<double>(in) /
86 Values::kDrivetrainEncoderCountsPerRevolution()) *
87 (2.0 * M_PI)) *
88 Values::kDrivetrainEncoderRatio() *
89 control_loops::drivetrain::kWheelRadius;
90}
91
92double drivetrain_velocity_translate(double in) {
93 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
94 (2.0 * M_PI)) *
95 Values::kDrivetrainEncoderRatio() *
96 control_loops::drivetrain::kWheelRadius;
97}
98
99constexpr double kMaxFastEncoderPulsesPerSecond =
100 Values::kMaxDrivetrainEncoderPulsesPerSecond();
101static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
102 "fast encoders are too fast");
103constexpr double kMaxMediumEncoderPulsesPerSecond =
104 kMaxFastEncoderPulsesPerSecond;
105
106static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
107 "medium encoders are too fast");
108
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800109void PrintConfigs(ctre::phoenix6::hardware::TalonFX *talon) {
110 ctre::phoenix6::configs::TalonFXConfiguration configuration;
111 ctre::phoenix::StatusCode status =
112 talon->GetConfigurator().Refresh(configuration);
113 if (!status.IsOK()) {
114 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
115 status.GetName(), status.GetDescription());
116 }
117 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
118}
119
120void WriteConfigs(ctre::phoenix6::hardware::TalonFX *talon,
121 double stator_current_limit, double supply_current_limit) {
122 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
123 current_limits.StatorCurrentLimit = stator_current_limit;
124 current_limits.StatorCurrentLimitEnable = true;
125 current_limits.SupplyCurrentLimit = supply_current_limit;
126 current_limits.SupplyCurrentLimitEnable = true;
127
128 ctre::phoenix6::configs::TalonFXConfiguration configuration;
129 configuration.CurrentLimits = current_limits;
130
131 ctre::phoenix::StatusCode status =
132 talon->GetConfigurator().Apply(configuration);
133 if (!status.IsOK()) {
134 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
135 status.GetName(), status.GetDescription());
136 }
137
138 PrintConfigs(talon);
139}
140
141void Disable(ctre::phoenix6::hardware::TalonFX *talon) {
142 ctre::phoenix6::controls::DutyCycleOut stop_command(0.0);
143 stop_command.UpdateFreqHz = 0_Hz;
144 stop_command.EnableFOC = true;
145
146 talon->SetControl(stop_command);
147}
148
Ravago Jones486de802021-05-19 20:47:55 -0700149} // namespace
150
151// Class to send position messages with sensor readings to our loops.
152class SensorReader : public ::frc971::wpilib::SensorReader {
153 public:
154 SensorReader(::aos::ShmEventLoop *event_loop)
155 : ::frc971::wpilib::SensorReader(event_loop),
156 auto_mode_sender_(
157 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
158 "/autonomous")),
159 superstructure_position_sender_(
160 event_loop->MakeSender<superstructure::Position>(
161 "/superstructure")),
162 drivetrain_position_sender_(
163 event_loop
164 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
165 "/drivetrain")) {
166 // Set to filter out anything shorter than 1/4 of the minimum pulse width
167 // we should ever see.
168 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
169 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
170 }
171
172 // Auto mode switches.
173 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
174 autonomous_modes_.at(i) = ::std::move(sensor);
175 }
176
177 void RunIteration() override {
178 {
179 auto builder = drivetrain_position_sender_.MakeBuilder();
180 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
181 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
182 drivetrain_builder.add_left_encoder(
183 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
184 drivetrain_builder.add_left_speed(
185 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
186
187 drivetrain_builder.add_right_encoder(
188 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
189 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
190 drivetrain_right_encoder_->GetPeriod()));
191
milind1f1dca32021-07-03 13:50:07 -0700192 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700193 }
194
195 {
196 auto builder = superstructure_position_sender_.MakeBuilder();
197 superstructure::Position::Builder position_builder =
198 builder.MakeBuilder<superstructure::Position>();
milind1f1dca32021-07-03 13:50:07 -0700199 builder.CheckOk(builder.Send(position_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700200 }
201
202 {
203 auto builder = auto_mode_sender_.MakeBuilder();
204
205 uint32_t mode = 0;
206 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
207 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
208 mode |= 1 << i;
209 }
210 }
211
212 auto auto_mode_builder =
213 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
214
215 auto_mode_builder.add_mode(mode);
216
milind1f1dca32021-07-03 13:50:07 -0700217 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700218 }
219 }
220
221 private:
222 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
223 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
224 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
225 drivetrain_position_sender_;
226
227 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
228};
229
230class SuperstructureWriter
231 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
232 public:
233 SuperstructureWriter(::aos::EventLoop *event_loop)
234 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
235 event_loop, "/superstructure") {}
236
Vinay Siva62e3d322021-08-14 17:37:51 -0700237 void set_intake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800238 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Vinay Siva62e3d322021-08-14 17:37:51 -0700239 intake_falcon_ = ::std::move(t);
Vinay Siva7cea8a82021-09-25 15:06:28 -0700240 ConfigureRollerFalcon(intake_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700241 }
242
243 void set_outtake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800244 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Vinay Siva62e3d322021-08-14 17:37:51 -0700245 outtake_falcon_ = ::std::move(t);
Vinay Siva7cea8a82021-09-25 15:06:28 -0700246 ConfigureRollerFalcon(outtake_falcon_.get());
247 }
248
249 void set_climber_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800250 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Vinay Siva7cea8a82021-09-25 15:06:28 -0700251 climber_falcon_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800252 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
253 current_limits.SupplyCurrentLimit = Values::kClimberSupplyCurrentLimit();
254 current_limits.SupplyCurrentLimitEnable = true;
255
256 ctre::phoenix6::configs::TalonFXConfiguration configuration;
257 configuration.CurrentLimits = current_limits;
258
259 ctre::phoenix::StatusCode status =
260 climber_falcon_->GetConfigurator().Apply(configuration);
261 if (!status.IsOK()) {
262 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
263 status.GetName(), status.GetDescription());
264 }
265
266 PrintConfigs(climber_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700267 }
268
Ravago Jones486de802021-05-19 20:47:55 -0700269 private:
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800270 void ConfigureRollerFalcon(::ctre::phoenix6::hardware::TalonFX *falcon) {
271 WriteConfigs(falcon, Values::kRollerSupplyCurrentLimit(),
272 Values::kRollerStatorCurrentLimit());
Vinay Siva62e3d322021-08-14 17:37:51 -0700273 }
274
275 void WriteToFalcon(const double voltage,
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800276 ::ctre::phoenix6::hardware::TalonFX *falcon) {
277 ctre::phoenix6::controls::DutyCycleOut control(
Vinay Siva62e3d322021-08-14 17:37:51 -0700278 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800279 control.UpdateFreqHz = 0_Hz;
280 control.EnableFOC = true;
281
282 falcon->SetControl(control);
Vinay Siva62e3d322021-08-14 17:37:51 -0700283 }
284
285 void Write(const superstructure::Output &output) override {
286 WriteToFalcon(output.intake_volts(), intake_falcon_.get());
287 WriteToFalcon(output.outtake_volts(), outtake_falcon_.get());
Vinay Siva7cea8a82021-09-25 15:06:28 -0700288
289 WriteToFalcon(-output.climber_volts(), climber_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700290 }
Ravago Jones486de802021-05-19 20:47:55 -0700291
Vinay Siva7cea8a82021-09-25 15:06:28 -0700292 void Stop() override {
293 AOS_LOG(WARNING, "Superstructure output too old.\n");
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800294 Disable(climber_falcon_.get());
295 Disable(intake_falcon_.get());
296 Disable(outtake_falcon_.get());
Vinay Siva7cea8a82021-09-25 15:06:28 -0700297 }
Vinay Siva62e3d322021-08-14 17:37:51 -0700298
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800299 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> intake_falcon_,
Vinay Siva62e3d322021-08-14 17:37:51 -0700300 outtake_falcon_;
Vinay Siva7cea8a82021-09-25 15:06:28 -0700301
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800302 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> climber_falcon_;
Ravago Jones486de802021-05-19 20:47:55 -0700303};
304
305class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
306 public:
307 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
308 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
309 frc::Encoder::k4X);
310 }
311
312 void Run() override {
313 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800314 aos::configuration::ReadConfig("aos_config.json");
Ravago Jones486de802021-05-19 20:47:55 -0700315
316 // Thread 1.
317 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
318 ::frc971::wpilib::JoystickSender joystick_sender(
319 &joystick_sender_event_loop);
320 AddLoop(&joystick_sender_event_loop);
321
322 // Thread 2.
323 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
324 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
325 AddLoop(&pdp_fetcher_event_loop);
326
327 // Thread 3.
328 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
329 SensorReader sensor_reader(&sensor_reader_event_loop);
330 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
331 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
332
333 AddLoop(&sensor_reader_event_loop);
334
335 // Thread 4.
336 ::aos::ShmEventLoop output_event_loop(&config.message());
337 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
338 drivetrain_writer.set_left_controller0(
339 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
340 drivetrain_writer.set_right_controller0(
341 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
342
343 SuperstructureWriter superstructure_writer(&output_event_loop);
Vinay Siva62e3d322021-08-14 17:37:51 -0700344 superstructure_writer.set_intake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800345 make_unique<::ctre::phoenix6::hardware::TalonFX>(0));
Vinay Siva62e3d322021-08-14 17:37:51 -0700346 superstructure_writer.set_outtake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800347 make_unique<::ctre::phoenix6::hardware::TalonFX>(1));
Vinay Siva7cea8a82021-09-25 15:06:28 -0700348 superstructure_writer.set_climber_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800349 make_unique<::ctre::phoenix6::hardware::TalonFX>(2));
Ravago Jones486de802021-05-19 20:47:55 -0700350
351 AddLoop(&output_event_loop);
352
353 RunLoops();
354 }
355};
356
357} // namespace wpilib
358} // namespace y2021_bot3
359
360AOS_ROBOT_CLASS(::y2021_bot3::wpilib::WPILibRobot);