blob: 2f41c918ca5b08c129625db3ba3f2833acda43c3 [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()),
129 position_(talon_.GetPosition()) {
130 // device temp is not timesynced so don't add it to the list of signals
131 device_temp_.SetUpdateFrequency(kCANUpdateFreqHz);
132
milind-u738832d2023-02-24 19:55:54 -0800133 CHECK_NOTNULL(signals);
milind-u738832d2023-02-24 19:55:54 -0800134
135 supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz);
136 signals->push_back(&supply_voltage_);
137
138 supply_current_.SetUpdateFrequency(kCANUpdateFreqHz);
139 signals->push_back(&supply_current_);
140
141 torque_current_.SetUpdateFrequency(kCANUpdateFreqHz);
142 signals->push_back(&torque_current_);
143
144 position_.SetUpdateFrequency(kCANUpdateFreqHz);
145 signals->push_back(&position_);
146 }
147
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800148 void PrintConfigs() {
149 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
150 ctre::phoenix::StatusCode status =
151 talon_.GetConfigurator().Refresh(configuration);
152 if (!status.IsOK()) {
153 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
154 status.GetName(), status.GetDescription());
155 }
156 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
157 }
158
milind-u738832d2023-02-24 19:55:54 -0800159 void WriteConfigs(ctre::phoenixpro::signals::InvertedValue invert) {
160 inverted_ = invert;
161
162 ctre::phoenixpro::configs::CurrentLimitsConfigs current_limits;
163 current_limits.StatorCurrentLimit =
164 constants::Values::kDrivetrainStatorCurrentLimit();
165 current_limits.StatorCurrentLimitEnable = true;
166 current_limits.SupplyCurrentLimit =
167 constants::Values::kDrivetrainSupplyCurrentLimit();
168 current_limits.SupplyCurrentLimitEnable = true;
169
170 ctre::phoenixpro::configs::MotorOutputConfigs output_configs;
171 output_configs.NeutralMode =
172 ctre::phoenixpro::signals::NeutralModeValue::Brake;
173 output_configs.DutyCycleNeutralDeadband = 0;
174
175 output_configs.Inverted = inverted_;
176
177 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
178 configuration.CurrentLimits = current_limits;
179 configuration.MotorOutput = output_configs;
180
181 ctre::phoenix::StatusCode status =
182 talon_.GetConfigurator().Apply(configuration);
183 if (!status.IsOK()) {
184 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
185 status.GetName(), status.GetDescription());
186 }
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800187
188 PrintConfigs();
milind-u738832d2023-02-24 19:55:54 -0800189 }
190
191 void WriteRollerConfigs() {
192 ctre::phoenixpro::configs::CurrentLimitsConfigs current_limits;
193 current_limits.StatorCurrentLimit =
194 constants::Values::kRollerStatorCurrentLimit();
195 current_limits.StatorCurrentLimitEnable = true;
196 current_limits.SupplyCurrentLimit =
197 constants::Values::kRollerSupplyCurrentLimit();
198 current_limits.SupplyCurrentLimitEnable = true;
199
200 ctre::phoenixpro::configs::MotorOutputConfigs output_configs;
201 output_configs.NeutralMode =
202 ctre::phoenixpro::signals::NeutralModeValue::Brake;
203 output_configs.DutyCycleNeutralDeadband = 0;
204
205 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
206 configuration.CurrentLimits = current_limits;
207 configuration.MotorOutput = output_configs;
208
209 ctre::phoenix::StatusCode status =
210 talon_.GetConfigurator().Apply(configuration);
211 if (!status.IsOK()) {
212 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
213 status.GetName(), status.GetDescription());
214 }
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800215
216 PrintConfigs();
milind-u738832d2023-02-24 19:55:54 -0800217 }
218
219 ctre::phoenixpro::hardware::TalonFX *talon() { return &talon_; }
220
221 flatbuffers::Offset<drivetrain::CANFalcon> WritePosition(
222 flatbuffers::FlatBufferBuilder *fbb) {
223 drivetrain::CANFalcon::Builder builder(*fbb);
224 builder.add_id(device_id_);
225 builder.add_device_temp(device_temp());
226 builder.add_supply_voltage(supply_voltage());
227 builder.add_supply_current(supply_current());
228 builder.add_torque_current(torque_current());
229
230 double invert =
231 (inverted_ ==
232 ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive
233 ? 1
234 : -1);
235
236 builder.add_position(
237 constants::Values::DrivetrainCANEncoderToMeters(position()) * invert);
238
239 return builder.Finish();
240 }
241
242 int device_id() const { return device_id_; }
243 float device_temp() const { return device_temp_.GetValue().value(); }
244 float supply_voltage() const { return supply_voltage_.GetValue().value(); }
245 float supply_current() const { return supply_current_.GetValue().value(); }
246 float torque_current() const { return torque_current_.GetValue().value(); }
247 float position() const { return position_.GetValue().value(); }
248
249 // returns the monotonic timestamp of the latest timesynced reading in the
250 // timebase of the the syncronized CAN bus clock.
251 int64_t GetTimestamp() {
252 std::chrono::nanoseconds latest_timestamp =
253 torque_current_.GetTimestamp().GetTime();
254
255 return latest_timestamp.count();
256 }
257
258 void RefreshNontimesyncedSignals() { device_temp_.Refresh(); };
259
260 private:
261 ctre::phoenixpro::hardware::TalonFX talon_;
262 int device_id_;
263
264 ctre::phoenixpro::signals::InvertedValue inverted_;
265
266 ctre::phoenixpro::StatusSignalValue<units::temperature::celsius_t>
267 device_temp_;
268 ctre::phoenixpro::StatusSignalValue<units::voltage::volt_t> supply_voltage_;
269 ctre::phoenixpro::StatusSignalValue<units::current::ampere_t> supply_current_,
270 torque_current_;
271 ctre::phoenixpro::StatusSignalValue<units::angle::turn_t> position_;
272};
273
274class CANSensorReader {
275 public:
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700276 CANSensorReader(
277 aos::EventLoop *event_loop,
278 std::vector<ctre::phoenixpro::BaseStatusSignalValue *> signals_registry)
milind-u738832d2023-02-24 19:55:54 -0800279 : event_loop_(event_loop),
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700280 signals_(signals_registry.begin(), signals_registry.end()),
milind-u738832d2023-02-24 19:55:54 -0800281 can_position_sender_(
282 event_loop->MakeSender<drivetrain::CANPosition>("/drivetrain")),
283 roller_falcon_data_(std::nullopt) {
284 event_loop->SetRuntimeRealtimePriority(40);
285 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1}));
286 timer_handler_ = event_loop->AddTimer([this]() { Loop(); });
287 timer_handler_->set_name("CANSensorReader Loop");
288
289 event_loop->OnRun([this]() {
290 timer_handler_->Setup(event_loop_->monotonic_now(), 1 / kCANUpdateFreqHz);
291 });
292 }
293
milind-u738832d2023-02-24 19:55:54 -0800294 void set_falcons(std::shared_ptr<Falcon> right_front,
295 std::shared_ptr<Falcon> right_back,
296 std::shared_ptr<Falcon> right_under,
297 std::shared_ptr<Falcon> left_front,
298 std::shared_ptr<Falcon> left_back,
299 std::shared_ptr<Falcon> left_under,
300 std::shared_ptr<Falcon> roller_falcon) {
301 right_front_ = std::move(right_front);
302 right_back_ = std::move(right_back);
303 right_under_ = std::move(right_under);
304 left_front_ = std::move(left_front);
305 left_back_ = std::move(left_back);
306 left_under_ = std::move(left_under);
307 roller_falcon_ = std::move(roller_falcon);
308 }
309
310 std::optional<superstructure::CANFalconT> roller_falcon_data() {
311 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
312 return roller_falcon_data_;
313 }
314
315 private:
316 void Loop() {
milind-u738832d2023-02-24 19:55:54 -0800317 ctre::phoenix::StatusCode status =
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700318 ctre::phoenixpro::BaseStatusSignalValue::WaitForAll(2000_ms, signals_);
milind-u738832d2023-02-24 19:55:54 -0800319
320 if (!status.IsOK()) {
321 AOS_LOG(ERROR, "Failed to read signals from falcons: %s: %s",
322 status.GetName(), status.GetDescription());
323 }
324
325 auto builder = can_position_sender_.MakeBuilder();
326
327 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
328 left_back_, left_under_, roller_falcon_}) {
329 falcon->RefreshNontimesyncedSignals();
330 }
331
332 aos::SizedArray<flatbuffers::Offset<drivetrain::CANFalcon>, kCANFalconCount>
333 falcons;
334
335 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
336 left_back_, left_under_}) {
337 falcons.push_back(falcon->WritePosition(builder.fbb()));
338 }
339
340 auto falcons_list =
341 builder.fbb()->CreateVector<flatbuffers::Offset<drivetrain::CANFalcon>>(
342 falcons);
343
344 drivetrain::CANPosition::Builder can_position_builder =
345 builder.MakeBuilder<drivetrain::CANPosition>();
346
347 can_position_builder.add_falcons(falcons_list);
348 can_position_builder.add_timestamp(right_front_->GetTimestamp());
349 can_position_builder.add_status(static_cast<int>(status));
350
351 builder.CheckOk(builder.Send(can_position_builder.Finish()));
352
353 {
354 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
355 superstructure::CANFalconT roller_falcon_data;
356 roller_falcon_data.id = roller_falcon_->device_id();
357 roller_falcon_data.supply_current = roller_falcon_->supply_current();
Austin Schuh23a90022023-02-24 22:13:39 -0800358 roller_falcon_data.torque_current = -roller_falcon_->torque_current();
milind-u738832d2023-02-24 19:55:54 -0800359 roller_falcon_data.supply_voltage = roller_falcon_->supply_voltage();
360 roller_falcon_data.device_temp = roller_falcon_->device_temp();
Austin Schuh23a90022023-02-24 22:13:39 -0800361 roller_falcon_data.position = -roller_falcon_->position();
milind-u738832d2023-02-24 19:55:54 -0800362 roller_falcon_data_ =
363 std::make_optional<superstructure::CANFalconT>(roller_falcon_data);
364 }
365 }
366
367 aos::EventLoop *event_loop_;
368
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700369 const std::vector<ctre::phoenixpro::BaseStatusSignalValue *> signals_;
milind-u738832d2023-02-24 19:55:54 -0800370 aos::Sender<drivetrain::CANPosition> can_position_sender_;
371
372 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
373 left_back_, left_under_, roller_falcon_;
374
375 std::optional<superstructure::CANFalconT> roller_falcon_data_;
376
377 aos::stl_mutex roller_mutex_;
378
379 // Pointer to the timer handler used to modify the wakeup.
380 ::aos::TimerHandler *timer_handler_;
381};
382
Maxwell Hendersonad312342023-01-10 12:07:47 -0800383// Class to send position messages with sensor readings to our loops.
384class SensorReader : public ::frc971::wpilib::SensorReader {
385 public:
386 SensorReader(::aos::ShmEventLoop *event_loop,
milind-u738832d2023-02-24 19:55:54 -0800387 std::shared_ptr<const Values> values,
388 CANSensorReader *can_sensor_reader)
Maxwell Hendersonad312342023-01-10 12:07:47 -0800389 : ::frc971::wpilib::SensorReader(event_loop),
390 values_(std::move(values)),
391 auto_mode_sender_(
392 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
393 "/autonomous")),
394 superstructure_position_sender_(
395 event_loop->MakeSender<superstructure::Position>(
396 "/superstructure")),
397 drivetrain_position_sender_(
398 event_loop
399 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
400 "/drivetrain")),
401 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
milind-u738832d2023-02-24 19:55:54 -0800402 "/drivetrain")),
403 can_sensor_reader_(can_sensor_reader) {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800404 // Set to filter out anything shorter than 1/4 of the minimum pulse width
405 // we should ever see.
406 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
Austin Schuh595ffc72023-02-24 16:24:37 -0800407 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
Maxwell Hendersonad312342023-01-10 12:07:47 -0800408 }
409
410 void Start() override {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800411 AddToDMA(&imu_yaw_rate_reader_);
milind-u3a7f9212023-02-24 20:46:59 -0800412 AddToDMA(&cone_position_sensor_);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800413 }
414
415 // Auto mode switches.
416 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
417 autonomous_modes_.at(i) = ::std::move(sensor);
418 }
419
Ravago Jones2060ee62023-02-03 18:12:24 -0800420 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
421 imu_yaw_rate_input_ = ::std::move(sensor);
422 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
423 }
424
Maxwell Hendersonad312342023-01-10 12:07:47 -0800425 void RunIteration() override {
426 superstructure_reading_->Set(true);
milind-u18934eb2023-02-20 16:28:58 -0800427 {
428 auto builder = superstructure_position_sender_.MakeBuilder();
429 frc971::PotAndAbsolutePositionT proximal;
430 CopyPosition(proximal_encoder_, &proximal,
431 Values::kProximalEncoderCountsPerRevolution(),
432 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh7dcc49b2023-02-21 17:35:10 -0800433 true, values_->arm_proximal.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800434 frc971::PotAndAbsolutePositionT distal;
435 CopyPosition(distal_encoder_, &distal,
436 Values::kDistalEncoderCountsPerRevolution(),
Austin Schuh7dcc49b2023-02-21 17:35:10 -0800437 Values::kDistalEncoderRatio(), distal_pot_translate, true,
milind-u18934eb2023-02-20 16:28:58 -0800438 values_->arm_distal.potentiometer_offset);
439 frc971::PotAndAbsolutePositionT roll_joint;
440 CopyPosition(roll_joint_encoder_, &roll_joint,
441 Values::kRollJointEncoderCountsPerRevolution(),
442 Values::kRollJointEncoderRatio(), roll_joint_pot_translate,
Austin Schuh29d025c2023-03-03 21:41:04 -0800443 false, values_->roll_joint.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800444 frc971::AbsolutePositionT wrist;
445 CopyPosition(wrist_encoder_, &wrist,
446 Values::kWristEncoderCountsPerRevolution(),
Austin Schuhe5248cd2023-03-05 12:46:16 -0800447 values_->wrist.subsystem_params.zeroing_constants
448 .one_revolution_distance /
449 (M_PI * 2.0),
450 values_->wrist_flipped);
milind-u18934eb2023-02-20 16:28:58 -0800451
452 flatbuffers::Offset<frc971::PotAndAbsolutePosition> proximal_offset =
453 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &proximal);
454 flatbuffers::Offset<frc971::PotAndAbsolutePosition> distal_offset =
455 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &distal);
milind-u18934eb2023-02-20 16:28:58 -0800456 flatbuffers::Offset<frc971::PotAndAbsolutePosition> roll_joint_offset =
457 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &roll_joint);
milind-u18a901d2023-02-17 21:51:55 -0800458 flatbuffers::Offset<superstructure::ArmPosition> arm_offset =
459 superstructure::CreateArmPosition(*builder.fbb(), proximal_offset,
460 distal_offset, roll_joint_offset);
milind-u18934eb2023-02-20 16:28:58 -0800461 flatbuffers::Offset<frc971::AbsolutePosition> wrist_offset =
462 frc971::AbsolutePosition::Pack(*builder.fbb(), &wrist);
463
milind-u738832d2023-02-24 19:55:54 -0800464 flatbuffers::Offset<superstructure::CANFalcon> roller_falcon_offset;
465 auto optional_roller_falcon = can_sensor_reader_->roller_falcon_data();
466 if (optional_roller_falcon.has_value()) {
467 roller_falcon_offset = superstructure::CANFalcon::Pack(
468 *builder.fbb(), &optional_roller_falcon.value());
469 }
470
milind-u18934eb2023-02-20 16:28:58 -0800471 superstructure::Position::Builder position_builder =
472 builder.MakeBuilder<superstructure::Position>();
473
474 position_builder.add_arm(arm_offset);
milind-u18934eb2023-02-20 16:28:58 -0800475 position_builder.add_wrist(wrist_offset);
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800476 position_builder.add_end_effector_cube_beam_break(
477 end_effector_cube_beam_break_->Get());
milind-u3a7f9212023-02-24 20:46:59 -0800478 position_builder.add_cone_position(cone_position_sensor_.last_width() /
479 cone_position_sensor_.last_period());
milind-u738832d2023-02-24 19:55:54 -0800480 if (!roller_falcon_offset.IsNull()) {
481 position_builder.add_roller_falcon(roller_falcon_offset);
482 }
Henry Speisere139f802023-02-21 14:14:48 -0800483 builder.CheckOk(builder.Send(position_builder.Finish()));
milind-u18934eb2023-02-20 16:28:58 -0800484 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800485
486 {
487 auto builder = drivetrain_position_sender_.MakeBuilder();
488 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
489 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
490 drivetrain_builder.add_left_encoder(
491 constants::Values::DrivetrainEncoderToMeters(
492 drivetrain_left_encoder_->GetRaw()));
493 drivetrain_builder.add_left_speed(
494 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
495
496 drivetrain_builder.add_right_encoder(
497 -constants::Values::DrivetrainEncoderToMeters(
498 drivetrain_right_encoder_->GetRaw()));
499 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
500 drivetrain_right_encoder_->GetPeriod()));
501
502 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
503 }
504
505 {
506 auto builder = gyro_sender_.MakeBuilder();
507 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
508 builder.MakeBuilder<::frc971::sensors::GyroReading>();
509 // +/- 2000 deg / sec
510 constexpr double kMaxVelocity = 4000; // degrees / second
511 constexpr double kVelocityRadiansPerSecond =
512 kMaxVelocity / 360 * (2.0 * M_PI);
513
514 // Only part of the full range is used to prevent being 100% on or off.
515 constexpr double kScaledRangeLow = 0.1;
516 constexpr double kScaledRangeHigh = 0.9;
517
518 constexpr double kPWMFrequencyHz = 200;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800519 double velocity_duty_cycle =
520 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
521
522 constexpr double kDutyCycleScale =
523 1 / (kScaledRangeHigh - kScaledRangeLow);
524 // scale from 0.1 - 0.9 to 0 - 1
Maxwell Hendersonad312342023-01-10 12:07:47 -0800525 double rescaled_velocity_duty_cycle =
526 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
527
Maxwell Hendersonad312342023-01-10 12:07:47 -0800528 if (!std::isnan(rescaled_velocity_duty_cycle)) {
529 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
530 kVelocityRadiansPerSecond);
531 }
532 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
533 }
534
535 {
536 auto builder = auto_mode_sender_.MakeBuilder();
537
538 uint32_t mode = 0;
539 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
540 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
541 mode |= 1 << i;
542 }
543 }
544
545 auto auto_mode_builder =
546 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
547
548 auto_mode_builder.add_mode(mode);
549
550 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
551 }
552 }
553
554 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
555
556 void set_superstructure_reading(
557 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
558 superstructure_reading_ = superstructure_reading;
559 }
560
milind-u18934eb2023-02-20 16:28:58 -0800561 void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
562 fast_encoder_filter_.Add(encoder.get());
563 proximal_encoder_.set_encoder(::std::move(encoder));
564 }
565
566 void set_proximal_absolute_pwm(
567 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
568 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
569 }
570
571 void set_proximal_potentiometer(
572 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
573 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
574 }
575
576 void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
577 fast_encoder_filter_.Add(encoder.get());
578 distal_encoder_.set_encoder(::std::move(encoder));
579 }
580
581 void set_distal_absolute_pwm(
582 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
583 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
584 }
585
586 void set_distal_potentiometer(
587 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
588 distal_encoder_.set_potentiometer(::std::move(potentiometer));
589 }
590
591 void set_roll_joint_encoder(::std::unique_ptr<frc::Encoder> encoder) {
592 fast_encoder_filter_.Add(encoder.get());
593 roll_joint_encoder_.set_encoder(::std::move(encoder));
594 }
595
596 void set_roll_joint_absolute_pwm(
597 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
598 roll_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
599 }
600
601 void set_roll_joint_potentiometer(
602 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
603 roll_joint_encoder_.set_potentiometer(::std::move(potentiometer));
604 }
605
606 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
607 fast_encoder_filter_.Add(encoder.get());
608 wrist_encoder_.set_encoder(::std::move(encoder));
609 }
610
611 void set_wrist_absolute_pwm(
612 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
613 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
614 }
615
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800616 void set_end_effector_cube_beam_break(
617 ::std::unique_ptr<frc::DigitalInput> sensor) {
618 end_effector_cube_beam_break_ = ::std::move(sensor);
619 }
620
milind-u3a7f9212023-02-24 20:46:59 -0800621 void set_cone_position_sensor(::std::unique_ptr<frc::DigitalInput> sensor) {
622 cone_position_input_ = ::std::move(sensor);
623 cone_position_sensor_.set_input(cone_position_input_.get());
624 }
625
Maxwell Hendersonad312342023-01-10 12:07:47 -0800626 private:
627 std::shared_ptr<const Values> values_;
628
629 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
630 aos::Sender<superstructure::Position> superstructure_position_sender_;
631 aos::Sender<frc971::control_loops::drivetrain::Position>
632 drivetrain_position_sender_;
633 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
634
635 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
636
Ravago Jones2544ad82023-03-04 22:24:49 -0800637 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_,
milind-u3a7f9212023-02-24 20:46:59 -0800638 end_effector_cube_beam_break_;
Ravago Jones2060ee62023-02-03 18:12:24 -0800639
Ravago Jones2544ad82023-03-04 22:24:49 -0800640 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
milind-u18934eb2023-02-20 16:28:58 -0800641
642 frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
643 distal_encoder_, roll_joint_encoder_;
644 frc971::wpilib::AbsoluteEncoder wrist_encoder_;
milind-u3a7f9212023-02-24 20:46:59 -0800645
646 frc971::wpilib::DMAPulseWidthReader cone_position_sensor_;
647 std::unique_ptr<frc::DigitalInput> cone_position_input_;
milind-u738832d2023-02-24 19:55:54 -0800648
649 CANSensorReader *can_sensor_reader_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800650};
651
652class SuperstructureWriter
653 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
654 public:
655 SuperstructureWriter(aos::EventLoop *event_loop)
656 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
milind-u32d29d32023-02-24 21:11:51 -0800657 event_loop, "/superstructure") {
658 event_loop->SetRuntimeRealtimePriority(
659 constants::Values::kDrivetrainWriterPriority);
660 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800661
662 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
663
664 void set_superstructure_reading(
665 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
666 superstructure_reading_ = superstructure_reading;
667 }
668
milind-u18934eb2023-02-20 16:28:58 -0800669 void set_proximal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
670 proximal_falcon_ = ::std::move(t);
671 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800672
milind-u18934eb2023-02-20 16:28:58 -0800673 void set_distal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
674 distal_falcon_ = ::std::move(t);
675 }
676
677 void set_roll_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
678 roll_joint_victor_ = ::std::move(t);
679 }
680
681 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
682 wrist_victor_ = ::std::move(t);
683 }
684
milind-u18934eb2023-02-20 16:28:58 -0800685 private:
686 void Stop() override {
687 AOS_LOG(WARNING, "Superstructure output too old.\n");
688 proximal_falcon_->SetDisabled();
689 distal_falcon_->SetDisabled();
690 roll_joint_victor_->SetDisabled();
691 wrist_victor_->SetDisabled();
milind-u18934eb2023-02-20 16:28:58 -0800692 }
693
694 void Write(const superstructure::Output &output) override {
695 WritePwm(output.proximal_voltage(), proximal_falcon_.get());
696 WritePwm(output.distal_voltage(), distal_falcon_.get());
Austin Schuh3fd5f0e2023-02-22 11:10:37 -0800697 WritePwm(-output.roll_joint_voltage(), roll_joint_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800698 WritePwm(output.wrist_voltage(), wrist_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800699 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800700
701 static void WriteCan(const double voltage,
702 ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) {
703 falcon->Set(
704 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
705 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
706 }
707
708 template <typename T>
709 static void WritePwm(const double voltage, T *motor) {
710 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
711 12.0);
712 }
milind-u18934eb2023-02-20 16:28:58 -0800713
714 ::std::unique_ptr<::frc::TalonFX> proximal_falcon_, distal_falcon_;
715 ::std::unique_ptr<::frc::VictorSP> roll_joint_victor_, wrist_victor_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800716};
717
milind-u32d29d32023-02-24 21:11:51 -0800718class SuperstructureCANWriter
719 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
720 public:
721 SuperstructureCANWriter(::aos::EventLoop *event_loop)
722 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
723 event_loop, "/superstructure") {
724 event_loop->SetRuntimeRealtimePriority(
725 constants::Values::kSuperstructureCANWriterPriority);
726
727 event_loop->OnRun([this]() { WriteConfigs(); });
728 };
729
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800730 void HandleCANConfiguration(const CANConfiguration &configuration) {
731 roller_falcon_->PrintConfigs();
732 if (configuration.reapply()) {
733 WriteConfigs();
734 }
735 }
736
milind-u32d29d32023-02-24 21:11:51 -0800737 void set_roller_falcon(std::shared_ptr<Falcon> roller_falcon) {
738 roller_falcon_ = std::move(roller_falcon);
739 }
740
741 private:
742 void WriteConfigs() { roller_falcon_->WriteRollerConfigs(); }
743
744 void Write(const superstructure::Output &output) override {
745 ctre::phoenixpro::controls::DutyCycleOut roller_control(
746 SafeSpeed(-output.roller_voltage()));
747 roller_control.UpdateFreqHz = 0_Hz;
748 roller_control.EnableFOC = true;
749
750 ctre::phoenix::StatusCode status =
751 roller_falcon_->talon()->SetControl(roller_control);
752
753 if (!status.IsOK()) {
754 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
755 status.GetName(), status.GetDescription());
756 }
757 }
758
759 void Stop() override {
760 AOS_LOG(WARNING, "Superstructure CAN output too old.\n");
Austin Schuh1780e002023-03-03 21:39:26 -0800761 ctre::phoenixpro::controls::DutyCycleOut stop_command(0.0);
762 stop_command.UpdateFreqHz = 0_Hz;
763 stop_command.EnableFOC = true;
milind-u32d29d32023-02-24 21:11:51 -0800764
765 roller_falcon_->talon()->SetControl(stop_command);
766 }
767
768 double SafeSpeed(double voltage) {
769 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
770 }
771
772 std::shared_ptr<Falcon> roller_falcon_;
773};
774
Ravago Jones2060ee62023-02-03 18:12:24 -0800775class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler<
776 ::frc971::control_loops::drivetrain::Output> {
777 public:
778 DrivetrainWriter(::aos::EventLoop *event_loop)
779 : ::frc971::wpilib::LoopOutputHandler<
780 ::frc971::control_loops::drivetrain::Output>(event_loop,
781 "/drivetrain") {
782 event_loop->SetRuntimeRealtimePriority(
783 constants::Values::kDrivetrainWriterPriority);
784
Ravago Jones2060ee62023-02-03 18:12:24 -0800785 event_loop->OnRun([this]() { WriteConfigs(); });
786 }
787
788 void set_falcons(std::shared_ptr<Falcon> right_front,
789 std::shared_ptr<Falcon> right_back,
790 std::shared_ptr<Falcon> right_under,
791 std::shared_ptr<Falcon> left_front,
792 std::shared_ptr<Falcon> left_back,
793 std::shared_ptr<Falcon> left_under) {
794 right_front_ = std::move(right_front);
795 right_back_ = std::move(right_back);
796 right_under_ = std::move(right_under);
797 left_front_ = std::move(left_front);
798 left_back_ = std::move(left_back);
799 left_under_ = std::move(left_under);
800 }
801
802 void set_right_inverted(ctre::phoenixpro::signals::InvertedValue invert) {
803 right_inverted_ = invert;
804 }
805
806 void set_left_inverted(ctre::phoenixpro::signals::InvertedValue invert) {
807 left_inverted_ = invert;
808 }
809
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800810 void HandleCANConfiguration(const CANConfiguration &configuration) {
811 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
812 left_back_, left_under_}) {
813 falcon->PrintConfigs();
814 }
815 if (configuration.reapply()) {
816 WriteConfigs();
817 }
818 }
819
Ravago Jones2060ee62023-02-03 18:12:24 -0800820 private:
821 void WriteConfigs() {
822 for (auto falcon :
823 {right_front_.get(), right_back_.get(), right_under_.get()}) {
824 falcon->WriteConfigs(right_inverted_);
825 }
826
827 for (auto falcon :
828 {left_front_.get(), left_back_.get(), left_under_.get()}) {
829 falcon->WriteConfigs(left_inverted_);
830 }
831 }
832
833 void Write(
834 const ::frc971::control_loops::drivetrain::Output &output) override {
835 ctre::phoenixpro::controls::DutyCycleOut left_control(
836 SafeSpeed(output.left_voltage()));
837 left_control.UpdateFreqHz = 0_Hz;
838 left_control.EnableFOC = true;
839
840 ctre::phoenixpro::controls::DutyCycleOut right_control(
841 SafeSpeed(output.right_voltage()));
842 right_control.UpdateFreqHz = 0_Hz;
843 right_control.EnableFOC = true;
844
845 for (auto falcon :
846 {left_front_.get(), left_back_.get(), left_under_.get()}) {
847 ctre::phoenix::StatusCode status =
848 falcon->talon()->SetControl(left_control);
849
850 if (!status.IsOK()) {
851 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
852 status.GetName(), status.GetDescription());
853 }
854 }
855
856 for (auto falcon :
857 {right_front_.get(), right_back_.get(), right_under_.get()}) {
858 ctre::phoenix::StatusCode status =
859 falcon->talon()->SetControl(right_control);
860
861 if (!status.IsOK()) {
862 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
863 status.GetName(), status.GetDescription());
864 }
865 }
866 }
867
868 void Stop() override {
869 AOS_LOG(WARNING, "drivetrain output too old\n");
Austin Schuh1780e002023-03-03 21:39:26 -0800870 ctre::phoenixpro::controls::DutyCycleOut stop_command(0.0);
871 stop_command.UpdateFreqHz = 0_Hz;
872 stop_command.EnableFOC = true;
873
Ravago Jones2060ee62023-02-03 18:12:24 -0800874 for (auto falcon :
875 {right_front_.get(), right_back_.get(), right_under_.get(),
876 left_front_.get(), left_back_.get(), left_under_.get()}) {
877 falcon->talon()->SetControl(stop_command);
878 }
879 }
880
881 double SafeSpeed(double voltage) {
882 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
883 }
884
885 ctre::phoenixpro::signals::InvertedValue left_inverted_, right_inverted_;
886 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
887 left_back_, left_under_;
888};
Maxwell Hendersonad312342023-01-10 12:07:47 -0800889
890class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
891 public:
892 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
893 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
894 frc::Encoder::k4X);
895 }
896
897 void Run() override {
898 std::shared_ptr<const Values> values =
899 std::make_shared<const Values>(constants::MakeValues());
900
901 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
902 aos::configuration::ReadConfig("aos_config.json");
903
904 // Thread 1.
Ravago Jones2060ee62023-02-03 18:12:24 -0800905 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
906 ::frc971::wpilib::JoystickSender joystick_sender(
907 &joystick_sender_event_loop);
908 AddLoop(&joystick_sender_event_loop);
909
Maxwell Hendersonad312342023-01-10 12:07:47 -0800910 // Thread 2.
911 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
912 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
913 AddLoop(&pdp_fetcher_event_loop);
914
915 std::shared_ptr<frc::DigitalOutput> superstructure_reading =
916 make_unique<frc::DigitalOutput>(25);
917
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700918 std::vector<ctre::phoenixpro::BaseStatusSignalValue *> signals_registry;
919 std::shared_ptr<Falcon> right_front =
920 std::make_shared<Falcon>(1, "Drivetrain Bus", &signals_registry);
921 std::shared_ptr<Falcon> right_back =
922 std::make_shared<Falcon>(2, "Drivetrain Bus", &signals_registry);
923 std::shared_ptr<Falcon> right_under =
924 std::make_shared<Falcon>(3, "Drivetrain Bus", &signals_registry);
925 std::shared_ptr<Falcon> left_front =
926 std::make_shared<Falcon>(4, "Drivetrain Bus", &signals_registry);
927 std::shared_ptr<Falcon> left_back =
928 std::make_shared<Falcon>(5, "Drivetrain Bus", &signals_registry);
929 std::shared_ptr<Falcon> left_under =
930 std::make_shared<Falcon>(6, "Drivetrain Bus", &signals_registry);
931 std::shared_ptr<Falcon> roller =
932 std::make_shared<Falcon>(13, "Drivetrain Bus", &signals_registry);
933
Maxwell Hendersonad312342023-01-10 12:07:47 -0800934 // Thread 3.
milind-u738832d2023-02-24 19:55:54 -0800935 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
936 can_sensor_reader_event_loop.set_name("CANSensorReader");
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700937 CANSensorReader can_sensor_reader(&can_sensor_reader_event_loop,
938 std::move(signals_registry));
milind-u738832d2023-02-24 19:55:54 -0800939
940 can_sensor_reader.set_falcons(right_front, right_back, right_under,
941 left_front, left_back, left_under, roller);
942
943 AddLoop(&can_sensor_reader_event_loop);
944
945 // Thread 4.
Maxwell Hendersonad312342023-01-10 12:07:47 -0800946 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
milind-u738832d2023-02-24 19:55:54 -0800947 SensorReader sensor_reader(&sensor_reader_event_loop, values,
948 &can_sensor_reader);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800949 sensor_reader.set_pwm_trigger(true);
950 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
951 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
952 sensor_reader.set_superstructure_reading(superstructure_reading);
Henry Speisere139f802023-02-21 14:14:48 -0800953 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Ravago Jones2060ee62023-02-03 18:12:24 -0800954
milind-u18934eb2023-02-20 16:28:58 -0800955 sensor_reader.set_proximal_encoder(make_encoder(3));
956 sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(3));
957 sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(3));
958
Henry Speisere139f802023-02-21 14:14:48 -0800959 sensor_reader.set_distal_encoder(make_encoder(2));
960 sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(2));
961 sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(2));
milind-u18934eb2023-02-20 16:28:58 -0800962
Henry Speisere139f802023-02-21 14:14:48 -0800963 sensor_reader.set_roll_joint_encoder(make_encoder(5));
milind-u18934eb2023-02-20 16:28:58 -0800964 sensor_reader.set_roll_joint_absolute_pwm(
Henry Speisere139f802023-02-21 14:14:48 -0800965 make_unique<frc::DigitalInput>(5));
milind-u18934eb2023-02-20 16:28:58 -0800966 sensor_reader.set_roll_joint_potentiometer(
Henry Speisere139f802023-02-21 14:14:48 -0800967 make_unique<frc::AnalogInput>(5));
milind-u18934eb2023-02-20 16:28:58 -0800968
Henry Speisere139f802023-02-21 14:14:48 -0800969 sensor_reader.set_wrist_encoder(make_encoder(4));
970 sensor_reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(4));
milind-u18934eb2023-02-20 16:28:58 -0800971
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800972 sensor_reader.set_end_effector_cube_beam_break(
973 make_unique<frc::DigitalInput>(7));
milind-u3a7f9212023-02-24 20:46:59 -0800974 sensor_reader.set_cone_position_sensor(make_unique<frc::DigitalInput>(8));
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800975
Maxwell Hendersonad312342023-01-10 12:07:47 -0800976 AddLoop(&sensor_reader_event_loop);
977
Ravago Jones2060ee62023-02-03 18:12:24 -0800978 // Thread 5.
milind-u32d29d32023-02-24 21:11:51 -0800979 // Setup CAN.
980 if (!FLAGS_ctre_diag_server) {
981 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
982 c_Phoenix_Diagnostics_Dispose();
983 }
984
985 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
986 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
987 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
988 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
989
990 ::aos::ShmEventLoop can_output_event_loop(&config.message());
991 can_output_event_loop.set_name("CANOutputWriter");
992 DrivetrainWriter drivetrain_writer(&can_output_event_loop);
Ravago Jones2060ee62023-02-03 18:12:24 -0800993
994 drivetrain_writer.set_falcons(right_front, right_back, right_under,
995 left_front, left_back, left_under);
996 drivetrain_writer.set_right_inverted(
997 ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive);
998 drivetrain_writer.set_left_inverted(
999 ctre::phoenixpro::signals::InvertedValue::CounterClockwise_Positive);
milind-u32d29d32023-02-24 21:11:51 -08001000
1001 SuperstructureCANWriter superstructure_can_writer(&can_output_event_loop);
1002 superstructure_can_writer.set_roller_falcon(roller);
1003
Austin Schuhbb4c9ac2023-02-28 22:04:20 -08001004 can_output_event_loop.MakeWatcher(
1005 "/roborio", [&drivetrain_writer, &superstructure_can_writer](
1006 const CANConfiguration &configuration) {
1007 drivetrain_writer.HandleCANConfiguration(configuration);
1008 superstructure_can_writer.HandleCANConfiguration(configuration);
1009 });
1010
milind-u32d29d32023-02-24 21:11:51 -08001011 AddLoop(&can_output_event_loop);
1012
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001013 // Thread 6
1014 // Setup superstructure output
milind-u32d29d32023-02-24 21:11:51 -08001015 ::aos::ShmEventLoop output_event_loop(&config.message());
1016 output_event_loop.set_name("PWMOutputWriter");
Maxwell Hendersonad312342023-01-10 12:07:47 -08001017 SuperstructureWriter superstructure_writer(&output_event_loop);
1018
Henry Speisere139f802023-02-21 14:14:48 -08001019 superstructure_writer.set_proximal_falcon(make_unique<::frc::TalonFX>(1));
1020 superstructure_writer.set_distal_falcon(make_unique<::frc::TalonFX>(0));
milind-u18934eb2023-02-20 16:28:58 -08001021
1022 superstructure_writer.set_roll_joint_victor(
Henry Speisere139f802023-02-21 14:14:48 -08001023 make_unique<::frc::VictorSP>(3));
1024 superstructure_writer.set_wrist_victor(make_unique<::frc::VictorSP>(2));
1025
Maxwell Hendersonad312342023-01-10 12:07:47 -08001026 superstructure_writer.set_superstructure_reading(superstructure_reading);
1027
1028 AddLoop(&output_event_loop);
1029
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001030 // Thread 7
1031 // Setup led_indicator
1032 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
1033 led_indicator_event_loop.set_name("LedIndicator");
1034 control_loops::superstructure::LedIndicator led_indicator(
1035 &led_indicator_event_loop);
1036 AddLoop(&led_indicator_event_loop);
1037
Maxwell Hendersonad312342023-01-10 12:07:47 -08001038 RunLoops();
1039 }
1040};
1041
1042} // namespace wpilib
1043} // namespace y2023
1044
1045AOS_ROBOT_CLASS(::y2023::wpilib::WPILibRobot);