blob: 9ea9c5884c6153c279fca37b14221c2956e8a567 [file] [log] [blame]
Comran Morshed0d6cf9b2015-06-17 19:29:57 +00001#include <stdio.h>
2#include <string.h>
3#include <unistd.h>
4#include <inttypes.h>
5
6#include <thread>
7#include <mutex>
8#include <functional>
9
10#include "aos/common/logging/logging.h"
11#include "aos/common/logging/queue_logging.h"
12#include "aos/common/time.h"
13#include "aos/common/util/log_interval.h"
14#include "aos/common/util/phased_loop.h"
15#include "aos/common/util/wrapping_counter.h"
16#include "aos/common/stl_mutex.h"
17#include "aos/linux_code/init.h"
18#include "aos/common/messages/robot_state.q.h"
19
20#include "frc971/control_loops/control_loops.q.h"
21#include "bot3/control_loops/drivetrain/drivetrain.q.h"
Comran Morshede140e382015-08-19 20:35:25 +000022#include "bot3/control_loops/elevator/elevator.q.h"
23#include "bot3/control_loops/intake/intake.q.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000024
25#include "frc971/wpilib/hall_effect.h"
26#include "frc971/wpilib/joystick_sender.h"
27#include "frc971/wpilib/loop_output_handler.h"
28#include "frc971/wpilib/buffered_solenoid.h"
29#include "frc971/wpilib/buffered_pcm.h"
30#include "frc971/wpilib/gyro_sender.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000031#include "frc971/wpilib/logging.q.h"
32#include "bot3/control_loops/drivetrain/drivetrain.h"
Comran Morshede140e382015-08-19 20:35:25 +000033#include "bot3/control_loops/elevator/elevator.h"
34#include "bot3/control_loops/intake/intake.h"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000035
36#include "Encoder.h"
37#include "Talon.h"
38#include "DriverStation.h"
39#include "AnalogInput.h"
40#include "Compressor.h"
41#include "Relay.h"
42#include "RobotBase.h"
43#include "dma.h"
44#include "ControllerPower.h"
45
46#ifndef M_PI
47#define M_PI 3.14159265358979323846
48#endif
49
50using ::aos::util::SimpleLogInterval;
51using ::bot3::control_loops::drivetrain_queue;
Comran Morshede140e382015-08-19 20:35:25 +000052using ::bot3::control_loops::elevator_queue;
53using ::bot3::control_loops::intake_queue;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000054using ::frc971::wpilib::BufferedPcm;
Comran Morshede140e382015-08-19 20:35:25 +000055using ::frc971::wpilib::BufferedSolenoid;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000056using ::frc971::wpilib::LoopOutputHandler;
57using ::frc971::wpilib::JoystickSender;
58using ::frc971::wpilib::GyroSender;
Comran Morshede140e382015-08-19 20:35:25 +000059using ::frc971::wpilib::HallEffect;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000060
61namespace bot3 {
62namespace wpilib {
63
64double drivetrain_translate(int32_t in) {
Comran Morshede140e382015-08-19 20:35:25 +000065 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000066 ::bot3::control_loops::kDrivetrainEncoderRatio *
67 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
68}
69
Comran Morshede140e382015-08-19 20:35:25 +000070double elevator_translate(int32_t in) {
71 return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) *
72 ::bot3::control_loops::kElevEncoderRatio * (2 * M_PI /*radians*/) *
73 ::bot3::control_loops::kElevGearboxOutputRadianDistance;
74}
75
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000076static const double kMaximumEncoderPulsesPerSecond =
Comran Morshede140e382015-08-19 20:35:25 +000077 4650.0 /* free speed RPM */ * 18.0 / 48.0 /* belt reduction */ /
78 60.0 /* seconds / minute */ * 512.0 /* CPR */ *
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000079 4.0 /* index pulse = 1/4 cycle */;
80
Comran Morshede140e382015-08-19 20:35:25 +000081// Reads in our inputs. (sensors, voltages, etc.)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000082class SensorReader {
83 public:
84 SensorReader() {
85 // Set it to filter out anything shorter than 1/4 of the minimum pulse width
86 // we should ever see.
87 filter_.SetPeriodNanoSeconds(
88 static_cast<int>(1 / 4.0 / kMaximumEncoderPulsesPerSecond * 1e9 + 0.5));
89 }
90
Comran Morshede140e382015-08-19 20:35:25 +000091 // Drivetrain setters.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000092 void set_left_encoder(::std::unique_ptr<Encoder> left_encoder) {
93 left_encoder_ = ::std::move(left_encoder);
94 }
95
96 void set_right_encoder(::std::unique_ptr<Encoder> right_encoder) {
97 right_encoder_ = ::std::move(right_encoder);
98 }
99
Comran Morshede140e382015-08-19 20:35:25 +0000100 // Elevator setters.
101 void set_elevator_encoder(::std::unique_ptr<Encoder> encoder) {
102 filter_.Add(encoder.get());
103 elevator_encoder_ = ::std::move(encoder);
104 }
105
106 void set_elevator_zeroing_hall_effect(::std::unique_ptr<HallEffect> hall) {
107 zeroing_hall_effect_ = ::std::move(hall);
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000108 }
109
110 void operator()() {
111 LOG(INFO, "In sensor reader thread\n");
112 ::aos::SetCurrentThreadName("SensorReader");
113
114 my_pid_ = getpid();
115 ds_ = DriverStation::GetInstance();
116
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000117 LOG(INFO, "Things are now started\n");
118
119 ::aos::SetCurrentThreadRealtimePriority(kPriority);
120 while (run_) {
121 ::aos::time::PhasedLoopXMS(5, 4000);
122 RunIteration();
123 }
124 }
125
126 void RunIteration() {
Comran Morshede140e382015-08-19 20:35:25 +0000127 // General
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000128 {
129 auto new_state = ::aos::robot_state.MakeMessage();
130
131 new_state->reader_pid = my_pid_;
132 new_state->outputs_enabled = ds_->IsSysActive();
133 new_state->browned_out = ds_->IsSysBrownedOut();
134
135 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
136 new_state->is_5v_active = ControllerPower::GetEnabled5V();
137 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
138 new_state->voltage_5v = ControllerPower::GetVoltage5V();
139
140 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
141 new_state->voltage_battery = ds_->GetBatteryVoltage();
142
143 LOG_STRUCT(DEBUG, "robot_state", *new_state);
144
145 new_state.Send();
146 }
147
Comran Morshede140e382015-08-19 20:35:25 +0000148 // Drivetrain
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000149 {
150 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
151 drivetrain_message->right_encoder =
152 -drivetrain_translate(right_encoder_->GetRaw());
153 drivetrain_message->left_encoder =
154 drivetrain_translate(left_encoder_->GetRaw());
155
156 drivetrain_message.Send();
157 }
158
Comran Morshede140e382015-08-19 20:35:25 +0000159 // Elevator
160 {
161 // Update control loop positions.
162 auto elevator_message = elevator_queue.position.MakeMessage();
163 elevator_message->encoder =
164 elevator_translate(elevator_encoder_->GetRaw());
165 elevator_message->bottom_hall_effect =
166 zeroing_hall_effect_->Get();
167
168 elevator_message.Send();
169 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000170 }
171
172 void Quit() { run_ = false; }
173
174 private:
175 static const int kPriority = 30;
176 static const int kInterruptPriority = 55;
177
178 int32_t my_pid_;
179 DriverStation *ds_;
180
Comran Morshede140e382015-08-19 20:35:25 +0000181 ::std::unique_ptr<Encoder> left_encoder_, right_encoder_, elevator_encoder_;
182 ::std::unique_ptr<HallEffect> zeroing_hall_effect_;
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000183
184 ::std::atomic<bool> run_{true};
185 DigitalGlitchFilter filter_;
186};
187
Comran Morshede140e382015-08-19 20:35:25 +0000188// Writes out our pneumatic outputs.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000189class SolenoidWriter {
190 public:
191 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Comran Morshede140e382015-08-19 20:35:25 +0000192 : pcm_(pcm), elevator_(".bot3.control_loops.elevator_queue.output") {}
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000193
194 void set_pressure_switch(::std::unique_ptr<DigitalSource> pressure_switch) {
195 pressure_switch_ = ::std::move(pressure_switch);
196 }
197
198 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
199 compressor_relay_ = ::std::move(compressor_relay);
200 }
201
Comran Morshede140e382015-08-19 20:35:25 +0000202 void set_elevator_passive_support(
203 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support) {
204 elevator_passive_support_ = ::std::move(elevator_passive_support);
205 }
206
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000207 void operator()() {
208 ::aos::SetCurrentThreadName("Solenoids");
209 ::aos::SetCurrentThreadRealtimePriority(30);
210
211 while (run_) {
212 ::aos::time::PhasedLoopXMS(20, 1000);
213
Comran Morshede140e382015-08-19 20:35:25 +0000214 // Elevator
215 {
216 elevator_.FetchLatest();
217 if (elevator_.get()) {
218 LOG_STRUCT(DEBUG, "solenoids", *elevator_);
219 elevator_passive_support_->Set(elevator_->passive_support);
220 }
221 }
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000222
Comran Morshede140e382015-08-19 20:35:25 +0000223 // Compressor
224 ::aos::joystick_state.FetchLatest();
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000225 {
226 ::frc971::wpilib::PneumaticsToLog to_log;
227 {
Comran Morshede140e382015-08-19 20:35:25 +0000228 // Refill if pneumatic pressure goes too low.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000229 const bool compressor_on = !pressure_switch_->Get();
230 to_log.compressor_on = compressor_on;
231 if (compressor_on) {
232 compressor_relay_->Set(Relay::kForward);
233 } else {
234 compressor_relay_->Set(Relay::kOff);
235 }
236 }
237
238 pcm_->Flush();
239 to_log.read_solenoids = pcm_->GetAll();
240 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
241 }
242 }
243 }
244
245 void Quit() { run_ = false; }
246
247 private:
248 const ::std::unique_ptr<BufferedPcm> &pcm_;
Comran Morshede140e382015-08-19 20:35:25 +0000249
250 ::std::unique_ptr<BufferedSolenoid> elevator_passive_support_;
251
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000252 ::std::unique_ptr<DigitalSource> pressure_switch_;
253 ::std::unique_ptr<Relay> compressor_relay_;
254
Comran Morshede140e382015-08-19 20:35:25 +0000255 ::aos::Queue<::bot3::control_loops::ElevatorQueue::Output> elevator_;
256
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000257 ::std::atomic<bool> run_{true};
258};
259
Comran Morshede140e382015-08-19 20:35:25 +0000260// Writes out drivetrain voltages.
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000261class DrivetrainWriter : public LoopOutputHandler {
262 public:
263 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
264 left_drivetrain_talon_ = ::std::move(t);
265 }
266
267 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
268 right_drivetrain_talon_ = ::std::move(t);
269 }
270
271 private:
272 virtual void Read() override {
273 ::bot3::control_loops::drivetrain_queue.output.FetchAnother();
274 }
275
276 virtual void Write() override {
277 auto &queue = ::bot3::control_loops::drivetrain_queue.output;
278 LOG_STRUCT(DEBUG, "will output", *queue);
279 left_drivetrain_talon_->Set(queue->left_voltage / 12.0);
280 right_drivetrain_talon_->Set(-queue->right_voltage / 12.0);
281 }
282
283 virtual void Stop() override {
284 LOG(WARNING, "drivetrain output too old\n");
285 left_drivetrain_talon_->Disable();
286 right_drivetrain_talon_->Disable();
287 }
288
289 ::std::unique_ptr<Talon> left_drivetrain_talon_;
290 ::std::unique_ptr<Talon> right_drivetrain_talon_;
291};
292
Comran Morshede140e382015-08-19 20:35:25 +0000293// Writes out elevator voltages.
294class ElevatorWriter : public LoopOutputHandler {
295 public:
296 void set_elevator_talon(::std::unique_ptr<Talon> t) {
297 elevator_talon_ = ::std::move(t);
298 }
299
300 private:
301 virtual void Read() override {
302 ::bot3::control_loops::elevator_queue.output.FetchAnother();
303 }
304
305 virtual void Write() override {
306 auto &queue = ::bot3::control_loops::elevator_queue.output;
307 LOG_STRUCT(DEBUG, "will output", *queue);
308 elevator_talon_->Set(queue->elevator / 12.0);
309 }
310
311 virtual void Stop() override {
312 LOG(WARNING, "Elevator output too old.\n");
313 elevator_talon_->Disable();
314 }
315
316 ::std::unique_ptr<Talon> elevator_talon_;
317};
318
319// Writes out intake voltages.
320class IntakeWriter : public LoopOutputHandler {
321 public:
322 void set_intake_talon(::std::unique_ptr<Talon> t) {
323 intake_talon_ = ::std::move(t);
324 }
325
326 private:
327 virtual void Read() override {
328 ::bot3::control_loops::intake_queue.output.FetchAnother();
329 }
330
331 virtual void Write() override {
332 auto &queue = ::bot3::control_loops::intake_queue.output;
333 LOG_STRUCT(DEBUG, "will output", *queue);
334 intake_talon_->Set(queue->intake / 12.0);
335 }
336
337 virtual void Stop() override {
338 LOG(WARNING, "Intake output too old.\n");
339 intake_talon_->Disable();
340 }
341
342 ::std::unique_ptr<Talon> intake_talon_;
343};
344
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000345// TODO(brian): Replace this with ::std::make_unique once all our toolchains
346// have support.
347template <class T, class... U>
348std::unique_ptr<T> make_unique(U &&... u) {
349 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
350}
351
352class WPILibRobot : public RobotBase {
353 public:
354 ::std::unique_ptr<Encoder> encoder(int index) {
355 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
356 Encoder::k4X);
357 }
358 virtual void StartCompetition() {
359 ::aos::InitNRT();
360 ::aos::SetCurrentThreadName("StartCompetition");
361
362 JoystickSender joystick_sender;
363 ::std::thread joystick_thread(::std::ref(joystick_sender));
364
365 SensorReader reader;
366 LOG(INFO, "Creating the reader\n");
367
Comran Morshede140e382015-08-19 20:35:25 +0000368 // TODO(comran): Find talon/encoder numbers...
369 reader.set_elevator_encoder(encoder(4));
370 reader.set_elevator_zeroing_hall_effect(
371 make_unique<HallEffect>(6));
372
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000373 reader.set_left_encoder(encoder(2));
374 reader.set_right_encoder(encoder(3));
Comran Morshede140e382015-08-19 20:35:25 +0000375
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000376 ::std::thread reader_thread(::std::ref(reader));
377 GyroSender gyro_sender;
378 ::std::thread gyro_thread(::std::ref(gyro_sender));
379
380 DrivetrainWriter drivetrain_writer;
381 drivetrain_writer.set_left_drivetrain_talon(
382 ::std::unique_ptr<Talon>(new Talon(8)));
383 drivetrain_writer.set_right_drivetrain_talon(
384 ::std::unique_ptr<Talon>(new Talon(0)));
385 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
386
Comran Morshede140e382015-08-19 20:35:25 +0000387 ElevatorWriter elevator_writer;
388 elevator_writer.set_elevator_talon(::std::unique_ptr<Talon>(new Talon(5)));
389
390 IntakeWriter intake_writer;
391 intake_writer.set_intake_talon(::std::unique_ptr<Talon>(new Talon(9)));
392
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000393 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
394 new ::frc971::wpilib::BufferedPcm());
395 SolenoidWriter solenoid_writer(pcm);
396 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
397 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
398 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
Comran Morshede140e382015-08-19 20:35:25 +0000399 // TODO(comran): Find talon/encoder numbers ^^^
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000400
401 // Wait forever. Not much else to do...
402 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
403
404 LOG(ERROR, "Exiting WPILibRobot\n");
405
406 joystick_sender.Quit();
407 joystick_thread.join();
408 reader.Quit();
409 reader_thread.join();
410 gyro_sender.Quit();
411 gyro_thread.join();
412
413 drivetrain_writer.Quit();
414 drivetrain_writer_thread.join();
415 solenoid_writer.Quit();
416 solenoid_thread.join();
417
418 ::aos::Cleanup();
419 }
420};
421
422} // namespace wpilib
423} // namespace bot3
424
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000425START_ROBOT_CLASS(::bot3::wpilib::WPILibRobot);