blob: 94d62ceab0485dd7f39fd48a5f79b15f31cdaa35 [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
Stephan Pleinesf63bde82024-01-13 15:59:33 -080074namespace y2024::wpilib {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080075namespace {
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
90constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
91 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
92});
93static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
94 "fast encoders are too fast");
95
96} // namespace
97
98static constexpr int kCANFalconCount = 6;
99static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz;
100
101// Class to send position messages with sensor readings to our loops.
102class SensorReader : public ::frc971::wpilib::SensorReader {
103 public:
104 SensorReader(::aos::ShmEventLoop *event_loop,
105 std::shared_ptr<const Values> values)
106 : ::frc971::wpilib::SensorReader(event_loop),
107 values_(std::move(values)),
108 auto_mode_sender_(
109 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
110 "/autonomous")),
111 superstructure_position_sender_(
112 event_loop->MakeSender<superstructure::Position>(
113 "/superstructure")),
114 drivetrain_position_sender_(
115 event_loop
116 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
117 "/drivetrain")),
118 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
119 "/drivetrain")){};
120 void Start() override { AddToDMA(&imu_yaw_rate_reader_); }
121
122 // Auto mode switches.
123 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
124 autonomous_modes_.at(i) = ::std::move(sensor);
125 }
126
127 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
128 imu_yaw_rate_input_ = ::std::move(sensor);
129 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
130 }
131
132 void RunIteration() override {
133 {
134 auto builder = superstructure_position_sender_.MakeBuilder();
135
136 superstructure::Position::Builder position_builder =
137 builder.MakeBuilder<superstructure::Position>();
138 builder.CheckOk(builder.Send(position_builder.Finish()));
139 }
140
141 {
142 auto builder = drivetrain_position_sender_.MakeBuilder();
143 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
144 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
145 drivetrain_builder.add_left_encoder(
146 constants::Values::DrivetrainEncoderToMeters(
147 drivetrain_left_encoder_->GetRaw()));
148 drivetrain_builder.add_left_speed(
149 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
150
151 drivetrain_builder.add_right_encoder(
152 -constants::Values::DrivetrainEncoderToMeters(
153 drivetrain_right_encoder_->GetRaw()));
154 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
155 drivetrain_right_encoder_->GetPeriod()));
156
157 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
158 }
159
160 {
161 auto builder = gyro_sender_.MakeBuilder();
162 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
163 builder.MakeBuilder<::frc971::sensors::GyroReading>();
164 // +/- 2000 deg / sec
165 constexpr double kMaxVelocity = 4000; // degrees / second
166 constexpr double kVelocityRadiansPerSecond =
167 kMaxVelocity / 360 * (2.0 * M_PI);
168
169 // Only part of the full range is used to prevent being 100% on or off.
170 constexpr double kScaledRangeLow = 0.1;
171 constexpr double kScaledRangeHigh = 0.9;
172
173 constexpr double kPWMFrequencyHz = 200;
174 double velocity_duty_cycle =
175 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
176
177 constexpr double kDutyCycleScale =
178 1 / (kScaledRangeHigh - kScaledRangeLow);
179 // scale from 0.1 - 0.9 to 0 - 1
180 double rescaled_velocity_duty_cycle =
181 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
182
183 if (!std::isnan(rescaled_velocity_duty_cycle)) {
184 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
185 kVelocityRadiansPerSecond);
186 }
187 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
188 }
189
190 {
191 auto builder = auto_mode_sender_.MakeBuilder();
192
193 uint32_t mode = 0;
194 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
195 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
196 mode |= 1 << i;
197 }
198 }
199
200 auto auto_mode_builder =
201 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
202
203 auto_mode_builder.add_mode(mode);
204
205 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
206 }
207 }
208
209 private:
210 std::shared_ptr<const Values> values_;
211
212 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
213 aos::Sender<superstructure::Position> superstructure_position_sender_;
214 aos::Sender<frc971::control_loops::drivetrain::Position>
215 drivetrain_position_sender_;
216 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
217
218 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
219
220 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_;
221
222 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
223};
224
225class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
226 public:
227 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
228 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
229 frc::Encoder::k4X);
230 }
231
232 void Run() override {
233 std::shared_ptr<const Values> values =
234 std::make_shared<const Values>(constants::MakeValues());
235
236 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
237 aos::configuration::ReadConfig("aos_config.json");
238
239 // Thread 1.
240 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
241 ::frc971::wpilib::JoystickSender joystick_sender(
242 &joystick_sender_event_loop);
243 AddLoop(&joystick_sender_event_loop);
244
245 // Thread 2.
246 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
247 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
248 AddLoop(&pdp_fetcher_event_loop);
249
250 // Thread 3.
251 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
252 SensorReader sensor_reader(&sensor_reader_event_loop, values);
253 sensor_reader.set_pwm_trigger(true);
254 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
255 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
256 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
257
258 AddLoop(&sensor_reader_event_loop);
259
260 // Thread 4.
261 // Set up CAN.
262 if (!FLAGS_ctre_diag_server) {
263 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
264 c_Phoenix_Diagnostics_Dispose();
265 }
266
267 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
268 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
269 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
270 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
271
272 ::aos::ShmEventLoop can_output_event_loop(&config.message());
273 can_output_event_loop.set_name("CANOutputWriter");
274
275 // Thread 5
276 // Set up superstructure output.
277 ::aos::ShmEventLoop output_event_loop(&config.message());
278 output_event_loop.set_name("PWMOutputWriter");
279
280 AddLoop(&output_event_loop);
281
282 // Thread 6
283
284 RunLoops();
285 }
286};
287
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800288} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800289
290AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);