blob: ee4b897602130606fbbdfb9159f74dfc4faf9585 [file] [log] [blame]
Comran Morshed9a9948c2016-01-16 15:58:04 +00001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <inttypes.h>
5
Austin Schuh8aec1ed2016-05-01 13:29:20 -07006#include <chrono>
Comran Morshed9a9948c2016-01-16 15:58:04 +00007#include <thread>
8#include <mutex>
9#include <functional>
Brian Silverman68cb5c22016-03-20 18:11:14 -070010#include <array>
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"
23#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070024#include "aos/logging/logging.h"
25#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080026#include "aos/make_unique.h"
27#include "aos/robot_state/robot_state.q.h"
28#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -070029#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"
Philipp Schrader4bd29b12017-02-22 04:42:27 +000033#include "frc971/autonomous/auto.q.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000034#include "frc971/control_loops/control_loops.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000035#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080036#include "frc971/wpilib/ADIS16448.h"
37#include "frc971/wpilib/buffered_pcm.h"
38#include "frc971/wpilib/buffered_solenoid.h"
39#include "frc971/wpilib/dma.h"
40#include "frc971/wpilib/dma_edge_counting.h"
Sabina Davisb71bc282019-02-03 01:17:23 -080041#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080042#include "frc971/wpilib/encoder_and_potentiometer.h"
43#include "frc971/wpilib/gyro_sender.h"
44#include "frc971/wpilib/interrupt_edge_counting.h"
45#include "frc971/wpilib/joystick_sender.h"
46#include "frc971/wpilib/logging.q.h"
47#include "frc971/wpilib/loop_output_handler.h"
48#include "frc971/wpilib/pdp_fetcher.h"
49#include "frc971/wpilib/sensor_reader.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000050#include "y2016/constants.h"
Comran Morshed5bb12112016-02-16 13:48:57 +000051#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000052#include "y2016/control_loops/shooter/shooter.q.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080053#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000054#include "y2016/control_loops/superstructure/superstructure.q.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000055#include "y2016/queues/ball_detector.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000056
Comran Morshed9a9948c2016-01-16 15:58:04 +000057#ifndef M_PI
58#define M_PI 3.14159265358979323846
59#endif
60
61using ::frc971::control_loops::drivetrain_queue;
Comran Morshed225f0b92016-02-10 20:34:27 +000062using ::y2016::control_loops::shooter::shooter_queue;
63using ::y2016::control_loops::superstructure_queue;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080064using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080065using aos::make_unique;
Comran Morshed9a9948c2016-01-16 15:58:04 +000066
Comran Morshed6c6a0a92016-01-17 12:45:16 +000067namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000068namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080069namespace {
70constexpr double kMaxBringupPower = 12.0;
71} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000072
73// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
74// DMA stuff and then removing the * 2.0 in *_translate.
75// The low bit is direction.
76
Comran Morshed225f0b92016-02-10 20:34:27 +000077// Translates for the sensor values to convert raw index pulses into something
78// with proper units.
79
80// TODO(comran): Template these methods since there is a lot of repetition here.
81double hall_translate(double in) {
82 // Turn voltage from our 3-state halls into a ratio that the loop can use.
83 return in / 5.0;
84}
85
Comran Morshed9a9948c2016-01-16 15:58:04 +000086double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000087 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000088 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080089 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000090}
91
92double drivetrain_velocity_translate(double in) {
93 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000094 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080095 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000096}
97
Comran Morshed225f0b92016-02-10 20:34:27 +000098double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +000099 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000100 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
101}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000102
Comran Morshed225f0b92016-02-10 20:34:27 +0000103double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800104 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000105 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
106}
107
108double shoulder_translate(int32_t in) {
109 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
110 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
111}
112
113double wrist_translate(int32_t in) {
114 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
115 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
116}
117
118double intake_pot_translate(double voltage) {
119 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000120 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000121}
122
123double shoulder_pot_translate(double voltage) {
124 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000125 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000126}
127
128double wrist_pot_translate(double voltage) {
129 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000130 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000131}
132
Comran Morshed1b764322016-02-14 20:18:12 +0000133constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
134 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
135 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
136 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
137
138constexpr double kMaxShooterEncoderPulsesPerSecond =
139 18700.0 /* 775pro free speed RPM */ * 12.0 /
140 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
141 128.0 /* CPR */ * 4 /* edges per cycle */;
142
143double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
144 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
145
146constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
147 18700.0 /* 775pro free speed RPM */ * 12.0 /
148 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
149 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000150
Comran Morshed225f0b92016-02-10 20:34:27 +0000151// Class to send position messages with sensor readings to our loops.
Austin Schuh54667ac2019-02-02 16:44:49 -0800152class SensorReader : public ::frc971::wpilib::SensorReader {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000153 public:
154 SensorReader() {
155 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
156 // we should ever see.
Austin Schuh54667ac2019-02-02 16:44:49 -0800157 UpdateFastEncoderFilterHz(kMaxDrivetrainShooterEncoderPulsesPerSecond);
158 UpdateMediumEncoderFilterHz(kMaxSuperstructureEncoderPulsesPerSecond);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000159 hall_filter_.SetPeriodNanoSeconds(100000);
160 }
161
Comran Morshed225f0b92016-02-10 20:34:27 +0000162 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
163 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000164 }
165
Comran Morshed225f0b92016-02-10 20:34:27 +0000166 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
167 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000168 }
169
Comran Morshed225f0b92016-02-10 20:34:27 +0000170 // Shooter setters.
171 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800172 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000173 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000174 }
175
Comran Morshed225f0b92016-02-10 20:34:27 +0000176 void set_shooter_right_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800177 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000178 shooter_right_encoder_ = ::std::move(encoder);
179 }
180
181 // Intake setters.
182 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800183 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000184 intake_encoder_.set_encoder(::std::move(encoder));
185 }
186
187 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
188 intake_encoder_.set_potentiometer(::std::move(potentiometer));
189 }
190
191 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800192 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000193 intake_encoder_.set_index(::std::move(index));
194 }
195
196 // Shoulder setters.
197 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800198 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000199 shoulder_encoder_.set_encoder(::std::move(encoder));
200 }
201
202 void set_shoulder_potentiometer(
203 ::std::unique_ptr<AnalogInput> potentiometer) {
204 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
205 }
206
207 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800208 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000209 shoulder_encoder_.set_index(::std::move(index));
210 }
211
212 // Wrist setters.
213 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800214 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000215 wrist_encoder_.set_encoder(::std::move(encoder));
216 }
217
218 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
219 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
220 }
221
222 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800223 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000224 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000225 }
226
Comran Morshedaa0573c2016-03-05 19:05:54 +0000227 // Ball detector setter.
228 void set_ball_detector(::std::unique_ptr<AnalogInput> analog) {
229 ball_detector_ = ::std::move(analog);
230 }
231
Brian Silverman68cb5c22016-03-20 18:11:14 -0700232 // Autonomous mode switch setter.
233 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
234 autonomous_modes_.at(i) = ::std::move(sensor);
235 }
236
Comran Morshedaa0573c2016-03-05 19:05:54 +0000237
Comran Morshed9a9948c2016-01-16 15:58:04 +0000238 // All of the DMA-related set_* calls must be made before this, and it doesn't
239 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000240
Austin Schuh54667ac2019-02-02 16:44:49 -0800241 void Start() {
242 AddToDMA(&intake_encoder_);
243 AddToDMA(&shoulder_encoder_);
244 AddToDMA(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000245 }
246
247 void RunIteration() {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000248 {
249 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
250 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800251 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000252 drivetrain_message->left_encoder =
253 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
254 drivetrain_message->left_speed =
255 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
256 drivetrain_message->right_speed =
257 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
258
Comran Morshed9a9948c2016-01-16 15:58:04 +0000259 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000260 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000261 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000262 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000263
264 drivetrain_message.Send();
265 }
Austin Schuh54667ac2019-02-02 16:44:49 -0800266 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000267
Austin Schuh54667ac2019-02-02 16:44:49 -0800268 void RunDmaIteration() {
269 const auto &values = constants::GetValues();
Comran Morshed225f0b92016-02-10 20:34:27 +0000270 {
271 auto shooter_message = shooter_queue.position.MakeMessage();
272 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800273 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000274 shooter_message->theta_right =
275 shooter_translate(shooter_right_encoder_->GetRaw());
276 shooter_message.Send();
277 }
278
279 {
280 auto superstructure_message = superstructure_queue.position.MakeMessage();
281 CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake,
282 intake_translate, intake_pot_translate, false,
283 values.intake.pot_offset);
284 CopyPotAndIndexPosition(shoulder_encoder_,
285 &superstructure_message->shoulder,
286 shoulder_translate, shoulder_pot_translate, false,
287 values.shoulder.pot_offset);
288 CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist,
Austin Schuh9f77fd22016-02-21 02:53:58 -0800289 wrist_translate, wrist_pot_translate, true,
Comran Morshed225f0b92016-02-10 20:34:27 +0000290 values.wrist.pot_offset);
291
292 superstructure_message.Send();
293 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000294
295 {
296 auto ball_detector_message =
297 ::y2016::sensors::ball_detector.MakeMessage();
298 ball_detector_message->voltage = ball_detector_->GetVoltage();
299 LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message);
300 ball_detector_message.Send();
301 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700302
303 {
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000304 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
Brian Silverman68cb5c22016-03-20 18:11:14 -0700305 auto_mode_message->mode = 0;
306 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
307 if (autonomous_modes_[i]->Get()) {
308 auto_mode_message->mode |= 1 << i;
309 }
310 }
311 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
312 auto_mode_message.Send();
313 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000314 }
315
Comran Morshed9a9948c2016-01-16 15:58:04 +0000316 private:
Comran Morshed225f0b92016-02-10 20:34:27 +0000317 void CopyPotAndIndexPosition(
318 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
319 ::frc971::PotAndIndexPosition *position,
320 ::std::function<double(int32_t)> encoder_translate,
321 ::std::function<double(double)> potentiometer_translate, bool reverse,
322 double pot_offset) {
323 const double multiplier = reverse ? -1.0 : 1.0;
324 position->encoder =
325 multiplier * encoder_translate(encoder.polled_encoder_value());
326 position->pot = multiplier * potentiometer_translate(
327 encoder.polled_potentiometer_voltage()) +
328 pot_offset;
329 position->latched_encoder =
330 multiplier * encoder_translate(encoder.last_encoder_value());
331 position->latched_pot =
332 multiplier *
333 potentiometer_translate(encoder.last_potentiometer_voltage()) +
334 pot_offset;
335 position->index_pulses = encoder.index_posedge_count();
336 }
337
Comran Morshed225f0b92016-02-10 20:34:27 +0000338 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
339
340 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000341 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
342 shoulder_encoder_, wrist_encoder_;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000343 ::std::unique_ptr<AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000344
Brian Silverman68cb5c22016-03-20 18:11:14 -0700345 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
346
Austin Schuh54667ac2019-02-02 16:44:49 -0800347 DigitalGlitchFilter hall_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000348};
349
350class SolenoidWriter {
351 public:
352 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
353 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000354 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Austin Schuh843412b2016-03-20 16:48:46 -0700355 shooter_(".y2016.control_loops.shooter.shooter_queue.output"),
356 superstructure_(
357 ".y2016.control_loops.superstructure_queue.output") {
358 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000359
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800360 void set_compressor(::std::unique_ptr<Compressor> compressor) {
361 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000362 }
363
Comran Morshed71466fe2016-04-21 20:21:14 -0700364 void set_drivetrain_shifter(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000365 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700366 drivetrain_shifter_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000367 }
368
Comran Morshed71466fe2016-04-21 20:21:14 -0700369 void set_climber_trigger(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000370 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700371 climber_trigger_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000372 }
373
Austin Schuh843412b2016-03-20 16:48:46 -0700374 void set_traverse(
375 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
376 traverse_ = ::std::move(s);
377 }
378
379 void set_traverse_latch(
380 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
381 traverse_latch_ = ::std::move(s);
382 }
383
Comran Morshedb79c4242016-02-06 18:27:26 +0000384 void set_shooter_clamp(
385 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
386 shooter_clamp_ = ::std::move(s);
387 }
388
389 void set_shooter_pusher(
390 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
391 shooter_pusher_ = ::std::move(s);
392 }
393
Austin Schuhe0729a62016-03-12 21:54:17 -0800394 void set_lights(
395 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
396 lights_ = ::std::move(s);
397 }
398
Austin Schuh8b89d332016-03-24 20:19:12 -0700399 void set_flashlight(::std::unique_ptr<Relay> relay) {
400 flashlight_ = ::std::move(relay);
401 }
402
Comran Morshed9a9948c2016-01-16 15:58:04 +0000403 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800404 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000405 ::aos::SetCurrentThreadName("Solenoids");
406 ::aos::SetCurrentThreadRealtimePriority(27);
407
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700408 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
409 ::std::chrono::milliseconds(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000410
411 while (run_) {
412 {
413 const int iterations = phased_loop.SleepUntilNext();
414 if (iterations != 1) {
415 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
416 }
417 }
418
419 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000420 drivetrain_.FetchLatest();
421 if (drivetrain_.get()) {
422 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Comran Morshed71466fe2016-04-21 20:21:14 -0700423 drivetrain_shifter_->Set(
424 !(drivetrain_->left_high || drivetrain_->right_high));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000425 }
426 }
427
428 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000429 shooter_.FetchLatest();
430 if (shooter_.get()) {
431 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
432 shooter_clamp_->Set(shooter_->clamp_open);
433 shooter_pusher_->Set(shooter_->push_to_shooter);
Austin Schuhe0729a62016-03-12 21:54:17 -0800434 lights_->Set(shooter_->lights_on);
Austin Schuhb2c33382016-04-03 16:09:17 -0700435 if (shooter_->forwards_flashlight) {
436 if (shooter_->backwards_flashlight) {
437 flashlight_->Set(Relay::kOn);
438 } else {
439 flashlight_->Set(Relay::kReverse);
440 }
441 } else {
442 if (shooter_->backwards_flashlight) {
443 flashlight_->Set(Relay::kForward);
444 } else {
445 flashlight_->Set(Relay::kOff);
446 }
447 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000448 }
449 }
450
451 {
Austin Schuh843412b2016-03-20 16:48:46 -0700452 superstructure_.FetchLatest();
453 if (superstructure_.get()) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700454 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
455
456 climber_trigger_->Set(superstructure_->unfold_climber);
457
Austin Schuh843412b2016-03-20 16:48:46 -0700458 traverse_->Set(superstructure_->traverse_down);
459 traverse_latch_->Set(superstructure_->traverse_unlatched);
460 }
461 }
462
463 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000464 ::frc971::wpilib::PneumaticsToLog to_log;
465 {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800466 to_log.compressor_on = compressor_->Enabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000467 }
468
469 pcm_->Flush();
470 to_log.read_solenoids = pcm_->GetAll();
471 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
472 }
473 }
474 }
475
476 void Quit() { run_ = false; }
477
478 private:
479 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
480
Comran Morshed71466fe2016-04-21 20:21:14 -0700481 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_,
482 shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_,
483 climber_trigger_;
Austin Schuh8b89d332016-03-24 20:19:12 -0700484 ::std::unique_ptr<Relay> flashlight_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800485 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000486
Comran Morshed9a9948c2016-01-16 15:58:04 +0000487 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000488 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Austin Schuh843412b2016-03-20 16:48:46 -0700489 ::aos::Queue<
490 ::y2016::control_loops::SuperstructureQueue::Output>
491 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000492
493 ::std::atomic<bool> run_{true};
494};
495
Comran Morshed225f0b92016-02-10 20:34:27 +0000496class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
497 public:
498 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
499 shooter_left_talon_ = ::std::move(t);
500 }
501
502 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
503 shooter_right_talon_ = ::std::move(t);
504 }
505
506 private:
507 virtual void Read() override {
508 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
509 }
510
511 virtual void Write() override {
512 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
513 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800514
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800515 shooter_left_talon_->SetSpeed(queue->voltage_left / 12.0);
516 shooter_right_talon_->SetSpeed(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000517 }
518
519 virtual void Stop() override {
520 LOG(WARNING, "Shooter output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800521 shooter_left_talon_->SetDisabled();
522 shooter_right_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000523 }
524
525 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
526};
527
528class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
529 public:
530 void set_intake_talon(::std::unique_ptr<Talon> t) {
531 intake_talon_ = ::std::move(t);
532 }
533
534 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
535 shoulder_talon_ = ::std::move(t);
536 }
537
538 void set_wrist_talon(::std::unique_ptr<Talon> t) {
539 wrist_talon_ = ::std::move(t);
540 }
541
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800542 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
543 top_rollers_talon_ = ::std::move(t);
544 }
545
546 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
547 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000548 }
549
Comran Morshed71466fe2016-04-21 20:21:14 -0700550 void set_climber_talon(::std::unique_ptr<Talon> t) {
551 climber_talon_ = ::std::move(t);
552 }
553
Comran Morshed225f0b92016-02-10 20:34:27 +0000554 private:
555 virtual void Read() override {
556 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
557 }
558
559 virtual void Write() override {
560 auto &queue = ::y2016::control_loops::superstructure_queue.output;
561 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800562 intake_talon_->SetSpeed(::aos::Clip(queue->voltage_intake,
563 -kMaxBringupPower, kMaxBringupPower) /
564 12.0);
565 shoulder_talon_->SetSpeed(::aos::Clip(-queue->voltage_shoulder,
566 -kMaxBringupPower, kMaxBringupPower) /
567 12.0);
568 wrist_talon_->SetSpeed(
Austin Schuha9992ff2016-02-28 21:59:23 -0800569 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
570 12.0);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800571 top_rollers_talon_->SetSpeed(-queue->voltage_top_rollers / 12.0);
572 bottom_rollers_talon_->SetSpeed(-queue->voltage_bottom_rollers / 12.0);
573 climber_talon_->SetSpeed(-queue->voltage_climber / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000574 }
575
576 virtual void Stop() override {
577 LOG(WARNING, "Superstructure output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800578 intake_talon_->SetDisabled();
579 shoulder_talon_->SetDisabled();
580 wrist_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000581 }
582
Comran Morshedf4cd7642016-02-15 20:40:49 +0000583 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Comran Morshed71466fe2016-04-21 20:21:14 -0700584 top_rollers_talon_, bottom_rollers_talon_, climber_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000585};
586
Comran Morshed9a9948c2016-01-16 15:58:04 +0000587class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
588 public:
589 ::std::unique_ptr<Encoder> make_encoder(int index) {
590 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
591 Encoder::k4X);
592 }
593
594 void Run() override {
595 ::aos::InitNRT();
596 ::aos::SetCurrentThreadName("StartCompetition");
597
598 ::frc971::wpilib::JoystickSender joystick_sender;
599 ::std::thread joystick_thread(::std::ref(joystick_sender));
600
601 ::frc971::wpilib::PDPFetcher pdp_fetcher;
602 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
603 SensorReader reader;
604
Austin Schuh9f77fd22016-02-21 02:53:58 -0800605 reader.set_drivetrain_left_encoder(make_encoder(5));
606 reader.set_drivetrain_right_encoder(make_encoder(6));
607 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
608 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000609
Austin Schuhf0c05762016-04-03 16:06:49 -0700610 reader.set_shooter_left_encoder(make_encoder(7));
611 reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000612
613 reader.set_intake_encoder(make_encoder(0));
614 reader.set_intake_index(make_unique<DigitalInput>(0));
615 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
616
Austin Schuhf0c05762016-04-03 16:06:49 -0700617 reader.set_shoulder_encoder(make_encoder(4));
Austin Schuh9f77fd22016-02-21 02:53:58 -0800618 reader.set_shoulder_index(make_unique<DigitalInput>(2));
619 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000620
Austin Schuh9f77fd22016-02-21 02:53:58 -0800621 reader.set_wrist_encoder(make_encoder(1));
622 reader.set_wrist_index(make_unique<DigitalInput>(1));
623 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000624
Comran Morshedaa0573c2016-03-05 19:05:54 +0000625 reader.set_ball_detector(make_unique<AnalogInput>(7));
626
Brian Silverman68cb5c22016-03-20 18:11:14 -0700627 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
628 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
629 reader.set_autonomous_mode(2, make_unique<DigitalInput>(7));
630 reader.set_autonomous_mode(3, make_unique<DigitalInput>(6));
631
Comran Morshed9a9948c2016-01-16 15:58:04 +0000632 ::std::thread reader_thread(::std::ref(reader));
633
Brian Silverman003a4732018-03-11 14:02:15 -0700634 // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000635 ::frc971::wpilib::GyroSender gyro_sender;
636 ::std::thread gyro_thread(::std::ref(gyro_sender));
637
Austin Schuhf0c05762016-04-03 16:06:49 -0700638 auto imu_trigger = make_unique<DigitalInput>(3);
639 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get());
Brian Silverman5f17a972016-02-28 01:49:32 -0500640 ::std::thread imu_thread(::std::ref(imu));
Brian Silverman5f17a972016-02-28 01:49:32 -0500641
Sabina Davisb71bc282019-02-03 01:17:23 -0800642 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
643 drivetrain_writer.set_left_controller0(
644 ::std::unique_ptr<Talon>(new Talon(5)), false);
645 drivetrain_writer.set_right_controller0(
646 ::std::unique_ptr<Talon>(new Talon(4)), true);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000647 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
648
Comran Morshed225f0b92016-02-10 20:34:27 +0000649 ShooterWriter shooter_writer;
650 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800651 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000652 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800653 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000654 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
655
656 SuperstructureWriter superstructure_writer;
657 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800658 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000659 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800660 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000661 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800662 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800663 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800664 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800665 superstructure_writer.set_bottom_rollers_talon(
666 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed71466fe2016-04-21 20:21:14 -0700667 superstructure_writer.set_climber_talon(
668 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000669 ::std::thread superstructure_writer_thread(
670 ::std::ref(superstructure_writer));
671
Comran Morshed9a9948c2016-01-16 15:58:04 +0000672 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
673 new ::frc971::wpilib::BufferedPcm());
674 SolenoidWriter solenoid_writer(pcm);
Comran Morshed71466fe2016-04-21 20:21:14 -0700675 solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700676 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
677 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800678 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
679 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800680 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed71466fe2016-04-21 20:21:14 -0700681 solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1));
Austin Schuh8b89d332016-03-24 20:19:12 -0700682 solenoid_writer.set_flashlight(make_unique<Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000683
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800684 solenoid_writer.set_compressor(make_unique<Compressor>());
685
Comran Morshed9a9948c2016-01-16 15:58:04 +0000686 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
687
688 // Wait forever. Not much else to do...
689 while (true) {
690 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
691 if (r != 0) {
692 PLOG(WARNING, "infinite select failed");
693 } else {
694 PLOG(WARNING, "infinite select succeeded??\n");
695 }
696 }
697
698 LOG(ERROR, "Exiting WPILibRobot\n");
699
700 joystick_sender.Quit();
701 joystick_thread.join();
702 pdp_fetcher.Quit();
703 pdp_fetcher_thread.join();
704 reader.Quit();
705 reader_thread.join();
706 gyro_sender.Quit();
707 gyro_thread.join();
Brian Silverman5f17a972016-02-28 01:49:32 -0500708 imu.Quit();
709 imu_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000710
711 drivetrain_writer.Quit();
712 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000713 shooter_writer.Quit();
714 shooter_writer_thread.join();
715 superstructure_writer.Quit();
716 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000717 solenoid_writer.Quit();
718 solenoid_thread.join();
719
720 ::aos::Cleanup();
721 }
722};
723
724} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000725} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000726
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000727AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);