blob: 02c2ecf429a6be274861155408be54bf441a460d [file] [log] [blame]
Ravago Jones486de802021-05-19 20:47:55 -07001#include <unistd.h>
2
3#include <array>
4#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <cinttypes>
Ravago Jones486de802021-05-19 20:47:55 -07006#include <cmath>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07007#include <cstdio>
8#include <cstring>
Ravago Jones486de802021-05-19 20:47:55 -07009#include <functional>
Tyler Chatowbf0609c2021-07-31 16:13:27 -070010#include <memory>
Ravago Jones486de802021-05-19 20:47:55 -070011#include <mutex>
12#include <thread>
13
14#include "ctre/phoenix/CANifier.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070015
Ravago Jones486de802021-05-19 20:47:55 -070016#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/TalonFX.h"
22#include "frc971/wpilib/ahal/VictorSP.h"
23#undef ERROR
24
Philipp Schrader790cb542023-07-05 21:06:52 -070025#include "ctre/phoenix/motorcontrol/can/TalonFX.h"
26#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
27
Ravago Jones486de802021-05-19 20:47:55 -070028#include "aos/commonmath.h"
29#include "aos/events/event_loop.h"
30#include "aos/events/shm_event_loop.h"
31#include "aos/init.h"
32#include "aos/logging/logging.h"
Ravago Jones486de802021-05-19 20:47:55 -070033#include "aos/realtime.h"
Ravago Jones486de802021-05-19 20:47:55 -070034#include "aos/time/time.h"
35#include "aos/util/log_interval.h"
36#include "aos/util/phased_loop.h"
37#include "aos/util/wrapping_counter.h"
Ravago Jones486de802021-05-19 20:47:55 -070038#include "frc971/autonomous/auto_mode_generated.h"
39#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070040#include "frc971/input/robot_state_generated.h"
Ravago Jones486de802021-05-19 20:47:55 -070041#include "frc971/wpilib/ADIS16448.h"
42#include "frc971/wpilib/buffered_pcm.h"
43#include "frc971/wpilib/buffered_solenoid.h"
44#include "frc971/wpilib/dma.h"
45#include "frc971/wpilib/drivetrain_writer.h"
46#include "frc971/wpilib/encoder_and_potentiometer.h"
47#include "frc971/wpilib/joystick_sender.h"
48#include "frc971/wpilib/logging_generated.h"
49#include "frc971/wpilib/loop_output_handler.h"
50#include "frc971/wpilib/pdp_fetcher.h"
51#include "frc971/wpilib/sensor_reader.h"
52#include "frc971/wpilib/wpilib_robot_base.h"
53#include "y2021_bot3/constants.h"
54#include "y2021_bot3/control_loops/superstructure/superstructure_output_generated.h"
55#include "y2021_bot3/control_loops/superstructure/superstructure_position_generated.h"
56
57using ::aos::monotonic_clock;
58using ::y2021_bot3::constants::Values;
59namespace superstructure = ::y2021_bot3::control_loops::superstructure;
60namespace chrono = ::std::chrono;
Vinay Sivae52a6b32021-07-10 15:19:26 -070061using std::make_unique;
Ravago Jones486de802021-05-19 20:47:55 -070062
63namespace y2021_bot3 {
64namespace wpilib {
65namespace {
66
67constexpr double kMaxBringupPower = 12.0;
68
69// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
70// DMA stuff and then removing the * 2.0 in *_translate.
71// The low bit is direction.
72
73// TODO(brian): Use ::std::max instead once we have C++14 so that can be
74// constexpr.
75template <typename T>
76constexpr T max(T a, T b) {
77 return (a > b) ? a : b;
78}
79
80template <typename T, typename... Rest>
81constexpr T max(T a, T b, T c, Rest... rest) {
82 return max(max(a, b), c, rest...);
83}
84
85double drivetrain_translate(int32_t in) {
86 return ((static_cast<double>(in) /
87 Values::kDrivetrainEncoderCountsPerRevolution()) *
88 (2.0 * M_PI)) *
89 Values::kDrivetrainEncoderRatio() *
90 control_loops::drivetrain::kWheelRadius;
91}
92
93double drivetrain_velocity_translate(double in) {
94 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
95 (2.0 * M_PI)) *
96 Values::kDrivetrainEncoderRatio() *
97 control_loops::drivetrain::kWheelRadius;
98}
99
100constexpr double kMaxFastEncoderPulsesPerSecond =
101 Values::kMaxDrivetrainEncoderPulsesPerSecond();
102static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
103 "fast encoders are too fast");
104constexpr double kMaxMediumEncoderPulsesPerSecond =
105 kMaxFastEncoderPulsesPerSecond;
106
107static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
108 "medium encoders are too fast");
109
110} // namespace
111
112// Class to send position messages with sensor readings to our loops.
113class SensorReader : public ::frc971::wpilib::SensorReader {
114 public:
115 SensorReader(::aos::ShmEventLoop *event_loop)
116 : ::frc971::wpilib::SensorReader(event_loop),
117 auto_mode_sender_(
118 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
119 "/autonomous")),
120 superstructure_position_sender_(
121 event_loop->MakeSender<superstructure::Position>(
122 "/superstructure")),
123 drivetrain_position_sender_(
124 event_loop
125 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
126 "/drivetrain")) {
127 // Set to filter out anything shorter than 1/4 of the minimum pulse width
128 // we should ever see.
129 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
130 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
131 }
132
133 // Auto mode switches.
134 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
135 autonomous_modes_.at(i) = ::std::move(sensor);
136 }
137
138 void RunIteration() override {
139 {
140 auto builder = drivetrain_position_sender_.MakeBuilder();
141 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
142 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
143 drivetrain_builder.add_left_encoder(
144 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
145 drivetrain_builder.add_left_speed(
146 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
147
148 drivetrain_builder.add_right_encoder(
149 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
150 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
151 drivetrain_right_encoder_->GetPeriod()));
152
milind1f1dca32021-07-03 13:50:07 -0700153 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700154 }
155
156 {
157 auto builder = superstructure_position_sender_.MakeBuilder();
158 superstructure::Position::Builder position_builder =
159 builder.MakeBuilder<superstructure::Position>();
milind1f1dca32021-07-03 13:50:07 -0700160 builder.CheckOk(builder.Send(position_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700161 }
162
163 {
164 auto builder = auto_mode_sender_.MakeBuilder();
165
166 uint32_t mode = 0;
167 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
168 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
169 mode |= 1 << i;
170 }
171 }
172
173 auto auto_mode_builder =
174 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
175
176 auto_mode_builder.add_mode(mode);
177
milind1f1dca32021-07-03 13:50:07 -0700178 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700179 }
180 }
181
182 private:
183 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
184 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
185 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
186 drivetrain_position_sender_;
187
188 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
189};
190
191class SuperstructureWriter
192 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
193 public:
194 SuperstructureWriter(::aos::EventLoop *event_loop)
195 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
196 event_loop, "/superstructure") {}
197
Vinay Siva62e3d322021-08-14 17:37:51 -0700198 void set_intake_falcon(
199 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
200 intake_falcon_ = ::std::move(t);
Vinay Siva7cea8a82021-09-25 15:06:28 -0700201 ConfigureRollerFalcon(intake_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700202 }
203
204 void set_outtake_falcon(
205 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
206 outtake_falcon_ = ::std::move(t);
Vinay Siva7cea8a82021-09-25 15:06:28 -0700207 ConfigureRollerFalcon(outtake_falcon_.get());
208 }
209
210 void set_climber_falcon(
211 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
212 climber_falcon_ = ::std::move(t);
213 climber_falcon_->ConfigSupplyCurrentLimit(
214 {true, Values::kClimberSupplyCurrentLimit(),
215 Values::kClimberSupplyCurrentLimit(), 0});
Vinay Siva62e3d322021-08-14 17:37:51 -0700216 }
217
Ravago Jones486de802021-05-19 20:47:55 -0700218 private:
Vinay Siva7cea8a82021-09-25 15:06:28 -0700219 void ConfigureRollerFalcon(
220 ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) {
Vinay Siva62e3d322021-08-14 17:37:51 -0700221 falcon->ConfigSupplyCurrentLimit({true, Values::kRollerSupplyCurrentLimit(),
222 Values::kRollerSupplyCurrentLimit(), 0});
223 falcon->ConfigStatorCurrentLimit({true, Values::kRollerStatorCurrentLimit(),
224 Values::kRollerStatorCurrentLimit(), 0});
225 }
226
227 void WriteToFalcon(const double voltage,
228 ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) {
229 falcon->Set(
230 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
231 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
232 }
233
234 void Write(const superstructure::Output &output) override {
235 WriteToFalcon(output.intake_volts(), intake_falcon_.get());
236 WriteToFalcon(output.outtake_volts(), outtake_falcon_.get());
Vinay Siva7cea8a82021-09-25 15:06:28 -0700237
238 WriteToFalcon(-output.climber_volts(), climber_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700239 }
Ravago Jones486de802021-05-19 20:47:55 -0700240
Vinay Siva7cea8a82021-09-25 15:06:28 -0700241 void Stop() override {
242 AOS_LOG(WARNING, "Superstructure output too old.\n");
243 climber_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
244 intake_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
245 outtake_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
246 }
Vinay Siva62e3d322021-08-14 17:37:51 -0700247
248 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> intake_falcon_,
249 outtake_falcon_;
Vinay Siva7cea8a82021-09-25 15:06:28 -0700250
251 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
252 climber_falcon_;
Ravago Jones486de802021-05-19 20:47:55 -0700253};
254
255class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
256 public:
257 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
258 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
259 frc::Encoder::k4X);
260 }
261
262 void Run() override {
263 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
Austin Schuhc5fa6d92022-02-25 14:36:28 -0800264 aos::configuration::ReadConfig("aos_config.json");
Ravago Jones486de802021-05-19 20:47:55 -0700265
266 // Thread 1.
267 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
268 ::frc971::wpilib::JoystickSender joystick_sender(
269 &joystick_sender_event_loop);
270 AddLoop(&joystick_sender_event_loop);
271
272 // Thread 2.
273 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
274 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
275 AddLoop(&pdp_fetcher_event_loop);
276
277 // Thread 3.
278 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
279 SensorReader sensor_reader(&sensor_reader_event_loop);
280 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
281 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
282
283 AddLoop(&sensor_reader_event_loop);
284
285 // Thread 4.
286 ::aos::ShmEventLoop output_event_loop(&config.message());
287 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
288 drivetrain_writer.set_left_controller0(
289 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
290 drivetrain_writer.set_right_controller0(
291 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
292
293 SuperstructureWriter superstructure_writer(&output_event_loop);
Vinay Siva62e3d322021-08-14 17:37:51 -0700294 superstructure_writer.set_intake_falcon(
295 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0));
296 superstructure_writer.set_outtake_falcon(
297 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(1));
Vinay Siva7cea8a82021-09-25 15:06:28 -0700298 superstructure_writer.set_climber_falcon(
299 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(2));
Ravago Jones486de802021-05-19 20:47:55 -0700300
301 AddLoop(&output_event_loop);
302
303 RunLoops();
304 }
305};
306
307} // namespace wpilib
308} // namespace y2021_bot3
309
310AOS_ROBOT_CLASS(::y2021_bot3::wpilib::WPILibRobot);