blob: 3b6c8b1656b7106d2a5a1a4e4fea98977dd5256a [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"
16#include "RobotBase.h"
17#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
Comran Morshed41ed7c22015-11-04 21:03:37 +0000101 ::aos::SetCurrentThreadRealtimePriority(kPriority);
102 while (run_) {
103 ::aos::time::PhasedLoopXMS(5, 4000);
104 RunIteration();
105 }
106 }
107
108 void RunIteration() {
Brian Silverman425492b2015-12-30 15:23:55 -0800109 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_fetcher_);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000110
111 // Drivetrain
112 {
113 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
114 drivetrain_message->right_encoder =
115 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
116 drivetrain_message->left_encoder =
117 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -0800118 drivetrain_message->left_speed =
119 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
120 drivetrain_message->right_speed =
121 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Comran Morshed41ed7c22015-11-04 21:03:37 +0000122
123 drivetrain_message.Send();
124 }
125
126 // Rollers
127 {
128 auto rollers_message = rollers_queue.position.MakeMessage();
129 rollers_message.Send();
130 }
131 }
132
133 void Quit() { run_ = false; }
134
135 private:
136 static const int kPriority = 30;
137 static const int kInterruptPriority = 55;
138
139 int32_t my_pid_;
140 DriverStation *ds_;
Brian Silverman425492b2015-12-30 15:23:55 -0800141 ::frc971::wpilib::PDPFetcher *const pdp_fetcher_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000142
143 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
144 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
145
146 ::std::atomic<bool> run_{true};
147};
148
149// Writes out our pneumatic outputs.
150class SolenoidWriter {
151 public:
152 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
153 : pcm_(pcm),
154 drivetrain_(".y2014_bot3.control_loops.drivetrain_queue.output"),
155 rollers_(".y2014_bot3.control_loops.rollers_queue.output") {}
156
157 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
158 pressure_switch_ = ::std::move(pressure_switch);
159 }
160
161 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
162 compressor_relay_ = ::std::move(compressor_relay);
163 }
164
165 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
166 drivetrain_left_ = ::std::move(s);
167 }
168
169 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
170 drivetrain_right_ = ::std::move(s);
171 }
172
173 void set_rollers_front(::std::unique_ptr<BufferedSolenoid> s) {
174 rollers_front_ = ::std::move(s);
175 }
176
177 void set_rollers_back(::std::unique_ptr<BufferedSolenoid> s) {
178 rollers_back_ = ::std::move(s);
179 }
180
181 void operator()() {
182 ::aos::SetCurrentThreadName("Solenoids");
183 ::aos::SetCurrentThreadRealtimePriority(30);
184
185 while (run_) {
186 ::aos::time::PhasedLoopXMS(20, 1000);
187 // Drivetrain
188 {
189 drivetrain_.FetchLatest();
190 if (drivetrain_.get()) {
191 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
192 drivetrain_left_->Set(drivetrain_->left_high);
193 drivetrain_right_->Set(drivetrain_->right_high);
194 }
195 }
196
197 // Intake
198 {
199 rollers_.FetchLatest();
200 if (rollers_.get()) {
201 LOG_STRUCT(DEBUG, "solenoids", *rollers_);
202 rollers_front_->Set(rollers_->front_extended);
203 rollers_back_->Set(rollers_->back_extended);
204 }
205 }
206
207 // Compressor
208 ::aos::joystick_state.FetchLatest();
209 {
210 ::frc971::wpilib::PneumaticsToLog to_log;
211 {
212 // Refill if pneumatic pressure goes too low.
213 const bool compressor_on = !pressure_switch_->Get();
214 to_log.compressor_on = compressor_on;
215 if (compressor_on) {
216 compressor_relay_->Set(Relay::kForward);
217 } else {
218 compressor_relay_->Set(Relay::kOff);
219 }
220 }
221
222 pcm_->Flush();
223 to_log.read_solenoids = pcm_->GetAll();
224 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
225 }
226 }
227 }
228
229 void Quit() { run_ = false; }
230
231 private:
232 const ::std::unique_ptr<BufferedPcm> &pcm_;
233
234 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_, drivetrain_right_;
235 ::std::unique_ptr<BufferedSolenoid> rollers_front_, rollers_back_;
236
237 ::std::unique_ptr<DigitalInput> pressure_switch_;
238 ::std::unique_ptr<Relay> compressor_relay_;
239
240 ::aos::Queue<::y2014_bot3::control_loops::DrivetrainQueue::Output>
241 drivetrain_;
242 ::aos::Queue<::y2014_bot3::control_loops::RollersQueue::Output> rollers_;
243
244 ::std::atomic<bool> run_{true};
245};
246
247// Writes out drivetrain voltages.
248class DrivetrainWriter : public LoopOutputHandler {
249 public:
250 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
251 left_drivetrain_talon_ = ::std::move(t);
252 }
253
254 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
255 right_drivetrain_talon_ = ::std::move(t);
256 }
257
258 private:
259 virtual void Read() override {
260 ::y2014_bot3::control_loops::drivetrain_queue.output.FetchAnother();
261 }
262
263 virtual void Write() override {
264 auto &queue = ::y2014_bot3::control_loops::drivetrain_queue.output;
265 LOG_STRUCT(DEBUG, "will output", *queue);
266 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
267 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
268 }
269
270 virtual void Stop() override {
271 LOG(WARNING, "drivetrain output too old\n");
272 left_drivetrain_talon_->Disable();
273 right_drivetrain_talon_->Disable();
274 }
275
276 ::std::unique_ptr<Talon> left_drivetrain_talon_;
277 ::std::unique_ptr<Talon> right_drivetrain_talon_;
278};
279
280// Writes out rollers voltages.
281class RollersWriter : public LoopOutputHandler {
282 public:
283 void set_rollers_front_intake_talon(::std::unique_ptr<Talon> t_left, ::std::unique_ptr<Talon> t_right) {
284 rollers_front_left_intake_talon_ = ::std::move(t_left);
285 rollers_front_right_intake_talon_ = ::std::move(t_right);
286 }
287
288 void set_rollers_back_intake_talon(::std::unique_ptr<Talon> t_left, ::std::unique_ptr<Talon> t_right) {
289 rollers_back_left_intake_talon_ = ::std::move(t_left);
290 rollers_back_right_intake_talon_ = ::std::move(t_right);
291 }
292
293 void set_rollers_low_goal_talon(::std::unique_ptr<Talon> t) {
294 rollers_low_goal_talon_ = ::std::move(t);
295 }
296
297 private:
298 virtual void Read() override {
299 ::y2014_bot3::control_loops::rollers_queue.output.FetchAnother();
300 }
301
302 virtual void Write() override {
303 auto &queue = ::y2014_bot3::control_loops::rollers_queue.output;
304 LOG_STRUCT(DEBUG, "will output", *queue);
305 rollers_front_left_intake_talon_->Set(queue->front_intake_voltage / 12.0);
306 rollers_front_right_intake_talon_->Set(-(queue->front_intake_voltage / 12.0));
307 rollers_back_left_intake_talon_->Set(queue->back_intake_voltage / 12.0);
308 rollers_back_right_intake_talon_->Set(-(queue->back_intake_voltage / 12.0));
309 rollers_low_goal_talon_->Set(queue->low_goal_voltage / 12.0);
310 }
311
312 virtual void Stop() override {
313 LOG(WARNING, "Intake output too old\n");
314 rollers_front_left_intake_talon_->Disable();
315 rollers_front_right_intake_talon_->Disable();
316 rollers_back_left_intake_talon_->Disable();
317 rollers_back_right_intake_talon_->Disable();
318 rollers_low_goal_talon_->Disable();
319 }
320
321 ::std::unique_ptr<Talon> rollers_front_left_intake_talon_,
322 rollers_back_left_intake_talon_, rollers_front_right_intake_talon_,
323 rollers_back_right_intake_talon_, rollers_low_goal_talon_;
324};
325
326// TODO(brian): Replace this with ::std::make_unique once all our toolchains
327// have support.
328template <class T, class... U>
329std::unique_ptr<T> make_unique(U &&... u) {
330 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
331}
332
333class WPILibRobot : public RobotBase {
334 public:
335 ::std::unique_ptr<Encoder> make_encoder(int index) {
336 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
337 Encoder::k4X);
338 }
339 virtual void StartCompetition() {
340 ::aos::InitNRT();
341 ::aos::SetCurrentThreadName("StartCompetition");
342
343 JoystickSender joystick_sender;
344 ::std::thread joystick_thread(::std::ref(joystick_sender));
345
Brian Silverman425492b2015-12-30 15:23:55 -0800346 ::frc971::wpilib::PDPFetcher pdp_fetcher;
347 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
348
Comran Morshed41ed7c22015-11-04 21:03:37 +0000349 //TODO(comran): IO ports are placeholders at the moment, so match them to
350 // the robot before turning on.
351
352 // Sensors
Brian Silverman425492b2015-12-30 15:23:55 -0800353 SensorReader reader(&pdp_fetcher);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000354 reader.set_drivetrain_left_encoder(make_encoder(4));
355 reader.set_drivetrain_right_encoder(make_encoder(5));
356
357 ::std::thread reader_thread(::std::ref(reader));
358 GyroSender gyro_sender;
359 ::std::thread gyro_thread(::std::ref(gyro_sender));
360
361 // Outputs
362 DrivetrainWriter drivetrain_writer;
363 drivetrain_writer.set_left_drivetrain_talon(
364 ::std::unique_ptr<Talon>(new Talon(2)));
365 drivetrain_writer.set_right_drivetrain_talon(
366 ::std::unique_ptr<Talon>(new Talon(5)));
367 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
368
369 RollersWriter rollers_writer;
370 rollers_writer.set_rollers_front_intake_talon(
371 ::std::unique_ptr<Talon>(new Talon(3)), ::std::unique_ptr<Talon>(new Talon(7)));
372 rollers_writer.set_rollers_back_intake_talon(
373 ::std::unique_ptr<Talon>(new Talon(1)), ::std::unique_ptr<Talon>(new Talon(6)));
374
375 rollers_writer.set_rollers_low_goal_talon(
376 ::std::unique_ptr<Talon>(new Talon(4)));
377 ::std::thread rollers_writer_thread(::std::ref(rollers_writer));
378
379 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
380 new ::frc971::wpilib::BufferedPcm());
381 SolenoidWriter solenoid_writer(pcm);
382 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
383 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(5));
384 solenoid_writer.set_rollers_front(pcm->MakeSolenoid(2));
385 solenoid_writer.set_rollers_back(pcm->MakeSolenoid(4));
386
387 // Don't change the following IDs.
388 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
389 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
390 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
391
392 // Wait forever. Not much else to do...
393 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
394
395 LOG(ERROR, "Exiting WPILibRobot\n");
396
397 joystick_sender.Quit();
398 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800399 pdp_fetcher.Quit();
400 pdp_fetcher_thread.join();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000401 reader.Quit();
402 reader_thread.join();
403 gyro_sender.Quit();
404 gyro_thread.join();
405
406 drivetrain_writer.Quit();
407 drivetrain_writer_thread.join();
408
409 rollers_writer.Quit();
410 rollers_writer_thread.join();
411
412 solenoid_writer.Quit();
413 solenoid_thread.join();
414
415 ::aos::Cleanup();
416 }
417};
418
419} // namespace wpilib
420} // namespace frc971
421
422START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);