blob: 672667a90ae4e93986c3adc223e49d4ab9dffd35 [file] [log] [blame]
Niko Sohmers3860f8a2024-01-12 21:05:19 -08001#include <unistd.h>
2
3#include <array>
4#include <chrono>
5#include <cinttypes>
Niko Sohmers3860f8a2024-01-12 21:05:19 -08006#include <cstdio>
7#include <cstring>
8#include <functional>
9#include <memory>
10#include <mutex>
11#include <thread>
12
Niko Sohmers3860f8a2024-01-12 21:05:19 -080013#include "frc971/wpilib/ahal/AnalogInput.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080014#include "frc971/wpilib/ahal/DriverStation.h"
15#include "frc971/wpilib/ahal/Encoder.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080016#include "frc971/wpilib/ahal/TalonFX.h"
17#include "frc971/wpilib/ahal/VictorSP.h"
18#undef ERROR
19
20#include "ctre/phoenix/cci/Diagnostics_CCI.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080021
22#include "aos/commonmath.h"
23#include "aos/containers/sized_array.h"
24#include "aos/events/event_loop.h"
25#include "aos/events/shm_event_loop.h"
26#include "aos/init.h"
27#include "aos/logging/logging.h"
28#include "aos/realtime.h"
29#include "aos/time/time.h"
30#include "aos/util/log_interval.h"
31#include "aos/util/phased_loop.h"
32#include "aos/util/wrapping_counter.h"
33#include "frc971/autonomous/auto_mode_generated.h"
34#include "frc971/can_configuration_generated.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080035#include "frc971/control_loops/drivetrain/drivetrain_can_position_static.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080036#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
37#include "frc971/input/robot_state_generated.h"
38#include "frc971/queues/gyro_generated.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080039#include "frc971/wpilib/buffered_pcm.h"
40#include "frc971/wpilib/buffered_solenoid.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080041#include "frc971/wpilib/can_drivetrain_writer.h"
42#include "frc971/wpilib/can_sensor_reader.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080043#include "frc971/wpilib/dma.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080044#include "frc971/wpilib/encoder_and_potentiometer.h"
45#include "frc971/wpilib/joystick_sender.h"
46#include "frc971/wpilib/logging_generated.h"
47#include "frc971/wpilib/loop_output_handler.h"
48#include "frc971/wpilib/pdp_fetcher.h"
49#include "frc971/wpilib/sensor_reader.h"
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080050#include "frc971/wpilib/talonfx.h"
Niko Sohmers3860f8a2024-01-12 21:05:19 -080051#include "frc971/wpilib/wpilib_robot_base.h"
52#include "y2024/constants.h"
53#include "y2024/control_loops/superstructure/superstructure_output_generated.h"
54#include "y2024/control_loops/superstructure/superstructure_position_generated.h"
55
56DEFINE_bool(ctre_diag_server, false,
57 "If true, enable the diagnostics server for interacting with "
58 "devices on the CAN bus using Phoenix Tuner");
59
60using ::aos::monotonic_clock;
61using ::frc971::CANConfiguration;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -080062using ::frc971::control_loops::drivetrain::CANPositionStatic;
63using ::frc971::wpilib::TalonFX;
Niko Sohmers3860f8a2024-01-12 21:05:19 -080064using ::y2024::constants::Values;
65namespace superstructure = ::y2024::control_loops::superstructure;
66namespace drivetrain = ::y2024::control_loops::drivetrain;
67namespace chrono = ::std::chrono;
68using std::make_unique;
69
Stephan Pleinesf63bde82024-01-13 15:59:33 -080070namespace y2024::wpilib {
Niko Sohmers3860f8a2024-01-12 21:05:19 -080071namespace {
72
73constexpr double kMaxBringupPower = 12.0;
74
75// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
76// DMA stuff and then removing the * 2.0 in *_translate.
77// The low bit is direction.
78
79double drivetrain_velocity_translate(double in) {
80 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
81 (2.0 * M_PI)) *
82 Values::kDrivetrainEncoderRatio() *
83 control_loops::drivetrain::kWheelRadius;
84}
85
86constexpr double kMaxFastEncoderPulsesPerSecond = std::max({
87 Values::kMaxDrivetrainEncoderPulsesPerSecond(),
88});
89static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
90 "fast encoders are too fast");
91
92} // namespace
93
Niko Sohmers3860f8a2024-01-12 21:05:19 -080094// Class to send position messages with sensor readings to our loops.
95class SensorReader : public ::frc971::wpilib::SensorReader {
96 public:
97 SensorReader(::aos::ShmEventLoop *event_loop,
98 std::shared_ptr<const Values> values)
99 : ::frc971::wpilib::SensorReader(event_loop),
100 values_(std::move(values)),
101 auto_mode_sender_(
102 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
103 "/autonomous")),
104 superstructure_position_sender_(
105 event_loop->MakeSender<superstructure::Position>(
106 "/superstructure")),
107 drivetrain_position_sender_(
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800108 event_loop->MakeSender<
109 ::frc971::control_loops::drivetrain::PositionStatic>(
110 "/drivetrain")),
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800111 gyro_sender_(event_loop->MakeSender<::frc971::sensors::GyroReading>(
112 "/drivetrain")){};
113 void Start() override { AddToDMA(&imu_yaw_rate_reader_); }
114
115 // Auto mode switches.
116 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
117 autonomous_modes_.at(i) = ::std::move(sensor);
118 }
119
120 void set_yaw_rate_input(::std::unique_ptr<frc::DigitalInput> sensor) {
121 imu_yaw_rate_input_ = ::std::move(sensor);
122 imu_yaw_rate_reader_.set_input(imu_yaw_rate_input_.get());
123 }
124
125 void RunIteration() override {
126 {
127 auto builder = superstructure_position_sender_.MakeBuilder();
128
129 superstructure::Position::Builder position_builder =
130 builder.MakeBuilder<superstructure::Position>();
131 builder.CheckOk(builder.Send(position_builder.Finish()));
132 }
133
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800134 SendDrivetrainPosition(drivetrain_position_sender_.MakeStaticBuilder(),
135 drivetrain_velocity_translate,
136 constants::Values::DrivetrainEncoderToMeters, false,
137 false);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800138
139 {
140 auto builder = gyro_sender_.MakeBuilder();
141 ::frc971::sensors::GyroReading::Builder gyro_reading_builder =
142 builder.MakeBuilder<::frc971::sensors::GyroReading>();
143 // +/- 2000 deg / sec
144 constexpr double kMaxVelocity = 4000; // degrees / second
145 constexpr double kVelocityRadiansPerSecond =
146 kMaxVelocity / 360 * (2.0 * M_PI);
147
148 // Only part of the full range is used to prevent being 100% on or off.
149 constexpr double kScaledRangeLow = 0.1;
150 constexpr double kScaledRangeHigh = 0.9;
151
152 constexpr double kPWMFrequencyHz = 200;
153 double velocity_duty_cycle =
154 imu_yaw_rate_reader_.last_width() * kPWMFrequencyHz;
155
156 constexpr double kDutyCycleScale =
157 1 / (kScaledRangeHigh - kScaledRangeLow);
158 // scale from 0.1 - 0.9 to 0 - 1
159 double rescaled_velocity_duty_cycle =
160 (velocity_duty_cycle - kScaledRangeLow) * kDutyCycleScale;
161
162 if (!std::isnan(rescaled_velocity_duty_cycle)) {
163 gyro_reading_builder.add_velocity((rescaled_velocity_duty_cycle - 0.5) *
164 kVelocityRadiansPerSecond);
165 }
166 builder.CheckOk(builder.Send(gyro_reading_builder.Finish()));
167 }
168
169 {
170 auto builder = auto_mode_sender_.MakeBuilder();
171
172 uint32_t mode = 0;
173 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
174 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
175 mode |= 1 << i;
176 }
177 }
178
179 auto auto_mode_builder =
180 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
181
182 auto_mode_builder.add_mode(mode);
183
184 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
185 }
186 }
187
188 private:
189 std::shared_ptr<const Values> values_;
190
191 aos::Sender<frc971::autonomous::AutonomousMode> auto_mode_sender_;
192 aos::Sender<superstructure::Position> superstructure_position_sender_;
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800193 aos::Sender<frc971::control_loops::drivetrain::PositionStatic>
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800194 drivetrain_position_sender_;
195 ::aos::Sender<::frc971::sensors::GyroReading> gyro_sender_;
196
197 std::array<std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
198
199 std::unique_ptr<frc::DigitalInput> imu_yaw_rate_input_;
200
201 frc971::wpilib::DMAPulseWidthReader imu_yaw_rate_reader_;
202};
203
204class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
205 public:
206 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
207 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
208 frc::Encoder::k4X);
209 }
210
211 void Run() override {
212 std::shared_ptr<const Values> values =
213 std::make_shared<const Values>(constants::MakeValues());
214
215 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
216 aos::configuration::ReadConfig("aos_config.json");
217
218 // Thread 1.
219 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
220 ::frc971::wpilib::JoystickSender joystick_sender(
221 &joystick_sender_event_loop);
222 AddLoop(&joystick_sender_event_loop);
223
224 // Thread 2.
225 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
226 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
227 AddLoop(&pdp_fetcher_event_loop);
228
229 // Thread 3.
230 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
231 SensorReader sensor_reader(&sensor_reader_event_loop, values);
232 sensor_reader.set_pwm_trigger(true);
233 sensor_reader.set_drivetrain_left_encoder(make_encoder(1));
234 sensor_reader.set_drivetrain_right_encoder(make_encoder(0));
235 sensor_reader.set_yaw_rate_input(make_unique<frc::DigitalInput>(0));
236
237 AddLoop(&sensor_reader_event_loop);
238
239 // Thread 4.
240 // Set up CAN.
241 if (!FLAGS_ctre_diag_server) {
242 c_Phoenix_Diagnostics_SetSecondsToStart(-1);
243 c_Phoenix_Diagnostics_Dispose();
244 }
245
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800246 std::vector<ctre::phoenix6::BaseStatusSignal *> signals_registry;
247
248 std::shared_ptr<TalonFX> right_front = std::make_shared<TalonFX>(
249 0, false, "Drivetrain Bus", &signals_registry,
250 constants::Values::kDrivetrainStatorCurrentLimit(),
251 constants::Values::kDrivetrainSupplyCurrentLimit());
252 std::shared_ptr<TalonFX> right_back = std::make_shared<TalonFX>(
253 1, false, "Drivetrain Bus", &signals_registry,
254 constants::Values::kDrivetrainStatorCurrentLimit(),
255 constants::Values::kDrivetrainSupplyCurrentLimit());
256 std::shared_ptr<TalonFX> left_front = std::make_shared<TalonFX>(
257 2, false, "Drivetrain Bus", &signals_registry,
258 constants::Values::kDrivetrainStatorCurrentLimit(),
259 constants::Values::kDrivetrainSupplyCurrentLimit());
260 std::shared_ptr<TalonFX> left_back = std::make_shared<TalonFX>(
261 3, false, "Drivetrain Bus", &signals_registry,
262 constants::Values::kDrivetrainStatorCurrentLimit(),
263 constants::Values::kDrivetrainSupplyCurrentLimit());
264
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800265 ctre::phoenix::platform::can::CANComm_SetRxSchedPriority(
266 constants::Values::kDrivetrainRxPriority, true, "Drivetrain Bus");
267 ctre::phoenix::platform::can::CANComm_SetTxSchedPriority(
268 constants::Values::kDrivetrainTxPriority, true, "Drivetrain Bus");
269
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800270 ::aos::ShmEventLoop can_sensor_reader_event_loop(&config.message());
271 can_sensor_reader_event_loop.set_name("CANSensorReader");
272
273 // Creating list of talonfx for CANSensorReader
274 std::vector<std::shared_ptr<TalonFX>> talonfxs;
275 for (auto talonfx : {right_front, right_back, left_front, left_back}) {
276 talonfxs.push_back(talonfx);
277 }
278
279 aos::Sender<CANPositionStatic> can_position_sender =
280 can_sensor_reader_event_loop.MakeSender<CANPositionStatic>(
281 "/drivetrain");
282
283 frc971::wpilib::CANSensorReader can_sensor_reader(
284 &can_sensor_reader_event_loop, std::move(signals_registry), talonfxs,
285 [talonfxs, &can_position_sender](ctre::phoenix::StatusCode status) {
286 aos::Sender<CANPositionStatic>::StaticBuilder builder =
287 can_position_sender.MakeStaticBuilder();
288
289 auto falcon_vector = builder->add_talonfxs();
290
291 for (auto talonfx : talonfxs) {
292 talonfx->SerializePosition(
293 falcon_vector->emplace_back(),
294 control_loops::drivetrain::kHighOutputRatio);
295 }
296
297 builder->set_timestamp(talonfxs.front()->GetTimestamp());
298 builder->set_status(static_cast<int>(status));
299
300 builder.CheckOk(builder.Send());
301 });
302
303 AddLoop(&can_sensor_reader_event_loop);
304
305 // Thread 5.
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800306 ::aos::ShmEventLoop can_output_event_loop(&config.message());
307 can_output_event_loop.set_name("CANOutputWriter");
308
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800309 frc971::wpilib::CANDrivetrainWriter can_drivetrain_writer(
310 &can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800311
Maxwell Hendersonf75800f2024-01-12 19:52:05 -0800312 can_drivetrain_writer.set_talonfxs({right_front, right_back},
313 {left_front, left_back});
314
315 can_output_event_loop.MakeWatcher(
316 "/roborio", [&can_drivetrain_writer](
317 const frc971::CANConfiguration &configuration) {
318 can_drivetrain_writer.HandleCANConfiguration(configuration);
319 });
320
321 AddLoop(&can_output_event_loop);
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800322
323 // Thread 6
324
325 RunLoops();
326 }
327};
328
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800329} // namespace y2024::wpilib
Niko Sohmers3860f8a2024-01-12 21:05:19 -0800330
331AOS_ROBOT_CLASS(::y2024::wpilib::WPILibRobot);