blob: 6cb8079aaa5d65b51f810325689deda99a991229 [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
Filip Kujawad75252a2024-02-10 16:54:35 -080084double extend_pot_translate(double voltage) {
85 return voltage * Values::kExtendPotMetersPerVolt();
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(),
Filip Kujawad75252a2024-02-10 16:54:35 -080099 Values::kMaxExtendEncoderPulsesPerSecond(),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800100});
101static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
102 "fast encoders are too fast");
103
104} // namespace
105
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800106// Class to send position messages with sensor readings to our loops.
107class SensorReader : public ::frc971::wpilib::SensorReader {
108 public:
109 SensorReader(::aos::ShmEventLoop *event_loop,
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800110 const Constants *robot_constants)
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800111 : ::frc971::wpilib::SensorReader(event_loop),
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800112 robot_constants_(CHECK_NOTNULL(robot_constants)),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800113 auto_mode_sender_(
114 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
115 "/autonomous")),
116 superstructure_position_sender_(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800117 event_loop->MakeSender<superstructure::PositionStatic>(
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800118 "/superstructure")),
119 drivetrain_position_sender_(
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800120 event_loop->MakeSender<
121 ::frc971::control_loops::drivetrain::PositionStatic>(
122 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800123 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
124 "/drivetrain")){};
125 void Start() override { AddToDMA(&imu_yaw_rate_reader_); }
126
127 // Auto mode switches.
128 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
129 autonomous_modes_.at(i) = ::std::move(sensor);
130 }
131
132 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
133 imu_yaw_rate_input_ = ::std::move(sensor);
134 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
135 }
136
137 void RunIteration() override {
138 {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800139 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
140 superstructure_position_sender_.MakeStaticBuilder();
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800141
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800142 CopyPosition(intake_pivot_encoder_, builder->add_intake_pivot(),
143 Values::kIntakePivotEncoderCountsPerRevolution(),
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800144 Values::kIntakePivotEncoderRatio(), /* reversed: */ false);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800145
Filip Kujawa749f2442024-02-04 01:12:35 -0800146 CopyPosition(climber_encoder_, builder->add_climber(),
147 Values::kClimberEncoderCountsPerRevolution(),
Maxwell Hendersonce232a92024-02-18 12:17:37 -0800148 Values::kClimberEncoderMetersPerRevolution(),
149 climber_pot_translate, true,
Filip Kujawa749f2442024-02-04 01:12:35 -0800150 robot_constants_->robot()
151 ->climber_constants()
152 ->potentiometer_offset());
153
Filip Kujawad75252a2024-02-10 16:54:35 -0800154 CopyPosition(extend_encoder_, builder->add_extend(),
155 Values::kClimberEncoderCountsPerRevolution(),
156 Values::kClimberEncoderMetersPerRevolution(),
157 extend_pot_translate, true,
158 robot_constants_->robot()
159 ->extend_constants()
160 ->potentiometer_offset());
161
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800162 builder->set_transfer_beambreak(transfer_beam_break_->Get());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800163 builder.CheckOk(builder.Send());
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800164 }
165
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800166 SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(),
167 drivetrain_velocity_translate,
168 constants::Values::DrivetrainEncoderToMeters, false,
169 false);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800170
171 {
172 auto builder = gyro_sender_.MakeBuilder();
173 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
174 builder.MakeBuilder<::frc971::sensors::GyroReading>();
175 // +/- 2000 deg / sec
176 constexpr double kMaxVelocity = 4000; // degrees / second
177 constexpr double kVelocityRadiansPerSecond =
178 kMaxVelocity / 360 * (2.0 * M_PI);
179
180 // Only part of the full range is used to prevent being 100% on or off.
181 constexpr double kScaledRangeLow = 0.1;
182 constexpr double kScaledRangeHigh = 0.9;
183
184 constexpr double kPWMFrequencyHz = 200;
185 double velocity_duty_cycle =
186 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
187
188 constexpr double kDutyCycleScale =
189 1 / (kScaledRangeHigh - kScaledRangeLow);
190 // scale from 0.1 - 0.9 to 0 - 1
191 double rescaled_velocity_duty_cycle =
192 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
193
194 if (!std::isnan(rescaled_velocity_duty_cycle)) {
195 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
196 kVelocityRadiansPerSecond);
197 }
198 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
199 }
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
216 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
217 }
218 }
219
Maxwell Henderson62917832024-02-07 11:08:11 -0800220 void set_intake_pivot(::std::unique_ptr<frc::Encoder> encoder,
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800221 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800222 fast_encoder_filter_.Add(encoder.get());
223 intake_pivot_encoder_.set_encoder(::std::move(encoder));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800224 intake_pivot_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800225 }
226
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800227 void set_transfer_beambreak(::std::unique_ptr<frc::DigitalInput> sensor) {
228 transfer_beam_break_ = ::std::move(sensor);
229 }
230
Filip Kujawa749f2442024-02-04 01:12:35 -0800231 void set_climber(::std::unique_ptr<frc::Encoder> encoder,
232 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
233 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
234 fast_encoder_filter_.Add(encoder.get());
235 climber_encoder_.set_encoder(::std::move(encoder));
236 climber_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
237 climber_encoder_.set_potentiometer(::std::move(potentiometer));
238 }
239
Filip Kujawad75252a2024-02-10 16:54:35 -0800240 void set_extend(::std::unique_ptr<frc::Encoder> encoder,
241 ::std::unique_ptr<frc::DigitalInput> absolute_pwm,
242 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
243 fast_encoder_filter_.Add(encoder.get());
244 extend_encoder_.set_encoder(::std::move(encoder));
245 extend_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
246 extend_encoder_.set_potentiometer(::std::move(potentiometer));
247 }
248
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800249 private:
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800250 const Constants *robot_constants_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800251
252 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800253 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800254 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800255 drivetrain_position_sender_;
256 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
257
258 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
259
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800260 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_, transfer_beam_break_;
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800261
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800262 frc971::wpilib::AbsoluteEncoder intake_pivot_encoder_;
Filip Kujawa749f2442024-02-04 01:12:35 -0800263 frc971::wpilib::AbsoluteEncoderAndPotentiometer climber_encoder_;
Filip Kujawad75252a2024-02-10 16:54:35 -0800264 frc971::wpilib::AbsoluteEncoderAndPotentiometer extend_encoder_;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800265
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800266 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
267};
268
269class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
270 public:
271 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
272 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
273 frc::Encoder::k4X);
274 }
275
276 void Run() override {
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800277 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
278 aos::configuration::ReadConfig("aos_config.json");
279
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800280 frc971::constants::WaitForConstants<y2024::Constants>(&config.message());
281
282 ::aos::ShmEventLoop constant_fetcher_event_loop(&config.message());
283 frc971::constants::ConstantsFetcher<Constants> constants_fetcher(
284 &constant_fetcher_event_loop);
285 const Constants *robot_constants = &constants_fetcher.constants();
286
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800287 // Thread 1.
288 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
289 ::frc971::wpilib::JoystickSender joystick_sender(
290 &joystick_sender_event_loop);
291 AddLoop(&joystick_sender_event_loop);
292
293 // Thread 2.
294 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
295 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
296 AddLoop(&pdp_fetcher_event_loop);
297
298 // Thread 3.
299 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800300 SensorReader sensor_reader(&sensor_reader_event_loop, robot_constants);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800301 sensor_reader.set_pwm_trigger(true);
302 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
303 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
304 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800305 // TODO: (niko) change values once robot is wired
Maxwell Henderson62917832024-02-07 11:08:11 -0800306 sensor_reader.set_intake_pivot(make_encoder(4),
Niko Sohmers74b0ad52024-02-03 18:00:31 -0800307 make_unique<frc::DigitalInput>(4));
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800308 sensor_reader.set_transfer_beambreak(make_unique<frc::DigitalInput>(7));
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800309
Filip Kujawa749f2442024-02-04 01:12:35 -0800310 sensor_reader.set_climber(make_encoder(5),
311 make_unique<frc::DigitalInput>(5),
312 make_unique<frc::AnalogInput>(5));
313
Filip Kujawad75252a2024-02-10 16:54:35 -0800314 sensor_reader.set_extend(make_encoder(6), make_unique<frc::DigitalInput>(6),
315 make_unique<frc::AnalogInput>(6));
316
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800317 AddLoop(&sensor_reader_event_loop);
318
319 // Thread 4.
320 // Set up CAN.
321 if (!FLAGS_ctre_diag_server) {
322 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
323 c_Phoenix_Diagnostics_Dispose();
324 }
325
Niko Sohmers84273952024-02-14 18:40:55 -0800326 std::vector<ctre::phoenix6::BaseStatusSignal *> canivore_signal_registry;
327 std::vector<ctre::phoenix6::BaseStatusSignal *> rio_signal_registry;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800328
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800329 const CurrentLimits *current_limits =
330 robot_constants->common()->current_limits();
331
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800332 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800333 0, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800334 current_limits->drivetrain_supply_current_limit(),
335 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800336 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800337 1, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800338 current_limits->drivetrain_supply_current_limit(),
339 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800340 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800341 2, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800342 current_limits->drivetrain_supply_current_limit(),
343 current_limits->drivetrain_stator_current_limit());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800344 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800345 3, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800346 current_limits->drivetrain_supply_current_limit(),
347 current_limits->drivetrain_stator_current_limit());
348 std::shared_ptr<TalonFX> intake_pivot = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800349 4, false, "Drivetrain Bus", &canivore_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800350 current_limits->intake_pivot_stator_current_limit(),
351 current_limits->intake_pivot_supply_current_limit());
352 std::shared_ptr<TalonFX> intake_roller = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800353 5, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800354 current_limits->intake_roller_stator_current_limit(),
355 current_limits->intake_roller_supply_current_limit());
356 std::shared_ptr<TalonFX> transfer_roller = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800357 6, false, "rio", &rio_signal_registry,
Maxwell Hendersonfb3bfea2024-02-03 19:24:46 -0800358 current_limits->transfer_roller_stator_current_limit(),
359 current_limits->transfer_roller_supply_current_limit());
Filip Kujawa749f2442024-02-04 01:12:35 -0800360 std::shared_ptr<TalonFX> climber = std::make_shared<TalonFX>(
Niko Sohmers84273952024-02-14 18:40:55 -0800361 7, false, "rio", &rio_signal_registry,
Filip Kujawa749f2442024-02-04 01:12:35 -0800362 current_limits->climber_stator_current_limit(),
363 current_limits->climber_supply_current_limit());
364
Filip Kujawad75252a2024-02-10 16:54:35 -0800365 std::shared_ptr<TalonFX> extend = std::make_shared<TalonFX>(
366 8, false, "Drivetrain Bus", &rio_signal_registry,
367 current_limits->extend_stator_current_limit(),
368 current_limits->extend_supply_current_limit());
369
370 std::shared_ptr<TalonFX> extend_roller = std::make_shared<TalonFX>(
371 9, false, "Drivetrain Bus", &rio_signal_registry,
372 current_limits->extend_roller_stator_current_limit(),
373 current_limits->extend_roller_supply_current_limit());
374
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800375 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
376 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
377 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
378 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
379
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800380 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
381 can_sensor_reader_event_loop.set_name("CANSensorReader");
382
Niko Sohmers84273952024-02-14 18:40:55 -0800383 ::aos::ShmEventLoop rio_sensor_reader_event_loop(&config.message());
384 rio_sensor_reader_event_loop.set_name("RioSensorReader");
385
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800386 // Creating list of talonfx for CANSensorReader
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800387 std::vector<std::shared_ptr<TalonFX>> drivetrain_talonfxs;
Niko Sohmers84273952024-02-14 18:40:55 -0800388 std::vector<std::shared_ptr<TalonFX>> canivore_talonfxs;
389 std::vector<std::shared_ptr<TalonFX>> rio_talonfxs;
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800390
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800391 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800392 drivetrain_talonfxs.push_back(talonfx);
Niko Sohmers84273952024-02-14 18:40:55 -0800393 canivore_talonfxs.push_back(talonfx);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800394 }
395
Niko Sohmers84273952024-02-14 18:40:55 -0800396 for (auto talonfx : {intake_pivot}) {
397 canivore_talonfxs.push_back(talonfx);
398 }
399
Filip Kujawad75252a2024-02-10 16:54:35 -0800400 for (auto talonfx :
401 {intake_roller, transfer_roller, climber, extend, extend_roller}) {
Niko Sohmers84273952024-02-14 18:40:55 -0800402 rio_talonfxs.push_back(talonfx);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800403 }
404
405 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>
406 drivetrain_can_position_sender =
407 can_sensor_reader_event_loop.MakeSender<
408 frc971::control_loops::drivetrain::CANPositionStatic>(
409 "/drivetrain");
410
411 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
412 superstructure_can_position_sender =
413 can_sensor_reader_event_loop.MakeSender<
414 y2024::control_loops::superstructure::CANPositionStatic>(
Niko Sohmers84273952024-02-14 18:40:55 -0800415 "/superstructure/canivore");
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800416
Niko Sohmers84273952024-02-14 18:40:55 -0800417 frc971::wpilib::CANSensorReader canivore_can_sensor_reader(
418 &can_sensor_reader_event_loop, std::move(canivore_signal_registry),
419 canivore_talonfxs,
420 [drivetrain_talonfxs, &intake_pivot, &drivetrain_can_position_sender,
Filip Kujawa749f2442024-02-04 01:12:35 -0800421 &superstructure_can_position_sender](
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800422 ctre::phoenix::StatusCode status) {
423 aos::Sender<frc971::control_loops::drivetrain::CANPositionStatic>::
424 StaticBuilder drivetrain_can_builder =
425 drivetrain_can_position_sender.MakeStaticBuilder();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800426
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800427 auto drivetrain_falcon_vector =
428 drivetrain_can_builder->add_talonfxs();
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800429
Maxwell Henderson9116e5b2024-01-21 12:14:26 -0800430 CHECK(drivetrain_falcon_vector->reserve(drivetrain_talonfxs.size()));
431
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800432 for (auto talonfx : drivetrain_talonfxs) {
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800433 talonfx->SerializePosition(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800434 drivetrain_falcon_vector->emplace_back(),
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800435 control_loops::drivetrain::kHighOutputRatio);
436 }
437
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800438 drivetrain_can_builder->set_timestamp(
439 drivetrain_talonfxs.front()->GetTimestamp());
440 drivetrain_can_builder->set_status(static_cast<int>(status));
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800441
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800442 drivetrain_can_builder.CheckOk(drivetrain_can_builder.Send());
443
444 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
445 StaticBuilder superstructure_can_builder =
446 superstructure_can_position_sender.MakeStaticBuilder();
447
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800448 intake_pivot->SerializePosition(
449 superstructure_can_builder->add_intake_pivot(),
450 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmers84273952024-02-14 18:40:55 -0800451
452 superstructure_can_builder->set_timestamp(
453 intake_pivot->GetTimestamp());
454 superstructure_can_builder->set_status(static_cast<int>(status));
455 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
456 });
457
458 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>
459 superstructure_rio_position_sender =
460 rio_sensor_reader_event_loop.MakeSender<
461 y2024::control_loops::superstructure::CANPositionStatic>(
462 "/superstructure/rio");
463
464 frc971::wpilib::CANSensorReader rio_can_sensor_reader(
465 &rio_sensor_reader_event_loop, std::move(rio_signal_registry),
466 rio_talonfxs,
Filip Kujawad75252a2024-02-10 16:54:35 -0800467 [&intake_roller, &transfer_roller, &climber, &extend, &extend_roller,
Niko Sohmers84273952024-02-14 18:40:55 -0800468 &superstructure_rio_position_sender](
469 ctre::phoenix::StatusCode status) {
470 aos::Sender<y2024::control_loops::superstructure::CANPositionStatic>::
471 StaticBuilder superstructure_can_builder =
472 superstructure_rio_position_sender.MakeStaticBuilder();
473
474 intake_roller->SerializePosition(
475 superstructure_can_builder->add_intake_roller(),
476 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800477 transfer_roller->SerializePosition(
478 superstructure_can_builder->add_transfer_roller(),
479 control_loops::drivetrain::kHighOutputRatio);
Filip Kujawa749f2442024-02-04 01:12:35 -0800480 climber->SerializePosition(
481 superstructure_can_builder->add_climber(),
482 control_loops::drivetrain::kHighOutputRatio);
Filip Kujawad75252a2024-02-10 16:54:35 -0800483 extend->SerializePosition(superstructure_can_builder->add_extend(),
484 superstructure::extend::kOutputRatio);
485 extend_roller->SerializePosition(
486 superstructure_can_builder->add_extend_roller(),
487 control_loops::drivetrain::kHighOutputRatio);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800488
489 superstructure_can_builder->set_timestamp(
490 intake_roller->GetTimestamp());
491 superstructure_can_builder->set_status(static_cast<int>(status));
492 superstructure_can_builder.CheckOk(superstructure_can_builder.Send());
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800493 });
494
495 AddLoop(&can_sensor_reader_event_loop);
Niko Sohmers84273952024-02-14 18:40:55 -0800496 AddLoop(&rio_sensor_reader_event_loop);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800497
498 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800499 ::aos::ShmEventLoop can_output_event_loop(&config.message());
500 can_output_event_loop.set_name("CANOutputWriter");
501
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800502 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
503 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800504
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800505 frc971::wpilib::GenericCANWriter<control_loops::superstructure::Output>
506 can_superstructure_writer(
507 &can_output_event_loop,
508 [](const control_loops::superstructure::Output &output,
509 const std::map<std::string_view, std::shared_ptr<TalonFX>>
510 &talonfx_map) {
511 talonfx_map.find("intake_pivot")
512 ->second->WriteVoltage(output.intake_pivot_voltage());
513 talonfx_map.find("intake_roller")
514 ->second->WriteVoltage(output.intake_roller_voltage());
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800515 talonfx_map.find("transfer_roller")
516 ->second->WriteVoltage(output.transfer_roller_voltage());
Filip Kujawa749f2442024-02-04 01:12:35 -0800517 talonfx_map.find("climber")->second->WriteVoltage(
518 output.climber_voltage());
Filip Kujawad75252a2024-02-10 16:54:35 -0800519 talonfx_map.find("extend")->second->WriteVoltage(
520 output.extend_voltage());
521 talonfx_map.find("extend_roller")
522 ->second->WriteVoltage(output.extend_roller_voltage());
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800523 });
524
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800525 can_drivetrain_writer.set_talonfxs({right_front, right_back},
526 {left_front, left_back});
527
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800528 can_superstructure_writer.add_talonfx("intake_pivot", intake_pivot);
529 can_superstructure_writer.add_talonfx("intake_roller", intake_roller);
Niko Sohmersed7ffc42024-02-03 16:05:19 -0800530 can_superstructure_writer.add_talonfx("transfer_roller", transfer_roller);
Filip Kujawa749f2442024-02-04 01:12:35 -0800531 can_superstructure_writer.add_talonfx("climber", climber);
Filip Kujawad75252a2024-02-10 16:54:35 -0800532 can_superstructure_writer.add_talonfx("extend", extend);
533 can_superstructure_writer.add_talonfx("extend_roller", extend_roller);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800534
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800535 can_output_event_loop.MakeWatcher(
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800536 "/roborio", [&can_drivetrain_writer, &can_superstructure_writer](
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800537 const frc971::CANConfiguration &configuration) {
538 can_drivetrain_writer.HandleCANConfiguration(configuration);
Niko Sohmers1259b2a2024-01-29 18:00:37 -0800539 can_superstructure_writer.HandleCANConfiguration(configuration);
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800540 });
541
542 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800543
544 // Thread 6
545
546 RunLoops();
547 }
548};
549
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800550} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800551
552AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);