blob: dc3999f244347bf05db9bca83ea066e3ff9d2c48 [file] [log] [blame]
Comran Morshed41ed7c22015-11-04 21:03:37 +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 "Encoder.h"
11#include "Talon.h"
12#include "DriverStation.h"
13#include "AnalogInput.h"
14#include "Compressor.h"
15#include "Relay.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080016#include "frc971/wpilib/wpilib_robot_base.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000017#include "dma.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000018#include "DigitalInput.h"
Austin Schuhbc9a2ab2015-11-26 16:20:17 -080019#undef ERROR
Comran Morshed41ed7c22015-11-04 21:03:37 +000020
21#include "aos/common/logging/logging.h"
22#include "aos/common/logging/queue_logging.h"
23#include "aos/common/time.h"
24#include "aos/common/util/log_interval.h"
25#include "aos/common/util/phased_loop.h"
26#include "aos/common/util/wrapping_counter.h"
27#include "aos/common/stl_mutex.h"
28#include "aos/linux_code/init.h"
29#include "aos/common/messages/robot_state.q.h"
30
31#include "y2014_bot3/control_loops/drivetrain/drivetrain.q.h"
32#include "y2014_bot3/control_loops/rollers/rollers.q.h"
33#include "y2014_bot3/autonomous/auto.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050034#include "y2014_bot3/control_loops/drivetrain/drivetrain.h"
35#include "y2014_bot3/control_loops/rollers/rollers.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000036
37#include "frc971/wpilib/joystick_sender.h"
38#include "frc971/wpilib/loop_output_handler.h"
39#include "frc971/wpilib/buffered_solenoid.h"
40#include "frc971/wpilib/buffered_pcm.h"
41#include "frc971/wpilib/gyro_sender.h"
42#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050043#include "frc971/wpilib/wpilib_interface.h"
Brian Silverman425492b2015-12-30 15:23:55 -080044#include "frc971/wpilib/pdp_fetcher.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000045
46#ifndef M_PI
47#define M_PI 3.14159265358979323846
48#endif
49
50using ::aos::util::SimpleLogInterval;
51using ::y2014_bot3::control_loops::drivetrain_queue;
52using ::y2014_bot3::control_loops::rollers_queue;
53using ::frc971::wpilib::BufferedPcm;
54using ::frc971::wpilib::BufferedSolenoid;
55using ::frc971::wpilib::LoopOutputHandler;
56using ::frc971::wpilib::JoystickSender;
57using ::frc971::wpilib::GyroSender;
58
59namespace frc971 {
60namespace wpilib {
61
62double drivetrain_translate(int32_t in) {
63 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
64 ::y2014_bot3::control_loops::kDrivetrainEncoderRatio *
65 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
66}
67
Brian Silverman51091a02015-12-26 15:56:58 -080068double drivetrain_velocity_translate(double in) {
69 return (1.0 / in) / 256.0 /*cpr*/ *
70 ::y2014_bot3::control_loops::kDrivetrainEncoderRatio *
71 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
72}
73
Comran Morshed41ed7c22015-11-04 21:03:37 +000074// Reads in our inputs. (sensors, voltages, etc.)
75class SensorReader {
76 public:
Brian Silverman425492b2015-12-30 15:23:55 -080077 SensorReader(::frc971::wpilib::PDPFetcher *pdp_fetcher)
78 : pdp_fetcher_(pdp_fetcher) {}
Comran Morshed41ed7c22015-11-04 21:03:37 +000079
80 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
81 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -080082 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Comran Morshed41ed7c22015-11-04 21:03:37 +000083 }
84
85 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
86 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -080087 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Comran Morshed41ed7c22015-11-04 21:03:37 +000088 }
89
90 void operator()() {
91 ::aos::SetCurrentThreadName("SensorReader");
Comran Morshed41ed7c22015-11-04 21:03:37 +000092
93 my_pid_ = getpid();
Austin Schuhbc9a2ab2015-11-26 16:20:17 -080094 ds_ =
95#ifdef WPILIB2015
96 DriverStation::GetInstance();
97#else
98 &DriverStation::GetInstance();
99#endif
Comran Morshed41ed7c22015-11-04 21:03:37 +0000100
Brian Silverman5090c432016-01-02 14:44:26 -0800101 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
102 ::aos::time::Time::InMS(4));
103
104 ::aos::SetCurrentThreadRealtimePriority(40);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000105 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800106 {
107 const int iterations = phased_loop.SleepUntilNext();
108 if (iterations != 1) {
109 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
110 }
111 }
Comran Morshed41ed7c22015-11-04 21:03:37 +0000112 RunIteration();
113 }
114 }
115
116 void RunIteration() {
Brian Silverman425492b2015-12-30 15:23:55 -0800117 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_fetcher_);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000118
119 // Drivetrain
120 {
121 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
122 drivetrain_message->right_encoder =
123 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
124 drivetrain_message->left_encoder =
125 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800126 drivetrain_message->left_speed =
127 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
128 drivetrain_message->right_speed =
129 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Comran Morshed41ed7c22015-11-04 21:03:37 +0000130
131 drivetrain_message.Send();
132 }
133
134 // Rollers
135 {
136 auto rollers_message = rollers_queue.position.MakeMessage();
137 rollers_message.Send();
138 }
139 }
140
141 void Quit() { run_ = false; }
142
143 private:
Comran Morshed41ed7c22015-11-04 21:03:37 +0000144 int32_t my_pid_;
145 DriverStation *ds_;
Brian Silverman425492b2015-12-30 15:23:55 -0800146 ::frc971::wpilib::PDPFetcher *const pdp_fetcher_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000147
148 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
149 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
150
151 ::std::atomic<bool> run_{true};
152};
153
154// Writes out our pneumatic outputs.
155class SolenoidWriter {
156 public:
157 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
158 : pcm_(pcm),
159 drivetrain_(".y2014_bot3.control_loops.drivetrain_queue.output"),
160 rollers_(".y2014_bot3.control_loops.rollers_queue.output") {}
161
162 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
163 pressure_switch_ = ::std::move(pressure_switch);
164 }
165
166 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
167 compressor_relay_ = ::std::move(compressor_relay);
168 }
169
170 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
171 drivetrain_left_ = ::std::move(s);
172 }
173
174 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
175 drivetrain_right_ = ::std::move(s);
176 }
177
178 void set_rollers_front(::std::unique_ptr<BufferedSolenoid> s) {
179 rollers_front_ = ::std::move(s);
180 }
181
182 void set_rollers_back(::std::unique_ptr<BufferedSolenoid> s) {
183 rollers_back_ = ::std::move(s);
184 }
185
186 void operator()() {
187 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800188 ::aos::SetCurrentThreadRealtimePriority(27);
189
190 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(20),
191 ::aos::time::Time::InMS(1));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000192
193 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800194 {
195 const int iterations = phased_loop.SleepUntilNext();
196 if (iterations != 1) {
197 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
198 }
199 }
200
Comran Morshed41ed7c22015-11-04 21:03:37 +0000201 // Drivetrain
202 {
203 drivetrain_.FetchLatest();
204 if (drivetrain_.get()) {
205 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
206 drivetrain_left_->Set(drivetrain_->left_high);
207 drivetrain_right_->Set(drivetrain_->right_high);
208 }
209 }
210
211 // Intake
212 {
213 rollers_.FetchLatest();
214 if (rollers_.get()) {
215 LOG_STRUCT(DEBUG, "solenoids", *rollers_);
216 rollers_front_->Set(rollers_->front_extended);
217 rollers_back_->Set(rollers_->back_extended);
218 }
219 }
220
221 // Compressor
222 ::aos::joystick_state.FetchLatest();
223 {
224 ::frc971::wpilib::PneumaticsToLog to_log;
225 {
226 // Refill if pneumatic pressure goes too low.
227 const bool compressor_on = !pressure_switch_->Get();
228 to_log.compressor_on = compressor_on;
229 if (compressor_on) {
230 compressor_relay_->Set(Relay::kForward);
231 } else {
232 compressor_relay_->Set(Relay::kOff);
233 }
234 }
235
236 pcm_->Flush();
237 to_log.read_solenoids = pcm_->GetAll();
238 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
239 }
240 }
241 }
242
243 void Quit() { run_ = false; }
244
245 private:
246 const ::std::unique_ptr<BufferedPcm> &pcm_;
247
248 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_, drivetrain_right_;
249 ::std::unique_ptr<BufferedSolenoid> rollers_front_, rollers_back_;
250
251 ::std::unique_ptr<DigitalInput> pressure_switch_;
252 ::std::unique_ptr<Relay> compressor_relay_;
253
254 ::aos::Queue<::y2014_bot3::control_loops::DrivetrainQueue::Output>
255 drivetrain_;
256 ::aos::Queue<::y2014_bot3::control_loops::RollersQueue::Output> rollers_;
257
258 ::std::atomic<bool> run_{true};
259};
260
261// Writes out drivetrain voltages.
262class DrivetrainWriter : public LoopOutputHandler {
263 public:
264 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
265 left_drivetrain_talon_ = ::std::move(t);
266 }
267
268 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
269 right_drivetrain_talon_ = ::std::move(t);
270 }
271
272 private:
273 virtual void Read() override {
274 ::y2014_bot3::control_loops::drivetrain_queue.output.FetchAnother();
275 }
276
277 virtual void Write() override {
278 auto &queue = ::y2014_bot3::control_loops::drivetrain_queue.output;
279 LOG_STRUCT(DEBUG, "will output", *queue);
280 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
281 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
282 }
283
284 virtual void Stop() override {
285 LOG(WARNING, "drivetrain output too old\n");
286 left_drivetrain_talon_->Disable();
287 right_drivetrain_talon_->Disable();
288 }
289
290 ::std::unique_ptr<Talon> left_drivetrain_talon_;
291 ::std::unique_ptr<Talon> right_drivetrain_talon_;
292};
293
294// Writes out rollers voltages.
295class RollersWriter : public LoopOutputHandler {
296 public:
297 void set_rollers_front_intake_talon(::std::unique_ptr<Talon> t_left, ::std::unique_ptr<Talon> t_right) {
298 rollers_front_left_intake_talon_ = ::std::move(t_left);
299 rollers_front_right_intake_talon_ = ::std::move(t_right);
300 }
301
302 void set_rollers_back_intake_talon(::std::unique_ptr<Talon> t_left, ::std::unique_ptr<Talon> t_right) {
303 rollers_back_left_intake_talon_ = ::std::move(t_left);
304 rollers_back_right_intake_talon_ = ::std::move(t_right);
305 }
306
307 void set_rollers_low_goal_talon(::std::unique_ptr<Talon> t) {
308 rollers_low_goal_talon_ = ::std::move(t);
309 }
310
311 private:
312 virtual void Read() override {
313 ::y2014_bot3::control_loops::rollers_queue.output.FetchAnother();
314 }
315
316 virtual void Write() override {
317 auto &queue = ::y2014_bot3::control_loops::rollers_queue.output;
318 LOG_STRUCT(DEBUG, "will output", *queue);
319 rollers_front_left_intake_talon_->Set(queue->front_intake_voltage / 12.0);
320 rollers_front_right_intake_talon_->Set(-(queue->front_intake_voltage / 12.0));
321 rollers_back_left_intake_talon_->Set(queue->back_intake_voltage / 12.0);
322 rollers_back_right_intake_talon_->Set(-(queue->back_intake_voltage / 12.0));
323 rollers_low_goal_talon_->Set(queue->low_goal_voltage / 12.0);
324 }
325
326 virtual void Stop() override {
327 LOG(WARNING, "Intake output too old\n");
328 rollers_front_left_intake_talon_->Disable();
329 rollers_front_right_intake_talon_->Disable();
330 rollers_back_left_intake_talon_->Disable();
331 rollers_back_right_intake_talon_->Disable();
332 rollers_low_goal_talon_->Disable();
333 }
334
335 ::std::unique_ptr<Talon> rollers_front_left_intake_talon_,
336 rollers_back_left_intake_talon_, rollers_front_right_intake_talon_,
337 rollers_back_right_intake_talon_, rollers_low_goal_talon_;
338};
339
340// TODO(brian): Replace this with ::std::make_unique once all our toolchains
341// have support.
342template <class T, class... U>
343std::unique_ptr<T> make_unique(U &&... u) {
344 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
345}
346
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800347class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000348 public:
349 ::std::unique_ptr<Encoder> make_encoder(int index) {
350 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
351 Encoder::k4X);
352 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800353 void Run() override {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000354 ::aos::InitNRT();
355 ::aos::SetCurrentThreadName("StartCompetition");
356
357 JoystickSender joystick_sender;
358 ::std::thread joystick_thread(::std::ref(joystick_sender));
359
Brian Silverman425492b2015-12-30 15:23:55 -0800360 ::frc971::wpilib::PDPFetcher pdp_fetcher;
361 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
362
Comran Morshed41ed7c22015-11-04 21:03:37 +0000363 //TODO(comran): IO ports are placeholders at the moment, so match them to
364 // the robot before turning on.
365
366 // Sensors
Brian Silverman425492b2015-12-30 15:23:55 -0800367 SensorReader reader(&pdp_fetcher);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000368 reader.set_drivetrain_left_encoder(make_encoder(4));
369 reader.set_drivetrain_right_encoder(make_encoder(5));
370
371 ::std::thread reader_thread(::std::ref(reader));
372 GyroSender gyro_sender;
373 ::std::thread gyro_thread(::std::ref(gyro_sender));
374
375 // Outputs
376 DrivetrainWriter drivetrain_writer;
377 drivetrain_writer.set_left_drivetrain_talon(
378 ::std::unique_ptr<Talon>(new Talon(2)));
379 drivetrain_writer.set_right_drivetrain_talon(
380 ::std::unique_ptr<Talon>(new Talon(5)));
381 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
382
383 RollersWriter rollers_writer;
384 rollers_writer.set_rollers_front_intake_talon(
385 ::std::unique_ptr<Talon>(new Talon(3)), ::std::unique_ptr<Talon>(new Talon(7)));
386 rollers_writer.set_rollers_back_intake_talon(
387 ::std::unique_ptr<Talon>(new Talon(1)), ::std::unique_ptr<Talon>(new Talon(6)));
388
389 rollers_writer.set_rollers_low_goal_talon(
390 ::std::unique_ptr<Talon>(new Talon(4)));
391 ::std::thread rollers_writer_thread(::std::ref(rollers_writer));
392
393 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
394 new ::frc971::wpilib::BufferedPcm());
395 SolenoidWriter solenoid_writer(pcm);
396 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
397 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(5));
398 solenoid_writer.set_rollers_front(pcm->MakeSolenoid(2));
399 solenoid_writer.set_rollers_back(pcm->MakeSolenoid(4));
400
401 // Don't change the following IDs.
402 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
403 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
404 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
405
406 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800407 while (true) {
408 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
409 if (r != 0) {
410 PLOG(WARNING, "infinite select failed");
411 } else {
412 PLOG(WARNING, "infinite select succeeded??\n");
413 }
414 }
Comran Morshed41ed7c22015-11-04 21:03:37 +0000415
416 LOG(ERROR, "Exiting WPILibRobot\n");
417
418 joystick_sender.Quit();
419 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800420 pdp_fetcher.Quit();
421 pdp_fetcher_thread.join();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000422 reader.Quit();
423 reader_thread.join();
424 gyro_sender.Quit();
425 gyro_thread.join();
426
427 drivetrain_writer.Quit();
428 drivetrain_writer_thread.join();
429
430 rollers_writer.Quit();
431 rollers_writer_thread.join();
432
433 solenoid_writer.Quit();
434 solenoid_thread.join();
435
436 ::aos::Cleanup();
437 }
438};
439
440} // namespace wpilib
441} // namespace frc971
442
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800443AOS_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);