blob: cd52cfda041747db670b339acc14959f8bfd4ea3 [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;
117static constexpr int kCANSignalsCount = 4;
118static constexpr int kRollerSignalsCount = 4;
119static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz;
120
121class Falcon {
122 public:
123 Falcon(int device_id, std::string canbus,
124 aos::SizedArray<ctre::phoenixpro::BaseStatusSignalValue *,
125 kCANFalconCount * kCANSignalsCount +
126 kRollerSignalsCount> *signals)
127 : talon_(device_id, canbus),
128 device_id_(device_id),
129 device_temp_(talon_.GetDeviceTemp()),
130 supply_voltage_(talon_.GetSupplyVoltage()),
131 supply_current_(talon_.GetSupplyCurrent()),
132 torque_current_(talon_.GetTorqueCurrent()),
133 position_(talon_.GetPosition()) {
134 // device temp is not timesynced so don't add it to the list of signals
135 device_temp_.SetUpdateFrequency(kCANUpdateFreqHz);
136
137 CHECK_EQ(kCANSignalsCount, 4);
138 CHECK_NOTNULL(signals);
139 CHECK_LE(signals->size() + 4u, signals->capacity());
140
141 supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz);
142 signals->push_back(&supply_voltage_);
143
144 supply_current_.SetUpdateFrequency(kCANUpdateFreqHz);
145 signals->push_back(&supply_current_);
146
147 torque_current_.SetUpdateFrequency(kCANUpdateFreqHz);
148 signals->push_back(&torque_current_);
149
150 position_.SetUpdateFrequency(kCANUpdateFreqHz);
151 signals->push_back(&position_);
152 }
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());
235
236 double invert =
237 (inverted_ ==
238 ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive
239 ? 1
240 : -1);
241
242 builder.add_position(
243 constants::Values::DrivetrainCANEncoderToMeters(position()) * invert);
244
245 return builder.Finish();
246 }
247
248 int device_id() const { return device_id_; }
249 float device_temp() const { return device_temp_.GetValue().value(); }
250 float supply_voltage() const { return supply_voltage_.GetValue().value(); }
251 float supply_current() const { return supply_current_.GetValue().value(); }
252 float torque_current() const { return torque_current_.GetValue().value(); }
253 float position() const { return position_.GetValue().value(); }
254
255 // returns the monotonic timestamp of the latest timesynced reading in the
256 // timebase of the the syncronized CAN bus clock.
257 int64_t GetTimestamp() {
258 std::chrono::nanoseconds latest_timestamp =
259 torque_current_.GetTimestamp().GetTime();
260
261 return latest_timestamp.count();
262 }
263
264 void RefreshNontimesyncedSignals() { device_temp_.Refresh(); };
265
266 private:
267 ctre::phoenixpro::hardware::TalonFX talon_;
268 int device_id_;
269
270 ctre::phoenixpro::signals::InvertedValue inverted_;
271
272 ctre::phoenixpro::StatusSignalValue<units::temperature::celsius_t>
273 device_temp_;
274 ctre::phoenixpro::StatusSignalValue<units::voltage::volt_t> supply_voltage_;
275 ctre::phoenixpro::StatusSignalValue<units::current::ampere_t> supply_current_,
276 torque_current_;
277 ctre::phoenixpro::StatusSignalValue<units::angle::turn_t> position_;
278};
279
280class CANSensorReader {
281 public:
282 CANSensorReader(aos::EventLoop *event_loop)
283 : event_loop_(event_loop),
284 can_position_sender_(
285 event_loop->MakeSender<drivetrain::CANPosition>("/drivetrain")),
286 roller_falcon_data_(std::nullopt) {
287 event_loop->SetRuntimeRealtimePriority(40);
288 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1}));
289 timer_handler_ = event_loop->AddTimer([this]() { Loop(); });
290 timer_handler_->set_name("CANSensorReader Loop");
291
292 event_loop->OnRun([this]() {
293 timer_handler_->Setup(event_loop_->monotonic_now(), 1 / kCANUpdateFreqHz);
294 });
295 }
296
297 aos::SizedArray<ctre::phoenixpro::BaseStatusSignalValue *,
298 kCANFalconCount * kCANSignalsCount + kRollerSignalsCount>
299 *get_signals_registry() {
300 return &signals_;
301 };
302
303 void set_falcons(std::shared_ptr<Falcon> right_front,
304 std::shared_ptr<Falcon> right_back,
305 std::shared_ptr<Falcon> right_under,
306 std::shared_ptr<Falcon> left_front,
307 std::shared_ptr<Falcon> left_back,
308 std::shared_ptr<Falcon> left_under,
309 std::shared_ptr<Falcon> roller_falcon) {
310 right_front_ = std::move(right_front);
311 right_back_ = std::move(right_back);
312 right_under_ = std::move(right_under);
313 left_front_ = std::move(left_front);
314 left_back_ = std::move(left_back);
315 left_under_ = std::move(left_under);
316 roller_falcon_ = std::move(roller_falcon);
317 }
318
319 std::optional<superstructure::CANFalconT> roller_falcon_data() {
320 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
321 return roller_falcon_data_;
322 }
323
324 private:
325 void Loop() {
326 CHECK_EQ(signals_.size(), 28u);
327 ctre::phoenix::StatusCode status =
328 ctre::phoenixpro::BaseStatusSignalValue::WaitForAll(
329 2000_ms, {signals_[0], signals_[1], signals_[2], signals_[3],
330 signals_[4], signals_[5], signals_[6], signals_[7],
331 signals_[8], signals_[9], signals_[10], signals_[11],
332 signals_[12], signals_[13], signals_[14], signals_[15],
333 signals_[16], signals_[17], signals_[18], signals_[19],
334 signals_[20], signals_[21], signals_[22], signals_[23],
335 signals_[24], signals_[25], signals_[26], signals_[27]});
336
337 if (!status.IsOK()) {
338 AOS_LOG(ERROR, "Failed to read signals from falcons: %s: %s",
339 status.GetName(), status.GetDescription());
340 }
341
342 auto builder = can_position_sender_.MakeBuilder();
343
344 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
345 left_back_, left_under_, roller_falcon_}) {
346 falcon->RefreshNontimesyncedSignals();
347 }
348
349 aos::SizedArray<flatbuffers::Offset<drivetrain::CANFalcon>, kCANFalconCount>
350 falcons;
351
352 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
353 left_back_, left_under_}) {
354 falcons.push_back(falcon->WritePosition(builder.fbb()));
355 }
356
357 auto falcons_list =
358 builder.fbb()->CreateVector<flatbuffers::Offset<drivetrain::CANFalcon>>(
359 falcons);
360
361 drivetrain::CANPosition::Builder can_position_builder =
362 builder.MakeBuilder<drivetrain::CANPosition>();
363
364 can_position_builder.add_falcons(falcons_list);
365 can_position_builder.add_timestamp(right_front_->GetTimestamp());
366 can_position_builder.add_status(static_cast<int>(status));
367
368 builder.CheckOk(builder.Send(can_position_builder.Finish()));
369
370 {
371 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
372 superstructure::CANFalconT roller_falcon_data;
373 roller_falcon_data.id = roller_falcon_->device_id();
374 roller_falcon_data.supply_current = roller_falcon_->supply_current();
Austin Schuh23a90022023-02-24 22:13:39 -0800375 roller_falcon_data.torque_current = -roller_falcon_->torque_current();
milind-u738832d2023-02-24 19:55:54 -0800376 roller_falcon_data.supply_voltage = roller_falcon_->supply_voltage();
377 roller_falcon_data.device_temp = roller_falcon_->device_temp();
Austin Schuh23a90022023-02-24 22:13:39 -0800378 roller_falcon_data.position = -roller_falcon_->position();
milind-u738832d2023-02-24 19:55:54 -0800379 roller_falcon_data_ =
380 std::make_optional<superstructure::CANFalconT>(roller_falcon_data);
381 }
382 }
383
384 aos::EventLoop *event_loop_;
385
386 aos::SizedArray<ctre::phoenixpro::BaseStatusSignalValue *,
387 kCANFalconCount * kCANSignalsCount + kRollerSignalsCount>
388 signals_;
389 aos::Sender<drivetrain::CANPosition> can_position_sender_;
390
391 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
392 left_back_, left_under_, roller_falcon_;
393
394 std::optional<superstructure::CANFalconT> roller_falcon_data_;
395
396 aos::stl_mutex roller_mutex_;
397
398 // Pointer to the timer handler used to modify the wakeup.
399 ::aos::TimerHandler *timer_handler_;
400};
401
Maxwell Hendersonad312342023-01-10 12:07:47 -0800402// Class to send position messages with sensor readings to our loops.
403class SensorReader : public ::frc971::wpilib::SensorReader {
404 public:
405 SensorReader(::aos::ShmEventLoop *event_loop,
milind-u738832d2023-02-24 19:55:54 -0800406 std::shared_ptr<const Values> values,
407 CANSensorReader *can_sensor_reader)
Maxwell Hendersonad312342023-01-10 12:07:47 -0800408 : ::frc971::wpilib::SensorReader(event_loop),
409 values_(std::move(values)),
410 auto_mode_sender_(
411 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
412 "/autonomous")),
413 superstructure_position_sender_(
414 event_loop->MakeSender<superstructure::Position>(
415 "/superstructure")),
416 drivetrain_position_sender_(
417 event_loop
418 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
419 "/drivetrain")),
420 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
milind-u738832d2023-02-24 19:55:54 -0800421 "/drivetrain")),
422 can_sensor_reader_(can_sensor_reader) {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800423 // Set to filter out anything shorter than 1/4 of the minimum pulse width
424 // we should ever see.
425 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
Austin Schuh595ffc72023-02-24 16:24:37 -0800426 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
Maxwell Hendersonad312342023-01-10 12:07:47 -0800427 }
428
429 void Start() override {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800430 AddToDMA(&imu_yaw_rate_reader_);
milind-u3a7f9212023-02-24 20:46:59 -0800431 AddToDMA(&cone_position_sensor_);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800432 }
433
434 // Auto mode switches.
435 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
436 autonomous_modes_.at(i) = ::std::move(sensor);
437 }
438
Ravago Jones2060ee62023-02-03 18:12:24 -0800439 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
440 imu_yaw_rate_input_ = ::std::move(sensor);
441 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
442 }
443
Maxwell Hendersonad312342023-01-10 12:07:47 -0800444 void RunIteration() override {
445 superstructure_reading_->Set(true);
milind-u18934eb2023-02-20 16:28:58 -0800446 {
447 auto builder = superstructure_position_sender_.MakeBuilder();
448 frc971::PotAndAbsolutePositionT proximal;
449 CopyPosition(proximal_encoder_, &proximal,
450 Values::kProximalEncoderCountsPerRevolution(),
451 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh7dcc49b2023-02-21 17:35:10 -0800452 true, values_->arm_proximal.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800453 frc971::PotAndAbsolutePositionT distal;
454 CopyPosition(distal_encoder_, &distal,
455 Values::kDistalEncoderCountsPerRevolution(),
Austin Schuh7dcc49b2023-02-21 17:35:10 -0800456 Values::kDistalEncoderRatio(), distal_pot_translate, true,
milind-u18934eb2023-02-20 16:28:58 -0800457 values_->arm_distal.potentiometer_offset);
458 frc971::PotAndAbsolutePositionT roll_joint;
459 CopyPosition(roll_joint_encoder_, &roll_joint,
460 Values::kRollJointEncoderCountsPerRevolution(),
461 Values::kRollJointEncoderRatio(), roll_joint_pot_translate,
Austin Schuh29d025c2023-03-03 21:41:04 -0800462 false, values_->roll_joint.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800463 frc971::AbsolutePositionT wrist;
464 CopyPosition(wrist_encoder_, &wrist,
465 Values::kWristEncoderCountsPerRevolution(),
Austin Schuhe5248cd2023-03-05 12:46:16 -0800466 values_->wrist.subsystem_params.zeroing_constants
467 .one_revolution_distance /
468 (M_PI * 2.0),
469 values_->wrist_flipped);
milind-u18934eb2023-02-20 16:28:58 -0800470
471 flatbuffers::Offset<frc971::PotAndAbsolutePosition> proximal_offset =
472 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &proximal);
473 flatbuffers::Offset<frc971::PotAndAbsolutePosition> distal_offset =
474 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &distal);
milind-u18934eb2023-02-20 16:28:58 -0800475 flatbuffers::Offset<frc971::PotAndAbsolutePosition> roll_joint_offset =
476 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &roll_joint);
milind-u18a901d2023-02-17 21:51:55 -0800477 flatbuffers::Offset<superstructure::ArmPosition> arm_offset =
478 superstructure::CreateArmPosition(*builder.fbb(), proximal_offset,
479 distal_offset, roll_joint_offset);
milind-u18934eb2023-02-20 16:28:58 -0800480 flatbuffers::Offset<frc971::AbsolutePosition> wrist_offset =
481 frc971::AbsolutePosition::Pack(*builder.fbb(), &wrist);
482
milind-u738832d2023-02-24 19:55:54 -0800483 flatbuffers::Offset<superstructure::CANFalcon> roller_falcon_offset;
484 auto optional_roller_falcon = can_sensor_reader_->roller_falcon_data();
485 if (optional_roller_falcon.has_value()) {
486 roller_falcon_offset = superstructure::CANFalcon::Pack(
487 *builder.fbb(), &optional_roller_falcon.value());
488 }
489
milind-u18934eb2023-02-20 16:28:58 -0800490 superstructure::Position::Builder position_builder =
491 builder.MakeBuilder<superstructure::Position>();
492
493 position_builder.add_arm(arm_offset);
milind-u18934eb2023-02-20 16:28:58 -0800494 position_builder.add_wrist(wrist_offset);
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800495 position_builder.add_end_effector_cube_beam_break(
496 end_effector_cube_beam_break_->Get());
milind-u3a7f9212023-02-24 20:46:59 -0800497 position_builder.add_cone_position(cone_position_sensor_.last_width() /
498 cone_position_sensor_.last_period());
milind-u738832d2023-02-24 19:55:54 -0800499 if (!roller_falcon_offset.IsNull()) {
500 position_builder.add_roller_falcon(roller_falcon_offset);
501 }
Henry Speisere139f802023-02-21 14:14:48 -0800502 builder.CheckOk(builder.Send(position_builder.Finish()));
milind-u18934eb2023-02-20 16:28:58 -0800503 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800504
505 {
506 auto builder = drivetrain_position_sender_.MakeBuilder();
507 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
508 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
509 drivetrain_builder.add_left_encoder(
510 constants::Values::DrivetrainEncoderToMeters(
511 drivetrain_left_encoder_->GetRaw()));
512 drivetrain_builder.add_left_speed(
513 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
514
515 drivetrain_builder.add_right_encoder(
516 -constants::Values::DrivetrainEncoderToMeters(
517 drivetrain_right_encoder_->GetRaw()));
518 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
519 drivetrain_right_encoder_->GetPeriod()));
520
521 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
522 }
523
524 {
525 auto builder = gyro_sender_.MakeBuilder();
526 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
527 builder.MakeBuilder<::frc971::sensors::GyroReading>();
528 // +/- 2000 deg / sec
529 constexpr double kMaxVelocity = 4000; // degrees / second
530 constexpr double kVelocityRadiansPerSecond =
531 kMaxVelocity / 360 * (2.0 * M_PI);
532
533 // Only part of the full range is used to prevent being 100% on or off.
534 constexpr double kScaledRangeLow = 0.1;
535 constexpr double kScaledRangeHigh = 0.9;
536
537 constexpr double kPWMFrequencyHz = 200;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800538 double velocity_duty_cycle =
539 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
540
541 constexpr double kDutyCycleScale =
542 1 / (kScaledRangeHigh - kScaledRangeLow);
543 // scale from 0.1 - 0.9 to 0 - 1
Maxwell Hendersonad312342023-01-10 12:07:47 -0800544 double rescaled_velocity_duty_cycle =
545 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
546
Maxwell Hendersonad312342023-01-10 12:07:47 -0800547 if (!std::isnan(rescaled_velocity_duty_cycle)) {
548 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
549 kVelocityRadiansPerSecond);
550 }
551 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
552 }
553
554 {
555 auto builder = auto_mode_sender_.MakeBuilder();
556
557 uint32_t mode = 0;
558 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
559 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
560 mode |= 1 << i;
561 }
562 }
563
564 auto auto_mode_builder =
565 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
566
567 auto_mode_builder.add_mode(mode);
568
569 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
570 }
571 }
572
573 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
574
575 void set_superstructure_reading(
576 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
577 superstructure_reading_ = superstructure_reading;
578 }
579
milind-u18934eb2023-02-20 16:28:58 -0800580 void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
581 fast_encoder_filter_.Add(encoder.get());
582 proximal_encoder_.set_encoder(::std::move(encoder));
583 }
584
585 void set_proximal_absolute_pwm(
586 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
587 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
588 }
589
590 void set_proximal_potentiometer(
591 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
592 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
593 }
594
595 void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
596 fast_encoder_filter_.Add(encoder.get());
597 distal_encoder_.set_encoder(::std::move(encoder));
598 }
599
600 void set_distal_absolute_pwm(
601 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
602 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
603 }
604
605 void set_distal_potentiometer(
606 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
607 distal_encoder_.set_potentiometer(::std::move(potentiometer));
608 }
609
610 void set_roll_joint_encoder(::std::unique_ptr<frc::Encoder> encoder) {
611 fast_encoder_filter_.Add(encoder.get());
612 roll_joint_encoder_.set_encoder(::std::move(encoder));
613 }
614
615 void set_roll_joint_absolute_pwm(
616 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
617 roll_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
618 }
619
620 void set_roll_joint_potentiometer(
621 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
622 roll_joint_encoder_.set_potentiometer(::std::move(potentiometer));
623 }
624
625 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
626 fast_encoder_filter_.Add(encoder.get());
627 wrist_encoder_.set_encoder(::std::move(encoder));
628 }
629
630 void set_wrist_absolute_pwm(
631 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
632 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
633 }
634
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800635 void set_end_effector_cube_beam_break(
636 ::std::unique_ptr<frc::DigitalInput> sensor) {
637 end_effector_cube_beam_break_ = ::std::move(sensor);
638 }
639
milind-u3a7f9212023-02-24 20:46:59 -0800640 void set_cone_position_sensor(::std::unique_ptr<frc::DigitalInput> sensor) {
641 cone_position_input_ = ::std::move(sensor);
642 cone_position_sensor_.set_input(cone_position_input_.get());
643 }
644
Maxwell Hendersonad312342023-01-10 12:07:47 -0800645 private:
646 std::shared_ptr<const Values> values_;
647
648 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
649 aos::Sender<superstructure::Position> superstructure_position_sender_;
650 aos::Sender<frc971::control_loops::drivetrain::Position>
651 drivetrain_position_sender_;
652 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
653
654 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
655
Ravago Jones2544ad82023-03-04 22:24:49 -0800656 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_,
milind-u3a7f9212023-02-24 20:46:59 -0800657 end_effector_cube_beam_break_;
Ravago Jones2060ee62023-02-03 18:12:24 -0800658
Ravago Jones2544ad82023-03-04 22:24:49 -0800659 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
milind-u18934eb2023-02-20 16:28:58 -0800660
661 frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
662 distal_encoder_, roll_joint_encoder_;
663 frc971::wpilib::AbsoluteEncoder wrist_encoder_;
milind-u3a7f9212023-02-24 20:46:59 -0800664
665 frc971::wpilib::DMAPulseWidthReader cone_position_sensor_;
666 std::unique_ptr<frc::DigitalInput> cone_position_input_;
milind-u738832d2023-02-24 19:55:54 -0800667
668 CANSensorReader *can_sensor_reader_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800669};
670
671class SuperstructureWriter
672 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
673 public:
674 SuperstructureWriter(aos::EventLoop *event_loop)
675 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
milind-u32d29d32023-02-24 21:11:51 -0800676 event_loop, "/superstructure") {
677 event_loop->SetRuntimeRealtimePriority(
678 constants::Values::kDrivetrainWriterPriority);
679 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800680
681 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
682
683 void set_superstructure_reading(
684 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
685 superstructure_reading_ = superstructure_reading;
686 }
687
milind-u18934eb2023-02-20 16:28:58 -0800688 void set_proximal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
689 proximal_falcon_ = ::std::move(t);
690 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800691
milind-u18934eb2023-02-20 16:28:58 -0800692 void set_distal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
693 distal_falcon_ = ::std::move(t);
694 }
695
696 void set_roll_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
697 roll_joint_victor_ = ::std::move(t);
698 }
699
700 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
701 wrist_victor_ = ::std::move(t);
702 }
703
milind-u18934eb2023-02-20 16:28:58 -0800704 private:
705 void Stop() override {
706 AOS_LOG(WARNING, "Superstructure output too old.\n");
707 proximal_falcon_->SetDisabled();
708 distal_falcon_->SetDisabled();
709 roll_joint_victor_->SetDisabled();
710 wrist_victor_->SetDisabled();
milind-u18934eb2023-02-20 16:28:58 -0800711 }
712
713 void Write(const superstructure::Output &output) override {
714 WritePwm(output.proximal_voltage(), proximal_falcon_.get());
715 WritePwm(output.distal_voltage(), distal_falcon_.get());
Austin Schuh3fd5f0e2023-02-22 11:10:37 -0800716 WritePwm(-output.roll_joint_voltage(), roll_joint_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800717 WritePwm(output.wrist_voltage(), wrist_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800718 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800719
720 static void WriteCan(const double voltage,
721 ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) {
722 falcon->Set(
723 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
724 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
725 }
726
727 template <typename T>
728 static void WritePwm(const double voltage, T *motor) {
729 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
730 12.0);
731 }
milind-u18934eb2023-02-20 16:28:58 -0800732
733 ::std::unique_ptr<::frc::TalonFX> proximal_falcon_, distal_falcon_;
734 ::std::unique_ptr<::frc::VictorSP> roll_joint_victor_, wrist_victor_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800735};
736
milind-u32d29d32023-02-24 21:11:51 -0800737class SuperstructureCANWriter
738 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
739 public:
740 SuperstructureCANWriter(::aos::EventLoop *event_loop)
741 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
742 event_loop, "/superstructure") {
743 event_loop->SetRuntimeRealtimePriority(
744 constants::Values::kSuperstructureCANWriterPriority);
745
746 event_loop->OnRun([this]() { WriteConfigs(); });
747 };
748
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800749 void HandleCANConfiguration(const CANConfiguration &configuration) {
750 roller_falcon_->PrintConfigs();
751 if (configuration.reapply()) {
752 WriteConfigs();
753 }
754 }
755
milind-u32d29d32023-02-24 21:11:51 -0800756 void set_roller_falcon(std::shared_ptr<Falcon> roller_falcon) {
757 roller_falcon_ = std::move(roller_falcon);
758 }
759
760 private:
761 void WriteConfigs() { roller_falcon_->WriteRollerConfigs(); }
762
763 void Write(const superstructure::Output &output) override {
764 ctre::phoenixpro::controls::DutyCycleOut roller_control(
765 SafeSpeed(-output.roller_voltage()));
766 roller_control.UpdateFreqHz = 0_Hz;
767 roller_control.EnableFOC = true;
768
769 ctre::phoenix::StatusCode status =
770 roller_falcon_->talon()->SetControl(roller_control);
771
772 if (!status.IsOK()) {
773 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
774 status.GetName(), status.GetDescription());
775 }
776 }
777
778 void Stop() override {
779 AOS_LOG(WARNING, "Superstructure CAN output too old.\n");
Austin Schuh1780e002023-03-03 21:39:26 -0800780 ctre::phoenixpro::controls::DutyCycleOut stop_command(0.0);
781 stop_command.UpdateFreqHz = 0_Hz;
782 stop_command.EnableFOC = true;
milind-u32d29d32023-02-24 21:11:51 -0800783
784 roller_falcon_->talon()->SetControl(stop_command);
785 }
786
787 double SafeSpeed(double voltage) {
788 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
789 }
790
791 std::shared_ptr<Falcon> roller_falcon_;
792};
793
Ravago Jones2060ee62023-02-03 18:12:24 -0800794class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler<
795 ::frc971::control_loops::drivetrain::Output> {
796 public:
797 DrivetrainWriter(::aos::EventLoop *event_loop)
798 : ::frc971::wpilib::LoopOutputHandler<
799 ::frc971::control_loops::drivetrain::Output>(event_loop,
800 "/drivetrain") {
801 event_loop->SetRuntimeRealtimePriority(
802 constants::Values::kDrivetrainWriterPriority);
803
Ravago Jones2060ee62023-02-03 18:12:24 -0800804 event_loop->OnRun([this]() { WriteConfigs(); });
805 }
806
807 void set_falcons(std::shared_ptr<Falcon> right_front,
808 std::shared_ptr<Falcon> right_back,
809 std::shared_ptr<Falcon> right_under,
810 std::shared_ptr<Falcon> left_front,
811 std::shared_ptr<Falcon> left_back,
812 std::shared_ptr<Falcon> left_under) {
813 right_front_ = std::move(right_front);
814 right_back_ = std::move(right_back);
815 right_under_ = std::move(right_under);
816 left_front_ = std::move(left_front);
817 left_back_ = std::move(left_back);
818 left_under_ = std::move(left_under);
819 }
820
821 void set_right_inverted(ctre::phoenixpro::signals::InvertedValue invert) {
822 right_inverted_ = invert;
823 }
824
825 void set_left_inverted(ctre::phoenixpro::signals::InvertedValue invert) {
826 left_inverted_ = invert;
827 }
828
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800829 void HandleCANConfiguration(const CANConfiguration &configuration) {
830 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
831 left_back_, left_under_}) {
832 falcon->PrintConfigs();
833 }
834 if (configuration.reapply()) {
835 WriteConfigs();
836 }
837 }
838
Ravago Jones2060ee62023-02-03 18:12:24 -0800839 private:
840 void WriteConfigs() {
841 for (auto falcon :
842 {right_front_.get(), right_back_.get(), right_under_.get()}) {
843 falcon->WriteConfigs(right_inverted_);
844 }
845
846 for (auto falcon :
847 {left_front_.get(), left_back_.get(), left_under_.get()}) {
848 falcon->WriteConfigs(left_inverted_);
849 }
850 }
851
852 void Write(
853 const ::frc971::control_loops::drivetrain::Output &output) override {
854 ctre::phoenixpro::controls::DutyCycleOut left_control(
855 SafeSpeed(output.left_voltage()));
856 left_control.UpdateFreqHz = 0_Hz;
857 left_control.EnableFOC = true;
858
859 ctre::phoenixpro::controls::DutyCycleOut right_control(
860 SafeSpeed(output.right_voltage()));
861 right_control.UpdateFreqHz = 0_Hz;
862 right_control.EnableFOC = true;
863
864 for (auto falcon :
865 {left_front_.get(), left_back_.get(), left_under_.get()}) {
866 ctre::phoenix::StatusCode status =
867 falcon->talon()->SetControl(left_control);
868
869 if (!status.IsOK()) {
870 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
871 status.GetName(), status.GetDescription());
872 }
873 }
874
875 for (auto falcon :
876 {right_front_.get(), right_back_.get(), right_under_.get()}) {
877 ctre::phoenix::StatusCode status =
878 falcon->talon()->SetControl(right_control);
879
880 if (!status.IsOK()) {
881 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
882 status.GetName(), status.GetDescription());
883 }
884 }
885 }
886
887 void Stop() override {
888 AOS_LOG(WARNING, "drivetrain output too old\n");
Austin Schuh1780e002023-03-03 21:39:26 -0800889 ctre::phoenixpro::controls::DutyCycleOut stop_command(0.0);
890 stop_command.UpdateFreqHz = 0_Hz;
891 stop_command.EnableFOC = true;
892
Ravago Jones2060ee62023-02-03 18:12:24 -0800893 for (auto falcon :
894 {right_front_.get(), right_back_.get(), right_under_.get(),
895 left_front_.get(), left_back_.get(), left_under_.get()}) {
896 falcon->talon()->SetControl(stop_command);
897 }
898 }
899
900 double SafeSpeed(double voltage) {
901 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
902 }
903
904 ctre::phoenixpro::signals::InvertedValue left_inverted_, right_inverted_;
905 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
906 left_back_, left_under_;
907};
Maxwell Hendersonad312342023-01-10 12:07:47 -0800908
909class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
910 public:
911 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
912 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
913 frc::Encoder::k4X);
914 }
915
916 void Run() override {
917 std::shared_ptr<const Values> values =
918 std::make_shared<const Values>(constants::MakeValues());
919
920 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
921 aos::configuration::ReadConfig("aos_config.json");
922
923 // Thread 1.
Ravago Jones2060ee62023-02-03 18:12:24 -0800924 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
925 ::frc971::wpilib::JoystickSender joystick_sender(
926 &joystick_sender_event_loop);
927 AddLoop(&joystick_sender_event_loop);
928
Maxwell Hendersonad312342023-01-10 12:07:47 -0800929 // Thread 2.
930 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
931 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
932 AddLoop(&pdp_fetcher_event_loop);
933
934 std::shared_ptr<frc::DigitalOutput> superstructure_reading =
935 make_unique<frc::DigitalOutput>(25);
936
937 // Thread 3.
milind-u738832d2023-02-24 19:55:54 -0800938 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
939 can_sensor_reader_event_loop.set_name("CANSensorReader");
940 CANSensorReader can_sensor_reader(&can_sensor_reader_event_loop);
941
942 std::shared_ptr<Falcon> right_front = std::make_shared<Falcon>(
943 1, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
944 std::shared_ptr<Falcon> right_back = std::make_shared<Falcon>(
945 2, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
946 std::shared_ptr<Falcon> right_under = std::make_shared<Falcon>(
947 3, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
948 std::shared_ptr<Falcon> left_front = std::make_shared<Falcon>(
949 4, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
950 std::shared_ptr<Falcon> left_back = std::make_shared<Falcon>(
951 5, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
952 std::shared_ptr<Falcon> left_under = std::make_shared<Falcon>(
953 6, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
954 std::shared_ptr<Falcon> roller = std::make_shared<Falcon>(
955 13, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
956
957 can_sensor_reader.set_falcons(right_front, right_back, right_under,
958 left_front, left_back, left_under, roller);
959
960 AddLoop(&can_sensor_reader_event_loop);
961
962 // Thread 4.
Maxwell Hendersonad312342023-01-10 12:07:47 -0800963 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
milind-u738832d2023-02-24 19:55:54 -0800964 SensorReader sensor_reader(&sensor_reader_event_loop, values,
965 &can_sensor_reader);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800966 sensor_reader.set_pwm_trigger(true);
967 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
968 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
969 sensor_reader.set_superstructure_reading(superstructure_reading);
Henry Speisere139f802023-02-21 14:14:48 -0800970 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Ravago Jones2060ee62023-02-03 18:12:24 -0800971
milind-u18934eb2023-02-20 16:28:58 -0800972 sensor_reader.set_proximal_encoder(make_encoder(3));
973 sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(3));
974 sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(3));
975
Henry Speisere139f802023-02-21 14:14:48 -0800976 sensor_reader.set_distal_encoder(make_encoder(2));
977 sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(2));
978 sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(2));
milind-u18934eb2023-02-20 16:28:58 -0800979
Henry Speisere139f802023-02-21 14:14:48 -0800980 sensor_reader.set_roll_joint_encoder(make_encoder(5));
milind-u18934eb2023-02-20 16:28:58 -0800981 sensor_reader.set_roll_joint_absolute_pwm(
Henry Speisere139f802023-02-21 14:14:48 -0800982 make_unique<frc::DigitalInput>(5));
milind-u18934eb2023-02-20 16:28:58 -0800983 sensor_reader.set_roll_joint_potentiometer(
Henry Speisere139f802023-02-21 14:14:48 -0800984 make_unique<frc::AnalogInput>(5));
milind-u18934eb2023-02-20 16:28:58 -0800985
Henry Speisere139f802023-02-21 14:14:48 -0800986 sensor_reader.set_wrist_encoder(make_encoder(4));
987 sensor_reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(4));
milind-u18934eb2023-02-20 16:28:58 -0800988
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800989 sensor_reader.set_end_effector_cube_beam_break(
990 make_unique<frc::DigitalInput>(7));
milind-u3a7f9212023-02-24 20:46:59 -0800991 sensor_reader.set_cone_position_sensor(make_unique<frc::DigitalInput>(8));
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800992
Maxwell Hendersonad312342023-01-10 12:07:47 -0800993 AddLoop(&sensor_reader_event_loop);
994
Ravago Jones2060ee62023-02-03 18:12:24 -0800995 // Thread 5.
milind-u32d29d32023-02-24 21:11:51 -0800996 // Setup CAN.
997 if (!FLAGS_ctre_diag_server) {
998 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
999 c_Phoenix_Diagnostics_Dispose();
1000 }
1001
1002 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
1003 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
1004 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
1005 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
1006
1007 ::aos::ShmEventLoop can_output_event_loop(&config.message());
1008 can_output_event_loop.set_name("CANOutputWriter");
1009 DrivetrainWriter drivetrain_writer(&can_output_event_loop);
Ravago Jones2060ee62023-02-03 18:12:24 -08001010
1011 drivetrain_writer.set_falcons(right_front, right_back, right_under,
1012 left_front, left_back, left_under);
1013 drivetrain_writer.set_right_inverted(
1014 ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive);
1015 drivetrain_writer.set_left_inverted(
1016 ctre::phoenixpro::signals::InvertedValue::CounterClockwise_Positive);
milind-u32d29d32023-02-24 21:11:51 -08001017
1018 SuperstructureCANWriter superstructure_can_writer(&can_output_event_loop);
1019 superstructure_can_writer.set_roller_falcon(roller);
1020
Austin Schuhbb4c9ac2023-02-28 22:04:20 -08001021 can_output_event_loop.MakeWatcher(
1022 "/roborio", [&drivetrain_writer, &superstructure_can_writer](
1023 const CANConfiguration &configuration) {
1024 drivetrain_writer.HandleCANConfiguration(configuration);
1025 superstructure_can_writer.HandleCANConfiguration(configuration);
1026 });
1027
milind-u32d29d32023-02-24 21:11:51 -08001028 AddLoop(&can_output_event_loop);
1029
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001030 // Thread 6
1031 // Setup superstructure output
milind-u32d29d32023-02-24 21:11:51 -08001032 ::aos::ShmEventLoop output_event_loop(&config.message());
1033 output_event_loop.set_name("PWMOutputWriter");
Maxwell Hendersonad312342023-01-10 12:07:47 -08001034 SuperstructureWriter superstructure_writer(&output_event_loop);
1035
Henry Speisere139f802023-02-21 14:14:48 -08001036 superstructure_writer.set_proximal_falcon(make_unique<::frc::TalonFX>(1));
1037 superstructure_writer.set_distal_falcon(make_unique<::frc::TalonFX>(0));
milind-u18934eb2023-02-20 16:28:58 -08001038
1039 superstructure_writer.set_roll_joint_victor(
Henry Speisere139f802023-02-21 14:14:48 -08001040 make_unique<::frc::VictorSP>(3));
1041 superstructure_writer.set_wrist_victor(make_unique<::frc::VictorSP>(2));
1042
Maxwell Hendersonad312342023-01-10 12:07:47 -08001043 superstructure_writer.set_superstructure_reading(superstructure_reading);
1044
1045 AddLoop(&output_event_loop);
1046
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001047 // Thread 7
1048 // Setup led_indicator
1049 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
1050 led_indicator_event_loop.set_name("LedIndicator");
1051 control_loops::superstructure::LedIndicator led_indicator(
1052 &led_indicator_event_loop);
1053 AddLoop(&led_indicator_event_loop);
1054
Maxwell Hendersonad312342023-01-10 12:07:47 -08001055 RunLoops();
1056 }
1057};
1058
1059} // namespace wpilib
1060} // namespace y2023
1061
1062AOS_ROBOT_CLASS(::y2023::wpilib::WPILibRobot);