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