blob: d4d74b906dd26a1e1360eb08c9094785f0ff76f4 [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -08001#include <unistd.h>
2
3#include <array>
4#include <chrono>
5#include <cinttypes>
Niko Sohmers3860f8a2024-01-12 21:05:19 -08006#include <cstdio>
7#include <cstring>
8#include <functional>
9#include <memory>
10#include <mutex>
11#include <thread>
12
Niko Sohmers3860f8a2024-01-12 21:05:19 -080013#include "frc971/wpilib/ahal/AnalogInput.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080014#include "frc971/wpilib/ahal/DriverStation.h"
15#include "frc971/wpilib/ahal/Encoder.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080016#include "frc971/wpilib/ahal/TalonFX.h"
17#include "frc971/wpilib/ahal/VictorSP.h"
18#undef ERROR
19
20#include "ctre/phoenix/cci/Diagnostics_CCI.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080021
22#include "aos/commonmath.h"
23#include "aos/containers/sized_array.h"
24#include "aos/events/event_loop.h"
25#include "aos/events/shm_event_loop.h"
26#include "aos/init.h"
27#include "aos/logging/logging.h"
28#include "aos/realtime.h"
29#include "aos/time/time.h"
30#include "aos/util/log_interval.h"
31#include "aos/util/phased_loop.h"
32#include "aos/util/wrapping_counter.h"
33#include "frc971/autonomous/auto_mode_generated.h"
34#include "frc971/can_configuration_generated.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080035#include "frc971/constants/constants_sender_lib.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080036#include "frc971/control_loops/drivetrain/drivetrain_can_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080037#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
38#include "frc971/input/robot_state_generated.h"
39#include "frc971/queues/gyro_generated.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080040#include "frc971/wpilib/buffered_pcm.h"
41#include "frc971/wpilib/buffered_solenoid.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080042#include "frc971/wpilib/can_drivetrain_writer.h"
43#include "frc971/wpilib/can_sensor_reader.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080044#include "frc971/wpilib/dma.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080045#include "frc971/wpilib/encoder_and_potentiometer.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080046#include "frc971/wpilib/generic_can_writer.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080047#include "frc971/wpilib/joystick_sender.h"
48#include "frc971/wpilib/logging_generated.h"
49#include "frc971/wpilib/loop_output_handler.h"
50#include "frc971/wpilib/pdp_fetcher.h"
51#include "frc971/wpilib/sensor_reader.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080052#include "frc971/wpilib/talonfx.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080053#include "frc971/wpilib/wpilib_robot_base.h"
54#include "y2024/constants.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080055#include "y2024/constants/constants_generated.h"
56#include "y2024/control_loops/superstructure/superstructure_can_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080057#include "y2024/control_loops/superstructure/superstructure_output_generated.h"
58#include "y2024/control_loops/superstructure/superstructure_position_generated.h"
Niko Sohmers1259b2a2024-01-29 18:00:37 -080059#include "y2024/control_loops/superstructure/superstructure_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080060
61DEFINE_bool(ctre_diag_server, false,
62 "If true, enable the diagnostics server for interacting with "
63 "devices on the CAN bus using Phoenix Tuner");
64
65using ::aos::monotonic_clock;
66using ::frc971::CANConfiguration;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080067using ::frc971::control_loops::drivetrain::CANPositionStatic;
68using ::frc971::wpilib::TalonFX;
Niko Sohmers3860f8a2024-01-12 21:05:19 -080069using ::y2024::constants::Values;
70namespace superstructure = ::y2024::control_loops::superstructure;
71namespace drivetrain = ::y2024::control_loops::drivetrain;
72namespace chrono = ::std::chrono;
73using std::make_unique;
74
Stephan Pleinesf63bde82024-01-13 15:59:33 -080075namespace y2024::wpilib {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080076namespace {
77
78constexpr double kMaxBringupPower = 12.0;
79
Niko Sohmers1259b2a2024-01-29 18:00:37 -080080double intake_pot_translate(double voltage) {
81 return voltage * Values::kIntakePivotPotRadiansPerVolt();
82}
Niko Sohmers3860f8a2024-01-12 21:05:19 -080083
Filip Kujawa749f2442024-02-04 01:12:35 -080084double climber_pot_translate(double voltage) {
85 return voltage * Values::kClimberPotRadiansPerVolt();
86}
87
Niko Sohmers3860f8a2024-01-12 21:05:19 -080088double drivetrain_velocity_translate(double in) {
89 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
90 (2.0 * M_PI)) *
91 Values::kDrivetrainEncoderRatio() *
92 control_loops::drivetrain::kWheelRadius;
93}
94
95constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
96 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
Maxwell Henderson1de15492024-02-07 11:09:47 -080097 Values::kMaxIntakePivotEncoderPulsesPerSecond(),
Filip Kujawa749f2442024-02-04 01:12:35 -080098 Values::kMaxClimberEncoderPulsesPerSecond(),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080099});
100static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
101 "fast encoders are too fast");
102
103} // namespace
104
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800105// Class to send position messages with sensor readings to our loops.
106class SensorReader : public ::frc971::wpilib::SensorReader {
107 public:
108 SensorReader(::aos::ShmEventLoop *event_loop,
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800109 const Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800110 : ::frc971::wpilib::SensorReader(event_loop),
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800111 robot_constants_(CHECK_NOTNULL(robot_constants)),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800112 auto_mode_sender_(
113 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
114 "/autonomous")),
115 superstructure_position_sender_(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800116 event_loop->MakeSender<superstructure::PositionStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800117 "/superstructure")),
118 drivetrain_position_sender_(
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800119 event_loop->MakeSender<
120 ::frc971::control_loops::drivetrain::PositionStatic>(
121 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800122 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
123 "/drivetrain")){};
124 void Start() override { AddToDMA(&imu_yaw_rate_reader_); }
125
126 // Auto mode switches.
127 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
128 autonomous_modes_.at(i) = ::std::move(sensor);
129 }
130
131 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
132 imu_yaw_rate_input_ = ::std::move(sensor);
133 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
134 }
135
136 void RunIteration() override {
137 {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800138 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
139 superstructure_position_sender_.MakeStaticBuilder();
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800140
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800141 CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(),
142 Values::kIntakePivotEncoderCountsPerRevolution(),
143 Values::kIntakePivotEncoderRatio(), intake_pot_translate,
144 true,
145 robot_constants_->robot()
146 ->intake_constants()
147 ->potentiometer_offset());
148
Filip Kujawa749f2442024-02-04 01:12:35 -0800149 CopyPosition(climber_encoder_, builder->add_climber(),
150 Values::kClimberEncoderCountsPerRevolution(),
151 Values::kClimberEncoderRatio(), climber_pot_translate, true,
152 robot_constants_->robot()
153 ->climber_constants()
154 ->potentiometer_offset());
155
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800156 builder->set_transfer_beambreak(transfer_beam_break_->Get());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800157 builder.CheckOk(builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800158 }
159
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800160 SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(),
161 drivetrain_velocity_translate,
162 constants::Values::DrivetrainEncoderToMeters, false,
163 false);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800164
165 {
166 auto builder = gyro_sender_.MakeBuilder();
167 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
168 builder.MakeBuilder<::frc971::sensors::GyroReading>();
169 // +/- 2000 deg / sec
170 constexpr double kMaxVelocity = 4000; // degrees / second
171 constexpr double kVelocityRadiansPerSecond =
172 kMaxVelocity / 360 * (2.0 * M_PI);
173
174 // Only part of the full range is used to prevent being 100% on or off.
175 constexpr double kScaledRangeLow = 0.1;
176 constexpr double kScaledRangeHigh = 0.9;
177
178 constexpr double kPWMFrequencyHz = 200;
179 double velocity_duty_cycle =
180 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
181
182 constexpr double kDutyCycleScale =
183 1 / (kScaledRangeHigh - kScaledRangeLow);
184 // scale from 0.1 - 0.9 to 0 - 1
185 double rescaled_velocity_duty_cycle =
186 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
187
188 if (!std::isnan(rescaled_velocity_duty_cycle)) {
189 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
190 kVelocityRadiansPerSecond);
191 }
192 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
193 }
194
195 {
196 auto builder = auto_mode_sender_.MakeBuilder();
197
198 uint32_t mode = 0;
199 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
200 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
201 mode |= 1 << i;
202 }
203 }
204
205 auto auto_mode_builder =
206 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
207
208 auto_mode_builder.add_mode(mode);
209
210 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
211 }
212 }
213
Maxwell Henderson62917832024-02-07 11:08:11 -0800214 void set_intake_pivot(::std::unique_ptr<frc::Encoder> encoder,
215 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
216 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800217 fast_encoder_filter_.Add(encoder.get());
218 intake_pivot_encoder_.set_encoder(::std::move(encoder));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800219 intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800220 intake_pivot_encoder_.set_potentiometer(::std::move(potentiometer));
221 }
222
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800223 void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
224 transfer_beam_break_ = ::std::move(sensor);
225 }
226
Filip Kujawa749f2442024-02-04 01:12:35 -0800227 void set_climber(::std::unique_ptr<frc::Encoder> encoder,
228 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
229 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
230 fast_encoder_filter_.Add(encoder.get());
231 climber_encoder_.set_encoder(::std::move(encoder));
232 climber_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
233 climber_encoder_.set_potentiometer(::std::move(potentiometer));
234 }
235
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800236 private:
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800237 const Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800238
239 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800240 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800241 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800242 drivetrain_position_sender_;
243 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
244
245 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
246
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800247 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800248
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800249 frc971::wpilib::AbsoluteEncoderAndPotentiometer intake_pivot_encoder_;
Filip Kujawa749f2442024-02-04 01:12:35 -0800250 frc971::wpilib::AbsoluteEncoderAndPotentiometer climber_encoder_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800251
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800252 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
253};
254
255class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
256 public:
257 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
258 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
259 frc::Encoder::k4X);
260 }
261
262 void Run() override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800263 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
264 aos::configuration::ReadConfig("aos_config.json");
265
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800266 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
267
268 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
269 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
270 &constant_fetcher_event_loop);
271 const Constants *robot_constants = &constants_fetcher.constants();
272
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800273 // Thread 1.
274 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
275 ::frc971::wpilib::JoystickSender joystick_sender(
276 &joystick_sender_event_loop);
277 AddLoop(&joystick_sender_event_loop);
278
279 // Thread 2.
280 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
281 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
282 AddLoop(&pdp_fetcher_event_loop);
283
284 // Thread 3.
285 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800286 SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800287 sensor_reader.set_pwm_trigger(true);
288 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
289 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
290 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800291 // TODO: (niko) change values once robot is wired
Maxwell Henderson62917832024-02-07 11:08:11 -0800292 sensor_reader.set_intake_pivot(make_encoder(4),
293 make_unique<frc::DigitalInput>(4),
294 make_unique<frc::AnalogInput>(4));
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800295 sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(7));
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800296
Filip Kujawa749f2442024-02-04 01:12:35 -0800297 sensor_reader.set_climber(make_encoder(5),
298 make_unique<frc::DigitalInput>(5),
299 make_unique<frc::AnalogInput>(5));
300
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800301 AddLoop(&sensor_reader_event_loop);
302
303 // Thread 4.
304 // Set up CAN.
305 if (!FLAGS_ctre_diag_server) {
306 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
307 c_Phoenix_Diagnostics_Dispose();
308 }
309
Niko Sohmers84273952024-02-14 18:40:55 -0800310 std::vector<ctre::phoenix6::BaseStatusSignal *> canivore_signal_registry;
311 std::vector<ctre::phoenix6::BaseStatusSignal *> rio_signal_registry;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800312
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800313 const CurrentLimits *current_limits =
314 robot_constants->common()->current_limits();
315
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800316 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800317 0, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800318 current_limits->drivetrain_supply_current_limit(),
319 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800320 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800321 1, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800322 current_limits->drivetrain_supply_current_limit(),
323 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800324 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800325 2, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800326 current_limits->drivetrain_supply_current_limit(),
327 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800328 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800329 3, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800330 current_limits->drivetrain_supply_current_limit(),
331 current_limits->drivetrain_stator_current_limit());
332 std::shared_ptr<TalonFX> intake_pivot = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800333 4, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800334 current_limits->intake_pivot_stator_current_limit(),
335 current_limits->intake_pivot_supply_current_limit());
336 std::shared_ptr<TalonFX> intake_roller = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800337 5, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800338 current_limits->intake_roller_stator_current_limit(),
339 current_limits->intake_roller_supply_current_limit());
340 std::shared_ptr<TalonFX> transfer_roller = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800341 6, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800342 current_limits->transfer_roller_stator_current_limit(),
343 current_limits->transfer_roller_supply_current_limit());
Filip Kujawa749f2442024-02-04 01:12:35 -0800344 std::shared_ptr<TalonFX> climber = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800345 7, false, "rio", &rio_signal_registry,
Filip Kujawa749f2442024-02-04 01:12:35 -0800346 current_limits->climber_stator_current_limit(),
347 current_limits->climber_supply_current_limit());
348
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800349 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
350 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
351 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
352 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
353
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800354 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
355 can_sensor_reader_event_loop.set_name("CANSensorReader");
356
Niko Sohmers84273952024-02-14 18:40:55 -0800357 ::aos::ShmEventLoop rio_sensor_reader_event_loop(&config.message());
358 rio_sensor_reader_event_loop.set_name("RioSensorReader");
359
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800360 // Creating list of talonfx for CANSensorReader
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800361 std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs;
Niko Sohmers84273952024-02-14 18:40:55 -0800362 std::vector<std::shared_ptr<TalonFX>> canivore_talonfxs;
363 std::vector<std::shared_ptr<TalonFX>> rio_talonfxs;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800364
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800365 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800366 drivetrain_talonfxs.push_back(talonfx);
Niko Sohmers84273952024-02-14 18:40:55 -0800367 canivore_talonfxs.push_back(talonfx);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800368 }
369
Niko Sohmers84273952024-02-14 18:40:55 -0800370 for (auto talonfx : {intake_pivot}) {
371 canivore_talonfxs.push_back(talonfx);
372 }
373
374 for (auto talonfx : {intake_roller, transfer_roller, climber}) {
375 rio_talonfxs.push_back(talonfx);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800376 }
377
378 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
379 drivetrain_can_position_sender =
380 can_sensor_reader_event_loop.MakeSender<
381 frc971::control_loops::drivetrain::CANPositionStatic>(
382 "/drivetrain");
383
384 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
385 superstructure_can_position_sender =
386 can_sensor_reader_event_loop.MakeSender<
387 y2024::control_loops::superstructure::CANPositionStatic>(
Niko Sohmers84273952024-02-14 18:40:55 -0800388 "/superstructure/canivore");
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800389
Niko Sohmers84273952024-02-14 18:40:55 -0800390 frc971::wpilib::CANSensorReader canivore_can_sensor_reader(
391 &can_sensor_reader_event_loop, std::move(canivore_signal_registry),
392 canivore_talonfxs,
393 [drivetrain_talonfxs, &intake_pivot, &drivetrain_can_position_sender,
Filip Kujawa749f2442024-02-04 01:12:35 -0800394 &superstructure_can_position_sender](
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800395 ctre::phoenix::StatusCode status) {
396 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
397 StaticBuilder drivetrain_can_builder =
398 drivetrain_can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800399
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800400 auto drivetrain_falcon_vector =
401 drivetrain_can_builder->add_talonfxs();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800402
Maxwell Henderson9116e5b2024-01-21 12:14:26 -0800403 CHECK(drivetrain_falcon_vector->reserve(drivetrain_talonfxs.size()));
404
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800405 for (auto talonfx : drivetrain_talonfxs) {
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800406 talonfx->SerializePosition(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800407 drivetrain_falcon_vector->emplace_back(),
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800408 control_loops::drivetrain::kHighOutputRatio);
409 }
410
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800411 drivetrain_can_builder->set_timestamp(
412 drivetrain_talonfxs.front()->GetTimestamp());
413 drivetrain_can_builder->set_status(static_cast<int>(status));
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800414
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800415 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
416
417 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
418 StaticBuilder superstructure_can_builder =
419 superstructure_can_position_sender.MakeStaticBuilder();
420
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800421 intake_pivot->SerializePosition(
422 superstructure_can_builder->add_intake_pivot(),
423 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmers84273952024-02-14 18:40:55 -0800424
425 superstructure_can_builder->set_timestamp(
426 intake_pivot->GetTimestamp());
427 superstructure_can_builder->set_status(static_cast<int>(status));
428 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
429 });
430
431 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
432 superstructure_rio_position_sender =
433 rio_sensor_reader_event_loop.MakeSender<
434 y2024::control_loops::superstructure::CANPositionStatic>(
435 "/superstructure/rio");
436
437 frc971::wpilib::CANSensorReader rio_can_sensor_reader(
438 &rio_sensor_reader_event_loop, std::move(rio_signal_registry),
439 rio_talonfxs,
440 [&intake_roller, &transfer_roller, &climber,
441 &superstructure_rio_position_sender](
442 ctre::phoenix::StatusCode status) {
443 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
444 StaticBuilder superstructure_can_builder =
445 superstructure_rio_position_sender.MakeStaticBuilder();
446
447 intake_roller->SerializePosition(
448 superstructure_can_builder->add_intake_roller(),
449 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800450 transfer_roller->SerializePosition(
451 superstructure_can_builder->add_transfer_roller(),
452 control_loops::drivetrain::kHighOutputRatio);
Filip Kujawa749f2442024-02-04 01:12:35 -0800453 climber->SerializePosition(
454 superstructure_can_builder->add_climber(),
455 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800456
457 superstructure_can_builder->set_timestamp(
458 intake_roller->GetTimestamp());
459 superstructure_can_builder->set_status(static_cast<int>(status));
460 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800461 });
462
463 AddLoop(&can_sensor_reader_event_loop);
Niko Sohmers84273952024-02-14 18:40:55 -0800464 AddLoop(&rio_sensor_reader_event_loop);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800465
466 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800467 ::aos::ShmEventLoop can_output_event_loop(&config.message());
468 can_output_event_loop.set_name("CANOutputWriter");
469
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800470 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
471 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800472
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800473 frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output>
474 can_superstructure_writer(
475 &can_output_event_loop,
476 [](const control_loops::superstructure::Output &output,
477 const std::map<std::string_view, std::shared_ptr<TalonFX>>
478 &talonfx_map) {
479 talonfx_map.find("intake_pivot")
480 ->second->WriteVoltage(output.intake_pivot_voltage());
481 talonfx_map.find("intake_roller")
482 ->second->WriteVoltage(output.intake_roller_voltage());
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800483 talonfx_map.find("transfer_roller")
484 ->second->WriteVoltage(output.transfer_roller_voltage());
Filip Kujawa749f2442024-02-04 01:12:35 -0800485 talonfx_map.find("climber")->second->WriteVoltage(
486 output.climber_voltage());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800487 });
488
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800489 can_drivetrain_writer.set_talonfxs({right_front, right_back},
490 {left_front, left_back});
491
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800492 can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot);
493 can_superstructure_writer.add_talonfx("intake_roller", intake_roller);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800494 can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller);
Filip Kujawa749f2442024-02-04 01:12:35 -0800495 can_superstructure_writer.add_talonfx("climber", climber);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800496
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800497 can_output_event_loop.MakeWatcher(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800498 "/roborio", [&can_drivetrain_writer, &can_superstructure_writer](
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800499 const frc971::CANConfiguration &configuration) {
500 can_drivetrain_writer.HandleCANConfiguration(configuration);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800501 can_superstructure_writer.HandleCANConfiguration(configuration);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800502 });
503
504 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800505
506 // Thread 6
507
508 RunLoops();
509 }
510};
511
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800512} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800513
514AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);