blob: 978d6e5750229966038c390d950bacc2d394dd79 [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"
Philipp Schrader790cb542023-07-05 21:06:52 -070015
Maxwell Hendersonad312342023-01-10 12:07:47 -080016#include "frc971/wpilib/ahal/AnalogInput.h"
17#include "frc971/wpilib/ahal/Counter.h"
18#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
19#include "frc971/wpilib/ahal/DriverStation.h"
20#include "frc971/wpilib/ahal/Encoder.h"
21#include "frc971/wpilib/ahal/Servo.h"
22#include "frc971/wpilib/ahal/TalonFX.h"
23#include "frc971/wpilib/ahal/VictorSP.h"
24#undef ERROR
25
Philipp Schrader790cb542023-07-05 21:06:52 -070026#include "ctre/phoenix/cci/Diagnostics_CCI.h"
27#include "ctre/phoenix/motorcontrol/can/TalonFX.h"
28#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
29#include "ctre/phoenixpro/TalonFX.hpp"
30
Maxwell Hendersonad312342023-01-10 12:07:47 -080031#include "aos/commonmath.h"
Ravago Jones2060ee62023-02-03 18:12:24 -080032#include "aos/containers/sized_array.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080033#include "aos/events/event_loop.h"
34#include "aos/events/shm_event_loop.h"
35#include "aos/init.h"
36#include "aos/logging/logging.h"
37#include "aos/realtime.h"
38#include "aos/time/time.h"
39#include "aos/util/log_interval.h"
40#include "aos/util/phased_loop.h"
41#include "aos/util/wrapping_counter.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080042#include "frc971/autonomous/auto_mode_generated.h"
43#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
44#include "frc971/input/robot_state_generated.h"
45#include "frc971/queues/gyro_generated.h"
46#include "frc971/wpilib/ADIS16448.h"
47#include "frc971/wpilib/buffered_pcm.h"
48#include "frc971/wpilib/buffered_solenoid.h"
49#include "frc971/wpilib/dma.h"
50#include "frc971/wpilib/drivetrain_writer.h"
51#include "frc971/wpilib/encoder_and_potentiometer.h"
52#include "frc971/wpilib/joystick_sender.h"
53#include "frc971/wpilib/logging_generated.h"
54#include "frc971/wpilib/loop_output_handler.h"
55#include "frc971/wpilib/pdp_fetcher.h"
56#include "frc971/wpilib/sensor_reader.h"
57#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuhbb4c9ac2023-02-28 22:04:20 -080058#include "y2023/can_configuration_generated.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080059#include "y2023/constants.h"
Ravago Jones2060ee62023-02-03 18:12:24 -080060#include "y2023/control_loops/drivetrain/drivetrain_can_position_generated.h"
Maxwell Henderson2a2faa62023-03-11 15:05:46 -080061#include "y2023/control_loops/superstructure/led_indicator.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080062#include "y2023/control_loops/superstructure/superstructure_output_generated.h"
63#include "y2023/control_loops/superstructure/superstructure_position_generated.h"
64
Ravago Jones2060ee62023-02-03 18:12:24 -080065DEFINE_bool(ctre_diag_server, false,
66 "If true, enable the diagnostics server for interacting with "
67 "devices on the CAN bus using Phoenix Tuner");
68
Maxwell Hendersonad312342023-01-10 12:07:47 -080069using ::aos::monotonic_clock;
70using ::y2023::constants::Values;
71namespace superstructure = ::y2023::control_loops::superstructure;
Ravago Jones2060ee62023-02-03 18:12:24 -080072namespace drivetrain = ::y2023::control_loops::drivetrain;
Maxwell Hendersonad312342023-01-10 12:07:47 -080073namespace chrono = ::std::chrono;
74using std::make_unique;
75
76namespace y2023 {
77namespace wpilib {
78namespace {
79
80constexpr double kMaxBringupPower = 12.0;
81
82// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
83// DMA stuff and then removing the * 2.0 in *_translate.
84// The low bit is direction.
85
86double drivetrain_velocity_translate(double in) {
87 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
88 (2.0 * M_PI)) *
89 Values::kDrivetrainEncoderRatio() *
90 control_loops::drivetrain::kWheelRadius;
91}
92
milind-u18934eb2023-02-20 16:28:58 -080093double proximal_pot_translate(double voltage) {
94 return voltage * Values::kProximalPotRadiansPerVolt();
95}
96
97double distal_pot_translate(double voltage) {
98 return voltage * Values::kDistalPotRadiansPerVolt();
99}
100
101double roll_joint_pot_translate(double voltage) {
102 return voltage * Values::kRollJointPotRadiansPerVolt();
103}
104
105constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
106 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
107 Values::kMaxProximalEncoderPulsesPerSecond(),
108 Values::kMaxDistalEncoderPulsesPerSecond(),
109 Values::kMaxRollJointEncoderPulsesPerSecond(),
Austin Schuhe5248cd2023-03-05 12:46:16 -0800110 Values::kMaxCompWristEncoderPulsesPerSecond(),
111 Values::kMaxPracticeWristEncoderPulsesPerSecond(),
milind-u18934eb2023-02-20 16:28:58 -0800112});
113static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
114 "fast encoders are too fast");
Maxwell Hendersonad312342023-01-10 12:07:47 -0800115
116} // namespace
117
milind-u738832d2023-02-24 19:55:54 -0800118static constexpr int kCANFalconCount = 6;
milind-u738832d2023-02-24 19:55:54 -0800119static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz;
120
121class Falcon {
122 public:
123 Falcon(int device_id, std::string canbus,
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700124 std::vector<ctre::phoenixpro::BaseStatusSignalValue *> *signals)
milind-u738832d2023-02-24 19:55:54 -0800125 : talon_(device_id, canbus),
126 device_id_(device_id),
127 device_temp_(talon_.GetDeviceTemp()),
128 supply_voltage_(talon_.GetSupplyVoltage()),
129 supply_current_(talon_.GetSupplyCurrent()),
130 torque_current_(talon_.GetTorqueCurrent()),
Ravago Jones088ca772023-03-25 22:14:24 -0700131 position_(talon_.GetPosition()),
132 duty_cycle_(talon_.GetDutyCycle()) {
milind-u738832d2023-02-24 19:55:54 -0800133 // device temp is not timesynced so don't add it to the list of signals
134 device_temp_.SetUpdateFrequency(kCANUpdateFreqHz);
135
milind-u738832d2023-02-24 19:55:54 -0800136 CHECK_NOTNULL(signals);
milind-u738832d2023-02-24 19:55:54 -0800137
138 supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz);
139 signals->push_back(&supply_voltage_);
140
141 supply_current_.SetUpdateFrequency(kCANUpdateFreqHz);
142 signals->push_back(&supply_current_);
143
144 torque_current_.SetUpdateFrequency(kCANUpdateFreqHz);
145 signals->push_back(&torque_current_);
146
147 position_.SetUpdateFrequency(kCANUpdateFreqHz);
148 signals->push_back(&position_);
Ravago Jones088ca772023-03-25 22:14:24 -0700149
150 duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz);
151 signals->push_back(&duty_cycle_);
milind-u738832d2023-02-24 19:55:54 -0800152 }
153
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800154 void PrintConfigs() {
155 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
156 ctre::phoenix::StatusCode status =
157 talon_.GetConfigurator().Refresh(configuration);
158 if (!status.IsOK()) {
159 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
160 status.GetName(), status.GetDescription());
161 }
162 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
163 }
164
milind-u738832d2023-02-24 19:55:54 -0800165 void WriteConfigs(ctre::phoenixpro::signals::InvertedValue invert) {
166 inverted_ = invert;
167
168 ctre::phoenixpro::configs::CurrentLimitsConfigs current_limits;
169 current_limits.StatorCurrentLimit =
170 constants::Values::kDrivetrainStatorCurrentLimit();
171 current_limits.StatorCurrentLimitEnable = true;
172 current_limits.SupplyCurrentLimit =
173 constants::Values::kDrivetrainSupplyCurrentLimit();
174 current_limits.SupplyCurrentLimitEnable = true;
175
176 ctre::phoenixpro::configs::MotorOutputConfigs output_configs;
177 output_configs.NeutralMode =
178 ctre::phoenixpro::signals::NeutralModeValue::Brake;
179 output_configs.DutyCycleNeutralDeadband = 0;
180
181 output_configs.Inverted = inverted_;
182
183 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
184 configuration.CurrentLimits = current_limits;
185 configuration.MotorOutput = output_configs;
186
187 ctre::phoenix::StatusCode status =
188 talon_.GetConfigurator().Apply(configuration);
189 if (!status.IsOK()) {
190 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
191 status.GetName(), status.GetDescription());
192 }
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800193
194 PrintConfigs();
milind-u738832d2023-02-24 19:55:54 -0800195 }
196
197 void WriteRollerConfigs() {
198 ctre::phoenixpro::configs::CurrentLimitsConfigs current_limits;
199 current_limits.StatorCurrentLimit =
200 constants::Values::kRollerStatorCurrentLimit();
201 current_limits.StatorCurrentLimitEnable = true;
202 current_limits.SupplyCurrentLimit =
203 constants::Values::kRollerSupplyCurrentLimit();
204 current_limits.SupplyCurrentLimitEnable = true;
205
206 ctre::phoenixpro::configs::MotorOutputConfigs output_configs;
207 output_configs.NeutralMode =
208 ctre::phoenixpro::signals::NeutralModeValue::Brake;
209 output_configs.DutyCycleNeutralDeadband = 0;
210
211 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
212 configuration.CurrentLimits = current_limits;
213 configuration.MotorOutput = output_configs;
214
215 ctre::phoenix::StatusCode status =
216 talon_.GetConfigurator().Apply(configuration);
217 if (!status.IsOK()) {
218 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
219 status.GetName(), status.GetDescription());
220 }
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800221
222 PrintConfigs();
milind-u738832d2023-02-24 19:55:54 -0800223 }
224
225 ctre::phoenixpro::hardware::TalonFX *talon() { return &talon_; }
226
227 flatbuffers::Offset<drivetrain::CANFalcon> WritePosition(
228 flatbuffers::FlatBufferBuilder *fbb) {
229 drivetrain::CANFalcon::Builder builder(*fbb);
230 builder.add_id(device_id_);
231 builder.add_device_temp(device_temp());
232 builder.add_supply_voltage(supply_voltage());
233 builder.add_supply_current(supply_current());
234 builder.add_torque_current(torque_current());
Ravago Jones088ca772023-03-25 22:14:24 -0700235 builder.add_duty_cycle(duty_cycle());
milind-u738832d2023-02-24 19:55:54 -0800236
237 double invert =
238 (inverted_ ==
239 ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive
240 ? 1
241 : -1);
242
243 builder.add_position(
244 constants::Values::DrivetrainCANEncoderToMeters(position()) * invert);
245
246 return builder.Finish();
247 }
248
249 int device_id() const { return device_id_; }
250 float device_temp() const { return device_temp_.GetValue().value(); }
251 float supply_voltage() const { return supply_voltage_.GetValue().value(); }
252 float supply_current() const { return supply_current_.GetValue().value(); }
253 float torque_current() const { return torque_current_.GetValue().value(); }
Ravago Jones088ca772023-03-25 22:14:24 -0700254 float duty_cycle() const { return duty_cycle_.GetValue().value(); }
milind-u738832d2023-02-24 19:55:54 -0800255 float position() const { return position_.GetValue().value(); }
256
257 // returns the monotonic timestamp of the latest timesynced reading in the
258 // timebase of the the syncronized CAN bus clock.
259 int64_t GetTimestamp() {
260 std::chrono::nanoseconds latest_timestamp =
261 torque_current_.GetTimestamp().GetTime();
262
263 return latest_timestamp.count();
264 }
265
266 void RefreshNontimesyncedSignals() { device_temp_.Refresh(); };
267
268 private:
269 ctre::phoenixpro::hardware::TalonFX talon_;
270 int device_id_;
271
272 ctre::phoenixpro::signals::InvertedValue inverted_;
273
274 ctre::phoenixpro::StatusSignalValue<units::temperature::celsius_t>
275 device_temp_;
276 ctre::phoenixpro::StatusSignalValue<units::voltage::volt_t> supply_voltage_;
277 ctre::phoenixpro::StatusSignalValue<units::current::ampere_t> supply_current_,
278 torque_current_;
279 ctre::phoenixpro::StatusSignalValue<units::angle::turn_t> position_;
Ravago Jones088ca772023-03-25 22:14:24 -0700280 ctre::phoenixpro::StatusSignalValue<units::dimensionless::scalar_t>
281 duty_cycle_;
milind-u738832d2023-02-24 19:55:54 -0800282};
283
284class CANSensorReader {
285 public:
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700286 CANSensorReader(
287 aos::EventLoop *event_loop,
288 std::vector<ctre::phoenixpro::BaseStatusSignalValue *> signals_registry)
milind-u738832d2023-02-24 19:55:54 -0800289 : event_loop_(event_loop),
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700290 signals_(signals_registry.begin(), signals_registry.end()),
milind-u738832d2023-02-24 19:55:54 -0800291 can_position_sender_(
292 event_loop->MakeSender<drivetrain::CANPosition>("/drivetrain")),
293 roller_falcon_data_(std::nullopt) {
294 event_loop->SetRuntimeRealtimePriority(40);
295 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1}));
296 timer_handler_ = event_loop->AddTimer([this]() { Loop(); });
297 timer_handler_->set_name("CANSensorReader Loop");
298
299 event_loop->OnRun([this]() {
Philipp Schradera6712522023-07-05 20:25:11 -0700300 timer_handler_->Schedule(event_loop_->monotonic_now(),
301 1 / kCANUpdateFreqHz);
milind-u738832d2023-02-24 19:55:54 -0800302 });
303 }
304
milind-u738832d2023-02-24 19:55:54 -0800305 void set_falcons(std::shared_ptr<Falcon> right_front,
306 std::shared_ptr<Falcon> right_back,
307 std::shared_ptr<Falcon> right_under,
308 std::shared_ptr<Falcon> left_front,
309 std::shared_ptr<Falcon> left_back,
310 std::shared_ptr<Falcon> left_under,
311 std::shared_ptr<Falcon> roller_falcon) {
312 right_front_ = std::move(right_front);
313 right_back_ = std::move(right_back);
314 right_under_ = std::move(right_under);
315 left_front_ = std::move(left_front);
316 left_back_ = std::move(left_back);
317 left_under_ = std::move(left_under);
318 roller_falcon_ = std::move(roller_falcon);
319 }
320
321 std::optional<superstructure::CANFalconT> roller_falcon_data() {
322 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
323 return roller_falcon_data_;
324 }
325
326 private:
327 void Loop() {
milind-u738832d2023-02-24 19:55:54 -0800328 ctre::phoenix::StatusCode status =
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700329 ctre::phoenixpro::BaseStatusSignalValue::WaitForAll(2000_ms, signals_);
milind-u738832d2023-02-24 19:55:54 -0800330
331 if (!status.IsOK()) {
332 AOS_LOG(ERROR, "Failed to read signals from falcons: %s: %s",
333 status.GetName(), status.GetDescription());
334 }
335
336 auto builder = can_position_sender_.MakeBuilder();
337
338 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
339 left_back_, left_under_, roller_falcon_}) {
340 falcon->RefreshNontimesyncedSignals();
341 }
342
343 aos::SizedArray<flatbuffers::Offset<drivetrain::CANFalcon>, kCANFalconCount>
344 falcons;
345
346 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
347 left_back_, left_under_}) {
348 falcons.push_back(falcon->WritePosition(builder.fbb()));
349 }
350
351 auto falcons_list =
352 builder.fbb()->CreateVector<flatbuffers::Offset<drivetrain::CANFalcon>>(
353 falcons);
354
355 drivetrain::CANPosition::Builder can_position_builder =
356 builder.MakeBuilder<drivetrain::CANPosition>();
357
358 can_position_builder.add_falcons(falcons_list);
359 can_position_builder.add_timestamp(right_front_->GetTimestamp());
360 can_position_builder.add_status(static_cast<int>(status));
361
362 builder.CheckOk(builder.Send(can_position_builder.Finish()));
363
364 {
365 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
366 superstructure::CANFalconT roller_falcon_data;
367 roller_falcon_data.id = roller_falcon_->device_id();
368 roller_falcon_data.supply_current = roller_falcon_->supply_current();
Austin Schuh23a90022023-02-24 22:13:39 -0800369 roller_falcon_data.torque_current = -roller_falcon_->torque_current();
milind-u738832d2023-02-24 19:55:54 -0800370 roller_falcon_data.supply_voltage = roller_falcon_->supply_voltage();
371 roller_falcon_data.device_temp = roller_falcon_->device_temp();
Austin Schuh23a90022023-02-24 22:13:39 -0800372 roller_falcon_data.position = -roller_falcon_->position();
Ravago Jones088ca772023-03-25 22:14:24 -0700373 roller_falcon_data.duty_cycle = roller_falcon_->duty_cycle();
milind-u738832d2023-02-24 19:55:54 -0800374 roller_falcon_data_ =
375 std::make_optional<superstructure::CANFalconT>(roller_falcon_data);
376 }
377 }
378
379 aos::EventLoop *event_loop_;
380
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700381 const std::vector<ctre::phoenixpro::BaseStatusSignalValue *> signals_;
milind-u738832d2023-02-24 19:55:54 -0800382 aos::Sender<drivetrain::CANPosition> can_position_sender_;
383
384 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
385 left_back_, left_under_, roller_falcon_;
386
387 std::optional<superstructure::CANFalconT> roller_falcon_data_;
388
389 aos::stl_mutex roller_mutex_;
390
391 // Pointer to the timer handler used to modify the wakeup.
392 ::aos::TimerHandler *timer_handler_;
393};
394
Maxwell Hendersonad312342023-01-10 12:07:47 -0800395// Class to send position messages with sensor readings to our loops.
396class SensorReader : public ::frc971::wpilib::SensorReader {
397 public:
398 SensorReader(::aos::ShmEventLoop *event_loop,
milind-u738832d2023-02-24 19:55:54 -0800399 std::shared_ptr<const Values> values,
400 CANSensorReader *can_sensor_reader)
Maxwell Hendersonad312342023-01-10 12:07:47 -0800401 : ::frc971::wpilib::SensorReader(event_loop),
402 values_(std::move(values)),
403 auto_mode_sender_(
404 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
405 "/autonomous")),
406 superstructure_position_sender_(
407 event_loop->MakeSender<superstructure::Position>(
408 "/superstructure")),
409 drivetrain_position_sender_(
410 event_loop
411 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
412 "/drivetrain")),
413 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
milind-u738832d2023-02-24 19:55:54 -0800414 "/drivetrain")),
415 can_sensor_reader_(can_sensor_reader) {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800416 // Set to filter out anything shorter than 1/4 of the minimum pulse width
417 // we should ever see.
418 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
Austin Schuh595ffc72023-02-24 16:24:37 -0800419 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
Maxwell Hendersonad312342023-01-10 12:07:47 -0800420 }
421
422 void Start() override {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800423 AddToDMA(&imu_yaw_rate_reader_);
milind-u3a7f9212023-02-24 20:46:59 -0800424 AddToDMA(&cone_position_sensor_);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800425 }
426
427 // Auto mode switches.
428 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
429 autonomous_modes_.at(i) = ::std::move(sensor);
430 }
431
Ravago Jones2060ee62023-02-03 18:12:24 -0800432 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
433 imu_yaw_rate_input_ = ::std::move(sensor);
434 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
435 }
436
Maxwell Hendersonad312342023-01-10 12:07:47 -0800437 void RunIteration() override {
438 superstructure_reading_->Set(true);
milind-u18934eb2023-02-20 16:28:58 -0800439 {
440 auto builder = superstructure_position_sender_.MakeBuilder();
441 frc971::PotAndAbsolutePositionT proximal;
442 CopyPosition(proximal_encoder_, &proximal,
443 Values::kProximalEncoderCountsPerRevolution(),
444 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh7dcc49b2023-02-21 17:35:10 -0800445 true, values_->arm_proximal.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800446 frc971::PotAndAbsolutePositionT distal;
Maxwell Hendersonce816122023-03-27 20:12:38 -0700447 CopyPosition(
448 distal_encoder_, &distal, Values::kDistalEncoderCountsPerRevolution(),
449 values_->arm_distal.zeroing.one_revolution_distance / (M_PI * 2.0),
450 distal_pot_translate, true, values_->arm_distal.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800451 frc971::PotAndAbsolutePositionT roll_joint;
452 CopyPosition(roll_joint_encoder_, &roll_joint,
453 Values::kRollJointEncoderCountsPerRevolution(),
454 Values::kRollJointEncoderRatio(), roll_joint_pot_translate,
Austin Schuh29d025c2023-03-03 21:41:04 -0800455 false, values_->roll_joint.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800456 frc971::AbsolutePositionT wrist;
457 CopyPosition(wrist_encoder_, &wrist,
458 Values::kWristEncoderCountsPerRevolution(),
Austin Schuhe5248cd2023-03-05 12:46:16 -0800459 values_->wrist.subsystem_params.zeroing_constants
460 .one_revolution_distance /
461 (M_PI * 2.0),
462 values_->wrist_flipped);
milind-u18934eb2023-02-20 16:28:58 -0800463
464 flatbuffers::Offset<frc971::PotAndAbsolutePosition> proximal_offset =
465 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &proximal);
466 flatbuffers::Offset<frc971::PotAndAbsolutePosition> distal_offset =
467 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &distal);
milind-u18934eb2023-02-20 16:28:58 -0800468 flatbuffers::Offset<frc971::PotAndAbsolutePosition> roll_joint_offset =
469 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &roll_joint);
milind-u18a901d2023-02-17 21:51:55 -0800470 flatbuffers::Offset<superstructure::ArmPosition> arm_offset =
471 superstructure::CreateArmPosition(*builder.fbb(), proximal_offset,
472 distal_offset, roll_joint_offset);
milind-u18934eb2023-02-20 16:28:58 -0800473 flatbuffers::Offset<frc971::AbsolutePosition> wrist_offset =
474 frc971::AbsolutePosition::Pack(*builder.fbb(), &wrist);
475
milind-u738832d2023-02-24 19:55:54 -0800476 flatbuffers::Offset<superstructure::CANFalcon> roller_falcon_offset;
477 auto optional_roller_falcon = can_sensor_reader_->roller_falcon_data();
478 if (optional_roller_falcon.has_value()) {
479 roller_falcon_offset = superstructure::CANFalcon::Pack(
480 *builder.fbb(), &optional_roller_falcon.value());
481 }
482
milind-u18934eb2023-02-20 16:28:58 -0800483 superstructure::Position::Builder position_builder =
484 builder.MakeBuilder<superstructure::Position>();
485
486 position_builder.add_arm(arm_offset);
milind-u18934eb2023-02-20 16:28:58 -0800487 position_builder.add_wrist(wrist_offset);
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800488 position_builder.add_end_effector_cube_beam_break(
489 end_effector_cube_beam_break_->Get());
milind-u3a7f9212023-02-24 20:46:59 -0800490 position_builder.add_cone_position(cone_position_sensor_.last_width() /
491 cone_position_sensor_.last_period());
milind-u738832d2023-02-24 19:55:54 -0800492 if (!roller_falcon_offset.IsNull()) {
493 position_builder.add_roller_falcon(roller_falcon_offset);
494 }
Henry Speisere139f802023-02-21 14:14:48 -0800495 builder.CheckOk(builder.Send(position_builder.Finish()));
milind-u18934eb2023-02-20 16:28:58 -0800496 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800497
498 {
499 auto builder = drivetrain_position_sender_.MakeBuilder();
500 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
501 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
502 drivetrain_builder.add_left_encoder(
503 constants::Values::DrivetrainEncoderToMeters(
504 drivetrain_left_encoder_->GetRaw()));
505 drivetrain_builder.add_left_speed(
506 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
507
508 drivetrain_builder.add_right_encoder(
509 -constants::Values::DrivetrainEncoderToMeters(
510 drivetrain_right_encoder_->GetRaw()));
511 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
512 drivetrain_right_encoder_->GetPeriod()));
513
514 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
515 }
516
517 {
518 auto builder = gyro_sender_.MakeBuilder();
519 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
520 builder.MakeBuilder<::frc971::sensors::GyroReading>();
521 // +/- 2000 deg / sec
522 constexpr double kMaxVelocity = 4000; // degrees / second
523 constexpr double kVelocityRadiansPerSecond =
524 kMaxVelocity / 360 * (2.0 * M_PI);
525
526 // Only part of the full range is used to prevent being 100% on or off.
527 constexpr double kScaledRangeLow = 0.1;
528 constexpr double kScaledRangeHigh = 0.9;
529
530 constexpr double kPWMFrequencyHz = 200;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800531 double velocity_duty_cycle =
532 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
533
534 constexpr double kDutyCycleScale =
535 1 / (kScaledRangeHigh - kScaledRangeLow);
536 // scale from 0.1 - 0.9 to 0 - 1
Maxwell Hendersonad312342023-01-10 12:07:47 -0800537 double rescaled_velocity_duty_cycle =
538 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
539
Maxwell Hendersonad312342023-01-10 12:07:47 -0800540 if (!std::isnan(rescaled_velocity_duty_cycle)) {
541 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
542 kVelocityRadiansPerSecond);
543 }
544 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
545 }
546
547 {
548 auto builder = auto_mode_sender_.MakeBuilder();
549
550 uint32_t mode = 0;
551 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
552 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
553 mode |= 1 << i;
554 }
555 }
556
557 auto auto_mode_builder =
558 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
559
560 auto_mode_builder.add_mode(mode);
561
562 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
563 }
564 }
565
566 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
567
568 void set_superstructure_reading(
569 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
570 superstructure_reading_ = superstructure_reading;
571 }
572
milind-u18934eb2023-02-20 16:28:58 -0800573 void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
574 fast_encoder_filter_.Add(encoder.get());
575 proximal_encoder_.set_encoder(::std::move(encoder));
576 }
577
578 void set_proximal_absolute_pwm(
579 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
580 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
581 }
582
583 void set_proximal_potentiometer(
584 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
585 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
586 }
587
588 void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
589 fast_encoder_filter_.Add(encoder.get());
590 distal_encoder_.set_encoder(::std::move(encoder));
591 }
592
593 void set_distal_absolute_pwm(
594 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
595 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
596 }
597
598 void set_distal_potentiometer(
599 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
600 distal_encoder_.set_potentiometer(::std::move(potentiometer));
601 }
602
603 void set_roll_joint_encoder(::std::unique_ptr<frc::Encoder> encoder) {
604 fast_encoder_filter_.Add(encoder.get());
605 roll_joint_encoder_.set_encoder(::std::move(encoder));
606 }
607
608 void set_roll_joint_absolute_pwm(
609 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
610 roll_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
611 }
612
613 void set_roll_joint_potentiometer(
614 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
615 roll_joint_encoder_.set_potentiometer(::std::move(potentiometer));
616 }
617
618 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
619 fast_encoder_filter_.Add(encoder.get());
620 wrist_encoder_.set_encoder(::std::move(encoder));
621 }
622
623 void set_wrist_absolute_pwm(
624 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
625 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
626 }
627
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800628 void set_end_effector_cube_beam_break(
629 ::std::unique_ptr<frc::DigitalInput> sensor) {
630 end_effector_cube_beam_break_ = ::std::move(sensor);
631 }
632
milind-u3a7f9212023-02-24 20:46:59 -0800633 void set_cone_position_sensor(::std::unique_ptr<frc::DigitalInput> sensor) {
634 cone_position_input_ = ::std::move(sensor);
635 cone_position_sensor_.set_input(cone_position_input_.get());
636 }
637
Maxwell Hendersonad312342023-01-10 12:07:47 -0800638 private:
639 std::shared_ptr<const Values> values_;
640
641 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
642 aos::Sender<superstructure::Position> superstructure_position_sender_;
643 aos::Sender<frc971::control_loops::drivetrain::Position>
644 drivetrain_position_sender_;
645 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
646
647 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
648
Ravago Jones2544ad82023-03-04 22:24:49 -0800649 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_,
milind-u3a7f9212023-02-24 20:46:59 -0800650 end_effector_cube_beam_break_;
Ravago Jones2060ee62023-02-03 18:12:24 -0800651
Ravago Jones2544ad82023-03-04 22:24:49 -0800652 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
milind-u18934eb2023-02-20 16:28:58 -0800653
654 frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
655 distal_encoder_, roll_joint_encoder_;
656 frc971::wpilib::AbsoluteEncoder wrist_encoder_;
milind-u3a7f9212023-02-24 20:46:59 -0800657
658 frc971::wpilib::DMAPulseWidthReader cone_position_sensor_;
659 std::unique_ptr<frc::DigitalInput> cone_position_input_;
milind-u738832d2023-02-24 19:55:54 -0800660
661 CANSensorReader *can_sensor_reader_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800662};
663
664class SuperstructureWriter
665 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
666 public:
667 SuperstructureWriter(aos::EventLoop *event_loop)
668 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
milind-u32d29d32023-02-24 21:11:51 -0800669 event_loop, "/superstructure") {
670 event_loop->SetRuntimeRealtimePriority(
671 constants::Values::kDrivetrainWriterPriority);
672 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800673
674 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
675
676 void set_superstructure_reading(
677 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
678 superstructure_reading_ = superstructure_reading;
679 }
680
milind-u18934eb2023-02-20 16:28:58 -0800681 void set_proximal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
682 proximal_falcon_ = ::std::move(t);
683 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800684
milind-u18934eb2023-02-20 16:28:58 -0800685 void set_distal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
686 distal_falcon_ = ::std::move(t);
687 }
688
689 void set_roll_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
690 roll_joint_victor_ = ::std::move(t);
691 }
692
693 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
694 wrist_victor_ = ::std::move(t);
695 }
696
milind-u18934eb2023-02-20 16:28:58 -0800697 private:
698 void Stop() override {
699 AOS_LOG(WARNING, "Superstructure output too old.\n");
700 proximal_falcon_->SetDisabled();
701 distal_falcon_->SetDisabled();
702 roll_joint_victor_->SetDisabled();
703 wrist_victor_->SetDisabled();
milind-u18934eb2023-02-20 16:28:58 -0800704 }
705
706 void Write(const superstructure::Output &output) override {
707 WritePwm(output.proximal_voltage(), proximal_falcon_.get());
708 WritePwm(output.distal_voltage(), distal_falcon_.get());
Austin Schuh3fd5f0e2023-02-22 11:10:37 -0800709 WritePwm(-output.roll_joint_voltage(), roll_joint_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800710 WritePwm(output.wrist_voltage(), wrist_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800711 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800712
713 static void WriteCan(const double voltage,
714 ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) {
715 falcon->Set(
716 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
717 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
718 }
719
720 template <typename T>
721 static void WritePwm(const double voltage, T *motor) {
722 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
723 12.0);
724 }
milind-u18934eb2023-02-20 16:28:58 -0800725
726 ::std::unique_ptr<::frc::TalonFX> proximal_falcon_, distal_falcon_;
727 ::std::unique_ptr<::frc::VictorSP> roll_joint_victor_, wrist_victor_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800728};
729
milind-u32d29d32023-02-24 21:11:51 -0800730class SuperstructureCANWriter
731 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
732 public:
733 SuperstructureCANWriter(::aos::EventLoop *event_loop)
734 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
735 event_loop, "/superstructure") {
736 event_loop->SetRuntimeRealtimePriority(
737 constants::Values::kSuperstructureCANWriterPriority);
738
739 event_loop->OnRun([this]() { WriteConfigs(); });
740 };
741
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800742 void HandleCANConfiguration(const CANConfiguration &configuration) {
743 roller_falcon_->PrintConfigs();
744 if (configuration.reapply()) {
745 WriteConfigs();
746 }
747 }
748
milind-u32d29d32023-02-24 21:11:51 -0800749 void set_roller_falcon(std::shared_ptr<Falcon> roller_falcon) {
750 roller_falcon_ = std::move(roller_falcon);
751 }
752
753 private:
754 void WriteConfigs() { roller_falcon_->WriteRollerConfigs(); }
755
756 void Write(const superstructure::Output &output) override {
757 ctre::phoenixpro::controls::DutyCycleOut roller_control(
758 SafeSpeed(-output.roller_voltage()));
759 roller_control.UpdateFreqHz = 0_Hz;
760 roller_control.EnableFOC = true;
761
762 ctre::phoenix::StatusCode status =
763 roller_falcon_->talon()->SetControl(roller_control);
764
765 if (!status.IsOK()) {
766 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
767 status.GetName(), status.GetDescription());
768 }
769 }
770
771 void Stop() override {
772 AOS_LOG(WARNING, "Superstructure CAN output too old.\n");
Austin Schuh1780e002023-03-03 21:39:26 -0800773 ctre::phoenixpro::controls::DutyCycleOut stop_command(0.0);
774 stop_command.UpdateFreqHz = 0_Hz;
775 stop_command.EnableFOC = true;
milind-u32d29d32023-02-24 21:11:51 -0800776
777 roller_falcon_->talon()->SetControl(stop_command);
778 }
779
780 double SafeSpeed(double voltage) {
781 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
782 }
783
784 std::shared_ptr<Falcon> roller_falcon_;
785};
786
Ravago Jones2060ee62023-02-03 18:12:24 -0800787class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler<
788 ::frc971::control_loops::drivetrain::Output> {
789 public:
790 DrivetrainWriter(::aos::EventLoop *event_loop)
791 : ::frc971::wpilib::LoopOutputHandler<
792 ::frc971::control_loops::drivetrain::Output>(event_loop,
793 "/drivetrain") {
794 event_loop->SetRuntimeRealtimePriority(
795 constants::Values::kDrivetrainWriterPriority);
796
Ravago Jones2060ee62023-02-03 18:12:24 -0800797 event_loop->OnRun([this]() { WriteConfigs(); });
798 }
799
800 void set_falcons(std::shared_ptr<Falcon> right_front,
801 std::shared_ptr<Falcon> right_back,
802 std::shared_ptr<Falcon> right_under,
803 std::shared_ptr<Falcon> left_front,
804 std::shared_ptr<Falcon> left_back,
805 std::shared_ptr<Falcon> left_under) {
806 right_front_ = std::move(right_front);
807 right_back_ = std::move(right_back);
808 right_under_ = std::move(right_under);
809 left_front_ = std::move(left_front);
810 left_back_ = std::move(left_back);
811 left_under_ = std::move(left_under);
812 }
813
814 void set_right_inverted(ctre::phoenixpro::signals::InvertedValue invert) {
815 right_inverted_ = invert;
816 }
817
818 void set_left_inverted(ctre::phoenixpro::signals::InvertedValue invert) {
819 left_inverted_ = invert;
820 }
821
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800822 void HandleCANConfiguration(const CANConfiguration &configuration) {
823 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
824 left_back_, left_under_}) {
825 falcon->PrintConfigs();
826 }
827 if (configuration.reapply()) {
828 WriteConfigs();
829 }
830 }
831
Ravago Jones2060ee62023-02-03 18:12:24 -0800832 private:
833 void WriteConfigs() {
834 for (auto falcon :
835 {right_front_.get(), right_back_.get(), right_under_.get()}) {
836 falcon->WriteConfigs(right_inverted_);
837 }
838
839 for (auto falcon :
840 {left_front_.get(), left_back_.get(), left_under_.get()}) {
841 falcon->WriteConfigs(left_inverted_);
842 }
843 }
844
845 void Write(
846 const ::frc971::control_loops::drivetrain::Output &output) override {
847 ctre::phoenixpro::controls::DutyCycleOut left_control(
848 SafeSpeed(output.left_voltage()));
849 left_control.UpdateFreqHz = 0_Hz;
850 left_control.EnableFOC = true;
851
852 ctre::phoenixpro::controls::DutyCycleOut right_control(
853 SafeSpeed(output.right_voltage()));
854 right_control.UpdateFreqHz = 0_Hz;
855 right_control.EnableFOC = true;
856
857 for (auto falcon :
858 {left_front_.get(), left_back_.get(), left_under_.get()}) {
859 ctre::phoenix::StatusCode status =
860 falcon->talon()->SetControl(left_control);
861
862 if (!status.IsOK()) {
863 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
864 status.GetName(), status.GetDescription());
865 }
866 }
867
868 for (auto falcon :
869 {right_front_.get(), right_back_.get(), right_under_.get()}) {
870 ctre::phoenix::StatusCode status =
871 falcon->talon()->SetControl(right_control);
872
873 if (!status.IsOK()) {
874 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
875 status.GetName(), status.GetDescription());
876 }
877 }
878 }
879
880 void Stop() override {
881 AOS_LOG(WARNING, "drivetrain output too old\n");
Austin Schuh1780e002023-03-03 21:39:26 -0800882 ctre::phoenixpro::controls::DutyCycleOut stop_command(0.0);
883 stop_command.UpdateFreqHz = 0_Hz;
884 stop_command.EnableFOC = true;
885
Ravago Jones2060ee62023-02-03 18:12:24 -0800886 for (auto falcon :
887 {right_front_.get(), right_back_.get(), right_under_.get(),
888 left_front_.get(), left_back_.get(), left_under_.get()}) {
889 falcon->talon()->SetControl(stop_command);
890 }
891 }
892
893 double SafeSpeed(double voltage) {
894 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
895 }
896
897 ctre::phoenixpro::signals::InvertedValue left_inverted_, right_inverted_;
898 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
899 left_back_, left_under_;
900};
Maxwell Hendersonad312342023-01-10 12:07:47 -0800901
902class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
903 public:
904 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
905 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
906 frc::Encoder::k4X);
907 }
908
909 void Run() override {
910 std::shared_ptr<const Values> values =
911 std::make_shared<const Values>(constants::MakeValues());
912
913 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
914 aos::configuration::ReadConfig("aos_config.json");
915
916 // Thread 1.
Ravago Jones2060ee62023-02-03 18:12:24 -0800917 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
918 ::frc971::wpilib::JoystickSender joystick_sender(
919 &joystick_sender_event_loop);
920 AddLoop(&joystick_sender_event_loop);
921
Maxwell Hendersonad312342023-01-10 12:07:47 -0800922 // Thread 2.
923 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
924 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
925 AddLoop(&pdp_fetcher_event_loop);
926
927 std::shared_ptr<frc::DigitalOutput> superstructure_reading =
928 make_unique<frc::DigitalOutput>(25);
929
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700930 std::vector<ctre::phoenixpro::BaseStatusSignalValue *> signals_registry;
931 std::shared_ptr<Falcon> right_front =
932 std::make_shared<Falcon>(1, "Drivetrain Bus", &signals_registry);
933 std::shared_ptr<Falcon> right_back =
934 std::make_shared<Falcon>(2, "Drivetrain Bus", &signals_registry);
935 std::shared_ptr<Falcon> right_under =
936 std::make_shared<Falcon>(3, "Drivetrain Bus", &signals_registry);
937 std::shared_ptr<Falcon> left_front =
938 std::make_shared<Falcon>(4, "Drivetrain Bus", &signals_registry);
939 std::shared_ptr<Falcon> left_back =
940 std::make_shared<Falcon>(5, "Drivetrain Bus", &signals_registry);
941 std::shared_ptr<Falcon> left_under =
942 std::make_shared<Falcon>(6, "Drivetrain Bus", &signals_registry);
943 std::shared_ptr<Falcon> roller =
944 std::make_shared<Falcon>(13, "Drivetrain Bus", &signals_registry);
945
Maxwell Hendersonad312342023-01-10 12:07:47 -0800946 // Thread 3.
milind-u738832d2023-02-24 19:55:54 -0800947 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
948 can_sensor_reader_event_loop.set_name("CANSensorReader");
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700949 CANSensorReader can_sensor_reader(&can_sensor_reader_event_loop,
950 std::move(signals_registry));
milind-u738832d2023-02-24 19:55:54 -0800951
952 can_sensor_reader.set_falcons(right_front, right_back, right_under,
953 left_front, left_back, left_under, roller);
954
955 AddLoop(&can_sensor_reader_event_loop);
956
957 // Thread 4.
Maxwell Hendersonad312342023-01-10 12:07:47 -0800958 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
milind-u738832d2023-02-24 19:55:54 -0800959 SensorReader sensor_reader(&sensor_reader_event_loop, values,
960 &can_sensor_reader);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800961 sensor_reader.set_pwm_trigger(true);
962 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
963 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
964 sensor_reader.set_superstructure_reading(superstructure_reading);
Henry Speisere139f802023-02-21 14:14:48 -0800965 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Ravago Jones2060ee62023-02-03 18:12:24 -0800966
milind-u18934eb2023-02-20 16:28:58 -0800967 sensor_reader.set_proximal_encoder(make_encoder(3));
968 sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(3));
969 sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(3));
970
Henry Speisere139f802023-02-21 14:14:48 -0800971 sensor_reader.set_distal_encoder(make_encoder(2));
972 sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(2));
973 sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(2));
milind-u18934eb2023-02-20 16:28:58 -0800974
Henry Speisere139f802023-02-21 14:14:48 -0800975 sensor_reader.set_roll_joint_encoder(make_encoder(5));
milind-u18934eb2023-02-20 16:28:58 -0800976 sensor_reader.set_roll_joint_absolute_pwm(
Henry Speisere139f802023-02-21 14:14:48 -0800977 make_unique<frc::DigitalInput>(5));
milind-u18934eb2023-02-20 16:28:58 -0800978 sensor_reader.set_roll_joint_potentiometer(
Henry Speisere139f802023-02-21 14:14:48 -0800979 make_unique<frc::AnalogInput>(5));
milind-u18934eb2023-02-20 16:28:58 -0800980
Henry Speisere139f802023-02-21 14:14:48 -0800981 sensor_reader.set_wrist_encoder(make_encoder(4));
982 sensor_reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(4));
milind-u18934eb2023-02-20 16:28:58 -0800983
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800984 sensor_reader.set_end_effector_cube_beam_break(
985 make_unique<frc::DigitalInput>(7));
milind-u3a7f9212023-02-24 20:46:59 -0800986 sensor_reader.set_cone_position_sensor(make_unique<frc::DigitalInput>(8));
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800987
Maxwell Hendersonad312342023-01-10 12:07:47 -0800988 AddLoop(&sensor_reader_event_loop);
989
Ravago Jones2060ee62023-02-03 18:12:24 -0800990 // Thread 5.
Philipp Schradera6712522023-07-05 20:25:11 -0700991 // Set up CAN.
milind-u32d29d32023-02-24 21:11:51 -0800992 if (!FLAGS_ctre_diag_server) {
993 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
994 c_Phoenix_Diagnostics_Dispose();
995 }
996
997 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
998 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
999 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
1000 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
1001
1002 ::aos::ShmEventLoop can_output_event_loop(&config.message());
1003 can_output_event_loop.set_name("CANOutputWriter");
1004 DrivetrainWriter drivetrain_writer(&can_output_event_loop);
Ravago Jones2060ee62023-02-03 18:12:24 -08001005
1006 drivetrain_writer.set_falcons(right_front, right_back, right_under,
1007 left_front, left_back, left_under);
1008 drivetrain_writer.set_right_inverted(
1009 ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive);
1010 drivetrain_writer.set_left_inverted(
1011 ctre::phoenixpro::signals::InvertedValue::CounterClockwise_Positive);
milind-u32d29d32023-02-24 21:11:51 -08001012
1013 SuperstructureCANWriter superstructure_can_writer(&can_output_event_loop);
1014 superstructure_can_writer.set_roller_falcon(roller);
1015
Austin Schuhbb4c9ac2023-02-28 22:04:20 -08001016 can_output_event_loop.MakeWatcher(
1017 "/roborio", [&drivetrain_writer, &superstructure_can_writer](
1018 const CANConfiguration &configuration) {
1019 drivetrain_writer.HandleCANConfiguration(configuration);
1020 superstructure_can_writer.HandleCANConfiguration(configuration);
1021 });
1022
milind-u32d29d32023-02-24 21:11:51 -08001023 AddLoop(&can_output_event_loop);
1024
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001025 // Thread 6
Philipp Schradera6712522023-07-05 20:25:11 -07001026 // Set up superstructure output.
milind-u32d29d32023-02-24 21:11:51 -08001027 ::aos::ShmEventLoop output_event_loop(&config.message());
1028 output_event_loop.set_name("PWMOutputWriter");
Maxwell Hendersonad312342023-01-10 12:07:47 -08001029 SuperstructureWriter superstructure_writer(&output_event_loop);
1030
Henry Speisere139f802023-02-21 14:14:48 -08001031 superstructure_writer.set_proximal_falcon(make_unique<::frc::TalonFX>(1));
1032 superstructure_writer.set_distal_falcon(make_unique<::frc::TalonFX>(0));
milind-u18934eb2023-02-20 16:28:58 -08001033
1034 superstructure_writer.set_roll_joint_victor(
Henry Speisere139f802023-02-21 14:14:48 -08001035 make_unique<::frc::VictorSP>(3));
1036 superstructure_writer.set_wrist_victor(make_unique<::frc::VictorSP>(2));
1037
Maxwell Hendersonad312342023-01-10 12:07:47 -08001038 superstructure_writer.set_superstructure_reading(superstructure_reading);
1039
1040 AddLoop(&output_event_loop);
1041
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001042 // Thread 7
Philipp Schradera6712522023-07-05 20:25:11 -07001043 // Set up led_indicator.
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001044 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
1045 led_indicator_event_loop.set_name("LedIndicator");
1046 control_loops::superstructure::LedIndicator led_indicator(
1047 &led_indicator_event_loop);
1048 AddLoop(&led_indicator_event_loop);
1049
Maxwell Hendersonad312342023-01-10 12:07:47 -08001050 RunLoops();
1051 }
1052};
1053
1054} // namespace wpilib
1055} // namespace y2023
1056
1057AOS_ROBOT_CLASS(::y2023::wpilib::WPILibRobot);