blob: 05a8895d3336d2a9fcef1a61ba801a3563f6760e [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"
15#include "frc971/wpilib/ahal/AnalogInput.h"
16#include "frc971/wpilib/ahal/Counter.h"
17#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
18#include "frc971/wpilib/ahal/DriverStation.h"
19#include "frc971/wpilib/ahal/Encoder.h"
20#include "frc971/wpilib/ahal/TalonFX.h"
21#include "frc971/wpilib/ahal/VictorSP.h"
22#undef ERROR
23
24#include "aos/commonmath.h"
25#include "aos/events/event_loop.h"
26#include "aos/events/shm_event_loop.h"
27#include "aos/init.h"
28#include "aos/logging/logging.h"
Ravago Jones486de802021-05-19 20:47:55 -070029#include "aos/realtime.h"
Ravago Jones486de802021-05-19 20:47:55 -070030#include "aos/time/time.h"
31#include "aos/util/log_interval.h"
32#include "aos/util/phased_loop.h"
33#include "aos/util/wrapping_counter.h"
Vinay Siva62e3d322021-08-14 17:37:51 -070034#include "ctre/phoenix/motorcontrol/can/TalonFX.h"
Ravago Jones486de802021-05-19 20:47:55 -070035#include "ctre/phoenix/motorcontrol/can/TalonSRX.h"
36#include "frc971/autonomous/auto_mode_generated.h"
37#include "frc971/control_loops/drivetrain/drivetrain_position_generated.h"
James Kuszmaul7077d342021-06-09 20:23:58 -070038#include "frc971/input/robot_state_generated.h"
Ravago Jones486de802021-05-19 20:47:55 -070039#include "frc971/wpilib/ADIS16448.h"
40#include "frc971/wpilib/buffered_pcm.h"
41#include "frc971/wpilib/buffered_solenoid.h"
42#include "frc971/wpilib/dma.h"
43#include "frc971/wpilib/drivetrain_writer.h"
44#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"
50#include "frc971/wpilib/wpilib_robot_base.h"
51#include "y2021_bot3/constants.h"
52#include "y2021_bot3/control_loops/superstructure/superstructure_output_generated.h"
53#include "y2021_bot3/control_loops/superstructure/superstructure_position_generated.h"
54
55using ::aos::monotonic_clock;
56using ::y2021_bot3::constants::Values;
57namespace superstructure = ::y2021_bot3::control_loops::superstructure;
58namespace chrono = ::std::chrono;
Vinay Sivae52a6b32021-07-10 15:19:26 -070059using std::make_unique;
Ravago Jones486de802021-05-19 20:47:55 -070060
61namespace y2021_bot3 {
62namespace wpilib {
63namespace {
64
65constexpr double kMaxBringupPower = 12.0;
66
67// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
68// DMA stuff and then removing the * 2.0 in *_translate.
69// The low bit is direction.
70
71// TODO(brian): Use ::std::max instead once we have C++14 so that can be
72// constexpr.
73template <typename T>
74constexpr T max(T a, T b) {
75 return (a > b) ? a : b;
76}
77
78template <typename T, typename... Rest>
79constexpr T max(T a, T b, T c, Rest... rest) {
80 return max(max(a, b), c, rest...);
81}
82
83double drivetrain_translate(int32_t in) {
84 return ((static_cast<double>(in) /
85 Values::kDrivetrainEncoderCountsPerRevolution()) *
86 (2.0 * M_PI)) *
87 Values::kDrivetrainEncoderRatio() *
88 control_loops::drivetrain::kWheelRadius;
89}
90
91double drivetrain_velocity_translate(double in) {
92 return (((1.0 / in) / Values::kDrivetrainCyclesPerRevolution()) *
93 (2.0 * M_PI)) *
94 Values::kDrivetrainEncoderRatio() *
95 control_loops::drivetrain::kWheelRadius;
96}
97
98constexpr double kMaxFastEncoderPulsesPerSecond =
99 Values::kMaxDrivetrainEncoderPulsesPerSecond();
100static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000,
101 "fast encoders are too fast");
102constexpr double kMaxMediumEncoderPulsesPerSecond =
103 kMaxFastEncoderPulsesPerSecond;
104
105static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000,
106 "medium encoders are too fast");
107
108} // namespace
109
110// Class to send position messages with sensor readings to our loops.
111class SensorReader : public ::frc971::wpilib::SensorReader {
112 public:
113 SensorReader(::aos::ShmEventLoop *event_loop)
114 : ::frc971::wpilib::SensorReader(event_loop),
115 auto_mode_sender_(
116 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
117 "/autonomous")),
118 superstructure_position_sender_(
119 event_loop->MakeSender<superstructure::Position>(
120 "/superstructure")),
121 drivetrain_position_sender_(
122 event_loop
123 ->MakeSender<::frc971::control_loops::drivetrain::Position>(
124 "/drivetrain")) {
125 // Set to filter out anything shorter than 1/4 of the minimum pulse width
126 // we should ever see.
127 UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
128 UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
129 }
130
131 // Auto mode switches.
132 void set_autonomous_mode(int i, ::std::unique_ptr<frc::DigitalInput> sensor) {
133 autonomous_modes_.at(i) = ::std::move(sensor);
134 }
135
136 void RunIteration() override {
137 {
138 auto builder = drivetrain_position_sender_.MakeBuilder();
139 frc971::control_loops::drivetrain::Position::Builder drivetrain_builder =
140 builder.MakeBuilder<frc971::control_loops::drivetrain::Position>();
141 drivetrain_builder.add_left_encoder(
142 drivetrain_translate(drivetrain_left_encoder_->GetRaw()));
143 drivetrain_builder.add_left_speed(
144 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()));
145
146 drivetrain_builder.add_right_encoder(
147 -drivetrain_translate(drivetrain_right_encoder_->GetRaw()));
148 drivetrain_builder.add_right_speed(-drivetrain_velocity_translate(
149 drivetrain_right_encoder_->GetPeriod()));
150
milind1f1dca32021-07-03 13:50:07 -0700151 builder.CheckOk(builder.Send(drivetrain_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700152 }
153
154 {
155 auto builder = superstructure_position_sender_.MakeBuilder();
156 superstructure::Position::Builder position_builder =
157 builder.MakeBuilder<superstructure::Position>();
milind1f1dca32021-07-03 13:50:07 -0700158 builder.CheckOk(builder.Send(position_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700159 }
160
161 {
162 auto builder = auto_mode_sender_.MakeBuilder();
163
164 uint32_t mode = 0;
165 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
166 if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) {
167 mode |= 1 << i;
168 }
169 }
170
171 auto auto_mode_builder =
172 builder.MakeBuilder<frc971::autonomous::AutonomousMode>();
173
174 auto_mode_builder.add_mode(mode);
175
milind1f1dca32021-07-03 13:50:07 -0700176 builder.CheckOk(builder.Send(auto_mode_builder.Finish()));
Ravago Jones486de802021-05-19 20:47:55 -0700177 }
178 }
179
180 private:
181 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
182 ::aos::Sender<superstructure::Position> superstructure_position_sender_;
183 ::aos::Sender<::frc971::control_loops::drivetrain::Position>
184 drivetrain_position_sender_;
185
186 ::std::array<::std::unique_ptr<frc::DigitalInput>, 2> autonomous_modes_;
187};
188
189class SuperstructureWriter
190 : public ::frc971::wpilib::LoopOutputHandler<superstructure::Output> {
191 public:
192 SuperstructureWriter(::aos::EventLoop *event_loop)
193 : ::frc971::wpilib::LoopOutputHandler<superstructure::Output>(
194 event_loop, "/superstructure") {}
195
Vinay Siva62e3d322021-08-14 17:37:51 -0700196 void set_intake_falcon(
197 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
198 intake_falcon_ = ::std::move(t);
Vinay Siva7cea8a82021-09-25 15:06:28 -0700199 ConfigureRollerFalcon(intake_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700200 }
201
202 void set_outtake_falcon(
203 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
204 outtake_falcon_ = ::std::move(t);
Vinay Siva7cea8a82021-09-25 15:06:28 -0700205 ConfigureRollerFalcon(outtake_falcon_.get());
206 }
207
208 void set_climber_falcon(
209 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> t) {
210 climber_falcon_ = ::std::move(t);
211 climber_falcon_->ConfigSupplyCurrentLimit(
212 {true, Values::kClimberSupplyCurrentLimit(),
213 Values::kClimberSupplyCurrentLimit(), 0});
Vinay Siva62e3d322021-08-14 17:37:51 -0700214 }
215
Ravago Jones486de802021-05-19 20:47:55 -0700216 private:
Vinay Siva7cea8a82021-09-25 15:06:28 -0700217 void ConfigureRollerFalcon(
218 ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) {
Vinay Siva62e3d322021-08-14 17:37:51 -0700219 falcon->ConfigSupplyCurrentLimit({true, Values::kRollerSupplyCurrentLimit(),
220 Values::kRollerSupplyCurrentLimit(), 0});
221 falcon->ConfigStatorCurrentLimit({true, Values::kRollerStatorCurrentLimit(),
222 Values::kRollerStatorCurrentLimit(), 0});
223 }
224
225 void WriteToFalcon(const double voltage,
226 ::ctre::phoenix::motorcontrol::can::TalonFX *falcon) {
227 falcon->Set(
228 ctre::phoenix::motorcontrol::ControlMode::PercentOutput,
229 std::clamp(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
230 }
231
232 void Write(const superstructure::Output &output) override {
233 WriteToFalcon(output.intake_volts(), intake_falcon_.get());
234 WriteToFalcon(output.outtake_volts(), outtake_falcon_.get());
Vinay Siva7cea8a82021-09-25 15:06:28 -0700235
236 WriteToFalcon(-output.climber_volts(), climber_falcon_.get());
Vinay Siva62e3d322021-08-14 17:37:51 -0700237 }
Ravago Jones486de802021-05-19 20:47:55 -0700238
Vinay Siva7cea8a82021-09-25 15:06:28 -0700239 void Stop() override {
240 AOS_LOG(WARNING, "Superstructure output too old.\n");
241 climber_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
242 intake_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
243 outtake_falcon_->Set(ctre::phoenix::motorcontrol::ControlMode::Disabled, 0);
244 }
Vinay Siva62e3d322021-08-14 17:37:51 -0700245
246 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX> intake_falcon_,
247 outtake_falcon_;
Vinay Siva7cea8a82021-09-25 15:06:28 -0700248
249 ::std::unique_ptr<::ctre::phoenix::motorcontrol::can::TalonFX>
250 climber_falcon_;
Ravago Jones486de802021-05-19 20:47:55 -0700251};
252
253class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
254 public:
255 ::std::unique_ptr<frc::Encoder> make_encoder(int index) {
256 return make_unique<frc::Encoder>(10 + index * 2, 11 + index * 2, false,
257 frc::Encoder::k4X);
258 }
259
260 void Run() override {
261 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
262 aos::configuration::ReadConfig("config.json");
263
264 // Thread 1.
265 ::aos::ShmEventLoop joystick_sender_event_loop(&config.message());
266 ::frc971::wpilib::JoystickSender joystick_sender(
267 &joystick_sender_event_loop);
268 AddLoop(&joystick_sender_event_loop);
269
270 // Thread 2.
271 ::aos::ShmEventLoop pdp_fetcher_event_loop(&config.message());
272 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
273 AddLoop(&pdp_fetcher_event_loop);
274
275 // Thread 3.
276 ::aos::ShmEventLoop sensor_reader_event_loop(&config.message());
277 SensorReader sensor_reader(&sensor_reader_event_loop);
278 sensor_reader.set_drivetrain_left_encoder(make_encoder(0));
279 sensor_reader.set_drivetrain_right_encoder(make_encoder(1));
280
281 AddLoop(&sensor_reader_event_loop);
282
283 // Thread 4.
284 ::aos::ShmEventLoop output_event_loop(&config.message());
285 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
286 drivetrain_writer.set_left_controller0(
287 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0)), true);
288 drivetrain_writer.set_right_controller0(
289 ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1)), false);
290
291 SuperstructureWriter superstructure_writer(&output_event_loop);
Vinay Siva62e3d322021-08-14 17:37:51 -0700292 superstructure_writer.set_intake_falcon(
293 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(0));
294 superstructure_writer.set_outtake_falcon(
295 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(1));
Vinay Siva7cea8a82021-09-25 15:06:28 -0700296 superstructure_writer.set_climber_falcon(
297 make_unique<::ctre::phoenix::motorcontrol::can::TalonFX>(2));
Ravago Jones486de802021-05-19 20:47:55 -0700298
299 AddLoop(&output_event_loop);
300
301 RunLoops();
302 }
303};
304
305} // namespace wpilib
306} // namespace y2021_bot3
307
308AOS_ROBOT_CLASS(::y2021_bot3::wpilib::WPILibRobot);