blob: 4dc421d5b2fbb9fabd4c0170a24cdd4bf5c72c74 [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"
18#include "ControllerPower.h"
19#include "DigitalInput.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"
35
36#include "frc971/wpilib/joystick_sender.h"
37#include "frc971/wpilib/loop_output_handler.h"
38#include "frc971/wpilib/buffered_solenoid.h"
39#include "frc971/wpilib/buffered_pcm.h"
40#include "frc971/wpilib/gyro_sender.h"
41#include "frc971/wpilib/logging.q.h"
42#include "y2014_bot3/control_loops/drivetrain/drivetrain.h"
43#include "y2014_bot3/control_loops/rollers/rollers.h"
44
45#ifndef M_PI
46#define M_PI 3.14159265358979323846
47#endif
48
49using ::aos::util::SimpleLogInterval;
50using ::y2014_bot3::control_loops::drivetrain_queue;
51using ::y2014_bot3::control_loops::rollers_queue;
52using ::frc971::wpilib::BufferedPcm;
53using ::frc971::wpilib::BufferedSolenoid;
54using ::frc971::wpilib::LoopOutputHandler;
55using ::frc971::wpilib::JoystickSender;
56using ::frc971::wpilib::GyroSender;
57
58namespace frc971 {
59namespace wpilib {
60
61double drivetrain_translate(int32_t in) {
62 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
63 ::y2014_bot3::control_loops::kDrivetrainEncoderRatio *
64 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
65}
66
67// Reads in our inputs. (sensors, voltages, etc.)
68class SensorReader {
69 public:
70 SensorReader() {}
71
72 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
73 drivetrain_left_encoder_ = ::std::move(encoder);
74 }
75
76 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
77 drivetrain_right_encoder_ = ::std::move(encoder);
78 }
79
80 void operator()() {
81 ::aos::SetCurrentThreadName("SensorReader");
82 LOG(INFO, "In sensor reader thread\n");
83
84 my_pid_ = getpid();
Austin Schuhbc9a2ab2015-11-26 16:20:17 -080085 ds_ =
86#ifdef WPILIB2015
87 DriverStation::GetInstance();
88#else
89 &DriverStation::GetInstance();
90#endif
Comran Morshed41ed7c22015-11-04 21:03:37 +000091
92 LOG(INFO, "Things are now started\n");
93
94 ::aos::SetCurrentThreadRealtimePriority(kPriority);
95 while (run_) {
96 ::aos::time::PhasedLoopXMS(5, 4000);
97 RunIteration();
98 }
99 }
100
101 void RunIteration() {
102 // General
103 {
104 auto new_state = ::aos::robot_state.MakeMessage();
105
106 new_state->reader_pid = my_pid_;
107 new_state->outputs_enabled = ds_->IsSysActive();
108 new_state->browned_out = ds_->IsSysBrownedOut();
109
110 new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
111 new_state->is_5v_active = ControllerPower::GetEnabled5V();
112 new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
113 new_state->voltage_5v = ControllerPower::GetVoltage5V();
114
115 new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
116 new_state->voltage_battery = ds_->GetBatteryVoltage();
117
118 LOG_STRUCT(DEBUG, "robot_state", *new_state);
119
120 new_state.Send();
121 }
122
123 // Drivetrain
124 {
125 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
126 drivetrain_message->right_encoder =
127 drivetrain_translate(drivetrain_right_encoder_->GetRaw());
128 drivetrain_message->left_encoder =
129 -drivetrain_translate(drivetrain_left_encoder_->GetRaw());
130
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:
144 static const int kPriority = 30;
145 static const int kInterruptPriority = 55;
146
147 int32_t my_pid_;
148 DriverStation *ds_;
149
150 ::std::unique_ptr<Encoder> drivetrain_left_encoder_;
151 ::std::unique_ptr<Encoder> drivetrain_right_encoder_;
152
153 ::std::atomic<bool> run_{true};
154};
155
156// Writes out our pneumatic outputs.
157class SolenoidWriter {
158 public:
159 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
160 : pcm_(pcm),
161 drivetrain_(".y2014_bot3.control_loops.drivetrain_queue.output"),
162 rollers_(".y2014_bot3.control_loops.rollers_queue.output") {}
163
164 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
165 pressure_switch_ = ::std::move(pressure_switch);
166 }
167
168 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
169 compressor_relay_ = ::std::move(compressor_relay);
170 }
171
172 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
173 drivetrain_left_ = ::std::move(s);
174 }
175
176 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
177 drivetrain_right_ = ::std::move(s);
178 }
179
180 void set_rollers_front(::std::unique_ptr<BufferedSolenoid> s) {
181 rollers_front_ = ::std::move(s);
182 }
183
184 void set_rollers_back(::std::unique_ptr<BufferedSolenoid> s) {
185 rollers_back_ = ::std::move(s);
186 }
187
188 void operator()() {
189 ::aos::SetCurrentThreadName("Solenoids");
190 ::aos::SetCurrentThreadRealtimePriority(30);
191
192 while (run_) {
193 ::aos::time::PhasedLoopXMS(20, 1000);
194 // Drivetrain
195 {
196 drivetrain_.FetchLatest();
197 if (drivetrain_.get()) {
198 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
199 drivetrain_left_->Set(drivetrain_->left_high);
200 drivetrain_right_->Set(drivetrain_->right_high);
201 }
202 }
203
204 // Intake
205 {
206 rollers_.FetchLatest();
207 if (rollers_.get()) {
208 LOG_STRUCT(DEBUG, "solenoids", *rollers_);
209 rollers_front_->Set(rollers_->front_extended);
210 rollers_back_->Set(rollers_->back_extended);
211 }
212 }
213
214 // Compressor
215 ::aos::joystick_state.FetchLatest();
216 {
217 ::frc971::wpilib::PneumaticsToLog to_log;
218 {
219 // Refill if pneumatic pressure goes too low.
220 const bool compressor_on = !pressure_switch_->Get();
221 to_log.compressor_on = compressor_on;
222 if (compressor_on) {
223 compressor_relay_->Set(Relay::kForward);
224 } else {
225 compressor_relay_->Set(Relay::kOff);
226 }
227 }
228
229 pcm_->Flush();
230 to_log.read_solenoids = pcm_->GetAll();
231 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
232 }
233 }
234 }
235
236 void Quit() { run_ = false; }
237
238 private:
239 const ::std::unique_ptr<BufferedPcm> &pcm_;
240
241 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_, drivetrain_right_;
242 ::std::unique_ptr<BufferedSolenoid> rollers_front_, rollers_back_;
243
244 ::std::unique_ptr<DigitalInput> pressure_switch_;
245 ::std::unique_ptr<Relay> compressor_relay_;
246
247 ::aos::Queue<::y2014_bot3::control_loops::DrivetrainQueue::Output>
248 drivetrain_;
249 ::aos::Queue<::y2014_bot3::control_loops::RollersQueue::Output> rollers_;
250
251 ::std::atomic<bool> run_{true};
252};
253
254// Writes out drivetrain voltages.
255class DrivetrainWriter : public LoopOutputHandler {
256 public:
257 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
258 left_drivetrain_talon_ = ::std::move(t);
259 }
260
261 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
262 right_drivetrain_talon_ = ::std::move(t);
263 }
264
265 private:
266 virtual void Read() override {
267 ::y2014_bot3::control_loops::drivetrain_queue.output.FetchAnother();
268 }
269
270 virtual void Write() override {
271 auto &queue = ::y2014_bot3::control_loops::drivetrain_queue.output;
272 LOG_STRUCT(DEBUG, "will output", *queue);
273 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
274 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
275 }
276
277 virtual void Stop() override {
278 LOG(WARNING, "drivetrain output too old\n");
279 left_drivetrain_talon_->Disable();
280 right_drivetrain_talon_->Disable();
281 }
282
283 ::std::unique_ptr<Talon> left_drivetrain_talon_;
284 ::std::unique_ptr<Talon> right_drivetrain_talon_;
285};
286
287// Writes out rollers voltages.
288class RollersWriter : public LoopOutputHandler {
289 public:
290 void set_rollers_front_intake_talon(::std::unique_ptr<Talon> t_left, ::std::unique_ptr<Talon> t_right) {
291 rollers_front_left_intake_talon_ = ::std::move(t_left);
292 rollers_front_right_intake_talon_ = ::std::move(t_right);
293 }
294
295 void set_rollers_back_intake_talon(::std::unique_ptr<Talon> t_left, ::std::unique_ptr<Talon> t_right) {
296 rollers_back_left_intake_talon_ = ::std::move(t_left);
297 rollers_back_right_intake_talon_ = ::std::move(t_right);
298 }
299
300 void set_rollers_low_goal_talon(::std::unique_ptr<Talon> t) {
301 rollers_low_goal_talon_ = ::std::move(t);
302 }
303
304 private:
305 virtual void Read() override {
306 ::y2014_bot3::control_loops::rollers_queue.output.FetchAnother();
307 }
308
309 virtual void Write() override {
310 auto &queue = ::y2014_bot3::control_loops::rollers_queue.output;
311 LOG_STRUCT(DEBUG, "will output", *queue);
312 rollers_front_left_intake_talon_->Set(queue->front_intake_voltage / 12.0);
313 rollers_front_right_intake_talon_->Set(-(queue->front_intake_voltage / 12.0));
314 rollers_back_left_intake_talon_->Set(queue->back_intake_voltage / 12.0);
315 rollers_back_right_intake_talon_->Set(-(queue->back_intake_voltage / 12.0));
316 rollers_low_goal_talon_->Set(queue->low_goal_voltage / 12.0);
317 }
318
319 virtual void Stop() override {
320 LOG(WARNING, "Intake output too old\n");
321 rollers_front_left_intake_talon_->Disable();
322 rollers_front_right_intake_talon_->Disable();
323 rollers_back_left_intake_talon_->Disable();
324 rollers_back_right_intake_talon_->Disable();
325 rollers_low_goal_talon_->Disable();
326 }
327
328 ::std::unique_ptr<Talon> rollers_front_left_intake_talon_,
329 rollers_back_left_intake_talon_, rollers_front_right_intake_talon_,
330 rollers_back_right_intake_talon_, rollers_low_goal_talon_;
331};
332
333// TODO(brian): Replace this with ::std::make_unique once all our toolchains
334// have support.
335template <class T, class... U>
336std::unique_ptr<T> make_unique(U &&... u) {
337 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
338}
339
340class WPILibRobot : public RobotBase {
341 public:
342 ::std::unique_ptr<Encoder> make_encoder(int index) {
343 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
344 Encoder::k4X);
345 }
346 virtual void StartCompetition() {
347 ::aos::InitNRT();
348 ::aos::SetCurrentThreadName("StartCompetition");
349
350 JoystickSender joystick_sender;
351 ::std::thread joystick_thread(::std::ref(joystick_sender));
352
353 //TODO(comran): IO ports are placeholders at the moment, so match them to
354 // the robot before turning on.
355
356 // Sensors
357 SensorReader reader;
358 LOG(INFO, "Creating the reader\n");
359 reader.set_drivetrain_left_encoder(make_encoder(4));
360 reader.set_drivetrain_right_encoder(make_encoder(5));
361
362 ::std::thread reader_thread(::std::ref(reader));
363 GyroSender gyro_sender;
364 ::std::thread gyro_thread(::std::ref(gyro_sender));
365
366 // Outputs
367 DrivetrainWriter drivetrain_writer;
368 drivetrain_writer.set_left_drivetrain_talon(
369 ::std::unique_ptr<Talon>(new Talon(2)));
370 drivetrain_writer.set_right_drivetrain_talon(
371 ::std::unique_ptr<Talon>(new Talon(5)));
372 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
373
374 RollersWriter rollers_writer;
375 rollers_writer.set_rollers_front_intake_talon(
376 ::std::unique_ptr<Talon>(new Talon(3)), ::std::unique_ptr<Talon>(new Talon(7)));
377 rollers_writer.set_rollers_back_intake_talon(
378 ::std::unique_ptr<Talon>(new Talon(1)), ::std::unique_ptr<Talon>(new Talon(6)));
379
380 rollers_writer.set_rollers_low_goal_talon(
381 ::std::unique_ptr<Talon>(new Talon(4)));
382 ::std::thread rollers_writer_thread(::std::ref(rollers_writer));
383
384 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
385 new ::frc971::wpilib::BufferedPcm());
386 SolenoidWriter solenoid_writer(pcm);
387 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
388 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(5));
389 solenoid_writer.set_rollers_front(pcm->MakeSolenoid(2));
390 solenoid_writer.set_rollers_back(pcm->MakeSolenoid(4));
391
392 // Don't change the following IDs.
393 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
394 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
395 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
396
397 // Wait forever. Not much else to do...
398 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
399
400 LOG(ERROR, "Exiting WPILibRobot\n");
401
402 joystick_sender.Quit();
403 joystick_thread.join();
404 reader.Quit();
405 reader_thread.join();
406 gyro_sender.Quit();
407 gyro_thread.join();
408
409 drivetrain_writer.Quit();
410 drivetrain_writer_thread.join();
411
412 rollers_writer.Quit();
413 rollers_writer_thread.join();
414
415 solenoid_writer.Quit();
416 solenoid_thread.join();
417
418 ::aos::Cleanup();
419 }
420};
421
422} // namespace wpilib
423} // namespace frc971
424
425START_ROBOT_CLASS(::frc971::wpilib::WPILibRobot);