blob: 84382811536a9e68f875b8729562d4d7ce5a7331 [file] [log] [blame]
Maxwell Hendersonad312342023-01-10 12:07:47 -08001#include <unistd.h>
2
3#include <array>
4#include <chrono>
5#include <cinttypes>
6#include <cmath>
7#include <cstdio>
8#include <cstring>
9#include <functional>
10#include <memory>
11#include <mutex>
12#include <thread>
13
14#include "ctre/phoenix/CANifier.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070015
Maxwell Hendersonad312342023-01-10 12:07:47 -080016#include "frc971/wpilib/ahal/AnalogInput.h"
17#include "frc971/wpilib/ahal/Counter.h"
18#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
19#include "frc971/wpilib/ahal/DriverStation.h"
20#include "frc971/wpilib/ahal/Encoder.h"
21#include "frc971/wpilib/ahal/Servo.h"
22#include "frc971/wpilib/ahal/TalonFX.h"
23#include "frc971/wpilib/ahal/VictorSP.h"
24#undef ERROR
25
Philipp Schrader790cb542023-07-05 21:06:52 -070026#include "ctre/phoenix/cci/Diagnostics_CCI.h"
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070027#include "ctre/phoenix6/TalonFX.hpp"
Philipp Schrader790cb542023-07-05 21:06:52 -070028
Maxwell Hendersonad312342023-01-10 12:07:47 -080029#include "aos/commonmath.h"
Ravago Jones2060ee62023-02-03 18:12:24 -080030#include "aos/containers/sized_array.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080031#include "aos/events/event_loop.h"
32#include "aos/events/shm_event_loop.h"
33#include "aos/init.h"
34#include "aos/logging/logging.h"
35#include "aos/realtime.h"
36#include "aos/time/time.h"
37#include "aos/util/log_interval.h"
38#include "aos/util/phased_loop.h"
39#include "aos/util/wrapping_counter.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080040#include "frc971/autonomous/auto_mode_generated.h"
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070041#include "frc971/can_configuration_generated.h"
Maxwell Hendersoncef6f042023-05-26 14:38:09 -070042#include "frc971/control_loops/drivetrain/drivetrain_can_position_generated.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080043#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
44#include "frc971/input/robot_state_generated.h"
45#include "frc971/queues/gyro_generated.h"
46#include "frc971/wpilib/ADIS16448.h"
47#include "frc971/wpilib/buffered_pcm.h"
48#include "frc971/wpilib/buffered_solenoid.h"
49#include "frc971/wpilib/dma.h"
50#include "frc971/wpilib/drivetrain_writer.h"
51#include "frc971/wpilib/encoder_and_potentiometer.h"
52#include "frc971/wpilib/joystick_sender.h"
53#include "frc971/wpilib/logging_generated.h"
54#include "frc971/wpilib/loop_output_handler.h"
55#include "frc971/wpilib/pdp_fetcher.h"
56#include "frc971/wpilib/sensor_reader.h"
57#include "frc971/wpilib/wpilib_robot_base.h"
58#include "y2023/constants.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"
Maxwell Hendersoncb78f352024-01-15 00:27:16 -080061#include "y2023/control_loops/superstructure/superstructure_position_static.h"
Maxwell Hendersonad312342023-01-10 12:07:47 -080062
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;
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070068using ::frc971::CANConfiguration;
Maxwell Hendersonad312342023-01-10 12:07:47 -080069using ::y2023::constants::Values;
70namespace superstructure = ::y2023::control_loops::superstructure;
Ravago Jones2060ee62023-02-03 18:12:24 -080071namespace drivetrain = ::y2023::control_loops::drivetrain;
Maxwell Hendersonad312342023-01-10 12:07:47 -080072namespace chrono = ::std::chrono;
73using std::make_unique;
74
Stephan Pleinesf63bde82024-01-13 15:59:33 -080075namespace y2023::wpilib {
Maxwell Hendersonad312342023-01-10 12:07:47 -080076namespace {
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,
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700122 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals)
milind-u738832d2023-02-24 19:55:54 -0800123 : talon_(device_id, canbus),
124 device_id_(device_id),
125 device_temp_(talon_.GetDeviceTemp()),
126 supply_voltage_(talon_.GetSupplyVoltage()),
127 supply_current_(talon_.GetSupplyCurrent()),
128 torque_current_(talon_.GetTorqueCurrent()),
Ravago Jones088ca772023-03-25 22:14:24 -0700129 position_(talon_.GetPosition()),
130 duty_cycle_(talon_.GetDutyCycle()) {
milind-u738832d2023-02-24 19:55:54 -0800131 // device temp is not timesynced so don't add it to the list of signals
132 device_temp_.SetUpdateFrequency(kCANUpdateFreqHz);
133
milind-u738832d2023-02-24 19:55:54 -0800134 CHECK_NOTNULL(signals);
milind-u738832d2023-02-24 19:55:54 -0800135
136 supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz);
137 signals->push_back(&supply_voltage_);
138
139 supply_current_.SetUpdateFrequency(kCANUpdateFreqHz);
140 signals->push_back(&supply_current_);
141
142 torque_current_.SetUpdateFrequency(kCANUpdateFreqHz);
143 signals->push_back(&torque_current_);
144
145 position_.SetUpdateFrequency(kCANUpdateFreqHz);
146 signals->push_back(&position_);
Ravago Jones088ca772023-03-25 22:14:24 -0700147
148 duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz);
149 signals->push_back(&duty_cycle_);
milind-u738832d2023-02-24 19:55:54 -0800150 }
151
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800152 void PrintConfigs() {
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700153 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800154 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
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700163 void WriteConfigs(ctre::phoenix6::signals::InvertedValue invert) {
milind-u738832d2023-02-24 19:55:54 -0800164 inverted_ = invert;
165
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700166 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
milind-u738832d2023-02-24 19:55:54 -0800167 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
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700174 ctre::phoenix6::configs::MotorOutputConfigs output_configs;
milind-u738832d2023-02-24 19:55:54 -0800175 output_configs.NeutralMode =
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700176 ctre::phoenix6::signals::NeutralModeValue::Brake;
milind-u738832d2023-02-24 19:55:54 -0800177 output_configs.DutyCycleNeutralDeadband = 0;
178
179 output_configs.Inverted = inverted_;
180
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700181 ctre::phoenix6::configs::TalonFXConfiguration configuration;
milind-u738832d2023-02-24 19:55:54 -0800182 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() {
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700196 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
milind-u738832d2023-02-24 19:55:54 -0800197 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
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700204 ctre::phoenix6::configs::MotorOutputConfigs output_configs;
milind-u738832d2023-02-24 19:55:54 -0800205 output_configs.NeutralMode =
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700206 ctre::phoenix6::signals::NeutralModeValue::Brake;
milind-u738832d2023-02-24 19:55:54 -0800207 output_configs.DutyCycleNeutralDeadband = 0;
208
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700209 ctre::phoenix6::configs::TalonFXConfiguration configuration;
milind-u738832d2023-02-24 19:55:54 -0800210 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
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700223 ctre::phoenix6::hardware::TalonFX *talon() { return &talon_; }
milind-u738832d2023-02-24 19:55:54 -0800224
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800225 flatbuffers::Offset<frc971::control_loops::CANTalonFX> WritePosition(
Maxwell Hendersonca1d18f2023-07-26 21:06:14 -0700226 flatbuffers::FlatBufferBuilder *fbb) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800227 frc971::control_loops::CANTalonFX::Builder builder(*fbb);
milind-u738832d2023-02-24 19:55:54 -0800228 builder.add_id(device_id_);
229 builder.add_device_temp(device_temp());
230 builder.add_supply_voltage(supply_voltage());
231 builder.add_supply_current(supply_current());
232 builder.add_torque_current(torque_current());
Ravago Jones088ca772023-03-25 22:14:24 -0700233 builder.add_duty_cycle(duty_cycle());
milind-u738832d2023-02-24 19:55:54 -0800234
235 double invert =
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700236 (inverted_ == ctre::phoenix6::signals::InvertedValue::Clockwise_Positive
milind-u738832d2023-02-24 19:55:54 -0800237 ? 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(); }
Ravago Jones088ca772023-03-25 22:14:24 -0700251 float duty_cycle() const { return duty_cycle_.GetValue().value(); }
milind-u738832d2023-02-24 19:55:54 -0800252 float position() const { return position_.GetValue().value(); }
253
254 // returns the monotonic timestamp of the latest timesynced reading in the
255 // timebase of the the syncronized CAN bus clock.
256 int64_t GetTimestamp() {
257 std::chrono::nanoseconds latest_timestamp =
258 torque_current_.GetTimestamp().GetTime();
259
260 return latest_timestamp.count();
261 }
262
263 void RefreshNontimesyncedSignals() { device_temp_.Refresh(); };
264
265 private:
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700266 ctre::phoenix6::hardware::TalonFX talon_;
milind-u738832d2023-02-24 19:55:54 -0800267 int device_id_;
268
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700269 ctre::phoenix6::signals::InvertedValue inverted_;
milind-u738832d2023-02-24 19:55:54 -0800270
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700271 ctre::phoenix6::StatusSignal<units::temperature::celsius_t> device_temp_;
272 ctre::phoenix6::StatusSignal<units::voltage::volt_t> supply_voltage_;
273 ctre::phoenix6::StatusSignal<units::current::ampere_t> supply_current_,
milind-u738832d2023-02-24 19:55:54 -0800274 torque_current_;
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700275 ctre::phoenix6::StatusSignal<units::angle::turn_t> position_;
276 ctre::phoenix6::StatusSignal<units::dimensionless::scalar_t> duty_cycle_;
milind-u738832d2023-02-24 19:55:54 -0800277};
278
279class CANSensorReader {
280 public:
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700281 CANSensorReader(
282 aos::EventLoop *event_loop,
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700283 std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry)
milind-u738832d2023-02-24 19:55:54 -0800284 : event_loop_(event_loop),
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700285 signals_(signals_registry.begin(), signals_registry.end()),
milind-u738832d2023-02-24 19:55:54 -0800286 can_position_sender_(
Maxwell Hendersoncef6f042023-05-26 14:38:09 -0700287 event_loop
288 ->MakeSender<frc971::control_loops::drivetrain::CANPosition>(
289 "/drivetrain")),
milind-u738832d2023-02-24 19:55:54 -0800290 roller_falcon_data_(std::nullopt) {
291 event_loop->SetRuntimeRealtimePriority(40);
292 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1}));
293 timer_handler_ = event_loop->AddTimer([this]() { Loop(); });
294 timer_handler_->set_name("CANSensorReader Loop");
295
296 event_loop->OnRun([this]() {
Philipp Schradera6712522023-07-05 20:25:11 -0700297 timer_handler_->Schedule(event_loop_->monotonic_now(),
298 1 / kCANUpdateFreqHz);
milind-u738832d2023-02-24 19:55:54 -0800299 });
300 }
301
milind-u738832d2023-02-24 19:55:54 -0800302 void set_falcons(std::shared_ptr<Falcon> right_front,
303 std::shared_ptr<Falcon> right_back,
304 std::shared_ptr<Falcon> right_under,
305 std::shared_ptr<Falcon> left_front,
306 std::shared_ptr<Falcon> left_back,
307 std::shared_ptr<Falcon> left_under,
308 std::shared_ptr<Falcon> roller_falcon) {
309 right_front_ = std::move(right_front);
310 right_back_ = std::move(right_back);
311 right_under_ = std::move(right_under);
312 left_front_ = std::move(left_front);
313 left_back_ = std::move(left_back);
314 left_under_ = std::move(left_under);
315 roller_falcon_ = std::move(roller_falcon);
316 }
317
318 std::optional<superstructure::CANFalconT> roller_falcon_data() {
319 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
320 return roller_falcon_data_;
321 }
322
323 private:
324 void Loop() {
milind-u738832d2023-02-24 19:55:54 -0800325 ctre::phoenix::StatusCode status =
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700326 ctre::phoenix6::BaseStatusSignal::WaitForAll(2000_ms, signals_);
milind-u738832d2023-02-24 19:55:54 -0800327
328 if (!status.IsOK()) {
329 AOS_LOG(ERROR, "Failed to read signals from falcons: %s: %s",
330 status.GetName(), status.GetDescription());
331 }
332
333 auto builder = can_position_sender_.MakeBuilder();
334
335 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
336 left_back_, left_under_, roller_falcon_}) {
337 falcon->RefreshNontimesyncedSignals();
338 }
339
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800340 aos::SizedArray<flatbuffers::Offset<frc971::control_loops::CANTalonFX>,
Maxwell Hendersonca1d18f2023-07-26 21:06:14 -0700341 kCANFalconCount>
milind-u738832d2023-02-24 19:55:54 -0800342 falcons;
343
344 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
345 left_back_, left_under_}) {
346 falcons.push_back(falcon->WritePosition(builder.fbb()));
347 }
348
349 auto falcons_list =
Maxwell Hendersoncef6f042023-05-26 14:38:09 -0700350 builder.fbb()
Maxwell Hendersonca1d18f2023-07-26 21:06:14 -0700351 ->CreateVector<
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800352 flatbuffers::Offset<frc971::control_loops::CANTalonFX>>(
353 falcons);
milind-u738832d2023-02-24 19:55:54 -0800354
Maxwell Hendersoncef6f042023-05-26 14:38:09 -0700355 frc971::control_loops::drivetrain::CANPosition::Builder
356 can_position_builder =
357 builder
358 .MakeBuilder<frc971::control_loops::drivetrain::CANPosition>();
milind-u738832d2023-02-24 19:55:54 -0800359
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800360 can_position_builder.add_talonfxs(falcons_list);
milind-u738832d2023-02-24 19:55:54 -0800361 can_position_builder.add_timestamp(right_front_->GetTimestamp());
362 can_position_builder.add_status(static_cast<int>(status));
363
364 builder.CheckOk(builder.Send(can_position_builder.Finish()));
365
366 {
367 std::unique_lock<aos::stl_mutex> lock(roller_mutex_);
368 superstructure::CANFalconT roller_falcon_data;
369 roller_falcon_data.id = roller_falcon_->device_id();
370 roller_falcon_data.supply_current = roller_falcon_->supply_current();
Austin Schuh23a90022023-02-24 22:13:39 -0800371 roller_falcon_data.torque_current = -roller_falcon_->torque_current();
milind-u738832d2023-02-24 19:55:54 -0800372 roller_falcon_data.supply_voltage = roller_falcon_->supply_voltage();
373 roller_falcon_data.device_temp = roller_falcon_->device_temp();
Austin Schuh23a90022023-02-24 22:13:39 -0800374 roller_falcon_data.position = -roller_falcon_->position();
Ravago Jones088ca772023-03-25 22:14:24 -0700375 roller_falcon_data.duty_cycle = roller_falcon_->duty_cycle();
milind-u738832d2023-02-24 19:55:54 -0800376 roller_falcon_data_ =
377 std::make_optional<superstructure::CANFalconT>(roller_falcon_data);
378 }
379 }
380
381 aos::EventLoop *event_loop_;
382
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700383 const std::vector<ctre::phoenix6::BaseStatusSignal *> signals_;
Maxwell Hendersoncef6f042023-05-26 14:38:09 -0700384 aos::Sender<frc971::control_loops::drivetrain::CANPosition>
385 can_position_sender_;
milind-u738832d2023-02-24 19:55:54 -0800386
387 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
388 left_back_, left_under_, roller_falcon_;
389
390 std::optional<superstructure::CANFalconT> roller_falcon_data_;
391
392 aos::stl_mutex roller_mutex_;
393
394 // Pointer to the timer handler used to modify the wakeup.
395 ::aos::TimerHandler *timer_handler_;
396};
397
Maxwell Hendersonad312342023-01-10 12:07:47 -0800398// Class to send position messages with sensor readings to our loops.
399class SensorReader : public ::frc971::wpilib::SensorReader {
400 public:
401 SensorReader(::aos::ShmEventLoop *event_loop,
milind-u738832d2023-02-24 19:55:54 -0800402 std::shared_ptr<const Values> values,
403 CANSensorReader *can_sensor_reader)
Maxwell Hendersonad312342023-01-10 12:07:47 -0800404 : ::frc971::wpilib::SensorReader(event_loop),
405 values_(std::move(values)),
406 auto_mode_sender_(
407 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
408 "/autonomous")),
409 superstructure_position_sender_(
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800410 event_loop->MakeSender<superstructure::PositionStatic>(
Maxwell Hendersonad312342023-01-10 12:07:47 -0800411 "/superstructure")),
412 drivetrain_position_sender_(
413 event_loop
414 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
415 "/drivetrain")),
416 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
milind-u738832d2023-02-24 19:55:54 -0800417 "/drivetrain")),
418 can_sensor_reader_(can_sensor_reader) {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800419 // Set to filter out anything shorter than 1/4 of the minimum pulse width
420 // we should ever see.
421 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
Austin Schuh595ffc72023-02-24 16:24:37 -0800422 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({0}));
Maxwell Hendersonad312342023-01-10 12:07:47 -0800423 }
424
425 void Start() override {
Maxwell Hendersonad312342023-01-10 12:07:47 -0800426 AddToDMA(&imu_yaw_rate_reader_);
milind-u3a7f9212023-02-24 20:46:59 -0800427 AddToDMA(&cone_position_sensor_);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800428 }
429
430 // Auto mode switches.
431 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
432 autonomous_modes_.at(i) = ::std::move(sensor);
433 }
434
Ravago Jones2060ee62023-02-03 18:12:24 -0800435 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
436 imu_yaw_rate_input_ = ::std::move(sensor);
437 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
438 }
439
Maxwell Hendersonad312342023-01-10 12:07:47 -0800440 void RunIteration() override {
441 superstructure_reading_->Set(true);
milind-u18934eb2023-02-20 16:28:58 -0800442 {
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800443 aos::Sender<superstructure::PositionStatic>::StaticBuilder builder =
444 superstructure_position_sender_.MakeStaticBuilder();
445
446 superstructure::ArmPositionStatic *arm = builder->add_arm();
447
448 CopyPosition(proximal_encoder_, arm->add_proximal(),
milind-u18934eb2023-02-20 16:28:58 -0800449 Values::kProximalEncoderCountsPerRevolution(),
450 Values::kProximalEncoderRatio(), proximal_pot_translate,
Austin Schuh7dcc49b2023-02-21 17:35:10 -0800451 true, values_->arm_proximal.potentiometer_offset);
Maxwell Hendersonce816122023-03-27 20:12:38 -0700452 CopyPosition(
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800453 distal_encoder_, arm->add_distal(),
454 Values::kDistalEncoderCountsPerRevolution(),
Maxwell Hendersonce816122023-03-27 20:12:38 -0700455 values_->arm_distal.zeroing.one_revolution_distance / (M_PI * 2.0),
456 distal_pot_translate, true, values_->arm_distal.potentiometer_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800457 CopyPosition(roll_joint_encoder_, arm->add_roll_joint(),
milind-u18934eb2023-02-20 16:28:58 -0800458 Values::kRollJointEncoderCountsPerRevolution(),
459 Values::kRollJointEncoderRatio(), roll_joint_pot_translate,
Austin Schuh29d025c2023-03-03 21:41:04 -0800460 false, values_->roll_joint.potentiometer_offset);
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800461 CopyPosition(wrist_encoder_, builder->add_wrist(),
milind-u18934eb2023-02-20 16:28:58 -0800462 Values::kWristEncoderCountsPerRevolution(),
Austin Schuhe5248cd2023-03-05 12:46:16 -0800463 values_->wrist.subsystem_params.zeroing_constants
464 .one_revolution_distance /
465 (M_PI * 2.0),
466 values_->wrist_flipped);
milind-u18934eb2023-02-20 16:28:58 -0800467
milind-u738832d2023-02-24 19:55:54 -0800468 flatbuffers::Offset<superstructure::CANFalcon> roller_falcon_offset;
469 auto optional_roller_falcon = can_sensor_reader_->roller_falcon_data();
470 if (optional_roller_falcon.has_value()) {
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800471 superstructure::CANFalconT roller_falcon_buffer =
472 optional_roller_falcon.value();
473
474 superstructure::CANFalconStatic *roller_falcon =
475 builder->add_roller_falcon();
476 roller_falcon->set_id(roller_falcon_buffer.id);
477 roller_falcon->set_supply_current(roller_falcon_buffer.supply_current);
478 roller_falcon->set_torque_current(roller_falcon_buffer.torque_current);
479 roller_falcon->set_supply_voltage(roller_falcon_buffer.supply_voltage);
480 roller_falcon->set_device_temp(roller_falcon_buffer.device_temp);
481 roller_falcon->set_position(roller_falcon_buffer.position);
482 roller_falcon->set_duty_cycle(roller_falcon_buffer.duty_cycle);
milind-u738832d2023-02-24 19:55:54 -0800483 }
484
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800485 builder->set_end_effector_cube_beam_break(
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800486 end_effector_cube_beam_break_->Get());
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800487 builder->set_cone_position(cone_position_sensor_.last_width() /
488 cone_position_sensor_.last_period());
489
490 builder.CheckOk(builder.Send());
milind-u18934eb2023-02-20 16:28:58 -0800491 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800492
493 {
494 auto builder = drivetrain_position_sender_.MakeBuilder();
495 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
496 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
497 drivetrain_builder.add_left_encoder(
498 constants::Values::DrivetrainEncoderToMeters(
499 drivetrain_left_encoder_->GetRaw()));
500 drivetrain_builder.add_left_speed(
501 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
502
503 drivetrain_builder.add_right_encoder(
504 -constants::Values::DrivetrainEncoderToMeters(
505 drivetrain_right_encoder_->GetRaw()));
506 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
507 drivetrain_right_encoder_->GetPeriod()));
508
509 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
510 }
511
512 {
513 auto builder = gyro_sender_.MakeBuilder();
514 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
515 builder.MakeBuilder<::frc971::sensors::GyroReading>();
516 // +/- 2000 deg / sec
517 constexpr double kMaxVelocity = 4000; // degrees / second
518 constexpr double kVelocityRadiansPerSecond =
519 kMaxVelocity / 360 * (2.0 * M_PI);
520
521 // Only part of the full range is used to prevent being 100% on or off.
522 constexpr double kScaledRangeLow = 0.1;
523 constexpr double kScaledRangeHigh = 0.9;
524
525 constexpr double kPWMFrequencyHz = 200;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800526 double velocity_duty_cycle =
527 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
528
529 constexpr double kDutyCycleScale =
530 1 / (kScaledRangeHigh - kScaledRangeLow);
531 // scale from 0.1 - 0.9 to 0 - 1
Maxwell Hendersonad312342023-01-10 12:07:47 -0800532 double rescaled_velocity_duty_cycle =
533 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
534
Maxwell Hendersonad312342023-01-10 12:07:47 -0800535 if (!std::isnan(rescaled_velocity_duty_cycle)) {
536 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
537 kVelocityRadiansPerSecond);
538 }
539 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
540 }
541
542 {
543 auto builder = auto_mode_sender_.MakeBuilder();
544
545 uint32_t mode = 0;
546 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
547 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
548 mode |= 1 << i;
549 }
550 }
551
552 auto auto_mode_builder =
553 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
554
555 auto_mode_builder.add_mode(mode);
556
557 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
558 }
559 }
560
561 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
562
563 void set_superstructure_reading(
564 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
565 superstructure_reading_ = superstructure_reading;
566 }
567
milind-u18934eb2023-02-20 16:28:58 -0800568 void set_proximal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
569 fast_encoder_filter_.Add(encoder.get());
570 proximal_encoder_.set_encoder(::std::move(encoder));
571 }
572
573 void set_proximal_absolute_pwm(
574 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
575 proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
576 }
577
578 void set_proximal_potentiometer(
579 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
580 proximal_encoder_.set_potentiometer(::std::move(potentiometer));
581 }
582
583 void set_distal_encoder(::std::unique_ptr<frc::Encoder> encoder) {
584 fast_encoder_filter_.Add(encoder.get());
585 distal_encoder_.set_encoder(::std::move(encoder));
586 }
587
588 void set_distal_absolute_pwm(
589 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
590 distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
591 }
592
593 void set_distal_potentiometer(
594 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
595 distal_encoder_.set_potentiometer(::std::move(potentiometer));
596 }
597
598 void set_roll_joint_encoder(::std::unique_ptr<frc::Encoder> encoder) {
599 fast_encoder_filter_.Add(encoder.get());
600 roll_joint_encoder_.set_encoder(::std::move(encoder));
601 }
602
603 void set_roll_joint_absolute_pwm(
604 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
605 roll_joint_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
606 }
607
608 void set_roll_joint_potentiometer(
609 ::std::unique_ptr<frc::AnalogInput> potentiometer) {
610 roll_joint_encoder_.set_potentiometer(::std::move(potentiometer));
611 }
612
613 void set_wrist_encoder(::std::unique_ptr<frc::Encoder> encoder) {
614 fast_encoder_filter_.Add(encoder.get());
615 wrist_encoder_.set_encoder(::std::move(encoder));
616 }
617
618 void set_wrist_absolute_pwm(
619 ::std::unique_ptr<frc::DigitalInput> absolute_pwm) {
620 wrist_encoder_.set_absolute_pwm(::std::move(absolute_pwm));
621 }
622
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800623 void set_end_effector_cube_beam_break(
624 ::std::unique_ptr<frc::DigitalInput> sensor) {
625 end_effector_cube_beam_break_ = ::std::move(sensor);
626 }
627
milind-u3a7f9212023-02-24 20:46:59 -0800628 void set_cone_position_sensor(::std::unique_ptr<frc::DigitalInput> sensor) {
629 cone_position_input_ = ::std::move(sensor);
630 cone_position_sensor_.set_input(cone_position_input_.get());
631 }
632
Maxwell Hendersonad312342023-01-10 12:07:47 -0800633 private:
634 std::shared_ptr<const Values> values_;
635
636 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
Maxwell Hendersoncb78f352024-01-15 00:27:16 -0800637 aos::Sender<superstructure::PositionStatic> superstructure_position_sender_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800638 aos::Sender<frc971::control_loops::drivetrain::Position>
639 drivetrain_position_sender_;
640 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
641
642 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
643
Ravago Jones2544ad82023-03-04 22:24:49 -0800644 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_,
milind-u3a7f9212023-02-24 20:46:59 -0800645 end_effector_cube_beam_break_;
Ravago Jones2060ee62023-02-03 18:12:24 -0800646
Ravago Jones2544ad82023-03-04 22:24:49 -0800647 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
milind-u18934eb2023-02-20 16:28:58 -0800648
649 frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_,
650 distal_encoder_, roll_joint_encoder_;
651 frc971::wpilib::AbsoluteEncoder wrist_encoder_;
milind-u3a7f9212023-02-24 20:46:59 -0800652
653 frc971::wpilib::DMAPulseWidthReader cone_position_sensor_;
654 std::unique_ptr<frc::DigitalInput> cone_position_input_;
milind-u738832d2023-02-24 19:55:54 -0800655
656 CANSensorReader *can_sensor_reader_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800657};
658
659class SuperstructureWriter
660 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
661 public:
662 SuperstructureWriter(aos::EventLoop *event_loop)
663 : frc971::wpilib::LoopOutputHandler<superstructure::Output>(
milind-u32d29d32023-02-24 21:11:51 -0800664 event_loop, "/superstructure") {
665 event_loop->SetRuntimeRealtimePriority(
666 constants::Values::kDrivetrainWriterPriority);
667 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800668
669 std::shared_ptr<frc::DigitalOutput> superstructure_reading_;
670
671 void set_superstructure_reading(
672 std::shared_ptr<frc::DigitalOutput> superstructure_reading) {
673 superstructure_reading_ = superstructure_reading;
674 }
675
milind-u18934eb2023-02-20 16:28:58 -0800676 void set_proximal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
677 proximal_falcon_ = ::std::move(t);
678 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800679
milind-u18934eb2023-02-20 16:28:58 -0800680 void set_distal_falcon(::std::unique_ptr<::frc::TalonFX> t) {
681 distal_falcon_ = ::std::move(t);
682 }
683
684 void set_roll_joint_victor(::std::unique_ptr<::frc::VictorSP> t) {
685 roll_joint_victor_ = ::std::move(t);
686 }
687
688 void set_wrist_victor(::std::unique_ptr<::frc::VictorSP> t) {
689 wrist_victor_ = ::std::move(t);
690 }
691
milind-u18934eb2023-02-20 16:28:58 -0800692 private:
693 void Stop() override {
694 AOS_LOG(WARNING, "Superstructure output too old.\n");
695 proximal_falcon_->SetDisabled();
696 distal_falcon_->SetDisabled();
697 roll_joint_victor_->SetDisabled();
698 wrist_victor_->SetDisabled();
milind-u18934eb2023-02-20 16:28:58 -0800699 }
700
701 void Write(const superstructure::Output &output) override {
702 WritePwm(output.proximal_voltage(), proximal_falcon_.get());
703 WritePwm(output.distal_voltage(), distal_falcon_.get());
Austin Schuh3fd5f0e2023-02-22 11:10:37 -0800704 WritePwm(-output.roll_joint_voltage(), roll_joint_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800705 WritePwm(output.wrist_voltage(), wrist_victor_.get());
milind-u18934eb2023-02-20 16:28:58 -0800706 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800707
708 static void WriteCan(const double voltage,
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800709 ::ctre::phoenix6::hardware::TalonFX *talon) {
710 ctre::phoenix6::controls::DutyCycleOut motor_control(SafeSpeed(voltage));
711
712 motor_control.UpdateFreqHz = 0_Hz;
713 motor_control.EnableFOC = true;
714
715 ctre::phoenix::StatusCode status = talon->SetControl(motor_control);
716
717 if (!status.IsOK()) {
718 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
719 status.GetName(), status.GetDescription());
720 }
Maxwell Hendersonad312342023-01-10 12:07:47 -0800721 }
722
723 template <typename T>
724 static void WritePwm(const double voltage, T *motor) {
Maxwell Henderson1c0843c2023-12-22 16:20:59 -0800725 motor->SetSpeed(SafeSpeed(voltage));
726 }
727
728 static double SafeSpeed(double voltage) {
729 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800730 }
milind-u18934eb2023-02-20 16:28:58 -0800731
732 ::std::unique_ptr<::frc::TalonFX> proximal_falcon_, distal_falcon_;
733 ::std::unique_ptr<::frc::VictorSP> roll_joint_victor_, wrist_victor_;
Maxwell Hendersonad312342023-01-10 12:07:47 -0800734};
735
milind-u32d29d32023-02-24 21:11:51 -0800736class SuperstructureCANWriter
737 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
738 public:
739 SuperstructureCANWriter(::aos::EventLoop *event_loop)
740 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
741 event_loop, "/superstructure") {
742 event_loop->SetRuntimeRealtimePriority(
743 constants::Values::kSuperstructureCANWriterPriority);
744
745 event_loop->OnRun([this]() { WriteConfigs(); });
746 };
747
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800748 void HandleCANConfiguration(const CANConfiguration &configuration) {
749 roller_falcon_->PrintConfigs();
750 if (configuration.reapply()) {
751 WriteConfigs();
752 }
753 }
754
milind-u32d29d32023-02-24 21:11:51 -0800755 void set_roller_falcon(std::shared_ptr<Falcon> roller_falcon) {
756 roller_falcon_ = std::move(roller_falcon);
757 }
758
759 private:
760 void WriteConfigs() { roller_falcon_->WriteRollerConfigs(); }
761
762 void Write(const superstructure::Output &output) override {
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700763 ctre::phoenix6::controls::DutyCycleOut roller_control(
milind-u32d29d32023-02-24 21:11:51 -0800764 SafeSpeed(-output.roller_voltage()));
765 roller_control.UpdateFreqHz = 0_Hz;
766 roller_control.EnableFOC = true;
767
768 ctre::phoenix::StatusCode status =
769 roller_falcon_->talon()->SetControl(roller_control);
770
771 if (!status.IsOK()) {
772 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
773 status.GetName(), status.GetDescription());
774 }
775 }
776
777 void Stop() override {
778 AOS_LOG(WARNING, "Superstructure CAN output too old.\n");
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700779 ctre::phoenix6::controls::DutyCycleOut stop_command(0.0);
Austin Schuh1780e002023-03-03 21:39:26 -0800780 stop_command.UpdateFreqHz = 0_Hz;
781 stop_command.EnableFOC = true;
milind-u32d29d32023-02-24 21:11:51 -0800782
783 roller_falcon_->talon()->SetControl(stop_command);
784 }
785
786 double SafeSpeed(double voltage) {
787 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
788 }
789
790 std::shared_ptr<Falcon> roller_falcon_;
791};
792
Ravago Jones2060ee62023-02-03 18:12:24 -0800793class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler<
794 ::frc971::control_loops::drivetrain::Output> {
795 public:
796 DrivetrainWriter(::aos::EventLoop *event_loop)
797 : ::frc971::wpilib::LoopOutputHandler<
798 ::frc971::control_loops::drivetrain::Output>(event_loop,
799 "/drivetrain") {
800 event_loop->SetRuntimeRealtimePriority(
801 constants::Values::kDrivetrainWriterPriority);
802
Ravago Jones2060ee62023-02-03 18:12:24 -0800803 event_loop->OnRun([this]() { WriteConfigs(); });
804 }
805
806 void set_falcons(std::shared_ptr<Falcon> right_front,
807 std::shared_ptr<Falcon> right_back,
808 std::shared_ptr<Falcon> right_under,
809 std::shared_ptr<Falcon> left_front,
810 std::shared_ptr<Falcon> left_back,
811 std::shared_ptr<Falcon> left_under) {
812 right_front_ = std::move(right_front);
813 right_back_ = std::move(right_back);
814 right_under_ = std::move(right_under);
815 left_front_ = std::move(left_front);
816 left_back_ = std::move(left_back);
817 left_under_ = std::move(left_under);
818 }
819
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700820 void set_right_inverted(ctre::phoenix6::signals::InvertedValue invert) {
Ravago Jones2060ee62023-02-03 18:12:24 -0800821 right_inverted_ = invert;
822 }
823
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700824 void set_left_inverted(ctre::phoenix6::signals::InvertedValue invert) {
Ravago Jones2060ee62023-02-03 18:12:24 -0800825 left_inverted_ = invert;
826 }
827
Austin Schuhbb4c9ac2023-02-28 22:04:20 -0800828 void HandleCANConfiguration(const CANConfiguration &configuration) {
829 for (auto falcon : {right_front_, right_back_, right_under_, left_front_,
830 left_back_, left_under_}) {
831 falcon->PrintConfigs();
832 }
833 if (configuration.reapply()) {
834 WriteConfigs();
835 }
836 }
837
Ravago Jones2060ee62023-02-03 18:12:24 -0800838 private:
839 void WriteConfigs() {
840 for (auto falcon :
841 {right_front_.get(), right_back_.get(), right_under_.get()}) {
842 falcon->WriteConfigs(right_inverted_);
843 }
844
845 for (auto falcon :
846 {left_front_.get(), left_back_.get(), left_under_.get()}) {
847 falcon->WriteConfigs(left_inverted_);
848 }
849 }
850
851 void Write(
852 const ::frc971::control_loops::drivetrain::Output &output) override {
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700853 ctre::phoenix6::controls::DutyCycleOut left_control(
Ravago Jones2060ee62023-02-03 18:12:24 -0800854 SafeSpeed(output.left_voltage()));
855 left_control.UpdateFreqHz = 0_Hz;
856 left_control.EnableFOC = true;
857
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700858 ctre::phoenix6::controls::DutyCycleOut right_control(
Ravago Jones2060ee62023-02-03 18:12:24 -0800859 SafeSpeed(output.right_voltage()));
860 right_control.UpdateFreqHz = 0_Hz;
861 right_control.EnableFOC = true;
862
863 for (auto falcon :
864 {left_front_.get(), left_back_.get(), left_under_.get()}) {
865 ctre::phoenix::StatusCode status =
866 falcon->talon()->SetControl(left_control);
867
868 if (!status.IsOK()) {
869 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
870 status.GetName(), status.GetDescription());
871 }
872 }
873
874 for (auto falcon :
875 {right_front_.get(), right_back_.get(), right_under_.get()}) {
876 ctre::phoenix::StatusCode status =
877 falcon->talon()->SetControl(right_control);
878
879 if (!status.IsOK()) {
880 AOS_LOG(ERROR, "Failed to write control to falcon: %s: %s",
881 status.GetName(), status.GetDescription());
882 }
883 }
884 }
885
886 void Stop() override {
887 AOS_LOG(WARNING, "drivetrain output too old\n");
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700888 ctre::phoenix6::controls::DutyCycleOut stop_command(0.0);
Austin Schuh1780e002023-03-03 21:39:26 -0800889 stop_command.UpdateFreqHz = 0_Hz;
890 stop_command.EnableFOC = true;
891
Ravago Jones2060ee62023-02-03 18:12:24 -0800892 for (auto falcon :
893 {right_front_.get(), right_back_.get(), right_under_.get(),
894 left_front_.get(), left_back_.get(), left_under_.get()}) {
895 falcon->talon()->SetControl(stop_command);
896 }
897 }
898
899 double SafeSpeed(double voltage) {
900 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
901 }
902
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700903 ctre::phoenix6::signals::InvertedValue left_inverted_, right_inverted_;
Ravago Jones2060ee62023-02-03 18:12:24 -0800904 std::shared_ptr<Falcon> right_front_, right_back_, right_under_, left_front_,
905 left_back_, left_under_;
906};
Maxwell Hendersonad312342023-01-10 12:07:47 -0800907
908class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
909 public:
910 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
911 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
912 frc::Encoder::k4X);
913 }
914
915 void Run() override {
916 std::shared_ptr<const Values> values =
917 std::make_shared<const Values>(constants::MakeValues());
918
919 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
920 aos::configuration::ReadConfig("aos_config.json");
921
922 // Thread 1.
Ravago Jones2060ee62023-02-03 18:12:24 -0800923 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
924 ::frc971::wpilib::JoystickSender joystick_sender(
925 &joystick_sender_event_loop);
926 AddLoop(&joystick_sender_event_loop);
927
Maxwell Hendersonad312342023-01-10 12:07:47 -0800928 // Thread 2.
929 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
930 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
931 AddLoop(&pdp_fetcher_event_loop);
932
933 std::shared_ptr<frc::DigitalOutput> superstructure_reading =
934 make_unique<frc::DigitalOutput>(25);
935
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -0700936 std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry;
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700937 std::shared_ptr<Falcon> right_front =
938 std::make_shared<Falcon>(1, "Drivetrain Bus", &signals_registry);
939 std::shared_ptr<Falcon> right_back =
940 std::make_shared<Falcon>(2, "Drivetrain Bus", &signals_registry);
941 std::shared_ptr<Falcon> right_under =
942 std::make_shared<Falcon>(3, "Drivetrain Bus", &signals_registry);
943 std::shared_ptr<Falcon> left_front =
944 std::make_shared<Falcon>(4, "Drivetrain Bus", &signals_registry);
945 std::shared_ptr<Falcon> left_back =
946 std::make_shared<Falcon>(5, "Drivetrain Bus", &signals_registry);
947 std::shared_ptr<Falcon> left_under =
948 std::make_shared<Falcon>(6, "Drivetrain Bus", &signals_registry);
949 std::shared_ptr<Falcon> roller =
950 std::make_shared<Falcon>(13, "Drivetrain Bus", &signals_registry);
951
Maxwell Hendersonad312342023-01-10 12:07:47 -0800952 // Thread 3.
milind-u738832d2023-02-24 19:55:54 -0800953 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
954 can_sensor_reader_event_loop.set_name("CANSensorReader");
Ravago Jones2bfcecd2023-03-14 13:13:26 -0700955 CANSensorReader can_sensor_reader(&can_sensor_reader_event_loop,
956 std::move(signals_registry));
milind-u738832d2023-02-24 19:55:54 -0800957
958 can_sensor_reader.set_falcons(right_front, right_back, right_under,
959 left_front, left_back, left_under, roller);
960
961 AddLoop(&can_sensor_reader_event_loop);
962
963 // Thread 4.
Maxwell Hendersonad312342023-01-10 12:07:47 -0800964 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
milind-u738832d2023-02-24 19:55:54 -0800965 SensorReader sensor_reader(&sensor_reader_event_loop, values,
966 &can_sensor_reader);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800967 sensor_reader.set_pwm_trigger(true);
968 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
969 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
970 sensor_reader.set_superstructure_reading(superstructure_reading);
Henry Speisere139f802023-02-21 14:14:48 -0800971 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
Ravago Jones2060ee62023-02-03 18:12:24 -0800972
milind-u18934eb2023-02-20 16:28:58 -0800973 sensor_reader.set_proximal_encoder(make_encoder(3));
974 sensor_reader.set_proximal_absolute_pwm(make_unique<frc::DigitalInput>(3));
975 sensor_reader.set_proximal_potentiometer(make_unique<frc::AnalogInput>(3));
976
Henry Speisere139f802023-02-21 14:14:48 -0800977 sensor_reader.set_distal_encoder(make_encoder(2));
978 sensor_reader.set_distal_absolute_pwm(make_unique<frc::DigitalInput>(2));
979 sensor_reader.set_distal_potentiometer(make_unique<frc::AnalogInput>(2));
milind-u18934eb2023-02-20 16:28:58 -0800980
Henry Speisere139f802023-02-21 14:14:48 -0800981 sensor_reader.set_roll_joint_encoder(make_encoder(5));
milind-u18934eb2023-02-20 16:28:58 -0800982 sensor_reader.set_roll_joint_absolute_pwm(
Henry Speisere139f802023-02-21 14:14:48 -0800983 make_unique<frc::DigitalInput>(5));
milind-u18934eb2023-02-20 16:28:58 -0800984 sensor_reader.set_roll_joint_potentiometer(
Henry Speisere139f802023-02-21 14:14:48 -0800985 make_unique<frc::AnalogInput>(5));
milind-u18934eb2023-02-20 16:28:58 -0800986
Henry Speisere139f802023-02-21 14:14:48 -0800987 sensor_reader.set_wrist_encoder(make_encoder(4));
988 sensor_reader.set_wrist_absolute_pwm(make_unique<frc::DigitalInput>(4));
milind-u18934eb2023-02-20 16:28:58 -0800989
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800990 sensor_reader.set_end_effector_cube_beam_break(
991 make_unique<frc::DigitalInput>(7));
milind-u3a7f9212023-02-24 20:46:59 -0800992 sensor_reader.set_cone_position_sensor(make_unique<frc::DigitalInput>(8));
Maxwell Henderson6c7e61f2023-02-22 16:43:43 -0800993
Maxwell Hendersonad312342023-01-10 12:07:47 -0800994 AddLoop(&sensor_reader_event_loop);
995
Ravago Jones2060ee62023-02-03 18:12:24 -0800996 // Thread 5.
Philipp Schradera6712522023-07-05 20:25:11 -0700997 // Set up CAN.
milind-u32d29d32023-02-24 21:11:51 -0800998 if (!FLAGS_ctre_diag_server) {
999 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
1000 c_Phoenix_Diagnostics_Dispose();
1001 }
1002
1003 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
1004 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
1005 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
1006 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
1007
1008 ::aos::ShmEventLoop can_output_event_loop(&config.message());
1009 can_output_event_loop.set_name("CANOutputWriter");
1010 DrivetrainWriter drivetrain_writer(&can_output_event_loop);
Ravago Jones2060ee62023-02-03 18:12:24 -08001011
1012 drivetrain_writer.set_falcons(right_front, right_back, right_under,
1013 left_front, left_back, left_under);
1014 drivetrain_writer.set_right_inverted(
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -07001015 ctre::phoenix6::signals::InvertedValue::Clockwise_Positive);
Ravago Jones2060ee62023-02-03 18:12:24 -08001016 drivetrain_writer.set_left_inverted(
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -07001017 ctre::phoenix6::signals::InvertedValue::CounterClockwise_Positive);
milind-u32d29d32023-02-24 21:11:51 -08001018
1019 SuperstructureCANWriter superstructure_can_writer(&can_output_event_loop);
1020 superstructure_can_writer.set_roller_falcon(roller);
1021
Austin Schuhbb4c9ac2023-02-28 22:04:20 -08001022 can_output_event_loop.MakeWatcher(
1023 "/roborio", [&drivetrain_writer, &superstructure_can_writer](
1024 const CANConfiguration &configuration) {
1025 drivetrain_writer.HandleCANConfiguration(configuration);
1026 superstructure_can_writer.HandleCANConfiguration(configuration);
1027 });
1028
milind-u32d29d32023-02-24 21:11:51 -08001029 AddLoop(&can_output_event_loop);
1030
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001031 // Thread 6
Philipp Schradera6712522023-07-05 20:25:11 -07001032 // Set up superstructure output.
milind-u32d29d32023-02-24 21:11:51 -08001033 ::aos::ShmEventLoop output_event_loop(&config.message());
1034 output_event_loop.set_name("PWMOutputWriter");
Maxwell Hendersonad312342023-01-10 12:07:47 -08001035 SuperstructureWriter superstructure_writer(&output_event_loop);
1036
Henry Speisere139f802023-02-21 14:14:48 -08001037 superstructure_writer.set_proximal_falcon(make_unique<::frc::TalonFX>(1));
1038 superstructure_writer.set_distal_falcon(make_unique<::frc::TalonFX>(0));
milind-u18934eb2023-02-20 16:28:58 -08001039
1040 superstructure_writer.set_roll_joint_victor(
Henry Speisere139f802023-02-21 14:14:48 -08001041 make_unique<::frc::VictorSP>(3));
1042 superstructure_writer.set_wrist_victor(make_unique<::frc::VictorSP>(2));
1043
Maxwell Hendersonad312342023-01-10 12:07:47 -08001044 superstructure_writer.set_superstructure_reading(superstructure_reading);
1045
1046 AddLoop(&output_event_loop);
1047
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001048 // Thread 7
Philipp Schradera6712522023-07-05 20:25:11 -07001049 // Set up led_indicator.
Maxwell Henderson2a2faa62023-03-11 15:05:46 -08001050 ::aos::ShmEventLoop led_indicator_event_loop(&config.message());
1051 led_indicator_event_loop.set_name("LedIndicator");
1052 control_loops::superstructure::LedIndicator led_indicator(
1053 &led_indicator_event_loop);
1054 AddLoop(&led_indicator_event_loop);
1055
Maxwell Hendersonad312342023-01-10 12:07:47 -08001056 RunLoops();
1057 }
1058};
1059
Stephan Pleinesf63bde82024-01-13 15:59:33 -08001060} // namespace y2023::wpilib
Maxwell Hendersonad312342023-01-10 12:07:47 -08001061
1062AOS_ROBOT_CLASS(::y2023::wpilib::WPILibRobot);