blob: 8050b57ff4e04afb5339987286d562d6401c59f1 [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
Filip Kujawa749f2442024-02-04 01:12:35 -080080double climber_pot_translate(double voltage) {
Maxwell Hendersonce232a92024-02-18 12:17:37 -080081 return voltage * Values::kClimberPotMetersPerVolt();
Filip Kujawa749f2442024-02-04 01:12:35 -080082}
83
Niko Sohmers3860f8a2024-01-12 21:05:19 -080084double drivetrain_velocity_translate(double in) {
85 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
86 (2.0 * M_PI)) *
87 Values::kDrivetrainEncoderRatio() *
88 control_loops::drivetrain::kWheelRadius;
89}
90
91constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
92 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
Maxwell Henderson1de15492024-02-07 11:09:47 -080093 Values::kMaxIntakePivotEncoderPulsesPerSecond(),
Filip Kujawa749f2442024-02-04 01:12:35 -080094 Values::kMaxClimberEncoderPulsesPerSecond(),
Niko Sohmers3860f8a2024-01-12 21:05:19 -080095});
96static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
97 "fast encoders are too fast");
98
99} // namespace
100
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800101// Class to send position messages with sensor readings to our loops.
102class SensorReader : public ::frc971::wpilib::SensorReader {
103 public:
104 SensorReader(::aos::ShmEventLoop *event_loop,
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800105 const Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800106 : ::frc971::wpilib::SensorReader(event_loop),
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800107 robot_constants_(CHECK_NOTNULL(robot_constants)),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800108 auto_mode_sender_(
109 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
110 "/autonomous")),
111 superstructure_position_sender_(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800112 event_loop->MakeSender<superstructure::PositionStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800113 "/superstructure")),
114 drivetrain_position_sender_(
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800115 event_loop->MakeSender<
116 ::frc971::control_loops::drivetrain::PositionStatic>(
117 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800118 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
119 "/drivetrain")){};
120 void Start() override { AddToDMA(&imu_yaw_rate_reader_); }
121
122 // Auto mode switches.
123 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
124 autonomous_modes_.at(i) = ::std::move(sensor);
125 }
126
127 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
128 imu_yaw_rate_input_ = ::std::move(sensor);
129 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
130 }
131
132 void RunIteration() override {
133 {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800134 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
135 superstructure_position_sender_.MakeStaticBuilder();
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800136
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800137 CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(),
138 Values::kIntakePivotEncoderCountsPerRevolution(),
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800139 Values::kIntakePivotEncoderRatio(), /* reversed: */ false);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800140
Filip Kujawa749f2442024-02-04 01:12:35 -0800141 CopyPosition(climber_encoder_, builder->add_climber(),
142 Values::kClimberEncoderCountsPerRevolution(),
Maxwell Hendersonce232a92024-02-18 12:17:37 -0800143 Values::kClimberEncoderMetersPerRevolution(),
144 climber_pot_translate, true,
Filip Kujawa749f2442024-02-04 01:12:35 -0800145 robot_constants_->robot()
146 ->climber_constants()
147 ->potentiometer_offset());
148
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800149 builder->set_transfer_beambreak(transfer_beam_break_->Get());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800150 builder.CheckOk(builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800151 }
152
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800153 SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(),
154 drivetrain_velocity_translate,
155 constants::Values::DrivetrainEncoderToMeters, false,
156 false);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800157
158 {
159 auto builder = gyro_sender_.MakeBuilder();
160 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
161 builder.MakeBuilder<::frc971::sensors::GyroReading>();
162 // +/- 2000 deg / sec
163 constexpr double kMaxVelocity = 4000; // degrees / second
164 constexpr double kVelocityRadiansPerSecond =
165 kMaxVelocity / 360 * (2.0 * M_PI);
166
167 // Only part of the full range is used to prevent being 100% on or off.
168 constexpr double kScaledRangeLow = 0.1;
169 constexpr double kScaledRangeHigh = 0.9;
170
171 constexpr double kPWMFrequencyHz = 200;
172 double velocity_duty_cycle =
173 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
174
175 constexpr double kDutyCycleScale =
176 1 / (kScaledRangeHigh - kScaledRangeLow);
177 // scale from 0.1 - 0.9 to 0 - 1
178 double rescaled_velocity_duty_cycle =
179 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
180
181 if (!std::isnan(rescaled_velocity_duty_cycle)) {
182 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
183 kVelocityRadiansPerSecond);
184 }
185 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
186 }
187
188 {
189 auto builder = auto_mode_sender_.MakeBuilder();
190
191 uint32_t mode = 0;
192 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
193 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
194 mode |= 1 << i;
195 }
196 }
197
198 auto auto_mode_builder =
199 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
200
201 auto_mode_builder.add_mode(mode);
202
203 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
204 }
205 }
206
Maxwell Henderson62917832024-02-07 11:08:11 -0800207 void set_intake_pivot(::std::unique_ptr<frc::Encoder> encoder,
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800208 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800209 fast_encoder_filter_.Add(encoder.get());
210 intake_pivot_encoder_.set_encoder(::std::move(encoder));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800211 intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800212 }
213
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800214 void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
215 transfer_beam_break_ = ::std::move(sensor);
216 }
217
Filip Kujawa749f2442024-02-04 01:12:35 -0800218 void set_climber(::std::unique_ptr<frc::Encoder> encoder,
219 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
220 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
221 fast_encoder_filter_.Add(encoder.get());
222 climber_encoder_.set_encoder(::std::move(encoder));
223 climber_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
224 climber_encoder_.set_potentiometer(::std::move(potentiometer));
225 }
226
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800227 private:
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800228 const Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800229
230 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800231 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800232 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800233 drivetrain_position_sender_;
234 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
235
236 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
237
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800238 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800239
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800240 frc971::wpilib::AbsoluteEncoder intake_pivot_encoder_;
Filip Kujawa749f2442024-02-04 01:12:35 -0800241 frc971::wpilib::AbsoluteEncoderAndPotentiometer climber_encoder_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800242
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800243 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
244};
245
246class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
247 public:
248 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
249 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
250 frc::Encoder::k4X);
251 }
252
253 void Run() override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800254 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
255 aos::configuration::ReadConfig("aos_config.json");
256
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800257 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
258
259 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
260 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
261 &constant_fetcher_event_loop);
262 const Constants *robot_constants = &constants_fetcher.constants();
263
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800264 // Thread 1.
265 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
266 ::frc971::wpilib::JoystickSender joystick_sender(
267 &joystick_sender_event_loop);
268 AddLoop(&joystick_sender_event_loop);
269
270 // Thread 2.
271 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
272 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
273 AddLoop(&pdp_fetcher_event_loop);
274
275 // Thread 3.
276 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800277 SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800278 sensor_reader.set_pwm_trigger(true);
279 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
280 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
281 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800282 // TODO: (niko) change values once robot is wired
Maxwell Henderson62917832024-02-07 11:08:11 -0800283 sensor_reader.set_intake_pivot(make_encoder(4),
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800284 make_unique<frc::DigitalInput>(4));
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800285 sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(7));
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800286
Filip Kujawa749f2442024-02-04 01:12:35 -0800287 sensor_reader.set_climber(make_encoder(5),
288 make_unique<frc::DigitalInput>(5),
289 make_unique<frc::AnalogInput>(5));
290
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800291 AddLoop(&sensor_reader_event_loop);
292
293 // Thread 4.
294 // Set up CAN.
295 if (!FLAGS_ctre_diag_server) {
296 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
297 c_Phoenix_Diagnostics_Dispose();
298 }
299
Niko Sohmers84273952024-02-14 18:40:55 -0800300 std::vector<ctre::phoenix6::BaseStatusSignal *> canivore_signal_registry;
301 std::vector<ctre::phoenix6::BaseStatusSignal *> rio_signal_registry;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800302
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800303 const CurrentLimits *current_limits =
304 robot_constants->common()->current_limits();
305
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800306 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800307 0, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800308 current_limits->drivetrain_supply_current_limit(),
309 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800310 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800311 1, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800312 current_limits->drivetrain_supply_current_limit(),
313 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800314 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800315 2, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800316 current_limits->drivetrain_supply_current_limit(),
317 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800318 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800319 3, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800320 current_limits->drivetrain_supply_current_limit(),
321 current_limits->drivetrain_stator_current_limit());
322 std::shared_ptr<TalonFX> intake_pivot = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800323 4, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800324 current_limits->intake_pivot_stator_current_limit(),
325 current_limits->intake_pivot_supply_current_limit());
326 std::shared_ptr<TalonFX> intake_roller = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800327 5, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800328 current_limits->intake_roller_stator_current_limit(),
329 current_limits->intake_roller_supply_current_limit());
330 std::shared_ptr<TalonFX> transfer_roller = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800331 6, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800332 current_limits->transfer_roller_stator_current_limit(),
333 current_limits->transfer_roller_supply_current_limit());
Filip Kujawa749f2442024-02-04 01:12:35 -0800334 std::shared_ptr<TalonFX> climber = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800335 7, false, "rio", &rio_signal_registry,
Filip Kujawa749f2442024-02-04 01:12:35 -0800336 current_limits->climber_stator_current_limit(),
337 current_limits->climber_supply_current_limit());
338
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800339 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
340 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
341 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
342 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
343
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800344 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
345 can_sensor_reader_event_loop.set_name("CANSensorReader");
346
Niko Sohmers84273952024-02-14 18:40:55 -0800347 ::aos::ShmEventLoop rio_sensor_reader_event_loop(&config.message());
348 rio_sensor_reader_event_loop.set_name("RioSensorReader");
349
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800350 // Creating list of talonfx for CANSensorReader
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800351 std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs;
Niko Sohmers84273952024-02-14 18:40:55 -0800352 std::vector<std::shared_ptr<TalonFX>> canivore_talonfxs;
353 std::vector<std::shared_ptr<TalonFX>> rio_talonfxs;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800354
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800355 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800356 drivetrain_talonfxs.push_back(talonfx);
Niko Sohmers84273952024-02-14 18:40:55 -0800357 canivore_talonfxs.push_back(talonfx);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800358 }
359
Niko Sohmers84273952024-02-14 18:40:55 -0800360 for (auto talonfx : {intake_pivot}) {
361 canivore_talonfxs.push_back(talonfx);
362 }
363
364 for (auto talonfx : {intake_roller, transfer_roller, climber}) {
365 rio_talonfxs.push_back(talonfx);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800366 }
367
368 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
369 drivetrain_can_position_sender =
370 can_sensor_reader_event_loop.MakeSender<
371 frc971::control_loops::drivetrain::CANPositionStatic>(
372 "/drivetrain");
373
374 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
375 superstructure_can_position_sender =
376 can_sensor_reader_event_loop.MakeSender<
377 y2024::control_loops::superstructure::CANPositionStatic>(
Niko Sohmers84273952024-02-14 18:40:55 -0800378 "/superstructure/canivore");
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800379
Niko Sohmers84273952024-02-14 18:40:55 -0800380 frc971::wpilib::CANSensorReader canivore_can_sensor_reader(
381 &can_sensor_reader_event_loop, std::move(canivore_signal_registry),
382 canivore_talonfxs,
383 [drivetrain_talonfxs, &intake_pivot, &drivetrain_can_position_sender,
Filip Kujawa749f2442024-02-04 01:12:35 -0800384 &superstructure_can_position_sender](
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800385 ctre::phoenix::StatusCode status) {
386 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
387 StaticBuilder drivetrain_can_builder =
388 drivetrain_can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800389
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800390 auto drivetrain_falcon_vector =
391 drivetrain_can_builder->add_talonfxs();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800392
Maxwell Henderson9116e5b2024-01-21 12:14:26 -0800393 CHECK(drivetrain_falcon_vector->reserve(drivetrain_talonfxs.size()));
394
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800395 for (auto talonfx : drivetrain_talonfxs) {
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800396 talonfx->SerializePosition(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800397 drivetrain_falcon_vector->emplace_back(),
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800398 control_loops::drivetrain::kHighOutputRatio);
399 }
400
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800401 drivetrain_can_builder->set_timestamp(
402 drivetrain_talonfxs.front()->GetTimestamp());
403 drivetrain_can_builder->set_status(static_cast<int>(status));
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800404
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800405 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
406
407 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
408 StaticBuilder superstructure_can_builder =
409 superstructure_can_position_sender.MakeStaticBuilder();
410
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800411 intake_pivot->SerializePosition(
412 superstructure_can_builder->add_intake_pivot(),
413 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmers84273952024-02-14 18:40:55 -0800414
415 superstructure_can_builder->set_timestamp(
416 intake_pivot->GetTimestamp());
417 superstructure_can_builder->set_status(static_cast<int>(status));
418 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
419 });
420
421 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
422 superstructure_rio_position_sender =
423 rio_sensor_reader_event_loop.MakeSender<
424 y2024::control_loops::superstructure::CANPositionStatic>(
425 "/superstructure/rio");
426
427 frc971::wpilib::CANSensorReader rio_can_sensor_reader(
428 &rio_sensor_reader_event_loop, std::move(rio_signal_registry),
429 rio_talonfxs,
430 [&intake_roller, &transfer_roller, &climber,
431 &superstructure_rio_position_sender](
432 ctre::phoenix::StatusCode status) {
433 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
434 StaticBuilder superstructure_can_builder =
435 superstructure_rio_position_sender.MakeStaticBuilder();
436
437 intake_roller->SerializePosition(
438 superstructure_can_builder->add_intake_roller(),
439 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800440 transfer_roller->SerializePosition(
441 superstructure_can_builder->add_transfer_roller(),
442 control_loops::drivetrain::kHighOutputRatio);
Filip Kujawa749f2442024-02-04 01:12:35 -0800443 climber->SerializePosition(
444 superstructure_can_builder->add_climber(),
445 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800446
447 superstructure_can_builder->set_timestamp(
448 intake_roller->GetTimestamp());
449 superstructure_can_builder->set_status(static_cast<int>(status));
450 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800451 });
452
453 AddLoop(&can_sensor_reader_event_loop);
Niko Sohmers84273952024-02-14 18:40:55 -0800454 AddLoop(&rio_sensor_reader_event_loop);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800455
456 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800457 ::aos::ShmEventLoop can_output_event_loop(&config.message());
458 can_output_event_loop.set_name("CANOutputWriter");
459
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800460 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
461 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800462
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800463 frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output>
464 can_superstructure_writer(
465 &can_output_event_loop,
466 [](const control_loops::superstructure::Output &output,
467 const std::map<std::string_view, std::shared_ptr<TalonFX>>
468 &talonfx_map) {
469 talonfx_map.find("intake_pivot")
470 ->second->WriteVoltage(output.intake_pivot_voltage());
471 talonfx_map.find("intake_roller")
472 ->second->WriteVoltage(output.intake_roller_voltage());
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800473 talonfx_map.find("transfer_roller")
474 ->second->WriteVoltage(output.transfer_roller_voltage());
Filip Kujawa749f2442024-02-04 01:12:35 -0800475 talonfx_map.find("climber")->second->WriteVoltage(
476 output.climber_voltage());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800477 });
478
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800479 can_drivetrain_writer.set_talonfxs({right_front, right_back},
480 {left_front, left_back});
481
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800482 can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot);
483 can_superstructure_writer.add_talonfx("intake_roller", intake_roller);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800484 can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller);
Filip Kujawa749f2442024-02-04 01:12:35 -0800485 can_superstructure_writer.add_talonfx("climber", climber);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800486
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800487 can_output_event_loop.MakeWatcher(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800488 "/roborio", [&can_drivetrain_writer, &can_superstructure_writer](
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800489 const frc971::CANConfiguration &configuration) {
490 can_drivetrain_writer.HandleCANConfiguration(configuration);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800491 can_superstructure_writer.HandleCANConfiguration(configuration);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800492 });
493
494 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800495
496 // Thread 6
497
498 RunLoops();
499 }
500};
501
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800502} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800503
504AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);