blob: 86ac3492c39ff808a3bf33d1b3e3a40a7b7e2e11 [file] [log] [blame]
Sabina Davis2243cab2019-02-05 21:45:08 -08001#include <inttypes.h>
Comran Morshed9a9948c2016-01-16 15:58:04 +00002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Comran Morshed9a9948c2016-01-16 15:58:04 +00005
Brian Silverman68cb5c22016-03-20 18:11:14 -07006#include <array>
Sabina Davis2243cab2019-02-05 21:45:08 -08007#include <chrono>
8#include <functional>
9#include <mutex>
10#include <thread>
Comran Morshed9a9948c2016-01-16 15:58:04 +000011
Parker Schuhd3b7a8872018-02-19 16:42:27 -080012#include "frc971/wpilib/ahal/AnalogInput.h"
13#include "frc971/wpilib/ahal/Compressor.h"
14#include "frc971/wpilib/ahal/DigitalGlitchFilter.h"
15#include "frc971/wpilib/ahal/DriverStation.h"
16#include "frc971/wpilib/ahal/Encoder.h"
17#include "frc971/wpilib/ahal/Relay.h"
18#include "frc971/wpilib/ahal/Talon.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000019#include "frc971/wpilib/wpilib_robot_base.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000020#undef ERROR
21
Brian Silvermanf819b442019-01-20 16:51:04 -080022#include "aos/commonmath.h"
Austin Schuhdf6cbb12019-02-02 13:46:52 -080023#include "aos/events/shm-event-loop.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080024#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070025#include "aos/logging/logging.h"
26#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080027#include "aos/make_unique.h"
28#include "aos/robot_state/robot_state.q.h"
29#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -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"
Philipp Schrader4bd29b12017-02-22 04:42:27 +000034#include "frc971/autonomous/auto.q.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000035#include "frc971/control_loops/control_loops.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000036#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080037#include "frc971/wpilib/ADIS16448.h"
38#include "frc971/wpilib/buffered_pcm.h"
39#include "frc971/wpilib/buffered_solenoid.h"
40#include "frc971/wpilib/dma.h"
41#include "frc971/wpilib/dma_edge_counting.h"
Sabina Davisb71bc282019-02-03 01:17:23 -080042#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080043#include "frc971/wpilib/encoder_and_potentiometer.h"
44#include "frc971/wpilib/gyro_sender.h"
45#include "frc971/wpilib/interrupt_edge_counting.h"
46#include "frc971/wpilib/joystick_sender.h"
47#include "frc971/wpilib/logging.q.h"
48#include "frc971/wpilib/loop_output_handler.h"
49#include "frc971/wpilib/pdp_fetcher.h"
50#include "frc971/wpilib/sensor_reader.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000051#include "y2016/constants.h"
Comran Morshed5bb12112016-02-16 13:48:57 +000052#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000053#include "y2016/control_loops/shooter/shooter.q.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080054#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000055#include "y2016/control_loops/superstructure/superstructure.q.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000056#include "y2016/queues/ball_detector.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000057
Comran Morshed9a9948c2016-01-16 15:58:04 +000058using ::frc971::control_loops::drivetrain_queue;
Comran Morshed225f0b92016-02-10 20:34:27 +000059using ::y2016::control_loops::shooter::shooter_queue;
60using ::y2016::control_loops::superstructure_queue;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080061using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080062using aos::make_unique;
Comran Morshed9a9948c2016-01-16 15:58:04 +000063
Comran Morshed6c6a0a92016-01-17 12:45:16 +000064namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000065namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080066namespace {
67constexpr double kMaxBringupPower = 12.0;
68} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000069
70// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
71// DMA stuff and then removing the * 2.0 in *_translate.
72// The low bit is direction.
73
Comran Morshed225f0b92016-02-10 20:34:27 +000074// Translates for the sensor values to convert raw index pulses into something
75// with proper units.
76
77// TODO(comran): Template these methods since there is a lot of repetition here.
78double hall_translate(double in) {
79 // Turn voltage from our 3-state halls into a ratio that the loop can use.
80 return in / 5.0;
81}
82
Comran Morshed9a9948c2016-01-16 15:58:04 +000083double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000084 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000085 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080086 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000087}
88
89double drivetrain_velocity_translate(double in) {
90 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000091 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080092 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000093}
94
Comran Morshed225f0b92016-02-10 20:34:27 +000095double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +000096 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000097 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
98}
Comran Morshed9a9948c2016-01-16 15:58:04 +000099
Comran Morshed225f0b92016-02-10 20:34:27 +0000100double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800101 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000102 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
103}
104
105double shoulder_translate(int32_t in) {
106 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
107 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
108}
109
110double wrist_translate(int32_t in) {
111 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
112 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
113}
114
115double intake_pot_translate(double voltage) {
116 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000117 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000118}
119
120double shoulder_pot_translate(double voltage) {
121 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000122 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000123}
124
125double wrist_pot_translate(double voltage) {
126 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000127 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000128}
129
Comran Morshed1b764322016-02-14 20:18:12 +0000130constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
131 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
132 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
133 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
134
135constexpr double kMaxShooterEncoderPulsesPerSecond =
136 18700.0 /* 775pro free speed RPM */ * 12.0 /
137 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
138 128.0 /* CPR */ * 4 /* edges per cycle */;
139
140double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
141 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
142
143constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
144 18700.0 /* 775pro free speed RPM */ * 12.0 /
145 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
146 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000147
Comran Morshed225f0b92016-02-10 20:34:27 +0000148// Class to send position messages with sensor readings to our loops.
Austin Schuh54667ac2019-02-02 16:44:49 -0800149class SensorReader : public ::frc971::wpilib::SensorReader {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000150 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800151 SensorReader(::aos::EventLoop *event_loop)
Austin Schuh4b652c92019-05-27 13:22:27 -0700152 : ::frc971::wpilib::SensorReader(event_loop),
153 ball_detector_sender_(
154 event_loop->MakeSender<::y2016::sensors::BallDetector>(
Austin Schuha250b2d2019-05-27 16:14:02 -0700155 ".y2016.sensors.ball_detector")),
156 auto_mode_sender_(
157 event_loop->MakeSender<::frc971::autonomous::AutonomousMode>(
158 ".frc971.autonomous.auto_mode")) {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000159 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
160 // we should ever see.
Austin Schuh54667ac2019-02-02 16:44:49 -0800161 UpdateFastEncoderFilterHz(kMaxDrivetrainShooterEncoderPulsesPerSecond);
162 UpdateMediumEncoderFilterHz(kMaxSuperstructureEncoderPulsesPerSecond);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000163 hall_filter_.SetPeriodNanoSeconds(100000);
164 }
165
Comran Morshed225f0b92016-02-10 20:34:27 +0000166 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
167 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000168 }
169
Comran Morshed225f0b92016-02-10 20:34:27 +0000170 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
171 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000172 }
173
Comran Morshed225f0b92016-02-10 20:34:27 +0000174 // Shooter setters.
175 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800176 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000177 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000178 }
179
Comran Morshed225f0b92016-02-10 20:34:27 +0000180 void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800181 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000182 shooter_right_encoder_ = ::std::move(encoder);
183 }
184
185 // Intake setters.
186 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800187 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000188 intake_encoder_.set_encoder(::std::move(encoder));
189 }
190
191 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
192 intake_encoder_.set_potentiometer(::std::move(potentiometer));
193 }
194
195 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800196 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000197 intake_encoder_.set_index(::std::move(index));
198 }
199
200 // Shoulder setters.
201 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800202 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000203 shoulder_encoder_.set_encoder(::std::move(encoder));
204 }
205
206 void set_shoulder_potentiometer(
207 ::std::unique_ptr<AnalogInput> potentiometer) {
208 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
209 }
210
211 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800212 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000213 shoulder_encoder_.set_index(::std::move(index));
214 }
215
216 // Wrist setters.
217 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800218 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000219 wrist_encoder_.set_encoder(::std::move(encoder));
220 }
221
222 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
223 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
224 }
225
226 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800227 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000228 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000229 }
230
Comran Morshedaa0573c2016-03-05 19:05:54 +0000231 // Ball detector setter.
232 void set_ball_detector(::std::unique_ptr<AnalogInput> analog) {
233 ball_detector_ = ::std::move(analog);
234 }
235
Brian Silverman68cb5c22016-03-20 18:11:14 -0700236 // Autonomous mode switch setter.
237 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
238 autonomous_modes_.at(i) = ::std::move(sensor);
239 }
240
Comran Morshed9a9948c2016-01-16 15:58:04 +0000241 // All of the DMA-related set_* calls must be made before this, and it doesn't
242 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000243
Austin Schuh54667ac2019-02-02 16:44:49 -0800244 void Start() {
245 AddToDMA(&intake_encoder_);
246 AddToDMA(&shoulder_encoder_);
247 AddToDMA(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000248 }
249
250 void RunIteration() {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000251 {
252 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
253 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800254 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000255 drivetrain_message->left_encoder =
256 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
257 drivetrain_message->left_speed =
258 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
259 drivetrain_message->right_speed =
260 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
261
Comran Morshed9a9948c2016-01-16 15:58:04 +0000262 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000263 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000264 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000265 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000266
267 drivetrain_message.Send();
268 }
Austin Schuh54667ac2019-02-02 16:44:49 -0800269 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000270
Austin Schuh54667ac2019-02-02 16:44:49 -0800271 void RunDmaIteration() {
272 const auto &values = constants::GetValues();
Comran Morshed225f0b92016-02-10 20:34:27 +0000273 {
274 auto shooter_message = shooter_queue.position.MakeMessage();
275 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800276 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000277 shooter_message->theta_right =
278 shooter_translate(shooter_right_encoder_->GetRaw());
279 shooter_message.Send();
280 }
281
282 {
283 auto superstructure_message = superstructure_queue.position.MakeMessage();
Sabina Davis2243cab2019-02-05 21:45:08 -0800284 CopyPosition(intake_encoder_, &superstructure_message->intake,
285 intake_translate, intake_pot_translate, false,
286 values.intake.pot_offset);
287 CopyPosition(shoulder_encoder_, &superstructure_message->shoulder,
288 shoulder_translate, shoulder_pot_translate, false,
289 values.shoulder.pot_offset);
290 CopyPosition(wrist_encoder_, &superstructure_message->wrist,
291 wrist_translate, wrist_pot_translate, true,
292 values.wrist.pot_offset);
Comran Morshed225f0b92016-02-10 20:34:27 +0000293
294 superstructure_message.Send();
295 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000296
297 {
Austin Schuh4b652c92019-05-27 13:22:27 -0700298 auto ball_detector_message = ball_detector_sender_.MakeMessage();
Comran Morshedaa0573c2016-03-05 19:05:54 +0000299 ball_detector_message->voltage = ball_detector_->GetVoltage();
300 LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message);
301 ball_detector_message.Send();
302 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700303
304 {
Austin Schuha250b2d2019-05-27 16:14:02 -0700305 auto auto_mode_message = auto_mode_sender_.MakeMessage();
Brian Silverman68cb5c22016-03-20 18:11:14 -0700306 auto_mode_message->mode = 0;
307 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
308 if (autonomous_modes_[i]->Get()) {
309 auto_mode_message->mode |= 1 << i;
310 }
311 }
312 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
313 auto_mode_message.Send();
314 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000315 }
316
Comran Morshed9a9948c2016-01-16 15:58:04 +0000317 private:
Austin Schuh4b652c92019-05-27 13:22:27 -0700318 ::aos::Sender<::y2016::sensors::BallDetector> ball_detector_sender_;
Austin Schuha250b2d2019-05-27 16:14:02 -0700319 ::aos::Sender<::frc971::autonomous::AutonomousMode> auto_mode_sender_;
Austin Schuh4b652c92019-05-27 13:22:27 -0700320
Comran Morshed225f0b92016-02-10 20:34:27 +0000321 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
322
323 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000324 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
325 shoulder_encoder_, wrist_encoder_;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000326 ::std::unique_ptr<AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000327
Brian Silverman68cb5c22016-03-20 18:11:14 -0700328 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
329
Austin Schuh54667ac2019-02-02 16:44:49 -0800330 DigitalGlitchFilter hall_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000331};
332
333class SolenoidWriter {
334 public:
335 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
336 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000337 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Austin Schuh843412b2016-03-20 16:48:46 -0700338 shooter_(".y2016.control_loops.shooter.shooter_queue.output"),
Sabina Davis2243cab2019-02-05 21:45:08 -0800339 superstructure_(".y2016.control_loops.superstructure_queue.output") {}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000340
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800341 void set_compressor(::std::unique_ptr<Compressor> compressor) {
342 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000343 }
344
Comran Morshed71466fe2016-04-21 20:21:14 -0700345 void set_drivetrain_shifter(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000346 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700347 drivetrain_shifter_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000348 }
349
Comran Morshed71466fe2016-04-21 20:21:14 -0700350 void set_climber_trigger(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000351 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700352 climber_trigger_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000353 }
354
Sabina Davis2243cab2019-02-05 21:45:08 -0800355 void set_traverse(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuh843412b2016-03-20 16:48:46 -0700356 traverse_ = ::std::move(s);
357 }
358
359 void set_traverse_latch(
360 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
361 traverse_latch_ = ::std::move(s);
362 }
363
Comran Morshedb79c4242016-02-06 18:27:26 +0000364 void set_shooter_clamp(
365 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
366 shooter_clamp_ = ::std::move(s);
367 }
368
369 void set_shooter_pusher(
370 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
371 shooter_pusher_ = ::std::move(s);
372 }
373
Sabina Davis2243cab2019-02-05 21:45:08 -0800374 void set_lights(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Austin Schuhe0729a62016-03-12 21:54:17 -0800375 lights_ = ::std::move(s);
376 }
377
Austin Schuh8b89d332016-03-24 20:19:12 -0700378 void set_flashlight(::std::unique_ptr<Relay> relay) {
379 flashlight_ = ::std::move(relay);
380 }
381
Comran Morshed9a9948c2016-01-16 15:58:04 +0000382 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800383 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000384 ::aos::SetCurrentThreadName("Solenoids");
385 ::aos::SetCurrentThreadRealtimePriority(27);
386
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700387 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
Austin Schuhd32b3622019-06-23 18:49:06 -0700388 ::aos::monotonic_clock::now(),
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700389 ::std::chrono::milliseconds(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000390
391 while (run_) {
392 {
393 const int iterations = phased_loop.SleepUntilNext();
394 if (iterations != 1) {
395 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
396 }
397 }
398
399 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000400 drivetrain_.FetchLatest();
401 if (drivetrain_.get()) {
402 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Comran Morshed71466fe2016-04-21 20:21:14 -0700403 drivetrain_shifter_->Set(
404 !(drivetrain_->left_high || drivetrain_->right_high));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000405 }
406 }
407
408 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000409 shooter_.FetchLatest();
410 if (shooter_.get()) {
411 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
412 shooter_clamp_->Set(shooter_->clamp_open);
413 shooter_pusher_->Set(shooter_->push_to_shooter);
Austin Schuhe0729a62016-03-12 21:54:17 -0800414 lights_->Set(shooter_->lights_on);
Austin Schuhb2c33382016-04-03 16:09:17 -0700415 if (shooter_->forwards_flashlight) {
416 if (shooter_->backwards_flashlight) {
417 flashlight_->Set(Relay::kOn);
418 } else {
419 flashlight_->Set(Relay::kReverse);
420 }
421 } else {
422 if (shooter_->backwards_flashlight) {
423 flashlight_->Set(Relay::kForward);
424 } else {
425 flashlight_->Set(Relay::kOff);
426 }
427 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000428 }
429 }
430
431 {
Austin Schuh843412b2016-03-20 16:48:46 -0700432 superstructure_.FetchLatest();
433 if (superstructure_.get()) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700434 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
435
436 climber_trigger_->Set(superstructure_->unfold_climber);
437
Austin Schuh843412b2016-03-20 16:48:46 -0700438 traverse_->Set(superstructure_->traverse_down);
439 traverse_latch_->Set(superstructure_->traverse_unlatched);
440 }
441 }
442
443 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000444 ::frc971::wpilib::PneumaticsToLog to_log;
Sabina Davis2243cab2019-02-05 21:45:08 -0800445 { to_log.compressor_on = compressor_->Enabled(); }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000446
447 pcm_->Flush();
448 to_log.read_solenoids = pcm_->GetAll();
449 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
450 }
451 }
452 }
453
454 void Quit() { run_ = false; }
455
456 private:
457 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
458
Comran Morshed71466fe2016-04-21 20:21:14 -0700459 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_,
460 shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_,
461 climber_trigger_;
Austin Schuh8b89d332016-03-24 20:19:12 -0700462 ::std::unique_ptr<Relay> flashlight_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800463 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000464
Comran Morshed9a9948c2016-01-16 15:58:04 +0000465 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000466 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Sabina Davis2243cab2019-02-05 21:45:08 -0800467 ::aos::Queue<::y2016::control_loops::SuperstructureQueue::Output>
Austin Schuh843412b2016-03-20 16:48:46 -0700468 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000469
470 ::std::atomic<bool> run_{true};
471};
472
Comran Morshed225f0b92016-02-10 20:34:27 +0000473class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
474 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800475 ShooterWriter(::aos::EventLoop *event_loop)
476 : ::frc971::wpilib::LoopOutputHandler(event_loop) {}
477
Comran Morshed225f0b92016-02-10 20:34:27 +0000478 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
479 shooter_left_talon_ = ::std::move(t);
480 }
481
482 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
483 shooter_right_talon_ = ::std::move(t);
484 }
485
486 private:
487 virtual void Read() override {
488 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
489 }
490
491 virtual void Write() override {
492 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
493 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800494
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800495 shooter_left_talon_->SetSpeed(queue->voltage_left / 12.0);
496 shooter_right_talon_->SetSpeed(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000497 }
498
499 virtual void Stop() override {
500 LOG(WARNING, "Shooter output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800501 shooter_left_talon_->SetDisabled();
502 shooter_right_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000503 }
504
505 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
506};
507
508class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
509 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800510 SuperstructureWriter(::aos::EventLoop *event_loop)
511 : ::frc971::wpilib::LoopOutputHandler(event_loop) {}
512
Comran Morshed225f0b92016-02-10 20:34:27 +0000513 void set_intake_talon(::std::unique_ptr<Talon> t) {
514 intake_talon_ = ::std::move(t);
515 }
516
517 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
518 shoulder_talon_ = ::std::move(t);
519 }
520
521 void set_wrist_talon(::std::unique_ptr<Talon> t) {
522 wrist_talon_ = ::std::move(t);
523 }
524
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800525 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
526 top_rollers_talon_ = ::std::move(t);
527 }
528
529 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
530 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000531 }
532
Comran Morshed71466fe2016-04-21 20:21:14 -0700533 void set_climber_talon(::std::unique_ptr<Talon> t) {
534 climber_talon_ = ::std::move(t);
535 }
536
Comran Morshed225f0b92016-02-10 20:34:27 +0000537 private:
538 virtual void Read() override {
539 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
540 }
541
542 virtual void Write() override {
543 auto &queue = ::y2016::control_loops::superstructure_queue.output;
544 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800545 intake_talon_->SetSpeed(::aos::Clip(queue->voltage_intake,
546 -kMaxBringupPower, kMaxBringupPower) /
547 12.0);
548 shoulder_talon_->SetSpeed(::aos::Clip(-queue->voltage_shoulder,
549 -kMaxBringupPower, kMaxBringupPower) /
550 12.0);
551 wrist_talon_->SetSpeed(
Austin Schuha9992ff2016-02-28 21:59:23 -0800552 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
553 12.0);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800554 top_rollers_talon_->SetSpeed(-queue->voltage_top_rollers / 12.0);
555 bottom_rollers_talon_->SetSpeed(-queue->voltage_bottom_rollers / 12.0);
556 climber_talon_->SetSpeed(-queue->voltage_climber / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000557 }
558
559 virtual void Stop() override {
560 LOG(WARNING, "Superstructure output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800561 intake_talon_->SetDisabled();
562 shoulder_talon_->SetDisabled();
563 wrist_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000564 }
565
Comran Morshedf4cd7642016-02-15 20:40:49 +0000566 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Comran Morshed71466fe2016-04-21 20:21:14 -0700567 top_rollers_talon_, bottom_rollers_talon_, climber_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000568};
569
Comran Morshed9a9948c2016-01-16 15:58:04 +0000570class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
571 public:
572 ::std::unique_ptr<Encoder> make_encoder(int index) {
573 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
574 Encoder::k4X);
575 }
576
577 void Run() override {
578 ::aos::InitNRT();
579 ::aos::SetCurrentThreadName("StartCompetition");
580
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800581 ::aos::ShmEventLoop event_loop;
582
583 ::frc971::wpilib::JoystickSender joystick_sender(&event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000584 ::std::thread joystick_thread(::std::ref(joystick_sender));
585
Austin Schuh0b545432019-05-12 15:46:12 -0700586 ::frc971::wpilib::PDPFetcher pdp_fetcher(&event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000587 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800588 SensorReader reader(&event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000589
Austin Schuh9f77fd22016-02-21 02:53:58 -0800590 reader.set_drivetrain_left_encoder(make_encoder(5));
591 reader.set_drivetrain_right_encoder(make_encoder(6));
592 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
593 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000594
Austin Schuhf0c05762016-04-03 16:06:49 -0700595 reader.set_shooter_left_encoder(make_encoder(7));
596 reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000597
598 reader.set_intake_encoder(make_encoder(0));
599 reader.set_intake_index(make_unique<DigitalInput>(0));
600 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
601
Austin Schuhf0c05762016-04-03 16:06:49 -0700602 reader.set_shoulder_encoder(make_encoder(4));
Austin Schuh9f77fd22016-02-21 02:53:58 -0800603 reader.set_shoulder_index(make_unique<DigitalInput>(2));
604 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000605
Austin Schuh9f77fd22016-02-21 02:53:58 -0800606 reader.set_wrist_encoder(make_encoder(1));
607 reader.set_wrist_index(make_unique<DigitalInput>(1));
608 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000609
Comran Morshedaa0573c2016-03-05 19:05:54 +0000610 reader.set_ball_detector(make_unique<AnalogInput>(7));
611
Brian Silverman68cb5c22016-03-20 18:11:14 -0700612 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
613 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
614 reader.set_autonomous_mode(2, make_unique<DigitalInput>(7));
615 reader.set_autonomous_mode(3, make_unique<DigitalInput>(6));
616
Comran Morshed9a9948c2016-01-16 15:58:04 +0000617 ::std::thread reader_thread(::std::ref(reader));
618
Brian Silverman003a4732018-03-11 14:02:15 -0700619 // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448.
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800620 ::frc971::wpilib::GyroSender gyro_sender(&event_loop);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000621 ::std::thread gyro_thread(::std::ref(gyro_sender));
622
Austin Schuhf0c05762016-04-03 16:06:49 -0700623 auto imu_trigger = make_unique<DigitalInput>(3);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800624 ::frc971::wpilib::ADIS16448 imu(&event_loop, SPI::Port::kMXP,
625 imu_trigger.get());
Brian Silverman5f17a972016-02-28 01:49:32 -0500626 ::std::thread imu_thread(::std::ref(imu));
Brian Silverman5f17a972016-02-28 01:49:32 -0500627
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800628 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&event_loop);
Sabina Davisb71bc282019-02-03 01:17:23 -0800629 drivetrain_writer.set_left_controller0(
630 ::std::unique_ptr<Talon>(new Talon(5)), false);
631 drivetrain_writer.set_right_controller0(
632 ::std::unique_ptr<Talon>(new Talon(4)), true);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000633 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
634
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800635 ShooterWriter shooter_writer(&event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000636 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800637 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000638 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800639 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000640 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
641
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800642 SuperstructureWriter superstructure_writer(&event_loop);
Comran Morshed225f0b92016-02-10 20:34:27 +0000643 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800644 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000645 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800646 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000647 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800648 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800649 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800650 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800651 superstructure_writer.set_bottom_rollers_talon(
652 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed71466fe2016-04-21 20:21:14 -0700653 superstructure_writer.set_climber_talon(
654 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000655 ::std::thread superstructure_writer_thread(
656 ::std::ref(superstructure_writer));
657
Comran Morshed9a9948c2016-01-16 15:58:04 +0000658 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
659 new ::frc971::wpilib::BufferedPcm());
660 SolenoidWriter solenoid_writer(pcm);
Comran Morshed71466fe2016-04-21 20:21:14 -0700661 solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700662 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
663 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800664 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
665 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800666 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed71466fe2016-04-21 20:21:14 -0700667 solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1));
Austin Schuh8b89d332016-03-24 20:19:12 -0700668 solenoid_writer.set_flashlight(make_unique<Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000669
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800670 solenoid_writer.set_compressor(make_unique<Compressor>());
671
Comran Morshed9a9948c2016-01-16 15:58:04 +0000672 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
673
674 // Wait forever. Not much else to do...
675 while (true) {
676 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
677 if (r != 0) {
678 PLOG(WARNING, "infinite select failed");
679 } else {
680 PLOG(WARNING, "infinite select succeeded??\n");
681 }
682 }
683
684 LOG(ERROR, "Exiting WPILibRobot\n");
685
686 joystick_sender.Quit();
687 joystick_thread.join();
688 pdp_fetcher.Quit();
689 pdp_fetcher_thread.join();
690 reader.Quit();
691 reader_thread.join();
692 gyro_sender.Quit();
693 gyro_thread.join();
Brian Silverman5f17a972016-02-28 01:49:32 -0500694 imu.Quit();
695 imu_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000696
697 drivetrain_writer.Quit();
698 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000699 shooter_writer.Quit();
700 shooter_writer_thread.join();
701 superstructure_writer.Quit();
702 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000703 solenoid_writer.Quit();
704 solenoid_thread.join();
705
706 ::aos::Cleanup();
707 }
708};
709
710} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000711} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000712
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000713AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);