blob: 400673681c9af35c3be861e17ed841397f0aefe3 [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;
122 current_limits.StatorCurrentLimit = stator_current_limit;
123 current_limits.StatorCurrentLimitEnable = true;
124 current_limits.SupplyCurrentLimit = supply_current_limit;
125 current_limits.SupplyCurrentLimitEnable = true;
126
127 ctre::phoenix6::configs::TalonFXConfiguration configuration;
128 configuration.CurrentLimits = current_limits;
129
130 ctre::phoenix::StatusCode status =
131 talon->GetConfigurator().Apply(configuration);
132 if (!status.IsOK()) {
133 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
134 status.GetName(), status.GetDescription());
135 }
136
137 PrintConfigs(talon);
138}
139
140void Disable(ctre::phoenix6::hardware::TalonFX *talon) {
141 ctre::phoenix6::controls::DutyCycleOut stop_command(0.0);
142 stop_command.UpdateFreqHz = 0_Hz;
143 stop_command.EnableFOC = true;
144
145 talon->SetControl(stop_command);
146}
147
Ravago Jones486de802021-05-19 20:47:55 -0700148} // namespace
149
150// Class to send position messages with sensor readings to our loops.
151class SensorReader : public ::frc971::wpilib::SensorReader {
152 public:
153 SensorReader(::aos::ShmEventLoop *event_loop)
154 : ::frc971::wpilib::SensorReader(event_loop),
155 auto_mode_sender_(
156 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
157 "/autonomous")),
158 superstructure_position_sender_(
159 event_loop->MakeSender<superstructure::Position>(
160 "/superstructure")),
161 drivetrain_position_sender_(
162 event_loop
163 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
164 "/drivetrain")) {
165 // Set to filter out anything shorter than 1/4 of the minimum pulse width
166 // we should ever see.
167 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
168 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
169 }
170
171 // Auto mode switches.
172 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
173 autonomous_modes_.at(i) = ::std::move(sensor);
174 }
175
176 void RunIteration() override {
177 {
178 auto builder = drivetrain_position_sender_.MakeBuilder();
179 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
180 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
181 drivetrain_builder.add_left_encoder(
182 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
183 drivetrain_builder.add_left_speed(
184 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
185
186 drivetrain_builder.add_right_encoder(
187 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
188 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
189 drivetrain_right_encoder_->GetPeriod()));
190
milind1f1dca32021-07-03 13:50:07 -0700191 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700192 }
193
194 {
195 auto builder = superstructure_position_sender_.MakeBuilder();
196 superstructure::Position::Builder position_builder =
197 builder.MakeBuilder<superstructure::Position>();
milind1f1dca32021-07-03 13:50:07 -0700198 builder.CheckOk(builder.Send(position_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700199 }
200
201 {
202 auto builder = auto_mode_sender_.MakeBuilder();
203
204 uint32_t mode = 0;
205 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
206 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
207 mode |= 1 << i;
208 }
209 }
210
211 auto auto_mode_builder =
212 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
213
214 auto_mode_builder.add_mode(mode);
215
milind1f1dca32021-07-03 13:50:07 -0700216 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700217 }
218 }
219
220 private:
221 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
222 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
223 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
224 drivetrain_position_sender_;
225
226 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
227};
228
229class SuperstructureWriter
230 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
231 public:
232 SuperstructureWriter(::aos::EventLoop *event_loop)
233 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
234 event_loop, "/superstructure") {}
235
Vinay Siva62e3d322021-08-14 17:37:51 -0700236 void set_intake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800237 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Vinay Siva62e3d322021-08-14 17:37:51 -0700238 intake_falcon_ = ::std::move(t);
Vinay Siva7cea8a82021-09-25 15:06:28 -0700239 ConfigureRollerFalcon(intake_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700240 }
241
242 void set_outtake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800243 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Vinay Siva62e3d322021-08-14 17:37:51 -0700244 outtake_falcon_ = ::std::move(t);
Vinay Siva7cea8a82021-09-25 15:06:28 -0700245 ConfigureRollerFalcon(outtake_falcon_.get());
246 }
247
248 void set_climber_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800249 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> t) {
Vinay Siva7cea8a82021-09-25 15:06:28 -0700250 climber_falcon_ = ::std::move(t);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800251 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
252 current_limits.SupplyCurrentLimit = Values::kClimberSupplyCurrentLimit();
253 current_limits.SupplyCurrentLimitEnable = true;
254
255 ctre::phoenix6::configs::TalonFXConfiguration configuration;
256 configuration.CurrentLimits = current_limits;
257
258 ctre::phoenix::StatusCode status =
259 climber_falcon_->GetConfigurator().Apply(configuration);
260 if (!status.IsOK()) {
261 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
262 status.GetName(), status.GetDescription());
263 }
264
265 PrintConfigs(climber_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700266 }
267
Ravago Jones486de802021-05-19 20:47:55 -0700268 private:
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800269 void ConfigureRollerFalcon(::ctre::phoenix6::hardware::TalonFX *falcon) {
270 WriteConfigs(falcon, Values::kRollerSupplyCurrentLimit(),
271 Values::kRollerStatorCurrentLimit());
Vinay Siva62e3d322021-08-14 17:37:51 -0700272 }
273
274 void WriteToFalcon(const double voltage,
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800275 ::ctre::phoenix6::hardware::TalonFX *falcon) {
276 ctre::phoenix6::controls::DutyCycleOut control(
Vinay Siva62e3d322021-08-14 17:37:51 -0700277 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800278 control.UpdateFreqHz = 0_Hz;
279 control.EnableFOC = true;
280
281 falcon->SetControl(control);
Vinay Siva62e3d322021-08-14 17:37:51 -0700282 }
283
284 void Write(const superstructure::Output &output) override {
285 WriteToFalcon(output.intake_volts(), intake_falcon_.get());
286 WriteToFalcon(output.outtake_volts(), outtake_falcon_.get());
Vinay Siva7cea8a82021-09-25 15:06:28 -0700287
288 WriteToFalcon(-output.climber_volts(), climber_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700289 }
Ravago Jones486de802021-05-19 20:47:55 -0700290
Vinay Siva7cea8a82021-09-25 15:06:28 -0700291 void Stop() override {
292 AOS_LOG(WARNING, "Superstructure output too old.\n");
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800293 Disable(climber_falcon_.get());
294 Disable(intake_falcon_.get());
295 Disable(outtake_falcon_.get());
Vinay Siva7cea8a82021-09-25 15:06:28 -0700296 }
Vinay Siva62e3d322021-08-14 17:37:51 -0700297
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800298 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> intake_falcon_,
Vinay Siva62e3d322021-08-14 17:37:51 -0700299 outtake_falcon_;
Vinay Siva7cea8a82021-09-25 15:06:28 -0700300
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800301 ::std::unique_ptr<::ctre::phoenix6::hardware::TalonFX> climber_falcon_;
Ravago Jones486de802021-05-19 20:47:55 -0700302};
303
304class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
305 public:
306 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
307 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
308 frc::Encoder::k4X);
309 }
310
311 void Run() override {
312 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800313 aos::configuration::ReadConfig("aos_config.json");
Ravago Jones486de802021-05-19 20:47:55 -0700314
315 // Thread 1.
316 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
317 ::frc971::wpilib::JoystickSender joystick_sender(
318 &joystick_sender_event_loop);
319 AddLoop(&joystick_sender_event_loop);
320
321 // Thread 2.
322 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
323 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
324 AddLoop(&pdp_fetcher_event_loop);
325
326 // Thread 3.
327 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
328 SensorReader sensor_reader(&sensor_reader_event_loop);
329 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
330 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
331
332 AddLoop(&sensor_reader_event_loop);
333
334 // Thread 4.
335 ::aos::ShmEventLoop output_event_loop(&config.message());
336 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
337 drivetrain_writer.set_left_controller0(
338 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
339 drivetrain_writer.set_right_controller0(
340 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
341
342 SuperstructureWriter superstructure_writer(&output_event_loop);
Vinay Siva62e3d322021-08-14 17:37:51 -0700343 superstructure_writer.set_intake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800344 make_unique<::ctre::phoenix6::hardware::TalonFX>(0));
Vinay Siva62e3d322021-08-14 17:37:51 -0700345 superstructure_writer.set_outtake_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800346 make_unique<::ctre::phoenix6::hardware::TalonFX>(1));
Vinay Siva7cea8a82021-09-25 15:06:28 -0700347 superstructure_writer.set_climber_falcon(
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800348 make_unique<::ctre::phoenix6::hardware::TalonFX>(2));
Ravago Jones486de802021-05-19 20:47:55 -0700349
350 AddLoop(&output_event_loop);
351
352 RunLoops();
353 }
354};
355
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800356} // namespace y2021_bot3::wpilib
Ravago Jones486de802021-05-19 20:47:55 -0700357
358AOS_ROBOT_CLASS(::y2021_bot3::wpilib::WPILibRobot);