blob: 8c1cfd38ca4dacc267e0e578e9c168b8e12f8b41 [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -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
16#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
26#include "ctre/phoenix/cci/Diagnostics_CCI.h"
27#include "ctre/phoenix6/TalonFX.hpp"
28
29#include "aos/commonmath.h"
30#include "aos/containers/sized_array.h"
31#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"
40#include "frc971/autonomous/auto_mode_generated.h"
41#include "frc971/can_configuration_generated.h"
42#include "frc971/control_loops/drivetrain/drivetrain_can_position_generated.h"
43#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
44#include "frc971/input/robot_state_generated.h"
45#include "frc971/queues/gyro_generated.h"
46#include "frc971/wpilib/ADIS16448.h"
47#include "frc971/wpilib/buffered_pcm.h"
48#include "frc971/wpilib/buffered_solenoid.h"
49#include "frc971/wpilib/dma.h"
50#include "frc971/wpilib/drivetrain_writer.h"
51#include "frc971/wpilib/encoder_and_potentiometer.h"
52#include "frc971/wpilib/joystick_sender.h"
53#include "frc971/wpilib/logging_generated.h"
54#include "frc971/wpilib/loop_output_handler.h"
55#include "frc971/wpilib/pdp_fetcher.h"
56#include "frc971/wpilib/sensor_reader.h"
57#include "frc971/wpilib/wpilib_robot_base.h"
58#include "y2024/constants.h"
59#include "y2024/control_loops/superstructure/superstructure_output_generated.h"
60#include "y2024/control_loops/superstructure/superstructure_position_generated.h"
61
62DEFINE_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
66using ::aos::monotonic_clock;
67using ::frc971::CANConfiguration;
68using ::y2024::constants::Values;
69namespace superstructure = ::y2024::control_loops::superstructure;
70namespace drivetrain = ::y2024::control_loops::drivetrain;
71namespace chrono = ::std::chrono;
72using std::make_unique;
73
74namespace y2024 {
75namespace wpilib {
76namespace {
77
78constexpr double kMaxBringupPower = 12.0;
79
80// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
81// DMA stuff and then removing the * 2.0 in *_translate.
82// The low bit is direction.
83
84double drivetrain_velocity_translate(double in) {
85 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
86 (2.0 * M_PI)) *
87 Values::kDrivetrainEncoderRatio() *
88 control_loops::drivetrain::kWheelRadius;
89}
90
91constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
92 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
93});
94static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
95 "fast encoders are too fast");
96
97} // namespace
98
99static constexpr int kCANFalconCount = 6;
100static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz;
101
102// Class to send position messages with sensor readings to our loops.
103class SensorReader : public ::frc971::wpilib::SensorReader {
104 public:
105 SensorReader(::aos::ShmEventLoop *event_loop,
106 std::shared_ptr<const Values> values)
107 : ::frc971::wpilib::SensorReader(event_loop),
108 values_(std::move(values)),
109 auto_mode_sender_(
110 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
111 "/autonomous")),
112 superstructure_position_sender_(
113 event_loop->MakeSender<superstructure::Position>(
114 "/superstructure")),
115 drivetrain_position_sender_(
116 event_loop
117 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
118 "/drivetrain")),
119 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
120 "/drivetrain")){};
121 void Start() override { AddToDMA(&imu_yaw_rate_reader_); }
122
123 // Auto mode switches.
124 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
125 autonomous_modes_.at(i) = ::std::move(sensor);
126 }
127
128 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
129 imu_yaw_rate_input_ = ::std::move(sensor);
130 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
131 }
132
133 void RunIteration() override {
134 {
135 auto builder = superstructure_position_sender_.MakeBuilder();
136
137 superstructure::Position::Builder position_builder =
138 builder.MakeBuilder<superstructure::Position>();
139 builder.CheckOk(builder.Send(position_builder.Finish()));
140 }
141
142 {
143 auto builder = drivetrain_position_sender_.MakeBuilder();
144 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
145 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
146 drivetrain_builder.add_left_encoder(
147 constants::Values::DrivetrainEncoderToMeters(
148 drivetrain_left_encoder_->GetRaw()));
149 drivetrain_builder.add_left_speed(
150 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
151
152 drivetrain_builder.add_right_encoder(
153 -constants::Values::DrivetrainEncoderToMeters(
154 drivetrain_right_encoder_->GetRaw()));
155 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
156 drivetrain_right_encoder_->GetPeriod()));
157
158 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
159 }
160
161 {
162 auto builder = gyro_sender_.MakeBuilder();
163 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
164 builder.MakeBuilder<::frc971::sensors::GyroReading>();
165 // +/- 2000 deg / sec
166 constexpr double kMaxVelocity = 4000; // degrees / second
167 constexpr double kVelocityRadiansPerSecond =
168 kMaxVelocity / 360 * (2.0 * M_PI);
169
170 // Only part of the full range is used to prevent being 100% on or off.
171 constexpr double kScaledRangeLow = 0.1;
172 constexpr double kScaledRangeHigh = 0.9;
173
174 constexpr double kPWMFrequencyHz = 200;
175 double velocity_duty_cycle =
176 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
177
178 constexpr double kDutyCycleScale =
179 1 / (kScaledRangeHigh - kScaledRangeLow);
180 // scale from 0.1 - 0.9 to 0 - 1
181 double rescaled_velocity_duty_cycle =
182 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
183
184 if (!std::isnan(rescaled_velocity_duty_cycle)) {
185 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
186 kVelocityRadiansPerSecond);
187 }
188 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
189 }
190
191 {
192 auto builder = auto_mode_sender_.MakeBuilder();
193
194 uint32_t mode = 0;
195 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
196 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
197 mode |= 1 << i;
198 }
199 }
200
201 auto auto_mode_builder =
202 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
203
204 auto_mode_builder.add_mode(mode);
205
206 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
207 }
208 }
209
210 private:
211 std::shared_ptr<const Values> values_;
212
213 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
214 aos::Sender<superstructure::Position> superstructure_position_sender_;
215 aos::Sender<frc971::control_loops::drivetrain::Position>
216 drivetrain_position_sender_;
217 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
218
219 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
220
221 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_;
222
223 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
224};
225
226class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
227 public:
228 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
229 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
230 frc::Encoder::k4X);
231 }
232
233 void Run() override {
234 std::shared_ptr<const Values> values =
235 std::make_shared<const Values>(constants::MakeValues());
236
237 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
238 aos::configuration::ReadConfig("aos_config.json");
239
240 // Thread 1.
241 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
242 ::frc971::wpilib::JoystickSender joystick_sender(
243 &joystick_sender_event_loop);
244 AddLoop(&joystick_sender_event_loop);
245
246 // Thread 2.
247 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
248 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
249 AddLoop(&pdp_fetcher_event_loop);
250
251 // Thread 3.
252 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
253 SensorReader sensor_reader(&sensor_reader_event_loop, values);
254 sensor_reader.set_pwm_trigger(true);
255 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
256 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
257 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
258
259 AddLoop(&sensor_reader_event_loop);
260
261 // Thread 4.
262 // Set up CAN.
263 if (!FLAGS_ctre_diag_server) {
264 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
265 c_Phoenix_Diagnostics_Dispose();
266 }
267
268 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
269 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
270 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
271 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
272
273 ::aos::ShmEventLoop can_output_event_loop(&config.message());
274 can_output_event_loop.set_name("CANOutputWriter");
275
276 // Thread 5
277 // Set up superstructure output.
278 ::aos::ShmEventLoop output_event_loop(&config.message());
279 output_event_loop.set_name("PWMOutputWriter");
280
281 AddLoop(&output_event_loop);
282
283 // Thread 6
284
285 RunLoops();
286 }
287};
288
289} // namespace wpilib
290} // namespace y2024
291
292AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);