blob: 36dd92688f2e8c5dcf264ef525f4d8db21144f41 [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"
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -050019#include "PowerDistributionPanel.h"
Austin Schuhbc9a2ab2015-11-26 16:20:17 -080020#undef ERROR
Comran Morshed41ed7c22015-11-04 21:03:37 +000021
22#include "aos/common/logging/logging.h"
23#include "aos/common/logging/queue_logging.h"
24#include "aos/common/time.h"
25#include "aos/common/util/log_interval.h"
26#include "aos/common/util/phased_loop.h"
27#include "aos/common/util/wrapping_counter.h"
28#include "aos/common/stl_mutex.h"
29#include "aos/linux_code/init.h"
30#include "aos/common/messages/robot_state.q.h"
31
32#include "y2014_bot3/control_loops/drivetrain/drivetrain.q.h"
33#include "y2014_bot3/control_loops/rollers/rollers.q.h"
34#include "y2014_bot3/autonomous/auto.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050035#include "y2014_bot3/control_loops/drivetrain/drivetrain.h"
36#include "y2014_bot3/control_loops/rollers/rollers.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000037
38#include "frc971/wpilib/joystick_sender.h"
39#include "frc971/wpilib/loop_output_handler.h"
40#include "frc971/wpilib/buffered_solenoid.h"
41#include "frc971/wpilib/buffered_pcm.h"
42#include "frc971/wpilib/gyro_sender.h"
43#include "frc971/wpilib/logging.q.h"
Brian Silverman811f8ec2015-12-06 01:29:42 -050044#include "frc971/wpilib/wpilib_interface.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:
77 SensorReader() {}
78
79 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
80 drivetrain_left_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -080081 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Comran Morshed41ed7c22015-11-04 21:03:37 +000082 }
83
84 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
85 drivetrain_right_encoder_ = ::std::move(encoder);
Brian Silverman51091a02015-12-26 15:56:58 -080086 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Comran Morshed41ed7c22015-11-04 21:03:37 +000087 }
88
89 void operator()() {
90 ::aos::SetCurrentThreadName("SensorReader");
Comran Morshed41ed7c22015-11-04 21:03:37 +000091
92 my_pid_ = getpid();
Austin Schuhbc9a2ab2015-11-26 16:20:17 -080093 ds_ =
94#ifdef WPILIB2015
95 DriverStation::GetInstance();
96#else
97 &DriverStation::GetInstance();
98#endif
Brian Silvermanc7e8fdd2015-12-06 02:48:27 -050099 pdp_.reset(new PowerDistributionPanel());
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 Silvermanc7e8fdd2015-12-06 02:48:27 -0500109 ::frc971::wpilib::SendRobotState(my_pid_, ds_, pdp_.get());
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 Silvermanc7e8fdd2015-12-06 02:48:27 -0500141 ::std::unique_ptr<PowerDistributionPanel> pdp_;
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
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800333class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000334 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 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800339 void Run() override {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000340 ::aos::InitNRT();
341 ::aos::SetCurrentThreadName("StartCompetition");
342
343 JoystickSender joystick_sender;
344 ::std::thread joystick_thread(::std::ref(joystick_sender));
345
346 //TODO(comran): IO ports are placeholders at the moment, so match them to
347 // the robot before turning on.
348
349 // Sensors
350 SensorReader reader;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000351 reader.set_drivetrain_left_encoder(make_encoder(4));
352 reader.set_drivetrain_right_encoder(make_encoder(5));
353
354 ::std::thread reader_thread(::std::ref(reader));
355 GyroSender gyro_sender;
356 ::std::thread gyro_thread(::std::ref(gyro_sender));
357
358 // Outputs
359 DrivetrainWriter drivetrain_writer;
360 drivetrain_writer.set_left_drivetrain_talon(
361 ::std::unique_ptr<Talon>(new Talon(2)));
362 drivetrain_writer.set_right_drivetrain_talon(
363 ::std::unique_ptr<Talon>(new Talon(5)));
364 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
365
366 RollersWriter rollers_writer;
367 rollers_writer.set_rollers_front_intake_talon(
368 ::std::unique_ptr<Talon>(new Talon(3)), ::std::unique_ptr<Talon>(new Talon(7)));
369 rollers_writer.set_rollers_back_intake_talon(
370 ::std::unique_ptr<Talon>(new Talon(1)), ::std::unique_ptr<Talon>(new Talon(6)));
371
372 rollers_writer.set_rollers_low_goal_talon(
373 ::std::unique_ptr<Talon>(new Talon(4)));
374 ::std::thread rollers_writer_thread(::std::ref(rollers_writer));
375
376 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
377 new ::frc971::wpilib::BufferedPcm());
378 SolenoidWriter solenoid_writer(pcm);
379 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
380 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(5));
381 solenoid_writer.set_rollers_front(pcm->MakeSolenoid(2));
382 solenoid_writer.set_rollers_back(pcm->MakeSolenoid(4));
383
384 // Don't change the following IDs.
385 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
386 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
387 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
388
389 // Wait forever. Not much else to do...
390 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
391
392 LOG(ERROR, "Exiting WPILibRobot\n");
393
394 joystick_sender.Quit();
395 joystick_thread.join();
396 reader.Quit();
397 reader_thread.join();
398 gyro_sender.Quit();
399 gyro_thread.join();
400
401 drivetrain_writer.Quit();
402 drivetrain_writer_thread.join();
403
404 rollers_writer.Quit();
405 rollers_writer_thread.join();
406
407 solenoid_writer.Quit();
408 solenoid_thread.join();
409
410 ::aos::Cleanup();
411 }
412};
413
414} // namespace wpilib
415} // namespace frc971
416
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800417AOS_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);