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