blob: f3c956ee881143fc455e3104788f4fb90b53f9ff [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080062namespace y2021_bot3::wpilib {
Ravago Jones486de802021-05-19 20:47:55 -070063namespace {
64
65constexpr double kMaxBringupPower = 12.0;
66
67// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
68// DMA stuff and then removing the * 2.0 in *_translate.
69// The low bit is direction.
70
71// TODO(brian): Use ::std::max instead once we have C++14 so that can be
72// constexpr.
73template <typename T>
74constexpr T max(T a, T b) {
75 return (a > b) ? a : b;
76}
77
78template <typename T, typename... Rest>
79constexpr T max(T a, T b, T c, Rest... rest) {
80 return max(max(a, b), c, rest...);
81}
82
83double drivetrain_translate(int32_t in) {
84 return ((static_cast<double>(in) /
85 Values::kDrivetrainEncoderCountsPerRevolution()) *
86 (2.0 * M_PI)) *
87 Values::kDrivetrainEncoderRatio() *
88 control_loops::drivetrain::kWheelRadius;
89}
90
91double drivetrain_velocity_translate(double in) {
92 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
93 (2.0 * M_PI)) *
94 Values::kDrivetrainEncoderRatio() *
95 control_loops::drivetrain::kWheelRadius;
96}
97
98constexpr double kMaxFastEncoderPulsesPerSecond =
99 Values::kMaxDrivetrainEncoderPulsesPerSecond();
100static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
101 "fast encoders are too fast");
102constexpr double kMaxMediumEncoderPulsesPerSecond =
103 kMaxFastEncoderPulsesPerSecond;
104
105static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
106 "medium encoders are too fast");
107
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800108void PrintConfigs(ctre::phoenix6::hardware::TalonFX *talon) {
109 ctre::phoenix6::configs::TalonFXConfiguration configuration;
110 ctre::phoenix::StatusCode status =
111 talon->GetConfigurator().Refresh(configuration);
112 if (!status.IsOK()) {
113 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
114 status.GetName(), status.GetDescription());
115 }
116 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
117}
118
119void WriteConfigs(ctre::phoenix6::hardware::TalonFX *talon,
120 double stator_current_limit, double supply_current_limit) {
121 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
James Kuszmaul2a951e22024-10-29 22:15:43 -0700122 current_limits.StatorCurrentLimit =
123 units::current::ampere_t{stator_current_limit};
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800124 current_limits.StatorCurrentLimitEnable = true;
James Kuszmaul2a951e22024-10-29 22:15:43 -0700125 current_limits.SupplyCurrentLimit =
126 units::current::ampere_t{supply_current_limit};
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800127 current_limits.SupplyCurrentLimitEnable = true;
128
129 ctre::phoenix6::configs::TalonFXConfiguration configuration;
130 configuration.CurrentLimits = current_limits;
131
132 ctre::phoenix::StatusCode status =
133 talon->GetConfigurator().Apply(configuration);
134 if (!status.IsOK()) {
135 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
136 status.GetName(), status.GetDescription());
137 }
138
139 PrintConfigs(talon);
140}
141
142void Disable(ctre::phoenix6::hardware::TalonFX *talon) {
143 ctre::phoenix6::controls::DutyCycleOut stop_command(0.0);
144 stop_command.UpdateFreqHz = 0_Hz;
145 stop_command.EnableFOC = true;
146
147 talon->SetControl(stop_command);
148}
149
Ravago Jones486de802021-05-19 20:47:55 -0700150} // namespace
151
152// Class to send position messages with sensor readings to our loops.
153class SensorReader : public ::frc971::wpilib::SensorReader {
154 public:
155 SensorReader(::aos::ShmEventLoop *event_loop)
156 : ::frc971::wpilib::SensorReader(event_loop),
157 auto_mode_sender_(
158 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
159 "/autonomous")),
160 superstructure_position_sender_(
161 event_loop->MakeSender<superstructure::Position>(
162 "/superstructure")),
163 drivetrain_position_sender_(
164 event_loop
165 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
166 "/drivetrain")) {
167 // Set to filter out anything shorter than 1/4 of the minimum pulse width
168 // we should ever see.
169 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
170 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
171 }
172
173 // Auto mode switches.
174 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
175 autonomous_modes_.at(i) = ::std::move(sensor);
176 }
177
178 void RunIteration() override {
179 {
180 auto builder = drivetrain_position_sender_.MakeBuilder();
181 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
182 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
183 drivetrain_builder.add_left_encoder(
184 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
185 drivetrain_builder.add_left_speed(
186 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
187
188 drivetrain_builder.add_right_encoder(
189 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
190 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
191 drivetrain_right_encoder_->GetPeriod()));
192
milind1f1dca32021-07-03 13:50:07 -0700193 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700194 }
195
196 {
197 auto builder = superstructure_position_sender_.MakeBuilder();
198 superstructure::Position::Builder position_builder =
199 builder.MakeBuilder<superstructure::Position>();
milind1f1dca32021-07-03 13:50:07 -0700200 builder.CheckOk(builder.Send(position_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700201 }
202
203 {
204 auto builder = auto_mode_sender_.MakeBuilder();
205
206 uint32_t mode = 0;
207 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
208 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
209 mode |= 1 << i;
210 }
211 }
212
213 auto auto_mode_builder =
214 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
215
216 auto_mode_builder.add_mode(mode);
217
milind1f1dca32021-07-03 13:50:07 -0700218 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700219 }
220 }
221
222 private:
223 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
224 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
225 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
226 drivetrain_position_sender_;
227
228 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
229};
230
231class SuperstructureWriter
232 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
233 public:
234 SuperstructureWriter(::aos::EventLoop *event_loop)
235 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
236 event_loop, "/superstructure") {}
237
Vinay Siva62e3d322021-08-14 17:37:51 -0700238 void set_intake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800239 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Vinay Siva62e3d322021-08-14 17:37:51 -0700240 intake_falcon_ = ::std::move(t);
Vinay Siva7cea8a82021-09-25 15:06:28 -0700241 ConfigureRollerFalcon(intake_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700242 }
243
244 void set_outtake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800245 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Vinay Siva62e3d322021-08-14 17:37:51 -0700246 outtake_falcon_ = ::std::move(t);
Vinay Siva7cea8a82021-09-25 15:06:28 -0700247 ConfigureRollerFalcon(outtake_falcon_.get());
248 }
249
250 void set_climber_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800251 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Vinay Siva7cea8a82021-09-25 15:06:28 -0700252 climber_falcon_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800253 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
James Kuszmaul2a951e22024-10-29 22:15:43 -0700254 current_limits.SupplyCurrentLimit =
255 units::current::ampere_t{Values::kClimberSupplyCurrentLimit()};
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800256 current_limits.SupplyCurrentLimitEnable = true;
257
258 ctre::phoenix6::configs::TalonFXConfiguration configuration;
259 configuration.CurrentLimits = current_limits;
260
261 ctre::phoenix::StatusCode status =
262 climber_falcon_->GetConfigurator().Apply(configuration);
263 if (!status.IsOK()) {
264 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
265 status.GetName(), status.GetDescription());
266 }
267
268 PrintConfigs(climber_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700269 }
270
Ravago Jones486de802021-05-19 20:47:55 -0700271 private:
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800272 void ConfigureRollerFalcon(::ctre::phoenix6::hardware::TalonFX *falcon) {
273 WriteConfigs(falcon, Values::kRollerSupplyCurrentLimit(),
274 Values::kRollerStatorCurrentLimit());
Vinay Siva62e3d322021-08-14 17:37:51 -0700275 }
276
277 void WriteToFalcon(const double voltage,
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800278 ::ctre::phoenix6::hardware::TalonFX *falcon) {
279 ctre::phoenix6::controls::DutyCycleOut control(
Vinay Siva62e3d322021-08-14 17:37:51 -0700280 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800281 control.UpdateFreqHz = 0_Hz;
282 control.EnableFOC = true;
283
284 falcon->SetControl(control);
Vinay Siva62e3d322021-08-14 17:37:51 -0700285 }
286
287 void Write(const superstructure::Output &output) override {
288 WriteToFalcon(output.intake_volts(), intake_falcon_.get());
289 WriteToFalcon(output.outtake_volts(), outtake_falcon_.get());
Vinay Siva7cea8a82021-09-25 15:06:28 -0700290
291 WriteToFalcon(-output.climber_volts(), climber_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700292 }
Ravago Jones486de802021-05-19 20:47:55 -0700293
Vinay Siva7cea8a82021-09-25 15:06:28 -0700294 void Stop() override {
295 AOS_LOG(WARNING, "Superstructure output too old.\n");
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800296 Disable(climber_falcon_.get());
297 Disable(intake_falcon_.get());
298 Disable(outtake_falcon_.get());
Vinay Siva7cea8a82021-09-25 15:06:28 -0700299 }
Vinay Siva62e3d322021-08-14 17:37:51 -0700300
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800301 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> intake_falcon_,
Vinay Siva62e3d322021-08-14 17:37:51 -0700302 outtake_falcon_;
Vinay Siva7cea8a82021-09-25 15:06:28 -0700303
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800304 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> climber_falcon_;
Ravago Jones486de802021-05-19 20:47:55 -0700305};
306
307class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
308 public:
309 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
310 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
311 frc::Encoder::k4X);
312 }
313
314 void Run() override {
315 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800316 aos::configuration::ReadConfig("aos_config.json");
Ravago Jones486de802021-05-19 20:47:55 -0700317
318 // Thread 1.
319 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
320 ::frc971::wpilib::JoystickSender joystick_sender(
321 &joystick_sender_event_loop);
322 AddLoop(&joystick_sender_event_loop);
323
324 // Thread 2.
325 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
326 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
327 AddLoop(&pdp_fetcher_event_loop);
328
329 // Thread 3.
330 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
331 SensorReader sensor_reader(&sensor_reader_event_loop);
332 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
333 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
334
335 AddLoop(&sensor_reader_event_loop);
336
337 // Thread 4.
338 ::aos::ShmEventLoop output_event_loop(&config.message());
339 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
340 drivetrain_writer.set_left_controller0(
341 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
342 drivetrain_writer.set_right_controller0(
343 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
344
345 SuperstructureWriter superstructure_writer(&output_event_loop);
Vinay Siva62e3d322021-08-14 17:37:51 -0700346 superstructure_writer.set_intake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800347 make_unique<::ctre::phoenix6::hardware::TalonFX>(0));
Vinay Siva62e3d322021-08-14 17:37:51 -0700348 superstructure_writer.set_outtake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800349 make_unique<::ctre::phoenix6::hardware::TalonFX>(1));
Vinay Siva7cea8a82021-09-25 15:06:28 -0700350 superstructure_writer.set_climber_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800351 make_unique<::ctre::phoenix6::hardware::TalonFX>(2));
Ravago Jones486de802021-05-19 20:47:55 -0700352
353 AddLoop(&output_event_loop);
354
355 RunLoops();
356 }
357};
358
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800359} // namespace y2021_bot3::wpilib
Ravago Jones486de802021-05-19 20:47:55 -0700360
361AOS_ROBOT_CLASS(::y2021_bot3::wpilib::WPILibRobot);