blob: 52d0b1bb140c56fdb1f4e7a63cb2b2b23a0c95d4 [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"
41#include "frc971/wpilib/encoder_and_potentiometer.h"
42#include "frc971/wpilib/gyro_sender.h"
43#include "frc971/wpilib/interrupt_edge_counting.h"
44#include "frc971/wpilib/joystick_sender.h"
45#include "frc971/wpilib/logging.q.h"
46#include "frc971/wpilib/loop_output_handler.h"
47#include "frc971/wpilib/pdp_fetcher.h"
48#include "frc971/wpilib/sensor_reader.h"
Comran Morshed6c6a0a92016-01-17 12:45:16 +000049#include "y2016/constants.h"
Comran Morshed5bb12112016-02-16 13:48:57 +000050#include "y2016/control_loops/drivetrain/drivetrain_dog_motor_plant.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000051#include "y2016/control_loops/shooter/shooter.q.h"
Austin Schuh54667ac2019-02-02 16:44:49 -080052#include "y2016/control_loops/shooter/shooter.q.h"
Comran Morshed225f0b92016-02-10 20:34:27 +000053#include "y2016/control_loops/superstructure/superstructure.q.h"
Comran Morshedaa0573c2016-03-05 19:05:54 +000054#include "y2016/queues/ball_detector.q.h"
Comran Morshed9a9948c2016-01-16 15:58:04 +000055
Comran Morshed9a9948c2016-01-16 15:58:04 +000056#ifndef M_PI
57#define M_PI 3.14159265358979323846
58#endif
59
60using ::frc971::control_loops::drivetrain_queue;
Comran Morshed225f0b92016-02-10 20:34:27 +000061using ::y2016::control_loops::shooter::shooter_queue;
62using ::y2016::control_loops::superstructure_queue;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080063using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080064using aos::make_unique;
Comran Morshed9a9948c2016-01-16 15:58:04 +000065
Comran Morshed6c6a0a92016-01-17 12:45:16 +000066namespace y2016 {
Comran Morshed9a9948c2016-01-16 15:58:04 +000067namespace wpilib {
Austin Schuha9992ff2016-02-28 21:59:23 -080068namespace {
69constexpr double kMaxBringupPower = 12.0;
70} // namespace
Comran Morshed9a9948c2016-01-16 15:58:04 +000071
72// TODO(Brian): Fix the interpretation of the result of GetRaw here and in the
73// DMA stuff and then removing the * 2.0 in *_translate.
74// The low bit is direction.
75
Comran Morshed225f0b92016-02-10 20:34:27 +000076// Translates for the sensor values to convert raw index pulses into something
77// with proper units.
78
79// TODO(comran): Template these methods since there is a lot of repetition here.
80double hall_translate(double in) {
81 // Turn voltage from our 3-state halls into a ratio that the loop can use.
82 return in / 5.0;
83}
84
Comran Morshed9a9948c2016-01-16 15:58:04 +000085double drivetrain_translate(int32_t in) {
Comran Morshed6c6a0a92016-01-17 12:45:16 +000086 return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000087 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080088 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000089}
90
91double drivetrain_velocity_translate(double in) {
92 return (1.0 / in) / 256.0 /*cpr*/ *
Comran Morshed225f0b92016-02-10 20:34:27 +000093 constants::Values::kDrivetrainEncoderRatio *
Austin Schuh9f77fd22016-02-21 02:53:58 -080094 control_loops::drivetrain::kWheelRadius * 2.0 * M_PI;
Comran Morshed9a9948c2016-01-16 15:58:04 +000095}
96
Comran Morshed225f0b92016-02-10 20:34:27 +000097double shooter_translate(int32_t in) {
Comran Morshed5bb12112016-02-16 13:48:57 +000098 return -static_cast<double>(in) / (128.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +000099 constants::Values::kShooterEncoderRatio * (2 * M_PI /*radians*/);
100}
Comran Morshed9a9948c2016-01-16 15:58:04 +0000101
Comran Morshed225f0b92016-02-10 20:34:27 +0000102double intake_translate(int32_t in) {
Austin Schuh9f77fd22016-02-21 02:53:58 -0800103 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed225f0b92016-02-10 20:34:27 +0000104 constants::Values::kIntakeEncoderRatio * (2 * M_PI /*radians*/);
105}
106
107double shoulder_translate(int32_t in) {
108 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
109 constants::Values::kShoulderEncoderRatio * (2 * M_PI /*radians*/);
110}
111
112double wrist_translate(int32_t in) {
113 return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
114 constants::Values::kWristEncoderRatio * (2 * M_PI /*radians*/);
115}
116
117double intake_pot_translate(double voltage) {
118 return voltage * constants::Values::kIntakePotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000119 (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000120}
121
122double shoulder_pot_translate(double voltage) {
123 return voltage * constants::Values::kShoulderPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000124 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed225f0b92016-02-10 20:34:27 +0000125}
126
127double wrist_pot_translate(double voltage) {
128 return voltage * constants::Values::kWristPotRatio *
Comran Morshed5bb12112016-02-16 13:48:57 +0000129 (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000130}
131
Comran Morshed1b764322016-02-14 20:18:12 +0000132constexpr double kMaxDrivetrainEncoderPulsesPerSecond =
133 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 /
134 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ /
135 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */;
136
137constexpr double kMaxShooterEncoderPulsesPerSecond =
138 18700.0 /* 775pro free speed RPM */ * 12.0 /
139 18.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
140 128.0 /* CPR */ * 4 /* edges per cycle */;
141
142double kMaxDrivetrainShooterEncoderPulsesPerSecond = ::std::max(
143 kMaxDrivetrainEncoderPulsesPerSecond, kMaxShooterEncoderPulsesPerSecond);
144
145constexpr double kMaxSuperstructureEncoderPulsesPerSecond =
146 18700.0 /* 775pro free speed RPM */ * 12.0 /
147 56.0 /* motor to encoder reduction */ / 60.0 /* seconds per minute */ *
148 512.0 /* CPR */ * 4 /* index pulse every quarter cycle */;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000149
Comran Morshed225f0b92016-02-10 20:34:27 +0000150// Class to send position messages with sensor readings to our loops.
Austin Schuh54667ac2019-02-02 16:44:49 -0800151class SensorReader : public ::frc971::wpilib::SensorReader {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000152 public:
153 SensorReader() {
154 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
155 // we should ever see.
Austin Schuh54667ac2019-02-02 16:44:49 -0800156 UpdateFastEncoderFilterHz(kMaxDrivetrainShooterEncoderPulsesPerSecond);
157 UpdateMediumEncoderFilterHz(kMaxSuperstructureEncoderPulsesPerSecond);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000158 hall_filter_.SetPeriodNanoSeconds(100000);
159 }
160
Comran Morshed225f0b92016-02-10 20:34:27 +0000161 void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) {
162 drivetrain_left_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000163 }
164
Comran Morshed225f0b92016-02-10 20:34:27 +0000165 void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) {
166 drivetrain_right_hall_ = ::std::move(analog);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000167 }
168
Comran Morshed225f0b92016-02-10 20:34:27 +0000169 // Shooter setters.
170 void set_shooter_left_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800171 fast_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000172 shooter_left_encoder_ = ::std::move(encoder);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000173 }
174
Comran Morshed225f0b92016-02-10 20:34:27 +0000175 void set_shooter_right_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_right_encoder_ = ::std::move(encoder);
178 }
179
180 // Intake setters.
181 void set_intake_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800182 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000183 intake_encoder_.set_encoder(::std::move(encoder));
184 }
185
186 void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
187 intake_encoder_.set_potentiometer(::std::move(potentiometer));
188 }
189
190 void set_intake_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800191 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000192 intake_encoder_.set_index(::std::move(index));
193 }
194
195 // Shoulder setters.
196 void set_shoulder_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800197 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000198 shoulder_encoder_.set_encoder(::std::move(encoder));
199 }
200
201 void set_shoulder_potentiometer(
202 ::std::unique_ptr<AnalogInput> potentiometer) {
203 shoulder_encoder_.set_potentiometer(::std::move(potentiometer));
204 }
205
206 void set_shoulder_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800207 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000208 shoulder_encoder_.set_index(::std::move(index));
209 }
210
211 // Wrist setters.
212 void set_wrist_encoder(::std::unique_ptr<Encoder> encoder) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800213 medium_encoder_filter_.Add(encoder.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000214 wrist_encoder_.set_encoder(::std::move(encoder));
215 }
216
217 void set_wrist_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) {
218 wrist_encoder_.set_potentiometer(::std::move(potentiometer));
219 }
220
221 void set_wrist_index(::std::unique_ptr<DigitalInput> index) {
Austin Schuh54667ac2019-02-02 16:44:49 -0800222 medium_encoder_filter_.Add(index.get());
Comran Morshed225f0b92016-02-10 20:34:27 +0000223 wrist_encoder_.set_index(::std::move(index));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000224 }
225
Comran Morshedaa0573c2016-03-05 19:05:54 +0000226 // Ball detector setter.
227 void set_ball_detector(::std::unique_ptr<AnalogInput> analog) {
228 ball_detector_ = ::std::move(analog);
229 }
230
Brian Silverman68cb5c22016-03-20 18:11:14 -0700231 // Autonomous mode switch setter.
232 void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) {
233 autonomous_modes_.at(i) = ::std::move(sensor);
234 }
235
Comran Morshedaa0573c2016-03-05 19:05:54 +0000236
Comran Morshed9a9948c2016-01-16 15:58:04 +0000237 // All of the DMA-related set_* calls must be made before this, and it doesn't
238 // hurt to do all of them.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000239
Austin Schuh54667ac2019-02-02 16:44:49 -0800240 void Start() {
241 AddToDMA(&intake_encoder_);
242 AddToDMA(&shoulder_encoder_);
243 AddToDMA(&wrist_encoder_);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000244 }
245
246 void RunIteration() {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000247 {
248 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
249 drivetrain_message->right_encoder =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800250 drivetrain_translate(-drivetrain_right_encoder_->GetRaw());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000251 drivetrain_message->left_encoder =
252 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
253 drivetrain_message->left_speed =
254 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
255 drivetrain_message->right_speed =
256 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
257
Comran Morshed9a9948c2016-01-16 15:58:04 +0000258 drivetrain_message->left_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000259 hall_translate(drivetrain_left_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000260 drivetrain_message->right_shifter_position =
Comran Morshed225f0b92016-02-10 20:34:27 +0000261 hall_translate(drivetrain_right_hall_->GetVoltage());
Comran Morshed9a9948c2016-01-16 15:58:04 +0000262
263 drivetrain_message.Send();
264 }
Austin Schuh54667ac2019-02-02 16:44:49 -0800265 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000266
Austin Schuh54667ac2019-02-02 16:44:49 -0800267 void RunDmaIteration() {
268 const auto &values = constants::GetValues();
Comran Morshed225f0b92016-02-10 20:34:27 +0000269 {
270 auto shooter_message = shooter_queue.position.MakeMessage();
271 shooter_message->theta_left =
Austin Schuh9f77fd22016-02-21 02:53:58 -0800272 shooter_translate(-shooter_left_encoder_->GetRaw());
Comran Morshed225f0b92016-02-10 20:34:27 +0000273 shooter_message->theta_right =
274 shooter_translate(shooter_right_encoder_->GetRaw());
275 shooter_message.Send();
276 }
277
278 {
279 auto superstructure_message = superstructure_queue.position.MakeMessage();
280 CopyPotAndIndexPosition(intake_encoder_, &superstructure_message->intake,
281 intake_translate, intake_pot_translate, false,
282 values.intake.pot_offset);
283 CopyPotAndIndexPosition(shoulder_encoder_,
284 &superstructure_message->shoulder,
285 shoulder_translate, shoulder_pot_translate, false,
286 values.shoulder.pot_offset);
287 CopyPotAndIndexPosition(wrist_encoder_, &superstructure_message->wrist,
Austin Schuh9f77fd22016-02-21 02:53:58 -0800288 wrist_translate, wrist_pot_translate, true,
Comran Morshed225f0b92016-02-10 20:34:27 +0000289 values.wrist.pot_offset);
290
291 superstructure_message.Send();
292 }
Comran Morshedaa0573c2016-03-05 19:05:54 +0000293
294 {
295 auto ball_detector_message =
296 ::y2016::sensors::ball_detector.MakeMessage();
297 ball_detector_message->voltage = ball_detector_->GetVoltage();
298 LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message);
299 ball_detector_message.Send();
300 }
Brian Silverman68cb5c22016-03-20 18:11:14 -0700301
302 {
Philipp Schrader4bd29b12017-02-22 04:42:27 +0000303 auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage();
Brian Silverman68cb5c22016-03-20 18:11:14 -0700304 auto_mode_message->mode = 0;
305 for (size_t i = 0; i < autonomous_modes_.size(); ++i) {
306 if (autonomous_modes_[i]->Get()) {
307 auto_mode_message->mode |= 1 << i;
308 }
309 }
310 LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message);
311 auto_mode_message.Send();
312 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000313 }
314
Comran Morshed9a9948c2016-01-16 15:58:04 +0000315 private:
Comran Morshed225f0b92016-02-10 20:34:27 +0000316 void CopyPotAndIndexPosition(
317 const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder,
318 ::frc971::PotAndIndexPosition *position,
319 ::std::function<double(int32_t)> encoder_translate,
320 ::std::function<double(double)> potentiometer_translate, bool reverse,
321 double pot_offset) {
322 const double multiplier = reverse ? -1.0 : 1.0;
323 position->encoder =
324 multiplier * encoder_translate(encoder.polled_encoder_value());
325 position->pot = multiplier * potentiometer_translate(
326 encoder.polled_potentiometer_voltage()) +
327 pot_offset;
328 position->latched_encoder =
329 multiplier * encoder_translate(encoder.last_encoder_value());
330 position->latched_pot =
331 multiplier *
332 potentiometer_translate(encoder.last_potentiometer_voltage()) +
333 pot_offset;
334 position->index_pulses = encoder.index_posedge_count();
335 }
336
Comran Morshed225f0b92016-02-10 20:34:27 +0000337 ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_;
338
339 ::std::unique_ptr<Encoder> shooter_left_encoder_, shooter_right_encoder_;
Comran Morshedb79c4242016-02-06 18:27:26 +0000340 ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_,
341 shoulder_encoder_, wrist_encoder_;
Comran Morshedaa0573c2016-03-05 19:05:54 +0000342 ::std::unique_ptr<AnalogInput> ball_detector_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000343
Brian Silverman68cb5c22016-03-20 18:11:14 -0700344 ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_;
345
Austin Schuh54667ac2019-02-02 16:44:49 -0800346 DigitalGlitchFilter hall_filter_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000347};
348
349class SolenoidWriter {
350 public:
351 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
352 : pcm_(pcm),
Comran Morshedb79c4242016-02-06 18:27:26 +0000353 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Austin Schuh843412b2016-03-20 16:48:46 -0700354 shooter_(".y2016.control_loops.shooter.shooter_queue.output"),
355 superstructure_(
356 ".y2016.control_loops.superstructure_queue.output") {
357 }
Comran Morshed9a9948c2016-01-16 15:58:04 +0000358
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800359 void set_compressor(::std::unique_ptr<Compressor> compressor) {
360 compressor_ = ::std::move(compressor);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000361 }
362
Comran Morshed71466fe2016-04-21 20:21:14 -0700363 void set_drivetrain_shifter(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000364 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700365 drivetrain_shifter_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000366 }
367
Comran Morshed71466fe2016-04-21 20:21:14 -0700368 void set_climber_trigger(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000369 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700370 climber_trigger_ = ::std::move(s);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000371 }
372
Austin Schuh843412b2016-03-20 16:48:46 -0700373 void set_traverse(
374 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
375 traverse_ = ::std::move(s);
376 }
377
378 void set_traverse_latch(
379 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
380 traverse_latch_ = ::std::move(s);
381 }
382
Comran Morshedb79c4242016-02-06 18:27:26 +0000383 void set_shooter_clamp(
384 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
385 shooter_clamp_ = ::std::move(s);
386 }
387
388 void set_shooter_pusher(
389 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
390 shooter_pusher_ = ::std::move(s);
391 }
392
Austin Schuhe0729a62016-03-12 21:54:17 -0800393 void set_lights(
394 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
395 lights_ = ::std::move(s);
396 }
397
Austin Schuh8b89d332016-03-24 20:19:12 -0700398 void set_flashlight(::std::unique_ptr<Relay> relay) {
399 flashlight_ = ::std::move(relay);
400 }
401
Comran Morshed9a9948c2016-01-16 15:58:04 +0000402 void operator()() {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800403 compressor_->Start();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000404 ::aos::SetCurrentThreadName("Solenoids");
405 ::aos::SetCurrentThreadRealtimePriority(27);
406
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700407 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
408 ::std::chrono::milliseconds(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000409
410 while (run_) {
411 {
412 const int iterations = phased_loop.SleepUntilNext();
413 if (iterations != 1) {
414 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
415 }
416 }
417
418 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000419 drivetrain_.FetchLatest();
420 if (drivetrain_.get()) {
421 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
Comran Morshed71466fe2016-04-21 20:21:14 -0700422 drivetrain_shifter_->Set(
423 !(drivetrain_->left_high || drivetrain_->right_high));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000424 }
425 }
426
427 {
Comran Morshedb79c4242016-02-06 18:27:26 +0000428 shooter_.FetchLatest();
429 if (shooter_.get()) {
430 LOG_STRUCT(DEBUG, "solenoids", *shooter_);
431 shooter_clamp_->Set(shooter_->clamp_open);
432 shooter_pusher_->Set(shooter_->push_to_shooter);
Austin Schuhe0729a62016-03-12 21:54:17 -0800433 lights_->Set(shooter_->lights_on);
Austin Schuhb2c33382016-04-03 16:09:17 -0700434 if (shooter_->forwards_flashlight) {
435 if (shooter_->backwards_flashlight) {
436 flashlight_->Set(Relay::kOn);
437 } else {
438 flashlight_->Set(Relay::kReverse);
439 }
440 } else {
441 if (shooter_->backwards_flashlight) {
442 flashlight_->Set(Relay::kForward);
443 } else {
444 flashlight_->Set(Relay::kOff);
445 }
446 }
Comran Morshedb79c4242016-02-06 18:27:26 +0000447 }
448 }
449
450 {
Austin Schuh843412b2016-03-20 16:48:46 -0700451 superstructure_.FetchLatest();
452 if (superstructure_.get()) {
Comran Morshed71466fe2016-04-21 20:21:14 -0700453 LOG_STRUCT(DEBUG, "solenoids", *superstructure_);
454
455 climber_trigger_->Set(superstructure_->unfold_climber);
456
Austin Schuh843412b2016-03-20 16:48:46 -0700457 traverse_->Set(superstructure_->traverse_down);
458 traverse_latch_->Set(superstructure_->traverse_unlatched);
459 }
460 }
461
462 {
Comran Morshed9a9948c2016-01-16 15:58:04 +0000463 ::frc971::wpilib::PneumaticsToLog to_log;
464 {
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800465 to_log.compressor_on = compressor_->Enabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000466 }
467
468 pcm_->Flush();
469 to_log.read_solenoids = pcm_->GetAll();
470 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
471 }
472 }
473 }
474
475 void Quit() { run_ = false; }
476
477 private:
478 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_;
479
Comran Morshed71466fe2016-04-21 20:21:14 -0700480 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_,
481 shooter_clamp_, shooter_pusher_, lights_, traverse_, traverse_latch_,
482 climber_trigger_;
Austin Schuh8b89d332016-03-24 20:19:12 -0700483 ::std::unique_ptr<Relay> flashlight_;
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800484 ::std::unique_ptr<Compressor> compressor_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000485
Comran Morshed9a9948c2016-01-16 15:58:04 +0000486 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed3263e8f2016-02-14 17:55:45 +0000487 ::aos::Queue<::y2016::control_loops::shooter::ShooterQueue::Output> shooter_;
Austin Schuh843412b2016-03-20 16:48:46 -0700488 ::aos::Queue<
489 ::y2016::control_loops::SuperstructureQueue::Output>
490 superstructure_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000491
492 ::std::atomic<bool> run_{true};
493};
494
495class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
496 public:
Comran Morshed225f0b92016-02-10 20:34:27 +0000497 void set_drivetrain_left_talon(::std::unique_ptr<Talon> t) {
498 drivetrain_left_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000499 }
500
Comran Morshed225f0b92016-02-10 20:34:27 +0000501 void set_drivetrain_right_talon(::std::unique_ptr<Talon> t) {
502 drivetrain_right_talon_ = ::std::move(t);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000503 }
504
505 private:
506 virtual void Read() override {
507 ::frc971::control_loops::drivetrain_queue.output.FetchAnother();
508 }
509
510 virtual void Write() override {
511 auto &queue = ::frc971::control_loops::drivetrain_queue.output;
512 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800513 drivetrain_left_talon_->SetSpeed(queue->left_voltage / 12.0);
514 drivetrain_right_talon_->SetSpeed(-queue->right_voltage / 12.0);
Comran Morshed9a9948c2016-01-16 15:58:04 +0000515 }
516
517 virtual void Stop() override {
518 LOG(WARNING, "drivetrain output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800519 drivetrain_left_talon_->SetDisabled();
520 drivetrain_right_talon_->SetDisabled();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000521 }
522
Comran Morshed225f0b92016-02-10 20:34:27 +0000523 ::std::unique_ptr<Talon> drivetrain_left_talon_, drivetrain_right_talon_;
524};
525
526class ShooterWriter : public ::frc971::wpilib::LoopOutputHandler {
527 public:
528 void set_shooter_left_talon(::std::unique_ptr<Talon> t) {
529 shooter_left_talon_ = ::std::move(t);
530 }
531
532 void set_shooter_right_talon(::std::unique_ptr<Talon> t) {
533 shooter_right_talon_ = ::std::move(t);
534 }
535
536 private:
537 virtual void Read() override {
538 ::y2016::control_loops::shooter::shooter_queue.output.FetchAnother();
539 }
540
541 virtual void Write() override {
542 auto &queue = ::y2016::control_loops::shooter::shooter_queue.output;
543 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800544
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800545 shooter_left_talon_->SetSpeed(queue->voltage_left / 12.0);
546 shooter_right_talon_->SetSpeed(-queue->voltage_right / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000547 }
548
549 virtual void Stop() override {
550 LOG(WARNING, "Shooter output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800551 shooter_left_talon_->SetDisabled();
552 shooter_right_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000553 }
554
555 ::std::unique_ptr<Talon> shooter_left_talon_, shooter_right_talon_;
556};
557
558class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler {
559 public:
560 void set_intake_talon(::std::unique_ptr<Talon> t) {
561 intake_talon_ = ::std::move(t);
562 }
563
564 void set_shoulder_talon(::std::unique_ptr<Talon> t) {
565 shoulder_talon_ = ::std::move(t);
566 }
567
568 void set_wrist_talon(::std::unique_ptr<Talon> t) {
569 wrist_talon_ = ::std::move(t);
570 }
571
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800572 void set_top_rollers_talon(::std::unique_ptr<Talon> t) {
573 top_rollers_talon_ = ::std::move(t);
574 }
575
576 void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) {
577 bottom_rollers_talon_ = ::std::move(t);
Comran Morshedf4cd7642016-02-15 20:40:49 +0000578 }
579
Comran Morshed71466fe2016-04-21 20:21:14 -0700580 void set_climber_talon(::std::unique_ptr<Talon> t) {
581 climber_talon_ = ::std::move(t);
582 }
583
Comran Morshed225f0b92016-02-10 20:34:27 +0000584 private:
585 virtual void Read() override {
586 ::y2016::control_loops::superstructure_queue.output.FetchAnother();
587 }
588
589 virtual void Write() override {
590 auto &queue = ::y2016::control_loops::superstructure_queue.output;
591 LOG_STRUCT(DEBUG, "will output", *queue);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800592 intake_talon_->SetSpeed(::aos::Clip(queue->voltage_intake,
593 -kMaxBringupPower, kMaxBringupPower) /
594 12.0);
595 shoulder_talon_->SetSpeed(::aos::Clip(-queue->voltage_shoulder,
596 -kMaxBringupPower, kMaxBringupPower) /
597 12.0);
598 wrist_talon_->SetSpeed(
Austin Schuha9992ff2016-02-28 21:59:23 -0800599 ::aos::Clip(queue->voltage_wrist, -kMaxBringupPower, kMaxBringupPower) /
600 12.0);
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800601 top_rollers_talon_->SetSpeed(-queue->voltage_top_rollers / 12.0);
602 bottom_rollers_talon_->SetSpeed(-queue->voltage_bottom_rollers / 12.0);
603 climber_talon_->SetSpeed(-queue->voltage_climber / 12.0);
Comran Morshed225f0b92016-02-10 20:34:27 +0000604 }
605
606 virtual void Stop() override {
607 LOG(WARNING, "Superstructure output too old.\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800608 intake_talon_->SetDisabled();
609 shoulder_talon_->SetDisabled();
610 wrist_talon_->SetDisabled();
Comran Morshed225f0b92016-02-10 20:34:27 +0000611 }
612
Comran Morshedf4cd7642016-02-15 20:40:49 +0000613 ::std::unique_ptr<Talon> intake_talon_, shoulder_talon_, wrist_talon_,
Comran Morshed71466fe2016-04-21 20:21:14 -0700614 top_rollers_talon_, bottom_rollers_talon_, climber_talon_;
Comran Morshed9a9948c2016-01-16 15:58:04 +0000615};
616
Comran Morshed9a9948c2016-01-16 15:58:04 +0000617class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
618 public:
619 ::std::unique_ptr<Encoder> make_encoder(int index) {
620 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
621 Encoder::k4X);
622 }
623
624 void Run() override {
625 ::aos::InitNRT();
626 ::aos::SetCurrentThreadName("StartCompetition");
627
628 ::frc971::wpilib::JoystickSender joystick_sender;
629 ::std::thread joystick_thread(::std::ref(joystick_sender));
630
631 ::frc971::wpilib::PDPFetcher pdp_fetcher;
632 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
633 SensorReader reader;
634
Austin Schuh9f77fd22016-02-21 02:53:58 -0800635 reader.set_drivetrain_left_encoder(make_encoder(5));
636 reader.set_drivetrain_right_encoder(make_encoder(6));
637 reader.set_drivetrain_left_hall(make_unique<AnalogInput>(5));
638 reader.set_drivetrain_right_hall(make_unique<AnalogInput>(6));
Comran Morshed225f0b92016-02-10 20:34:27 +0000639
Austin Schuhf0c05762016-04-03 16:06:49 -0700640 reader.set_shooter_left_encoder(make_encoder(7));
641 reader.set_shooter_right_encoder(make_encoder(-3));
Comran Morshed225f0b92016-02-10 20:34:27 +0000642
643 reader.set_intake_encoder(make_encoder(0));
644 reader.set_intake_index(make_unique<DigitalInput>(0));
645 reader.set_intake_potentiometer(make_unique<AnalogInput>(0));
646
Austin Schuhf0c05762016-04-03 16:06:49 -0700647 reader.set_shoulder_encoder(make_encoder(4));
Austin Schuh9f77fd22016-02-21 02:53:58 -0800648 reader.set_shoulder_index(make_unique<DigitalInput>(2));
649 reader.set_shoulder_potentiometer(make_unique<AnalogInput>(2));
Comran Morshed225f0b92016-02-10 20:34:27 +0000650
Austin Schuh9f77fd22016-02-21 02:53:58 -0800651 reader.set_wrist_encoder(make_encoder(1));
652 reader.set_wrist_index(make_unique<DigitalInput>(1));
653 reader.set_wrist_potentiometer(make_unique<AnalogInput>(1));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000654
Comran Morshedaa0573c2016-03-05 19:05:54 +0000655 reader.set_ball_detector(make_unique<AnalogInput>(7));
656
Brian Silverman68cb5c22016-03-20 18:11:14 -0700657 reader.set_autonomous_mode(0, make_unique<DigitalInput>(9));
658 reader.set_autonomous_mode(1, make_unique<DigitalInput>(8));
659 reader.set_autonomous_mode(2, make_unique<DigitalInput>(7));
660 reader.set_autonomous_mode(3, make_unique<DigitalInput>(6));
661
Comran Morshed9a9948c2016-01-16 15:58:04 +0000662 reader.set_dma(make_unique<DMA>());
663 ::std::thread reader_thread(::std::ref(reader));
664
Brian Silverman003a4732018-03-11 14:02:15 -0700665 // TODO(Brian): This interacts poorly with the SpiRxClearer in ADIS16448.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000666 ::frc971::wpilib::GyroSender gyro_sender;
667 ::std::thread gyro_thread(::std::ref(gyro_sender));
668
Austin Schuhf0c05762016-04-03 16:06:49 -0700669 auto imu_trigger = make_unique<DigitalInput>(3);
670 ::frc971::wpilib::ADIS16448 imu(SPI::Port::kMXP, imu_trigger.get());
Brian Silverman5f17a972016-02-28 01:49:32 -0500671 ::std::thread imu_thread(::std::ref(imu));
Brian Silverman5f17a972016-02-28 01:49:32 -0500672
Comran Morshed9a9948c2016-01-16 15:58:04 +0000673 DrivetrainWriter drivetrain_writer;
Comran Morshed225f0b92016-02-10 20:34:27 +0000674 drivetrain_writer.set_drivetrain_left_talon(
Comran Morshed9a9948c2016-01-16 15:58:04 +0000675 ::std::unique_ptr<Talon>(new Talon(5)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000676 drivetrain_writer.set_drivetrain_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800677 ::std::unique_ptr<Talon>(new Talon(4)));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000678 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
679
Comran Morshed225f0b92016-02-10 20:34:27 +0000680 ShooterWriter shooter_writer;
681 shooter_writer.set_shooter_left_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800682 ::std::unique_ptr<Talon>(new Talon(9)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000683 shooter_writer.set_shooter_right_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800684 ::std::unique_ptr<Talon>(new Talon(8)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000685 ::std::thread shooter_writer_thread(::std::ref(shooter_writer));
686
687 SuperstructureWriter superstructure_writer;
688 superstructure_writer.set_intake_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800689 ::std::unique_ptr<Talon>(new Talon(3)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000690 superstructure_writer.set_shoulder_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800691 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000692 superstructure_writer.set_wrist_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800693 ::std::unique_ptr<Talon>(new Talon(2)));
Campbell Crowleyd4fd6552016-02-21 17:53:46 -0800694 superstructure_writer.set_top_rollers_talon(
Austin Schuh0c2b58c2016-02-21 17:23:46 -0800695 ::std::unique_ptr<Talon>(new Talon(1)));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800696 superstructure_writer.set_bottom_rollers_talon(
697 ::std::unique_ptr<Talon>(new Talon(0)));
Comran Morshed71466fe2016-04-21 20:21:14 -0700698 superstructure_writer.set_climber_talon(
699 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed225f0b92016-02-10 20:34:27 +0000700 ::std::thread superstructure_writer_thread(
701 ::std::ref(superstructure_writer));
702
Comran Morshed9a9948c2016-01-16 15:58:04 +0000703 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
704 new ::frc971::wpilib::BufferedPcm());
705 SolenoidWriter solenoid_writer(pcm);
Comran Morshed71466fe2016-04-21 20:21:14 -0700706 solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0));
Austin Schuh843412b2016-03-20 16:48:46 -0700707 solenoid_writer.set_traverse_latch(pcm->MakeSolenoid(2));
708 solenoid_writer.set_traverse(pcm->MakeSolenoid(3));
Austin Schuhcaa1ee92016-02-27 14:45:37 -0800709 solenoid_writer.set_shooter_clamp(pcm->MakeSolenoid(4));
710 solenoid_writer.set_shooter_pusher(pcm->MakeSolenoid(5));
Austin Schuhe0729a62016-03-12 21:54:17 -0800711 solenoid_writer.set_lights(pcm->MakeSolenoid(6));
Comran Morshed71466fe2016-04-21 20:21:14 -0700712 solenoid_writer.set_climber_trigger(pcm->MakeSolenoid(1));
Austin Schuh8b89d332016-03-24 20:19:12 -0700713 solenoid_writer.set_flashlight(make_unique<Relay>(0));
Comran Morshed9a9948c2016-01-16 15:58:04 +0000714
Campbell Crowley1ab5fab2016-02-21 13:39:31 -0800715 solenoid_writer.set_compressor(make_unique<Compressor>());
716
Comran Morshed9a9948c2016-01-16 15:58:04 +0000717 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
718
719 // Wait forever. Not much else to do...
720 while (true) {
721 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
722 if (r != 0) {
723 PLOG(WARNING, "infinite select failed");
724 } else {
725 PLOG(WARNING, "infinite select succeeded??\n");
726 }
727 }
728
729 LOG(ERROR, "Exiting WPILibRobot\n");
730
731 joystick_sender.Quit();
732 joystick_thread.join();
733 pdp_fetcher.Quit();
734 pdp_fetcher_thread.join();
735 reader.Quit();
736 reader_thread.join();
737 gyro_sender.Quit();
738 gyro_thread.join();
Brian Silverman5f17a972016-02-28 01:49:32 -0500739 imu.Quit();
740 imu_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000741
742 drivetrain_writer.Quit();
743 drivetrain_writer_thread.join();
Comran Morshed225f0b92016-02-10 20:34:27 +0000744 shooter_writer.Quit();
745 shooter_writer_thread.join();
746 superstructure_writer.Quit();
747 superstructure_writer_thread.join();
Comran Morshed9a9948c2016-01-16 15:58:04 +0000748 solenoid_writer.Quit();
749 solenoid_thread.join();
750
751 ::aos::Cleanup();
752 }
753};
754
755} // namespace wpilib
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000756} // namespace y2016
Comran Morshed9a9948c2016-01-16 15:58:04 +0000757
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000758AOS_ROBOT_CLASS(::y2016::wpilib::WPILibRobot);