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