blob: b1f9018611efd614cca1ac919338e2afaf035d46 [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001#include <unistd.h>
2
3#include <array>
4#include <chrono>
5#include <cinttypes>
6#include <cmath>
7#include <cstdio>
8#include <cstring>
9#include <functional>
10#include <memory>
11#include <mutex>
12#include <thread>
13
14#include "ctre/phoenix/CANifier.h"
15#include "frc971/wpilib/ahal/AnalogInput.h"
16#include "frc971/wpilib/ahal/Counter.h"
17#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
18#include "frc971/wpilib/ahal/DriverStation.h"
19#include "frc971/wpilib/ahal/Encoder.h"
20#include "frc971/wpilib/ahal/Servo.h"
21#include "frc971/wpilib/ahal/TalonFX.h"
22#include "frc971/wpilib/ahal/VictorSP.h"
23#undef ERROR
24
25#include "aos/commonmath.h"
Ravago Jones2060ee62023-02-03 18:12:24 -080026#include "aos/containers/sized_array.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080027#include "aos/events/event_loop.h"
28#include "aos/events/shm_event_loop.h"
29#include "aos/init.h"
30#include "aos/logging/logging.h"
31#include "aos/realtime.h"
32#include "aos/time/time.h"
33#include "aos/util/log_interval.h"
34#include "aos/util/phased_loop.h"
35#include "aos/util/wrapping_counter.h"
Ravago Jones2060ee62023-02-03 18:12:24 -080036#include "ctre/phoenix/cci/Diagnostics_CCI.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080037#include "ctre/phoenix/motorcontrol/can/TalonFX.h"
38#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
Ravago Jones2060ee62023-02-03 18:12:24 -080039#include "ctre/phoenixpro/TalonFX.hpp"
Maxwell Hendersonad312342023-01-10 12:07:47 -080040#include "frc971/autonomous/auto_mode_generated.h"
41#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
42#include "frc971/input/robot_state_generated.h"
43#include "frc971/queues/gyro_generated.h"
44#include "frc971/wpilib/ADIS16448.h"
45#include "frc971/wpilib/buffered_pcm.h"
46#include "frc971/wpilib/buffered_solenoid.h"
47#include "frc971/wpilib/dma.h"
48#include "frc971/wpilib/drivetrain_writer.h"
49#include "frc971/wpilib/encoder_and_potentiometer.h"
50#include "frc971/wpilib/joystick_sender.h"
51#include "frc971/wpilib/logging_generated.h"
52#include "frc971/wpilib/loop_output_handler.h"
53#include "frc971/wpilib/pdp_fetcher.h"
54#include "frc971/wpilib/sensor_reader.h"
55#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuhbb4c9ac2023-02-28 22:04:20 -080056#include "y2023/can_configuration_generated.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080057#include "y2023/constants.h"
Ravago Jones2060ee62023-02-03 18:12:24 -080058#include "y2023/control_loops/drivetrain/drivetrain_can_position_generated.h"
Maxwell Henderson2a2faa62023-03-11 15:05:46 -080059#include "y2023/control_loops/superstructure/led_indicator.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080060#include "y2023/control_loops/superstructure/superstructure_output_generated.h"
61#include "y2023/control_loops/superstructure/superstructure_position_generated.h"
62
Ravago Jones2060ee62023-02-03 18:12:24 -080063DEFINE_bool(ctre_diag_server, false,
64 "If true, enable the diagnostics server for interacting with "
65 "devices on the CAN bus using Phoenix Tuner");
66
Maxwell Hendersonad312342023-01-10 12:07:47 -080067using ::aos::monotonic_clock;
68using ::y2023::constants::Values;
69namespace superstructure = ::y2023::control_loops::superstructure;
Ravago Jones2060ee62023-02-03 18:12:24 -080070namespace drivetrain = ::y2023::control_loops::drivetrain;
Maxwell Hendersonad312342023-01-10 12:07:47 -080071namespace chrono = ::std::chrono;
72using std::make_unique;
73
74namespace y2023 {
75namespace wpilib {
76namespace {
77
78constexpr double kMaxBringupPower = 12.0;
79
80// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
81// DMA stuff and then removing the * 2.0 in *_translate.
82// The low bit is direction.
83
84double 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
milind-u18934eb2023-02-20 16:28:58 -080091double proximal_pot_translate(double voltage) {
92 return voltage * Values::kProximalPotRadiansPerVolt();
93}
94
95double distal_pot_translate(double voltage) {
96 return voltage * Values::kDistalPotRadiansPerVolt();
97}
98
99double roll_joint_pot_translate(double voltage) {
100 return voltage * Values::kRollJointPotRadiansPerVolt();
101}
102
103constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
104 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
105 Values::kMaxProximalEncoderPulsesPerSecond(),
106 Values::kMaxDistalEncoderPulsesPerSecond(),
107 Values::kMaxRollJointEncoderPulsesPerSecond(),
Austin Schuhe5248cd2023-03-05 12:46:16 -0800108 Values::kMaxCompWristEncoderPulsesPerSecond(),
109 Values::kMaxPracticeWristEncoderPulsesPerSecond(),
milind-u18934eb2023-02-20 16:28:58 -0800110});
111static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
112 "fast encoders are too fast");
Maxwell Hendersonad312342023-01-10 12:07:47 -0800113
114} // namespace
115
milind-u738832d2023-02-24 19:55:54 -0800116static constexpr int kCANFalconCount = 6;
milind-u738832d2023-02-24 19:55:54 -0800117static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz;
118
119class Falcon {
120 public:
121 Falcon(int device_id, std::string canbus,
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700122 std::vector<ctre::phoenixpro::BaseStatusSignalValue *> *signals)
milind-u738832d2023-02-24 19:55:54 -0800123 : talon_(device_id, canbus),
124 device_id_(device_id),
125 device_temp_(talon_.GetDeviceTemp()),
126 supply_voltage_(talon_.GetSupplyVoltage()),
127 supply_current_(talon_.GetSupplyCurrent()),
128 torque_current_(talon_.GetTorqueCurrent()),
Ravago Jones088ca772023-03-25 22:14:24 -0700129 position_(talon_.GetPosition()),
130 duty_cycle_(talon_.GetDutyCycle()) {
milind-u738832d2023-02-24 19:55:54 -0800131 // device temp is not timesynced so don't add it to the list of signals
132 device_temp_.SetUpdateFrequency(kCANUpdateFreqHz);
133
milind-u738832d2023-02-24 19:55:54 -0800134 CHECK_NOTNULL(signals);
milind-u738832d2023-02-24 19:55:54 -0800135
136 supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz);
137 signals->push_back(&supply_voltage_);
138
139 supply_current_.SetUpdateFrequency(kCANUpdateFreqHz);
140 signals->push_back(&supply_current_);
141
142 torque_current_.SetUpdateFrequency(kCANUpdateFreqHz);
143 signals->push_back(&torque_current_);
144
145 position_.SetUpdateFrequency(kCANUpdateFreqHz);
146 signals->push_back(&position_);
Ravago Jones088ca772023-03-25 22:14:24 -0700147
148 duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz);
149 signals->push_back(&duty_cycle_);
milind-u738832d2023-02-24 19:55:54 -0800150 }
151
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800152 void PrintConfigs() {
153 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
154 ctre::phoenix::StatusCode status =
155 talon_.GetConfigurator().Refresh(configuration);
156 if (!status.IsOK()) {
157 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
158 status.GetName(), status.GetDescription());
159 }
160 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
161 }
162
milind-u738832d2023-02-24 19:55:54 -0800163 void WriteConfigs(ctre::phoenixpro::signals::InvertedValue invert) {
164 inverted_ = invert;
165
166 ctre::phoenixpro::configs::CurrentLimitsConfigs current_limits;
167 current_limits.StatorCurrentLimit =
168 constants::Values::kDrivetrainStatorCurrentLimit();
169 current_limits.StatorCurrentLimitEnable = true;
170 current_limits.SupplyCurrentLimit =
171 constants::Values::kDrivetrainSupplyCurrentLimit();
172 current_limits.SupplyCurrentLimitEnable = true;
173
174 ctre::phoenixpro::configs::MotorOutputConfigs output_configs;
175 output_configs.NeutralMode =
176 ctre::phoenixpro::signals::NeutralModeValue::Brake;
177 output_configs.DutyCycleNeutralDeadband = 0;
178
179 output_configs.Inverted = inverted_;
180
181 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
182 configuration.CurrentLimits = current_limits;
183 configuration.MotorOutput = output_configs;
184
185 ctre::phoenix::StatusCode status =
186 talon_.GetConfigurator().Apply(configuration);
187 if (!status.IsOK()) {
188 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
189 status.GetName(), status.GetDescription());
190 }
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800191
192 PrintConfigs();
milind-u738832d2023-02-24 19:55:54 -0800193 }
194
195 void WriteRollerConfigs() {
196 ctre::phoenixpro::configs::CurrentLimitsConfigs current_limits;
197 current_limits.StatorCurrentLimit =
198 constants::Values::kRollerStatorCurrentLimit();
199 current_limits.StatorCurrentLimitEnable = true;
200 current_limits.SupplyCurrentLimit =
201 constants::Values::kRollerSupplyCurrentLimit();
202 current_limits.SupplyCurrentLimitEnable = true;
203
204 ctre::phoenixpro::configs::MotorOutputConfigs output_configs;
205 output_configs.NeutralMode =
206 ctre::phoenixpro::signals::NeutralModeValue::Brake;
207 output_configs.DutyCycleNeutralDeadband = 0;
208
209 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
210 configuration.CurrentLimits = current_limits;
211 configuration.MotorOutput = output_configs;
212
213 ctre::phoenix::StatusCode status =
214 talon_.GetConfigurator().Apply(configuration);
215 if (!status.IsOK()) {
216 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
217 status.GetName(), status.GetDescription());
218 }
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800219
220 PrintConfigs();
milind-u738832d2023-02-24 19:55:54 -0800221 }
222
223 ctre::phoenixpro::hardware::TalonFX *talon() { return &talon_; }
224
225 flatbuffers::Offset<drivetrain::CANFalcon> WritePosition(
226 flatbuffers::FlatBufferBuilder *fbb) {
227 drivetrain::CANFalcon::Builder builder(*fbb);
228 builder.add_id(device_id_);
229 builder.add_device_temp(device_temp());
230 builder.add_supply_voltage(supply_voltage());
231 builder.add_supply_current(supply_current());
232 builder.add_torque_current(torque_current());
Ravago Jones088ca772023-03-25 22:14:24 -0700233 builder.add_duty_cycle(duty_cycle());
milind-u738832d2023-02-24 19:55:54 -0800234
235 double invert =
236 (inverted_ ==
237 ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive
238 ? 1
239 : -1);
240
241 builder.add_position(
242 constants::Values::DrivetrainCANEncoderToMeters(position()) * invert);
243
244 return builder.Finish();
245 }
246
247 int device_id() const { return device_id_; }
248 float device_temp() const { return device_temp_.GetValue().value(); }
249 float supply_voltage() const { return supply_voltage_.GetValue().value(); }
250 float supply_current() const { return supply_current_.GetValue().value(); }
251 float torque_current() const { return torque_current_.GetValue().value(); }
Ravago Jones088ca772023-03-25 22:14:24 -0700252 float duty_cycle() const { return duty_cycle_.GetValue().value(); }
milind-u738832d2023-02-24 19:55:54 -0800253 float position() const { return position_.GetValue().value(); }
254
255 // returns the monotonic timestamp of the latest timesynced reading in the
256 // timebase of the the syncronized CAN bus clock.
257 int64_t GetTimestamp() {
258 std::chrono::nanoseconds latest_timestamp =
259 torque_current_.GetTimestamp().GetTime();
260
261 return latest_timestamp.count();
262 }
263
264 void RefreshNontimesyncedSignals() { device_temp_.Refresh(); };
265
266 private:
267 ctre::phoenixpro::hardware::TalonFX talon_;
268 int device_id_;
269
270 ctre::phoenixpro::signals::InvertedValue inverted_;
271
272 ctre::phoenixpro::StatusSignalValue<units::temperature::celsius_t>
273 device_temp_;
274 ctre::phoenixpro::StatusSignalValue<units::voltage::volt_t> supply_voltage_;
275 ctre::phoenixpro::StatusSignalValue<units::current::ampere_t> supply_current_,
276 torque_current_;
277 ctre::phoenixpro::StatusSignalValue<units::angle::turn_t> position_;
Ravago Jones088ca772023-03-25 22:14:24 -0700278 ctre::phoenixpro::StatusSignalValue<units::dimensionless::scalar_t>
279 duty_cycle_;
milind-u738832d2023-02-24 19:55:54 -0800280};
281
282class CANSensorReader {
283 public:
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700284 CANSensorReader(
285 aos::EventLoop *event_loop,
286 std::vector<ctre::phoenixpro::BaseStatusSignalValue *> signals_registry)
milind-u738832d2023-02-24 19:55:54 -0800287 : event_loop_(event_loop),
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700288 signals_(signals_registry.begin(), signals_registry.end()),
milind-u738832d2023-02-24 19:55:54 -0800289 can_position_sender_(
290 event_loop->MakeSender<drivetrain::CANPosition>("/drivetrain")),
291 roller_falcon_data_(std::nullopt) {
292 event_loop->SetRuntimeRealtimePriority(40);
293 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1}));
294 timer_handler_ = event_loop->AddTimer([this]() { Loop(); });
295 timer_handler_->set_name("CANSensorReader Loop");
296
297 event_loop->OnRun([this]() {
298 timer_handler_->Setup(event_loop_->monotonic_now(), 1 / kCANUpdateFreqHz);
299 });
300 }
301
milind-u738832d2023-02-24 19:55:54 -0800302 void set_falcons(std::shared_ptr<Falcon> right_front,
303 std::shared_ptr<Falcon> right_back,
304 std::shared_ptr<Falcon> right_under,
305 std::shared_ptr<Falcon> left_front,
306 std::shared_ptr<Falcon> left_back,
307 std::shared_ptr<Falcon> left_under,
308 std::shared_ptr<Falcon> roller_falcon) {
309 right_front_ = std::move(right_front);
310 right_back_ = std::move(right_back);
311 right_under_ = std::move(right_under);
312 left_front_ = std::move(left_front);
313 left_back_ = std::move(left_back);
314 left_under_ = std::move(left_under);
315 roller_falcon_ = std::move(roller_falcon);
316 }
317
318 std::optional<superstructure::CANFalconT> roller_falcon_data() {
319 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
320 return roller_falcon_data_;
321 }
322
323 private:
324 void Loop() {
milind-u738832d2023-02-24 19:55:54 -0800325 ctre::phoenix::StatusCode status =
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700326 ctre::phoenixpro::BaseStatusSignalValue::WaitForAll(2000_ms, signals_);
milind-u738832d2023-02-24 19:55:54 -0800327
328 if (!status.IsOK()) {
329 AOS_LOG(ERROR, "Failed to read signals from falcons: %s: %s",
330 status.GetName(), status.GetDescription());
331 }
332
333 auto builder = can_position_sender_.MakeBuilder();
334
335 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
336 left_back_, left_under_, roller_falcon_}) {
337 falcon->RefreshNontimesyncedSignals();
338 }
339
340 aos::SizedArray<flatbuffers::Offset<drivetrain::CANFalcon>, kCANFalconCount>
341 falcons;
342
343 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
344 left_back_, left_under_}) {
345 falcons.push_back(falcon->WritePosition(builder.fbb()));
346 }
347
348 auto falcons_list =
349 builder.fbb()->CreateVector<flatbuffers::Offset<drivetrain::CANFalcon>>(
350 falcons);
351
352 drivetrain::CANPosition::Builder can_position_builder =
353 builder.MakeBuilder<drivetrain::CANPosition>();
354
355 can_position_builder.add_falcons(falcons_list);
356 can_position_builder.add_timestamp(right_front_->GetTimestamp());
357 can_position_builder.add_status(static_cast<int>(status));
358
359 builder.CheckOk(builder.Send(can_position_builder.Finish()));
360
361 {
362 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
363 superstructure::CANFalconT roller_falcon_data;
364 roller_falcon_data.id = roller_falcon_->device_id();
365 roller_falcon_data.supply_current = roller_falcon_->supply_current();
Austin Schuh23a90022023-02-24 22:13:39 -0800366 roller_falcon_data.torque_current = -roller_falcon_->torque_current();
milind-u738832d2023-02-24 19:55:54 -0800367 roller_falcon_data.supply_voltage = roller_falcon_->supply_voltage();
368 roller_falcon_data.device_temp = roller_falcon_->device_temp();
Austin Schuh23a90022023-02-24 22:13:39 -0800369 roller_falcon_data.position = -roller_falcon_->position();
Ravago Jones088ca772023-03-25 22:14:24 -0700370 roller_falcon_data.duty_cycle = roller_falcon_->duty_cycle();
milind-u738832d2023-02-24 19:55:54 -0800371 roller_falcon_data_ =
372 std::make_optional<superstructure::CANFalconT>(roller_falcon_data);
373 }
374 }
375
376 aos::EventLoop *event_loop_;
377
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700378 const std::vector<ctre::phoenixpro::BaseStatusSignalValue *> signals_;
milind-u738832d2023-02-24 19:55:54 -0800379 aos::Sender<drivetrain::CANPosition> can_position_sender_;
380
381 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
382 left_back_, left_under_, roller_falcon_;
383
384 std::optional<superstructure::CANFalconT> roller_falcon_data_;
385
386 aos::stl_mutex roller_mutex_;
387
388 // Pointer to the timer handler used to modify the wakeup.
389 ::aos::TimerHandler *timer_handler_;
390};
391
Maxwell Hendersonad312342023-01-10 12:07:47 -0800392// Class to send position messages with sensor readings to our loops.
393class SensorReader : public ::frc971::wpilib::SensorReader {
394 public:
395 SensorReader(::aos::ShmEventLoop *event_loop,
milind-u738832d2023-02-24 19:55:54 -0800396 std::shared_ptr<const Values> values,
397 CANSensorReader *can_sensor_reader)
Maxwell Hendersonad312342023-01-10 12:07:47 -0800398 : ::frc971::wpilib::SensorReader(event_loop),
399 values_(std::move(values)),
400 auto_mode_sender_(
401 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
402 "/autonomous")),
403 superstructure_position_sender_(
404 event_loop->MakeSender<superstructure::Position>(
405 "/superstructure")),
406 drivetrain_position_sender_(
407 event_loop
408 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
409 "/drivetrain")),
410 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
milind-u738832d2023-02-24 19:55:54 -0800411 "/drivetrain")),
412 can_sensor_reader_(can_sensor_reader) {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800413 // Set to filter out anything shorter than 1/4 of the minimum pulse width
414 // we should ever see.
415 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
Austin Schuh595ffc72023-02-24 16:24:37 -0800416 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
Maxwell Hendersonad312342023-01-10 12:07:47 -0800417 }
418
419 void Start() override {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800420 AddToDMA(&imu_yaw_rate_reader_);
milind-u3a7f9212023-02-24 20:46:59 -0800421 AddToDMA(&cone_position_sensor_);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800422 }
423
424 // Auto mode switches.
425 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
426 autonomous_modes_.at(i) = ::std::move(sensor);
427 }
428
Ravago Jones2060ee62023-02-03 18:12:24 -0800429 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
430 imu_yaw_rate_input_ = ::std::move(sensor);
431 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
432 }
433
Maxwell Hendersonad312342023-01-10 12:07:47 -0800434 void RunIteration() override {
435 superstructure_reading_->Set(true);
milind-u18934eb2023-02-20 16:28:58 -0800436 {
437 auto builder = superstructure_position_sender_.MakeBuilder();
438 frc971::PotAndAbsolutePositionT proximal;
439 CopyPosition(proximal_encoder_, &proximal,
440 Values::kProximalEncoderCountsPerRevolution(),
441 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh7dcc49b2023-02-21 17:35:10 -0800442 true, values_->arm_proximal.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800443 frc971::PotAndAbsolutePositionT distal;
Maxwell Hendersonce816122023-03-27 20:12:38 -0700444 CopyPosition(
445 distal_encoder_, &distal, Values::kDistalEncoderCountsPerRevolution(),
446 values_->arm_distal.zeroing.one_revolution_distance / (M_PI * 2.0),
447 distal_pot_translate, true, values_->arm_distal.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800448 frc971::PotAndAbsolutePositionT roll_joint;
449 CopyPosition(roll_joint_encoder_, &roll_joint,
450 Values::kRollJointEncoderCountsPerRevolution(),
451 Values::kRollJointEncoderRatio(), roll_joint_pot_translate,
Austin Schuh29d025c2023-03-03 21:41:04 -0800452 false, values_->roll_joint.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800453 frc971::AbsolutePositionT wrist;
454 CopyPosition(wrist_encoder_, &wrist,
455 Values::kWristEncoderCountsPerRevolution(),
Austin Schuhe5248cd2023-03-05 12:46:16 -0800456 values_->wrist.subsystem_params.zeroing_constants
457 .one_revolution_distance /
458 (M_PI * 2.0),
459 values_->wrist_flipped);
milind-u18934eb2023-02-20 16:28:58 -0800460
461 flatbuffers::Offset<frc971::PotAndAbsolutePosition> proximal_offset =
462 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &proximal);
463 flatbuffers::Offset<frc971::PotAndAbsolutePosition> distal_offset =
464 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &distal);
milind-u18934eb2023-02-20 16:28:58 -0800465 flatbuffers::Offset<frc971::PotAndAbsolutePosition> roll_joint_offset =
466 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &roll_joint);
milind-u18a901d2023-02-17 21:51:55 -0800467 flatbuffers::Offset<superstructure::ArmPosition> arm_offset =
468 superstructure::CreateArmPosition(*builder.fbb(), proximal_offset,
469 distal_offset, roll_joint_offset);
milind-u18934eb2023-02-20 16:28:58 -0800470 flatbuffers::Offset<frc971::AbsolutePosition> wrist_offset =
471 frc971::AbsolutePosition::Pack(*builder.fbb(), &wrist);
472
milind-u738832d2023-02-24 19:55:54 -0800473 flatbuffers::Offset<superstructure::CANFalcon> roller_falcon_offset;
474 auto optional_roller_falcon = can_sensor_reader_->roller_falcon_data();
475 if (optional_roller_falcon.has_value()) {
476 roller_falcon_offset = superstructure::CANFalcon::Pack(
477 *builder.fbb(), &optional_roller_falcon.value());
478 }
479
milind-u18934eb2023-02-20 16:28:58 -0800480 superstructure::Position::Builder position_builder =
481 builder.MakeBuilder<superstructure::Position>();
482
483 position_builder.add_arm(arm_offset);
milind-u18934eb2023-02-20 16:28:58 -0800484 position_builder.add_wrist(wrist_offset);
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800485 position_builder.add_end_effector_cube_beam_break(
486 end_effector_cube_beam_break_->Get());
milind-u3a7f9212023-02-24 20:46:59 -0800487 position_builder.add_cone_position(cone_position_sensor_.last_width() /
488 cone_position_sensor_.last_period());
milind-u738832d2023-02-24 19:55:54 -0800489 if (!roller_falcon_offset.IsNull()) {
490 position_builder.add_roller_falcon(roller_falcon_offset);
491 }
Henry Speisere139f802023-02-21 14:14:48 -0800492 builder.CheckOk(builder.Send(position_builder.Finish()));
milind-u18934eb2023-02-20 16:28:58 -0800493 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800494
495 {
496 auto builder = drivetrain_position_sender_.MakeBuilder();
497 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
498 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
499 drivetrain_builder.add_left_encoder(
500 constants::Values::DrivetrainEncoderToMeters(
501 drivetrain_left_encoder_->GetRaw()));
502 drivetrain_builder.add_left_speed(
503 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
504
505 drivetrain_builder.add_right_encoder(
506 -constants::Values::DrivetrainEncoderToMeters(
507 drivetrain_right_encoder_->GetRaw()));
508 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
509 drivetrain_right_encoder_->GetPeriod()));
510
511 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
512 }
513
514 {
515 auto builder = gyro_sender_.MakeBuilder();
516 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
517 builder.MakeBuilder<::frc971::sensors::GyroReading>();
518 // +/- 2000 deg / sec
519 constexpr double kMaxVelocity = 4000; // degrees / second
520 constexpr double kVelocityRadiansPerSecond =
521 kMaxVelocity / 360 * (2.0 * M_PI);
522
523 // Only part of the full range is used to prevent being 100% on or off.
524 constexpr double kScaledRangeLow = 0.1;
525 constexpr double kScaledRangeHigh = 0.9;
526
527 constexpr double kPWMFrequencyHz = 200;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800528 double velocity_duty_cycle =
529 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
530
531 constexpr double kDutyCycleScale =
532 1 / (kScaledRangeHigh - kScaledRangeLow);
533 // scale from 0.1 - 0.9 to 0 - 1
Maxwell Hendersonad312342023-01-10 12:07:47 -0800534 double rescaled_velocity_duty_cycle =
535 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
536
Maxwell Hendersonad312342023-01-10 12:07:47 -0800537 if (!std::isnan(rescaled_velocity_duty_cycle)) {
538 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
539 kVelocityRadiansPerSecond);
540 }
541 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
542 }
543
544 {
545 auto builder = auto_mode_sender_.MakeBuilder();
546
547 uint32_t mode = 0;
548 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
549 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
550 mode |= 1 << i;
551 }
552 }
553
554 auto auto_mode_builder =
555 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
556
557 auto_mode_builder.add_mode(mode);
558
559 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
560 }
561 }
562
563 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
564
565 void set_superstructure_reading(
566 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
567 superstructure_reading_ = superstructure_reading;
568 }
569
milind-u18934eb2023-02-20 16:28:58 -0800570 void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
571 fast_encoder_filter_.Add(encoder.get());
572 proximal_encoder_.set_encoder(::std::move(encoder));
573 }
574
575 void set_proximal_absolute_pwm(
576 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
577 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
578 }
579
580 void set_proximal_potentiometer(
581 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
582 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
583 }
584
585 void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
586 fast_encoder_filter_.Add(encoder.get());
587 distal_encoder_.set_encoder(::std::move(encoder));
588 }
589
590 void set_distal_absolute_pwm(
591 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
592 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
593 }
594
595 void set_distal_potentiometer(
596 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
597 distal_encoder_.set_potentiometer(::std::move(potentiometer));
598 }
599
600 void set_roll_joint_encoder(::std::unique_ptr<frc::Encoder> encoder) {
601 fast_encoder_filter_.Add(encoder.get());
602 roll_joint_encoder_.set_encoder(::std::move(encoder));
603 }
604
605 void set_roll_joint_absolute_pwm(
606 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
607 roll_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
608 }
609
610 void set_roll_joint_potentiometer(
611 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
612 roll_joint_encoder_.set_potentiometer(::std::move(potentiometer));
613 }
614
615 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
616 fast_encoder_filter_.Add(encoder.get());
617 wrist_encoder_.set_encoder(::std::move(encoder));
618 }
619
620 void set_wrist_absolute_pwm(
621 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
622 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
623 }
624
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800625 void set_end_effector_cube_beam_break(
626 ::std::unique_ptr<frc::DigitalInput> sensor) {
627 end_effector_cube_beam_break_ = ::std::move(sensor);
628 }
629
milind-u3a7f9212023-02-24 20:46:59 -0800630 void set_cone_position_sensor(::std::unique_ptr<frc::DigitalInput> sensor) {
631 cone_position_input_ = ::std::move(sensor);
632 cone_position_sensor_.set_input(cone_position_input_.get());
633 }
634
Maxwell Hendersonad312342023-01-10 12:07:47 -0800635 private:
636 std::shared_ptr<const Values> values_;
637
638 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
639 aos::Sender<superstructure::Position> superstructure_position_sender_;
640 aos::Sender<frc971::control_loops::drivetrain::Position>
641 drivetrain_position_sender_;
642 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
643
644 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
645
Ravago Jones2544ad82023-03-04 22:24:49 -0800646 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_,
milind-u3a7f9212023-02-24 20:46:59 -0800647 end_effector_cube_beam_break_;
Ravago Jones2060ee62023-02-03 18:12:24 -0800648
Ravago Jones2544ad82023-03-04 22:24:49 -0800649 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
milind-u18934eb2023-02-20 16:28:58 -0800650
651 frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
652 distal_encoder_, roll_joint_encoder_;
653 frc971::wpilib::AbsoluteEncoder wrist_encoder_;
milind-u3a7f9212023-02-24 20:46:59 -0800654
655 frc971::wpilib::DMAPulseWidthReader cone_position_sensor_;
656 std::unique_ptr<frc::DigitalInput> cone_position_input_;
milind-u738832d2023-02-24 19:55:54 -0800657
658 CANSensorReader *can_sensor_reader_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800659};
660
661class SuperstructureWriter
662 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
663 public:
664 SuperstructureWriter(aos::EventLoop *event_loop)
665 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
milind-u32d29d32023-02-24 21:11:51 -0800666 event_loop, "/superstructure") {
667 event_loop->SetRuntimeRealtimePriority(
668 constants::Values::kDrivetrainWriterPriority);
669 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800670
671 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
672
673 void set_superstructure_reading(
674 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
675 superstructure_reading_ = superstructure_reading;
676 }
677
milind-u18934eb2023-02-20 16:28:58 -0800678 void set_proximal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
679 proximal_falcon_ = ::std::move(t);
680 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800681
milind-u18934eb2023-02-20 16:28:58 -0800682 void set_distal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
683 distal_falcon_ = ::std::move(t);
684 }
685
686 void set_roll_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
687 roll_joint_victor_ = ::std::move(t);
688 }
689
690 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
691 wrist_victor_ = ::std::move(t);
692 }
693
milind-u18934eb2023-02-20 16:28:58 -0800694 private:
695 void Stop() override {
696 AOS_LOG(WARNING, "Superstructure output too old.\n");
697 proximal_falcon_->SetDisabled();
698 distal_falcon_->SetDisabled();
699 roll_joint_victor_->SetDisabled();
700 wrist_victor_->SetDisabled();
milind-u18934eb2023-02-20 16:28:58 -0800701 }
702
703 void Write(const superstructure::Output &output) override {
704 WritePwm(output.proximal_voltage(), proximal_falcon_.get());
705 WritePwm(output.distal_voltage(), distal_falcon_.get());
Austin Schuh3fd5f0e2023-02-22 11:10:37 -0800706 WritePwm(-output.roll_joint_voltage(), roll_joint_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800707 WritePwm(output.wrist_voltage(), wrist_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800708 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800709
710 static void WriteCan(const double voltage,
711 ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) {
712 falcon->Set(
713 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
714 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
715 }
716
717 template <typename T>
718 static void WritePwm(const double voltage, T *motor) {
719 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
720 12.0);
721 }
milind-u18934eb2023-02-20 16:28:58 -0800722
723 ::std::unique_ptr<::frc::TalonFX> proximal_falcon_, distal_falcon_;
724 ::std::unique_ptr<::frc::VictorSP> roll_joint_victor_, wrist_victor_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800725};
726
milind-u32d29d32023-02-24 21:11:51 -0800727class SuperstructureCANWriter
728 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
729 public:
730 SuperstructureCANWriter(::aos::EventLoop *event_loop)
731 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
732 event_loop, "/superstructure") {
733 event_loop->SetRuntimeRealtimePriority(
734 constants::Values::kSuperstructureCANWriterPriority);
735
736 event_loop->OnRun([this]() { WriteConfigs(); });
737 };
738
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800739 void HandleCANConfiguration(const CANConfiguration &configuration) {
740 roller_falcon_->PrintConfigs();
741 if (configuration.reapply()) {
742 WriteConfigs();
743 }
744 }
745
milind-u32d29d32023-02-24 21:11:51 -0800746 void set_roller_falcon(std::shared_ptr<Falcon> roller_falcon) {
747 roller_falcon_ = std::move(roller_falcon);
748 }
749
750 private:
751 void WriteConfigs() { roller_falcon_->WriteRollerConfigs(); }
752
753 void Write(const superstructure::Output &output) override {
754 ctre::phoenixpro::controls::DutyCycleOut roller_control(
755 SafeSpeed(-output.roller_voltage()));
756 roller_control.UpdateFreqHz = 0_Hz;
757 roller_control.EnableFOC = true;
758
759 ctre::phoenix::StatusCode status =
760 roller_falcon_->talon()->SetControl(roller_control);
761
762 if (!status.IsOK()) {
763 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
764 status.GetName(), status.GetDescription());
765 }
766 }
767
768 void Stop() override {
769 AOS_LOG(WARNING, "Superstructure CAN output too old.\n");
Austin Schuh1780e002023-03-03 21:39:26 -0800770 ctre::phoenixpro::controls::DutyCycleOut stop_command(0.0);
771 stop_command.UpdateFreqHz = 0_Hz;
772 stop_command.EnableFOC = true;
milind-u32d29d32023-02-24 21:11:51 -0800773
774 roller_falcon_->talon()->SetControl(stop_command);
775 }
776
777 double SafeSpeed(double voltage) {
778 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
779 }
780
781 std::shared_ptr<Falcon> roller_falcon_;
782};
783
Ravago Jones2060ee62023-02-03 18:12:24 -0800784class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler<
785 ::frc971::control_loops::drivetrain::Output> {
786 public:
787 DrivetrainWriter(::aos::EventLoop *event_loop)
788 : ::frc971::wpilib::LoopOutputHandler<
789 ::frc971::control_loops::drivetrain::Output>(event_loop,
790 "/drivetrain") {
791 event_loop->SetRuntimeRealtimePriority(
792 constants::Values::kDrivetrainWriterPriority);
793
Ravago Jones2060ee62023-02-03 18:12:24 -0800794 event_loop->OnRun([this]() { WriteConfigs(); });
795 }
796
797 void set_falcons(std::shared_ptr<Falcon> right_front,
798 std::shared_ptr<Falcon> right_back,
799 std::shared_ptr<Falcon> right_under,
800 std::shared_ptr<Falcon> left_front,
801 std::shared_ptr<Falcon> left_back,
802 std::shared_ptr<Falcon> left_under) {
803 right_front_ = std::move(right_front);
804 right_back_ = std::move(right_back);
805 right_under_ = std::move(right_under);
806 left_front_ = std::move(left_front);
807 left_back_ = std::move(left_back);
808 left_under_ = std::move(left_under);
809 }
810
811 void set_right_inverted(ctre::phoenixpro::signals::InvertedValue invert) {
812 right_inverted_ = invert;
813 }
814
815 void set_left_inverted(ctre::phoenixpro::signals::InvertedValue invert) {
816 left_inverted_ = invert;
817 }
818
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800819 void HandleCANConfiguration(const CANConfiguration &configuration) {
820 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
821 left_back_, left_under_}) {
822 falcon->PrintConfigs();
823 }
824 if (configuration.reapply()) {
825 WriteConfigs();
826 }
827 }
828
Ravago Jones2060ee62023-02-03 18:12:24 -0800829 private:
830 void WriteConfigs() {
831 for (auto falcon :
832 {right_front_.get(), right_back_.get(), right_under_.get()}) {
833 falcon->WriteConfigs(right_inverted_);
834 }
835
836 for (auto falcon :
837 {left_front_.get(), left_back_.get(), left_under_.get()}) {
838 falcon->WriteConfigs(left_inverted_);
839 }
840 }
841
842 void Write(
843 const ::frc971::control_loops::drivetrain::Output &output) override {
844 ctre::phoenixpro::controls::DutyCycleOut left_control(
845 SafeSpeed(output.left_voltage()));
846 left_control.UpdateFreqHz = 0_Hz;
847 left_control.EnableFOC = true;
848
849 ctre::phoenixpro::controls::DutyCycleOut right_control(
850 SafeSpeed(output.right_voltage()));
851 right_control.UpdateFreqHz = 0_Hz;
852 right_control.EnableFOC = true;
853
854 for (auto falcon :
855 {left_front_.get(), left_back_.get(), left_under_.get()}) {
856 ctre::phoenix::StatusCode status =
857 falcon->talon()->SetControl(left_control);
858
859 if (!status.IsOK()) {
860 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
861 status.GetName(), status.GetDescription());
862 }
863 }
864
865 for (auto falcon :
866 {right_front_.get(), right_back_.get(), right_under_.get()}) {
867 ctre::phoenix::StatusCode status =
868 falcon->talon()->SetControl(right_control);
869
870 if (!status.IsOK()) {
871 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
872 status.GetName(), status.GetDescription());
873 }
874 }
875 }
876
877 void Stop() override {
878 AOS_LOG(WARNING, "drivetrain output too old\n");
Austin Schuh1780e002023-03-03 21:39:26 -0800879 ctre::phoenixpro::controls::DutyCycleOut stop_command(0.0);
880 stop_command.UpdateFreqHz = 0_Hz;
881 stop_command.EnableFOC = true;
882
Ravago Jones2060ee62023-02-03 18:12:24 -0800883 for (auto falcon :
884 {right_front_.get(), right_back_.get(), right_under_.get(),
885 left_front_.get(), left_back_.get(), left_under_.get()}) {
886 falcon->talon()->SetControl(stop_command);
887 }
888 }
889
890 double SafeSpeed(double voltage) {
891 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
892 }
893
894 ctre::phoenixpro::signals::InvertedValue left_inverted_, right_inverted_;
895 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
896 left_back_, left_under_;
897};
Maxwell Hendersonad312342023-01-10 12:07:47 -0800898
899class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
900 public:
901 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
902 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
903 frc::Encoder::k4X);
904 }
905
906 void Run() override {
907 std::shared_ptr<const Values> values =
908 std::make_shared<const Values>(constants::MakeValues());
909
910 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
911 aos::configuration::ReadConfig("aos_config.json");
912
913 // Thread 1.
Ravago Jones2060ee62023-02-03 18:12:24 -0800914 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
915 ::frc971::wpilib::JoystickSender joystick_sender(
916 &joystick_sender_event_loop);
917 AddLoop(&joystick_sender_event_loop);
918
Maxwell Hendersonad312342023-01-10 12:07:47 -0800919 // Thread 2.
920 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
921 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
922 AddLoop(&pdp_fetcher_event_loop);
923
924 std::shared_ptr<frc::DigitalOutput> superstructure_reading =
925 make_unique<frc::DigitalOutput>(25);
926
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700927 std::vector<ctre::phoenixpro::BaseStatusSignalValue *> signals_registry;
928 std::shared_ptr<Falcon> right_front =
929 std::make_shared<Falcon>(1, "Drivetrain Bus", &signals_registry);
930 std::shared_ptr<Falcon> right_back =
931 std::make_shared<Falcon>(2, "Drivetrain Bus", &signals_registry);
932 std::shared_ptr<Falcon> right_under =
933 std::make_shared<Falcon>(3, "Drivetrain Bus", &signals_registry);
934 std::shared_ptr<Falcon> left_front =
935 std::make_shared<Falcon>(4, "Drivetrain Bus", &signals_registry);
936 std::shared_ptr<Falcon> left_back =
937 std::make_shared<Falcon>(5, "Drivetrain Bus", &signals_registry);
938 std::shared_ptr<Falcon> left_under =
939 std::make_shared<Falcon>(6, "Drivetrain Bus", &signals_registry);
940 std::shared_ptr<Falcon> roller =
941 std::make_shared<Falcon>(13, "Drivetrain Bus", &signals_registry);
942
Maxwell Hendersonad312342023-01-10 12:07:47 -0800943 // Thread 3.
milind-u738832d2023-02-24 19:55:54 -0800944 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
945 can_sensor_reader_event_loop.set_name("CANSensorReader");
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700946 CANSensorReader can_sensor_reader(&can_sensor_reader_event_loop,
947 std::move(signals_registry));
milind-u738832d2023-02-24 19:55:54 -0800948
949 can_sensor_reader.set_falcons(right_front, right_back, right_under,
950 left_front, left_back, left_under, roller);
951
952 AddLoop(&can_sensor_reader_event_loop);
953
954 // Thread 4.
Maxwell Hendersonad312342023-01-10 12:07:47 -0800955 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
milind-u738832d2023-02-24 19:55:54 -0800956 SensorReader sensor_reader(&sensor_reader_event_loop, values,
957 &can_sensor_reader);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800958 sensor_reader.set_pwm_trigger(true);
959 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
960 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
961 sensor_reader.set_superstructure_reading(superstructure_reading);
Henry Speisere139f802023-02-21 14:14:48 -0800962 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Ravago Jones2060ee62023-02-03 18:12:24 -0800963
milind-u18934eb2023-02-20 16:28:58 -0800964 sensor_reader.set_proximal_encoder(make_encoder(3));
965 sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(3));
966 sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(3));
967
Henry Speisere139f802023-02-21 14:14:48 -0800968 sensor_reader.set_distal_encoder(make_encoder(2));
969 sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(2));
970 sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(2));
milind-u18934eb2023-02-20 16:28:58 -0800971
Henry Speisere139f802023-02-21 14:14:48 -0800972 sensor_reader.set_roll_joint_encoder(make_encoder(5));
milind-u18934eb2023-02-20 16:28:58 -0800973 sensor_reader.set_roll_joint_absolute_pwm(
Henry Speisere139f802023-02-21 14:14:48 -0800974 make_unique<frc::DigitalInput>(5));
milind-u18934eb2023-02-20 16:28:58 -0800975 sensor_reader.set_roll_joint_potentiometer(
Henry Speisere139f802023-02-21 14:14:48 -0800976 make_unique<frc::AnalogInput>(5));
milind-u18934eb2023-02-20 16:28:58 -0800977
Henry Speisere139f802023-02-21 14:14:48 -0800978 sensor_reader.set_wrist_encoder(make_encoder(4));
979 sensor_reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(4));
milind-u18934eb2023-02-20 16:28:58 -0800980
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800981 sensor_reader.set_end_effector_cube_beam_break(
982 make_unique<frc::DigitalInput>(7));
milind-u3a7f9212023-02-24 20:46:59 -0800983 sensor_reader.set_cone_position_sensor(make_unique<frc::DigitalInput>(8));
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800984
Maxwell Hendersonad312342023-01-10 12:07:47 -0800985 AddLoop(&sensor_reader_event_loop);
986
Ravago Jones2060ee62023-02-03 18:12:24 -0800987 // Thread 5.
milind-u32d29d32023-02-24 21:11:51 -0800988 // Setup CAN.
989 if (!FLAGS_ctre_diag_server) {
990 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
991 c_Phoenix_Diagnostics_Dispose();
992 }
993
994 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
995 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
996 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
997 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
998
999 ::aos::ShmEventLoop can_output_event_loop(&config.message());
1000 can_output_event_loop.set_name("CANOutputWriter");
1001 DrivetrainWriter drivetrain_writer(&can_output_event_loop);
Ravago Jones2060ee62023-02-03 18:12:24 -08001002
1003 drivetrain_writer.set_falcons(right_front, right_back, right_under,
1004 left_front, left_back, left_under);
1005 drivetrain_writer.set_right_inverted(
1006 ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive);
1007 drivetrain_writer.set_left_inverted(
1008 ctre::phoenixpro::signals::InvertedValue::CounterClockwise_Positive);
milind-u32d29d32023-02-24 21:11:51 -08001009
1010 SuperstructureCANWriter superstructure_can_writer(&can_output_event_loop);
1011 superstructure_can_writer.set_roller_falcon(roller);
1012
Austin Schuhbb4c9ac2023-02-28 22:04:20 -08001013 can_output_event_loop.MakeWatcher(
1014 "/roborio", [&drivetrain_writer, &superstructure_can_writer](
1015 const CANConfiguration &configuration) {
1016 drivetrain_writer.HandleCANConfiguration(configuration);
1017 superstructure_can_writer.HandleCANConfiguration(configuration);
1018 });
1019
milind-u32d29d32023-02-24 21:11:51 -08001020 AddLoop(&can_output_event_loop);
1021
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001022 // Thread 6
1023 // Setup superstructure output
milind-u32d29d32023-02-24 21:11:51 -08001024 ::aos::ShmEventLoop output_event_loop(&config.message());
1025 output_event_loop.set_name("PWMOutputWriter");
Maxwell Hendersonad312342023-01-10 12:07:47 -08001026 SuperstructureWriter superstructure_writer(&output_event_loop);
1027
Henry Speisere139f802023-02-21 14:14:48 -08001028 superstructure_writer.set_proximal_falcon(make_unique<::frc::TalonFX>(1));
1029 superstructure_writer.set_distal_falcon(make_unique<::frc::TalonFX>(0));
milind-u18934eb2023-02-20 16:28:58 -08001030
1031 superstructure_writer.set_roll_joint_victor(
Henry Speisere139f802023-02-21 14:14:48 -08001032 make_unique<::frc::VictorSP>(3));
1033 superstructure_writer.set_wrist_victor(make_unique<::frc::VictorSP>(2));
1034
Maxwell Hendersonad312342023-01-10 12:07:47 -08001035 superstructure_writer.set_superstructure_reading(superstructure_reading);
1036
1037 AddLoop(&output_event_loop);
1038
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001039 // Thread 7
1040 // Setup led_indicator
1041 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
1042 led_indicator_event_loop.set_name("LedIndicator");
1043 control_loops::superstructure::LedIndicator led_indicator(
1044 &led_indicator_event_loop);
1045 AddLoop(&led_indicator_event_loop);
1046
Maxwell Hendersonad312342023-01-10 12:07:47 -08001047 RunLoops();
1048 }
1049};
1050
1051} // namespace wpilib
1052} // namespace y2023
1053
1054AOS_ROBOT_CLASS(::y2023::wpilib::WPILibRobot);