blob: 921531eb82534f137d02903e9adb122123ca08ef [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 Hendersonad312342023-01-10 12:07:47 -080059#include "y2023/control_loops/superstructure/superstructure_output_generated.h"
60#include "y2023/control_loops/superstructure/superstructure_position_generated.h"
61
Ravago Jones2060ee62023-02-03 18:12:24 -080062DEFINE_bool(ctre_diag_server, false,
63 "If true, enable the diagnostics server for interacting with "
64 "devices on the CAN bus using Phoenix Tuner");
65
Maxwell Hendersonad312342023-01-10 12:07:47 -080066using ::aos::monotonic_clock;
67using ::y2023::constants::Values;
68namespace superstructure = ::y2023::control_loops::superstructure;
Ravago Jones2060ee62023-02-03 18:12:24 -080069namespace drivetrain = ::y2023::control_loops::drivetrain;
Maxwell Hendersonad312342023-01-10 12:07:47 -080070namespace chrono = ::std::chrono;
71using std::make_unique;
72
73namespace y2023 {
74namespace wpilib {
75namespace {
76
77constexpr double kMaxBringupPower = 12.0;
78
79// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
80// DMA stuff and then removing the * 2.0 in *_translate.
81// The low bit is direction.
82
83double drivetrain_velocity_translate(double in) {
84 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
85 (2.0 * M_PI)) *
86 Values::kDrivetrainEncoderRatio() *
87 control_loops::drivetrain::kWheelRadius;
88}
89
milind-u18934eb2023-02-20 16:28:58 -080090double proximal_pot_translate(double voltage) {
91 return voltage * Values::kProximalPotRadiansPerVolt();
92}
93
94double distal_pot_translate(double voltage) {
95 return voltage * Values::kDistalPotRadiansPerVolt();
96}
97
98double roll_joint_pot_translate(double voltage) {
99 return voltage * Values::kRollJointPotRadiansPerVolt();
100}
101
102constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
103 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
104 Values::kMaxProximalEncoderPulsesPerSecond(),
105 Values::kMaxDistalEncoderPulsesPerSecond(),
106 Values::kMaxRollJointEncoderPulsesPerSecond(),
107 Values::kMaxWristEncoderPulsesPerSecond(),
108});
109static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
110 "fast encoders are too fast");
Maxwell Hendersonad312342023-01-10 12:07:47 -0800111
112} // namespace
113
milind-u738832d2023-02-24 19:55:54 -0800114static constexpr int kCANFalconCount = 6;
115static constexpr int kCANSignalsCount = 4;
116static constexpr int kRollerSignalsCount = 4;
117static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz;
118
119class Falcon {
120 public:
121 Falcon(int device_id, std::string canbus,
122 aos::SizedArray<ctre::phoenixpro::BaseStatusSignalValue *,
123 kCANFalconCount * kCANSignalsCount +
124 kRollerSignalsCount> *signals)
125 : 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()),
131 position_(talon_.GetPosition()) {
132 // device temp is not timesynced so don't add it to the list of signals
133 device_temp_.SetUpdateFrequency(kCANUpdateFreqHz);
134
135 CHECK_EQ(kCANSignalsCount, 4);
136 CHECK_NOTNULL(signals);
137 CHECK_LE(signals->size() + 4u, signals->capacity());
138
139 supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz);
140 signals->push_back(&supply_voltage_);
141
142 supply_current_.SetUpdateFrequency(kCANUpdateFreqHz);
143 signals->push_back(&supply_current_);
144
145 torque_current_.SetUpdateFrequency(kCANUpdateFreqHz);
146 signals->push_back(&torque_current_);
147
148 position_.SetUpdateFrequency(kCANUpdateFreqHz);
149 signals->push_back(&position_);
150 }
151
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800152 void PrintConfigs() {
153 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
154 ctre::phoenix::StatusCode status =
155 talon_.GetConfigurator().Refresh(configuration);
156 if (!status.IsOK()) {
157 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
158 status.GetName(), status.GetDescription());
159 }
160 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
161 }
162
milind-u738832d2023-02-24 19:55:54 -0800163 void WriteConfigs(ctre::phoenixpro::signals::InvertedValue invert) {
164 inverted_ = invert;
165
166 ctre::phoenixpro::configs::CurrentLimitsConfigs current_limits;
167 current_limits.StatorCurrentLimit =
168 constants::Values::kDrivetrainStatorCurrentLimit();
169 current_limits.StatorCurrentLimitEnable = true;
170 current_limits.SupplyCurrentLimit =
171 constants::Values::kDrivetrainSupplyCurrentLimit();
172 current_limits.SupplyCurrentLimitEnable = true;
173
174 ctre::phoenixpro::configs::MotorOutputConfigs output_configs;
175 output_configs.NeutralMode =
176 ctre::phoenixpro::signals::NeutralModeValue::Brake;
177 output_configs.DutyCycleNeutralDeadband = 0;
178
179 output_configs.Inverted = inverted_;
180
181 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
182 configuration.CurrentLimits = current_limits;
183 configuration.MotorOutput = output_configs;
184
185 ctre::phoenix::StatusCode status =
186 talon_.GetConfigurator().Apply(configuration);
187 if (!status.IsOK()) {
188 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
189 status.GetName(), status.GetDescription());
190 }
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800191
192 PrintConfigs();
milind-u738832d2023-02-24 19:55:54 -0800193 }
194
195 void WriteRollerConfigs() {
196 ctre::phoenixpro::configs::CurrentLimitsConfigs current_limits;
197 current_limits.StatorCurrentLimit =
198 constants::Values::kRollerStatorCurrentLimit();
199 current_limits.StatorCurrentLimitEnable = true;
200 current_limits.SupplyCurrentLimit =
201 constants::Values::kRollerSupplyCurrentLimit();
202 current_limits.SupplyCurrentLimitEnable = true;
203
204 ctre::phoenixpro::configs::MotorOutputConfigs output_configs;
205 output_configs.NeutralMode =
206 ctre::phoenixpro::signals::NeutralModeValue::Brake;
207 output_configs.DutyCycleNeutralDeadband = 0;
208
209 ctre::phoenixpro::configs::TalonFXConfiguration configuration;
210 configuration.CurrentLimits = current_limits;
211 configuration.MotorOutput = output_configs;
212
213 ctre::phoenix::StatusCode status =
214 talon_.GetConfigurator().Apply(configuration);
215 if (!status.IsOK()) {
216 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
217 status.GetName(), status.GetDescription());
218 }
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800219
220 PrintConfigs();
milind-u738832d2023-02-24 19:55:54 -0800221 }
222
223 ctre::phoenixpro::hardware::TalonFX *talon() { return &talon_; }
224
225 flatbuffers::Offset<drivetrain::CANFalcon> WritePosition(
226 flatbuffers::FlatBufferBuilder *fbb) {
227 drivetrain::CANFalcon::Builder builder(*fbb);
228 builder.add_id(device_id_);
229 builder.add_device_temp(device_temp());
230 builder.add_supply_voltage(supply_voltage());
231 builder.add_supply_current(supply_current());
232 builder.add_torque_current(torque_current());
233
234 double invert =
235 (inverted_ ==
236 ctre::phoenixpro::signals::InvertedValue::Clockwise_Positive
237 ? 1
238 : -1);
239
240 builder.add_position(
241 constants::Values::DrivetrainCANEncoderToMeters(position()) * invert);
242
243 return builder.Finish();
244 }
245
246 int device_id() const { return device_id_; }
247 float device_temp() const { return device_temp_.GetValue().value(); }
248 float supply_voltage() const { return supply_voltage_.GetValue().value(); }
249 float supply_current() const { return supply_current_.GetValue().value(); }
250 float torque_current() const { return torque_current_.GetValue().value(); }
251 float position() const { return position_.GetValue().value(); }
252
253 // returns the monotonic timestamp of the latest timesynced reading in the
254 // timebase of the the syncronized CAN bus clock.
255 int64_t GetTimestamp() {
256 std::chrono::nanoseconds latest_timestamp =
257 torque_current_.GetTimestamp().GetTime();
258
259 return latest_timestamp.count();
260 }
261
262 void RefreshNontimesyncedSignals() { device_temp_.Refresh(); };
263
264 private:
265 ctre::phoenixpro::hardware::TalonFX talon_;
266 int device_id_;
267
268 ctre::phoenixpro::signals::InvertedValue inverted_;
269
270 ctre::phoenixpro::StatusSignalValue<units::temperature::celsius_t>
271 device_temp_;
272 ctre::phoenixpro::StatusSignalValue<units::voltage::volt_t> supply_voltage_;
273 ctre::phoenixpro::StatusSignalValue<units::current::ampere_t> supply_current_,
274 torque_current_;
275 ctre::phoenixpro::StatusSignalValue<units::angle::turn_t> position_;
276};
277
278class CANSensorReader {
279 public:
280 CANSensorReader(aos::EventLoop *event_loop)
281 : event_loop_(event_loop),
282 can_position_sender_(
283 event_loop->MakeSender<drivetrain::CANPosition>("/drivetrain")),
284 roller_falcon_data_(std::nullopt) {
285 event_loop->SetRuntimeRealtimePriority(40);
286 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1}));
287 timer_handler_ = event_loop->AddTimer([this]() { Loop(); });
288 timer_handler_->set_name("CANSensorReader Loop");
289
290 event_loop->OnRun([this]() {
291 timer_handler_->Setup(event_loop_->monotonic_now(), 1 / kCANUpdateFreqHz);
292 });
293 }
294
295 aos::SizedArray<ctre::phoenixpro::BaseStatusSignalValue *,
296 kCANFalconCount * kCANSignalsCount + kRollerSignalsCount>
297 *get_signals_registry() {
298 return &signals_;
299 };
300
301 void set_falcons(std::shared_ptr<Falcon> right_front,
302 std::shared_ptr<Falcon> right_back,
303 std::shared_ptr<Falcon> right_under,
304 std::shared_ptr<Falcon> left_front,
305 std::shared_ptr<Falcon> left_back,
306 std::shared_ptr<Falcon> left_under,
307 std::shared_ptr<Falcon> roller_falcon) {
308 right_front_ = std::move(right_front);
309 right_back_ = std::move(right_back);
310 right_under_ = std::move(right_under);
311 left_front_ = std::move(left_front);
312 left_back_ = std::move(left_back);
313 left_under_ = std::move(left_under);
314 roller_falcon_ = std::move(roller_falcon);
315 }
316
317 std::optional<superstructure::CANFalconT> roller_falcon_data() {
318 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
319 return roller_falcon_data_;
320 }
321
322 private:
323 void Loop() {
324 CHECK_EQ(signals_.size(), 28u);
325 ctre::phoenix::StatusCode status =
326 ctre::phoenixpro::BaseStatusSignalValue::WaitForAll(
327 2000_ms, {signals_[0], signals_[1], signals_[2], signals_[3],
328 signals_[4], signals_[5], signals_[6], signals_[7],
329 signals_[8], signals_[9], signals_[10], signals_[11],
330 signals_[12], signals_[13], signals_[14], signals_[15],
331 signals_[16], signals_[17], signals_[18], signals_[19],
332 signals_[20], signals_[21], signals_[22], signals_[23],
333 signals_[24], signals_[25], signals_[26], signals_[27]});
334
335 if (!status.IsOK()) {
336 AOS_LOG(ERROR, "Failed to read signals from falcons: %s: %s",
337 status.GetName(), status.GetDescription());
338 }
339
340 auto builder = can_position_sender_.MakeBuilder();
341
342 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
343 left_back_, left_under_, roller_falcon_}) {
344 falcon->RefreshNontimesyncedSignals();
345 }
346
347 aos::SizedArray<flatbuffers::Offset<drivetrain::CANFalcon>, kCANFalconCount>
348 falcons;
349
350 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
351 left_back_, left_under_}) {
352 falcons.push_back(falcon->WritePosition(builder.fbb()));
353 }
354
355 auto falcons_list =
356 builder.fbb()->CreateVector<flatbuffers::Offset<drivetrain::CANFalcon>>(
357 falcons);
358
359 drivetrain::CANPosition::Builder can_position_builder =
360 builder.MakeBuilder<drivetrain::CANPosition>();
361
362 can_position_builder.add_falcons(falcons_list);
363 can_position_builder.add_timestamp(right_front_->GetTimestamp());
364 can_position_builder.add_status(static_cast<int>(status));
365
366 builder.CheckOk(builder.Send(can_position_builder.Finish()));
367
368 {
369 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
370 superstructure::CANFalconT roller_falcon_data;
371 roller_falcon_data.id = roller_falcon_->device_id();
372 roller_falcon_data.supply_current = roller_falcon_->supply_current();
Austin Schuh23a90022023-02-24 22:13:39 -0800373 roller_falcon_data.torque_current = -roller_falcon_->torque_current();
milind-u738832d2023-02-24 19:55:54 -0800374 roller_falcon_data.supply_voltage = roller_falcon_->supply_voltage();
375 roller_falcon_data.device_temp = roller_falcon_->device_temp();
Austin Schuh23a90022023-02-24 22:13:39 -0800376 roller_falcon_data.position = -roller_falcon_->position();
milind-u738832d2023-02-24 19:55:54 -0800377 roller_falcon_data_ =
378 std::make_optional<superstructure::CANFalconT>(roller_falcon_data);
379 }
380 }
381
382 aos::EventLoop *event_loop_;
383
384 aos::SizedArray<ctre::phoenixpro::BaseStatusSignalValue *,
385 kCANFalconCount * kCANSignalsCount + kRollerSignalsCount>
386 signals_;
387 aos::Sender<drivetrain::CANPosition> can_position_sender_;
388
389 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
390 left_back_, left_under_, roller_falcon_;
391
392 std::optional<superstructure::CANFalconT> roller_falcon_data_;
393
394 aos::stl_mutex roller_mutex_;
395
396 // Pointer to the timer handler used to modify the wakeup.
397 ::aos::TimerHandler *timer_handler_;
398};
399
Maxwell Hendersonad312342023-01-10 12:07:47 -0800400// Class to send position messages with sensor readings to our loops.
401class SensorReader : public ::frc971::wpilib::SensorReader {
402 public:
403 SensorReader(::aos::ShmEventLoop *event_loop,
milind-u738832d2023-02-24 19:55:54 -0800404 std::shared_ptr<const Values> values,
405 CANSensorReader *can_sensor_reader)
Maxwell Hendersonad312342023-01-10 12:07:47 -0800406 : ::frc971::wpilib::SensorReader(event_loop),
407 values_(std::move(values)),
408 auto_mode_sender_(
409 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
410 "/autonomous")),
411 superstructure_position_sender_(
412 event_loop->MakeSender<superstructure::Position>(
413 "/superstructure")),
414 drivetrain_position_sender_(
415 event_loop
416 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
417 "/drivetrain")),
418 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
milind-u738832d2023-02-24 19:55:54 -0800419 "/drivetrain")),
420 can_sensor_reader_(can_sensor_reader) {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800421 // Set to filter out anything shorter than 1/4 of the minimum pulse width
422 // we should ever see.
423 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
Austin Schuh595ffc72023-02-24 16:24:37 -0800424 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
Maxwell Hendersonad312342023-01-10 12:07:47 -0800425 }
426
427 void Start() override {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800428 AddToDMA(&imu_yaw_rate_reader_);
milind-u3a7f9212023-02-24 20:46:59 -0800429 AddToDMA(&cone_position_sensor_);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800430 }
431
432 // Auto mode switches.
433 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
434 autonomous_modes_.at(i) = ::std::move(sensor);
435 }
436
Ravago Jones2060ee62023-02-03 18:12:24 -0800437 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
438 imu_yaw_rate_input_ = ::std::move(sensor);
439 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
440 }
441
Maxwell Hendersonad312342023-01-10 12:07:47 -0800442 void RunIteration() override {
443 superstructure_reading_->Set(true);
milind-u18934eb2023-02-20 16:28:58 -0800444 {
445 auto builder = superstructure_position_sender_.MakeBuilder();
446 frc971::PotAndAbsolutePositionT proximal;
447 CopyPosition(proximal_encoder_, &proximal,
448 Values::kProximalEncoderCountsPerRevolution(),
449 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh7dcc49b2023-02-21 17:35:10 -0800450 true, values_->arm_proximal.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800451 frc971::PotAndAbsolutePositionT distal;
452 CopyPosition(distal_encoder_, &distal,
453 Values::kDistalEncoderCountsPerRevolution(),
Austin Schuh7dcc49b2023-02-21 17:35:10 -0800454 Values::kDistalEncoderRatio(), distal_pot_translate, true,
milind-u18934eb2023-02-20 16:28:58 -0800455 values_->arm_distal.potentiometer_offset);
456 frc971::PotAndAbsolutePositionT roll_joint;
457 CopyPosition(roll_joint_encoder_, &roll_joint,
458 Values::kRollJointEncoderCountsPerRevolution(),
459 Values::kRollJointEncoderRatio(), roll_joint_pot_translate,
Austin Schuh29d025c2023-03-03 21:41:04 -0800460 false, values_->roll_joint.potentiometer_offset);
milind-u18934eb2023-02-20 16:28:58 -0800461 frc971::AbsolutePositionT wrist;
462 CopyPosition(wrist_encoder_, &wrist,
463 Values::kWristEncoderCountsPerRevolution(),
464 Values::kWristEncoderRatio(), false);
465
466 flatbuffers::Offset<frc971::PotAndAbsolutePosition> proximal_offset =
467 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &proximal);
468 flatbuffers::Offset<frc971::PotAndAbsolutePosition> distal_offset =
469 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &distal);
milind-u18934eb2023-02-20 16:28:58 -0800470 flatbuffers::Offset<frc971::PotAndAbsolutePosition> roll_joint_offset =
471 frc971::PotAndAbsolutePosition::Pack(*builder.fbb(), &roll_joint);
milind-u18a901d2023-02-17 21:51:55 -0800472 flatbuffers::Offset<superstructure::ArmPosition> arm_offset =
473 superstructure::CreateArmPosition(*builder.fbb(), proximal_offset,
474 distal_offset, roll_joint_offset);
milind-u18934eb2023-02-20 16:28:58 -0800475 flatbuffers::Offset<frc971::AbsolutePosition> wrist_offset =
476 frc971::AbsolutePosition::Pack(*builder.fbb(), &wrist);
477
milind-u738832d2023-02-24 19:55:54 -0800478 flatbuffers::Offset<superstructure::CANFalcon> roller_falcon_offset;
479 auto optional_roller_falcon = can_sensor_reader_->roller_falcon_data();
480 if (optional_roller_falcon.has_value()) {
481 roller_falcon_offset = superstructure::CANFalcon::Pack(
482 *builder.fbb(), &optional_roller_falcon.value());
483 }
484
milind-u18934eb2023-02-20 16:28:58 -0800485 superstructure::Position::Builder position_builder =
486 builder.MakeBuilder<superstructure::Position>();
487
488 position_builder.add_arm(arm_offset);
milind-u18934eb2023-02-20 16:28:58 -0800489 position_builder.add_wrist(wrist_offset);
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800490 position_builder.add_end_effector_cube_beam_break(
491 end_effector_cube_beam_break_->Get());
milind-u3a7f9212023-02-24 20:46:59 -0800492 position_builder.add_cone_position(cone_position_sensor_.last_width() /
493 cone_position_sensor_.last_period());
milind-u738832d2023-02-24 19:55:54 -0800494 if (!roller_falcon_offset.IsNull()) {
495 position_builder.add_roller_falcon(roller_falcon_offset);
496 }
Henry Speisere139f802023-02-21 14:14:48 -0800497 builder.CheckOk(builder.Send(position_builder.Finish()));
milind-u18934eb2023-02-20 16:28:58 -0800498 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800499
500 {
501 auto builder = drivetrain_position_sender_.MakeBuilder();
502 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
503 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
504 drivetrain_builder.add_left_encoder(
505 constants::Values::DrivetrainEncoderToMeters(
506 drivetrain_left_encoder_->GetRaw()));
507 drivetrain_builder.add_left_speed(
508 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
509
510 drivetrain_builder.add_right_encoder(
511 -constants::Values::DrivetrainEncoderToMeters(
512 drivetrain_right_encoder_->GetRaw()));
513 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
514 drivetrain_right_encoder_->GetPeriod()));
515
516 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
517 }
518
519 {
520 auto builder = gyro_sender_.MakeBuilder();
521 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
522 builder.MakeBuilder<::frc971::sensors::GyroReading>();
523 // +/- 2000 deg / sec
524 constexpr double kMaxVelocity = 4000; // degrees / second
525 constexpr double kVelocityRadiansPerSecond =
526 kMaxVelocity / 360 * (2.0 * M_PI);
527
528 // Only part of the full range is used to prevent being 100% on or off.
529 constexpr double kScaledRangeLow = 0.1;
530 constexpr double kScaledRangeHigh = 0.9;
531
532 constexpr double kPWMFrequencyHz = 200;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800533 double velocity_duty_cycle =
534 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
535
536 constexpr double kDutyCycleScale =
537 1 / (kScaledRangeHigh - kScaledRangeLow);
538 // scale from 0.1 - 0.9 to 0 - 1
Maxwell Hendersonad312342023-01-10 12:07:47 -0800539 double rescaled_velocity_duty_cycle =
540 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
541
Maxwell Hendersonad312342023-01-10 12:07:47 -0800542 if (!std::isnan(rescaled_velocity_duty_cycle)) {
543 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
544 kVelocityRadiansPerSecond);
545 }
546 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
547 }
548
549 {
550 auto builder = auto_mode_sender_.MakeBuilder();
551
552 uint32_t mode = 0;
553 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
554 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
555 mode |= 1 << i;
556 }
557 }
558
559 auto auto_mode_builder =
560 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
561
562 auto_mode_builder.add_mode(mode);
563
564 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
565 }
566 }
567
568 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
569
570 void set_superstructure_reading(
571 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
572 superstructure_reading_ = superstructure_reading;
573 }
574
milind-u18934eb2023-02-20 16:28:58 -0800575 void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
576 fast_encoder_filter_.Add(encoder.get());
577 proximal_encoder_.set_encoder(::std::move(encoder));
578 }
579
580 void set_proximal_absolute_pwm(
581 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
582 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
583 }
584
585 void set_proximal_potentiometer(
586 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
587 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
588 }
589
590 void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
591 fast_encoder_filter_.Add(encoder.get());
592 distal_encoder_.set_encoder(::std::move(encoder));
593 }
594
595 void set_distal_absolute_pwm(
596 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
597 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
598 }
599
600 void set_distal_potentiometer(
601 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
602 distal_encoder_.set_potentiometer(::std::move(potentiometer));
603 }
604
605 void set_roll_joint_encoder(::std::unique_ptr<frc::Encoder> encoder) {
606 fast_encoder_filter_.Add(encoder.get());
607 roll_joint_encoder_.set_encoder(::std::move(encoder));
608 }
609
610 void set_roll_joint_absolute_pwm(
611 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
612 roll_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
613 }
614
615 void set_roll_joint_potentiometer(
616 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
617 roll_joint_encoder_.set_potentiometer(::std::move(potentiometer));
618 }
619
620 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
621 fast_encoder_filter_.Add(encoder.get());
622 wrist_encoder_.set_encoder(::std::move(encoder));
623 }
624
625 void set_wrist_absolute_pwm(
626 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
627 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
628 }
629
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800630 void set_end_effector_cube_beam_break(
631 ::std::unique_ptr<frc::DigitalInput> sensor) {
632 end_effector_cube_beam_break_ = ::std::move(sensor);
633 }
634
milind-u3a7f9212023-02-24 20:46:59 -0800635 void set_cone_position_sensor(::std::unique_ptr<frc::DigitalInput> sensor) {
636 cone_position_input_ = ::std::move(sensor);
637 cone_position_sensor_.set_input(cone_position_input_.get());
638 }
639
Maxwell Hendersonad312342023-01-10 12:07:47 -0800640 private:
641 std::shared_ptr<const Values> values_;
642
643 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
644 aos::Sender<superstructure::Position> superstructure_position_sender_;
645 aos::Sender<frc971::control_loops::drivetrain::Position>
646 drivetrain_position_sender_;
647 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
648
649 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
650
Ravago Jones2544ad82023-03-04 22:24:49 -0800651 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_,
milind-u3a7f9212023-02-24 20:46:59 -0800652 end_effector_cube_beam_break_;
Ravago Jones2060ee62023-02-03 18:12:24 -0800653
Ravago Jones2544ad82023-03-04 22:24:49 -0800654 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
milind-u18934eb2023-02-20 16:28:58 -0800655
656 frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
657 distal_encoder_, roll_joint_encoder_;
658 frc971::wpilib::AbsoluteEncoder wrist_encoder_;
milind-u3a7f9212023-02-24 20:46:59 -0800659
660 frc971::wpilib::DMAPulseWidthReader cone_position_sensor_;
661 std::unique_ptr<frc::DigitalInput> cone_position_input_;
milind-u738832d2023-02-24 19:55:54 -0800662
663 CANSensorReader *can_sensor_reader_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800664};
665
666class SuperstructureWriter
667 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
668 public:
669 SuperstructureWriter(aos::EventLoop *event_loop)
670 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
milind-u32d29d32023-02-24 21:11:51 -0800671 event_loop, "/superstructure") {
672 event_loop->SetRuntimeRealtimePriority(
673 constants::Values::kDrivetrainWriterPriority);
674 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800675
676 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
677
678 void set_superstructure_reading(
679 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
680 superstructure_reading_ = superstructure_reading;
681 }
682
milind-u18934eb2023-02-20 16:28:58 -0800683 void set_proximal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
684 proximal_falcon_ = ::std::move(t);
685 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800686
milind-u18934eb2023-02-20 16:28:58 -0800687 void set_distal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
688 distal_falcon_ = ::std::move(t);
689 }
690
691 void set_roll_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
692 roll_joint_victor_ = ::std::move(t);
693 }
694
695 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
696 wrist_victor_ = ::std::move(t);
697 }
698
milind-u18934eb2023-02-20 16:28:58 -0800699 private:
700 void Stop() override {
701 AOS_LOG(WARNING, "Superstructure output too old.\n");
702 proximal_falcon_->SetDisabled();
703 distal_falcon_->SetDisabled();
704 roll_joint_victor_->SetDisabled();
705 wrist_victor_->SetDisabled();
milind-u18934eb2023-02-20 16:28:58 -0800706 }
707
708 void Write(const superstructure::Output &output) override {
709 WritePwm(output.proximal_voltage(), proximal_falcon_.get());
710 WritePwm(output.distal_voltage(), distal_falcon_.get());
Austin Schuh3fd5f0e2023-02-22 11:10:37 -0800711 WritePwm(-output.roll_joint_voltage(), roll_joint_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800712 WritePwm(output.wrist_voltage(), wrist_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800713 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800714
715 static void WriteCan(const double voltage,
716 ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) {
717 falcon->Set(
718 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
719 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
720 }
721
722 template <typename T>
723 static void WritePwm(const double voltage, T *motor) {
724 motor->SetSpeed(std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) /
725 12.0);
726 }
milind-u18934eb2023-02-20 16:28:58 -0800727
728 ::std::unique_ptr<::frc::TalonFX> proximal_falcon_, distal_falcon_;
729 ::std::unique_ptr<::frc::VictorSP> roll_joint_victor_, wrist_victor_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800730};
731
milind-u32d29d32023-02-24 21:11:51 -0800732class SuperstructureCANWriter
733 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
734 public:
735 SuperstructureCANWriter(::aos::EventLoop *event_loop)
736 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
737 event_loop, "/superstructure") {
738 event_loop->SetRuntimeRealtimePriority(
739 constants::Values::kSuperstructureCANWriterPriority);
740
741 event_loop->OnRun([this]() { WriteConfigs(); });
742 };
743
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800744 void HandleCANConfiguration(const CANConfiguration &configuration) {
745 roller_falcon_->PrintConfigs();
746 if (configuration.reapply()) {
747 WriteConfigs();
748 }
749 }
750
milind-u32d29d32023-02-24 21:11:51 -0800751 void set_roller_falcon(std::shared_ptr<Falcon> roller_falcon) {
752 roller_falcon_ = std::move(roller_falcon);
753 }
754
755 private:
756 void WriteConfigs() { roller_falcon_->WriteRollerConfigs(); }
757
758 void Write(const superstructure::Output &output) override {
759 ctre::phoenixpro::controls::DutyCycleOut roller_control(
760 SafeSpeed(-output.roller_voltage()));
761 roller_control.UpdateFreqHz = 0_Hz;
762 roller_control.EnableFOC = true;
763
764 ctre::phoenix::StatusCode status =
765 roller_falcon_->talon()->SetControl(roller_control);
766
767 if (!status.IsOK()) {
768 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
769 status.GetName(), status.GetDescription());
770 }
771 }
772
773 void Stop() override {
774 AOS_LOG(WARNING, "Superstructure CAN output too old.\n");
Austin Schuh1780e002023-03-03 21:39:26 -0800775 ctre::phoenixpro::controls::DutyCycleOut stop_command(0.0);
776 stop_command.UpdateFreqHz = 0_Hz;
777 stop_command.EnableFOC = true;
milind-u32d29d32023-02-24 21:11:51 -0800778
779 roller_falcon_->talon()->SetControl(stop_command);
780 }
781
782 double SafeSpeed(double voltage) {
783 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
784 }
785
786 std::shared_ptr<Falcon> roller_falcon_;
787};
788
Ravago Jones2060ee62023-02-03 18:12:24 -0800789class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler<
790 ::frc971::control_loops::drivetrain::Output> {
791 public:
792 DrivetrainWriter(::aos::EventLoop *event_loop)
793 : ::frc971::wpilib::LoopOutputHandler<
794 ::frc971::control_loops::drivetrain::Output>(event_loop,
795 "/drivetrain") {
796 event_loop->SetRuntimeRealtimePriority(
797 constants::Values::kDrivetrainWriterPriority);
798
Ravago Jones2060ee62023-02-03 18:12:24 -0800799 event_loop->OnRun([this]() { WriteConfigs(); });
800 }
801
802 void set_falcons(std::shared_ptr<Falcon> right_front,
803 std::shared_ptr<Falcon> right_back,
804 std::shared_ptr<Falcon> right_under,
805 std::shared_ptr<Falcon> left_front,
806 std::shared_ptr<Falcon> left_back,
807 std::shared_ptr<Falcon> left_under) {
808 right_front_ = std::move(right_front);
809 right_back_ = std::move(right_back);
810 right_under_ = std::move(right_under);
811 left_front_ = std::move(left_front);
812 left_back_ = std::move(left_back);
813 left_under_ = std::move(left_under);
814 }
815
816 void set_right_inverted(ctre::phoenixpro::signals::InvertedValue invert) {
817 right_inverted_ = invert;
818 }
819
820 void set_left_inverted(ctre::phoenixpro::signals::InvertedValue invert) {
821 left_inverted_ = invert;
822 }
823
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800824 void HandleCANConfiguration(const CANConfiguration &configuration) {
825 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
826 left_back_, left_under_}) {
827 falcon->PrintConfigs();
828 }
829 if (configuration.reapply()) {
830 WriteConfigs();
831 }
832 }
833
Ravago Jones2060ee62023-02-03 18:12:24 -0800834 private:
835 void WriteConfigs() {
836 for (auto falcon :
837 {right_front_.get(), right_back_.get(), right_under_.get()}) {
838 falcon->WriteConfigs(right_inverted_);
839 }
840
841 for (auto falcon :
842 {left_front_.get(), left_back_.get(), left_under_.get()}) {
843 falcon->WriteConfigs(left_inverted_);
844 }
845 }
846
847 void Write(
848 const ::frc971::control_loops::drivetrain::Output &output) override {
849 ctre::phoenixpro::controls::DutyCycleOut left_control(
850 SafeSpeed(output.left_voltage()));
851 left_control.UpdateFreqHz = 0_Hz;
852 left_control.EnableFOC = true;
853
854 ctre::phoenixpro::controls::DutyCycleOut right_control(
855 SafeSpeed(output.right_voltage()));
856 right_control.UpdateFreqHz = 0_Hz;
857 right_control.EnableFOC = true;
858
859 for (auto falcon :
860 {left_front_.get(), left_back_.get(), left_under_.get()}) {
861 ctre::phoenix::StatusCode status =
862 falcon->talon()->SetControl(left_control);
863
864 if (!status.IsOK()) {
865 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
866 status.GetName(), status.GetDescription());
867 }
868 }
869
870 for (auto falcon :
871 {right_front_.get(), right_back_.get(), right_under_.get()}) {
872 ctre::phoenix::StatusCode status =
873 falcon->talon()->SetControl(right_control);
874
875 if (!status.IsOK()) {
876 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
877 status.GetName(), status.GetDescription());
878 }
879 }
880 }
881
882 void Stop() override {
883 AOS_LOG(WARNING, "drivetrain output too old\n");
Austin Schuh1780e002023-03-03 21:39:26 -0800884 ctre::phoenixpro::controls::DutyCycleOut stop_command(0.0);
885 stop_command.UpdateFreqHz = 0_Hz;
886 stop_command.EnableFOC = true;
887
Ravago Jones2060ee62023-02-03 18:12:24 -0800888 for (auto falcon :
889 {right_front_.get(), right_back_.get(), right_under_.get(),
890 left_front_.get(), left_back_.get(), left_under_.get()}) {
891 falcon->talon()->SetControl(stop_command);
892 }
893 }
894
895 double SafeSpeed(double voltage) {
896 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
897 }
898
899 ctre::phoenixpro::signals::InvertedValue left_inverted_, right_inverted_;
900 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
901 left_back_, left_under_;
902};
Maxwell Hendersonad312342023-01-10 12:07:47 -0800903
904class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
905 public:
906 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
907 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
908 frc::Encoder::k4X);
909 }
910
911 void Run() override {
912 std::shared_ptr<const Values> values =
913 std::make_shared<const Values>(constants::MakeValues());
914
915 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
916 aos::configuration::ReadConfig("aos_config.json");
917
918 // Thread 1.
Ravago Jones2060ee62023-02-03 18:12:24 -0800919 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
920 ::frc971::wpilib::JoystickSender joystick_sender(
921 &joystick_sender_event_loop);
922 AddLoop(&joystick_sender_event_loop);
923
Maxwell Hendersonad312342023-01-10 12:07:47 -0800924 // Thread 2.
925 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
926 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
927 AddLoop(&pdp_fetcher_event_loop);
928
929 std::shared_ptr<frc::DigitalOutput> superstructure_reading =
930 make_unique<frc::DigitalOutput>(25);
931
932 // Thread 3.
milind-u738832d2023-02-24 19:55:54 -0800933 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
934 can_sensor_reader_event_loop.set_name("CANSensorReader");
935 CANSensorReader can_sensor_reader(&can_sensor_reader_event_loop);
936
937 std::shared_ptr<Falcon> right_front = std::make_shared<Falcon>(
938 1, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
939 std::shared_ptr<Falcon> right_back = std::make_shared<Falcon>(
940 2, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
941 std::shared_ptr<Falcon> right_under = std::make_shared<Falcon>(
942 3, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
943 std::shared_ptr<Falcon> left_front = std::make_shared<Falcon>(
944 4, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
945 std::shared_ptr<Falcon> left_back = std::make_shared<Falcon>(
946 5, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
947 std::shared_ptr<Falcon> left_under = std::make_shared<Falcon>(
948 6, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
949 std::shared_ptr<Falcon> roller = std::make_shared<Falcon>(
950 13, "Drivetrain Bus", can_sensor_reader.get_signals_registry());
951
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.
milind-u32d29d32023-02-24 21:11:51 -0800991 // Setup CAN.
992 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
1025 ::aos::ShmEventLoop output_event_loop(&config.message());
1026 output_event_loop.set_name("PWMOutputWriter");
Maxwell Hendersonad312342023-01-10 12:07:47 -08001027 SuperstructureWriter superstructure_writer(&output_event_loop);
1028
Henry Speisere139f802023-02-21 14:14:48 -08001029 superstructure_writer.set_proximal_falcon(make_unique<::frc::TalonFX>(1));
1030 superstructure_writer.set_distal_falcon(make_unique<::frc::TalonFX>(0));
milind-u18934eb2023-02-20 16:28:58 -08001031
1032 superstructure_writer.set_roll_joint_victor(
Henry Speisere139f802023-02-21 14:14:48 -08001033 make_unique<::frc::VictorSP>(3));
1034 superstructure_writer.set_wrist_victor(make_unique<::frc::VictorSP>(2));
1035
Maxwell Hendersonad312342023-01-10 12:07:47 -08001036 superstructure_writer.set_superstructure_reading(superstructure_reading);
1037
1038 AddLoop(&output_event_loop);
1039
1040 RunLoops();
1041 }
1042};
1043
1044} // namespace wpilib
1045} // namespace y2023
1046
1047AOS_ROBOT_CLASS(::y2023::wpilib::WPILibRobot);