blob: 337f5d276a6563c630df51f72f163c342c1a48f0 [file] [log] [blame]
Brian Silvermanc71537c2016-01-01 13:43:14 -08001#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 "SampleRobot.h"
17#include "dma.h"
18#ifndef WPILIB2015
19#include "DigitalGlitchFilter.h"
20#endif
21#include "PowerDistributionPanel.h"
22#undef ERROR
23
24#include "aos/common/logging/logging.h"
25#include "aos/common/logging/queue_logging.h"
26#include "aos/common/time.h"
27#include "aos/common/util/log_interval.h"
28#include "aos/common/util/phased_loop.h"
29#include "aos/common/util/wrapping_counter.h"
30#include "aos/common/stl_mutex.h"
31#include "aos/linux_code/init.h"
32#include "aos/common/messages/robot_state.q.h"
33
34#include "y2012/control_loops/drivetrain/drivetrain.q.h"
35#include "y2012/control_loops/accessories/accessories.q.h"
36
37#include "frc971/wpilib/joystick_sender.h"
38#include "frc971/wpilib/loop_output_handler.h"
39#include "frc971/wpilib/buffered_solenoid.h"
40#include "frc971/wpilib/buffered_pcm.h"
41#include "frc971/wpilib/gyro_sender.h"
42#include "frc971/wpilib/dma_edge_counting.h"
43#include "frc971/wpilib/interrupt_edge_counting.h"
44#include "frc971/wpilib/encoder_and_potentiometer.h"
45#include "frc971/wpilib/logging.q.h"
46#include "frc971/wpilib/wpilib_interface.h"
47
48#ifndef M_PI
49#define M_PI 3.14159265358979323846
50#endif
51
52using ::y2012::control_loops::drivetrain_queue;
53using ::y2012::control_loops::accessories_queue;
54
55namespace y2012 {
56namespace wpilib {
57
58template <class T, class... U>
59std::unique_ptr<T> make_unique(U &&... u) {
60 return std::unique_ptr<T>(new T(std::forward<U>(u)...));
61}
62
63double drivetrain_translate(int32_t in) {
64 return -static_cast<double>(in) /
65 (256.0 /*cpr*/ * 4.0 /*4x*/) *
66 1 *
67 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
68}
69
70double drivetrain_velocity_translate(double in) {
71 return (1.0 / in) / 256.0 /*cpr*/ *
72 1 *
73 (3.5 /*wheel diameter*/ * 2.54 / 100.0 * M_PI) * 2.0 / 2.0;
74}
75
76static const double kMaximumEncoderPulsesPerSecond =
77 5600.0 /* free speed RPM */ * 14.0 / 48.0 /* bottom gear reduction */ *
78 18.0 / 32.0 /* big belt reduction */ *
79 18.0 / 66.0 /* top gear reduction */ * 48.0 / 18.0 /* encoder gears */ /
80 60.0 /* seconds / minute */ * 256.0 /* CPR */;
81
82class SensorReader {
83 public:
84 SensorReader() {}
85
86 void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) {
87 drivetrain_left_encoder_ = ::std::move(encoder);
88 drivetrain_left_encoder_->SetMaxPeriod(0.005);
89 }
90
91 void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) {
92 drivetrain_right_encoder_ = ::std::move(encoder);
93 drivetrain_right_encoder_->SetMaxPeriod(0.005);
94 }
95
96 void operator()() {
97 ::aos::SetCurrentThreadName("SensorReader");
98
99 my_pid_ = getpid();
100 ds_ =
101#ifdef WPILIB2015
102 DriverStation::GetInstance();
103#else
104 &DriverStation::GetInstance();
105#endif
106 ::aos::time::PhasedLoop phased_loop(::aos::time::Time::InMS(5),
107 ::aos::time::Time::InMS(4));
108
109 ::aos::SetCurrentThreadRealtimePriority(kPriority);
110 while (run_) {
111 {
112 const int iterations = phased_loop.SleepUntilNext();
113 if (iterations != 1) {
114 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
115 }
116 }
117 RunIteration();
118 }
119 }
120
121 void RunIteration() {
122 ::frc971::wpilib::SendRobotState(my_pid_, ds_, nullptr);
123
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 drivetrain_message->left_speed =
131 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
132 drivetrain_message->right_speed =
133 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
134
135 drivetrain_message.Send();
136 }
137
138 accessories_queue.position.MakeMessage().Send();
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 ::std::atomic<bool> run_{true};
153};
154
155class SolenoidWriter {
156 public:
157 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
158 : pcm_(pcm),
159 drivetrain_(".y2012.control_loops.drivetrain_queue.output"),
160 accessories_(".y2012.control_loops.accessories_queue.output") {}
161
162 void set_drivetrain_high(
163 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
164 drivetrain_high_ = ::std::move(s);
165 }
166
167 void set_drivetrain_low(
168 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
169 drivetrain_low_ = ::std::move(s);
170 }
171
172 void set_s1(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
173 s1_ = ::std::move(s);
174 }
175
176 void set_s2(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
177 s2_ = ::std::move(s);
178 }
179
180 void set_s3(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) {
181 s3_ = ::std::move(s);
182 }
183
184 void operator()() {
185 ::aos::SetCurrentThreadName("Solenoids");
186 ::aos::SetCurrentThreadRealtimePriority(30);
187
188 while (run_) {
189 ::aos::time::PhasedLoopXMS(20, 1000);
190
191 {
192 accessories_.FetchLatest();
193 if (accessories_.get()) {
194 LOG_STRUCT(DEBUG, "solenoids", *accessories_);
195 s1_->Set(accessories_->solenoids[0]);
196 s2_->Set(accessories_->solenoids[1]);
197 s3_->Set(accessories_->solenoids[2]);
198 }
199 }
200
201 {
202 drivetrain_.FetchLatest();
203 if (drivetrain_.get()) {
204 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
205 const bool high = drivetrain_->left_high || drivetrain_->right_high;
206 drivetrain_high_->Set(high);
207 drivetrain_low_->Set(!high);
208 }
209 }
210
211 {
212 ::frc971::wpilib::PneumaticsToLog to_log;
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<::frc971::wpilib::BufferedPcm> &pcm_;
224
225 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_high_;
226 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_low_;
227 ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s1_, s2_, s3_;
228
229 ::std::unique_ptr<Compressor> compressor_;
230
231 ::aos::Queue<::y2012::control_loops::DrivetrainQueue::Output> drivetrain_;
232 ::aos::Queue<::y2012::control_loops::AccessoriesQueue::Message> accessories_;
233
234 ::std::atomic<bool> run_{true};
235};
236
237class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler {
238 public:
239 void set_left_drivetrain_talon(::std::unique_ptr<Talon> t) {
240 left_drivetrain_talon_ = ::std::move(t);
241 }
242
243 void set_right_drivetrain_talon(::std::unique_ptr<Talon> t) {
244 right_drivetrain_talon_ = ::std::move(t);
245 }
246
247 private:
248 virtual void Read() override {
249 ::y2012::control_loops::drivetrain_queue.output.FetchAnother();
250 }
251
252 virtual void Write() override {
253 auto &queue = ::y2012::control_loops::drivetrain_queue.output;
254 LOG_STRUCT(DEBUG, "will output", *queue);
255 left_drivetrain_talon_->Set(-queue->left_voltage / 12.0);
256 right_drivetrain_talon_->Set(queue->right_voltage / 12.0);
257 }
258
259 virtual void Stop() override {
260 LOG(WARNING, "drivetrain output too old\n");
261 left_drivetrain_talon_->Disable();
262 right_drivetrain_talon_->Disable();
263 }
264
265 ::std::unique_ptr<Talon> left_drivetrain_talon_;
266 ::std::unique_ptr<Talon> right_drivetrain_talon_;
267};
268
269class AccessoriesWriter : public ::frc971::wpilib::LoopOutputHandler {
270 public:
271 void set_talon1(::std::unique_ptr<Talon> t) {
272 talon1_ = ::std::move(t);
273 }
274
275 void set_talon2(::std::unique_ptr<Talon> t) {
276 talon2_ = ::std::move(t);
277 }
278
279 private:
280 virtual void Read() override {
281 ::y2012::control_loops::accessories_queue.output.FetchAnother();
282 }
283
284 virtual void Write() override {
285 auto &queue = ::y2012::control_loops::accessories_queue.output;
286 talon1_->Set(queue->sticks[0]);
287 talon2_->Set(queue->sticks[1]);
288 LOG_STRUCT(DEBUG, "will output", *queue);
289 }
290
291 virtual void Stop() override {
292 LOG(WARNING, "shooter output too old\n");
293 talon1_->Disable();
294 talon2_->Disable();
295 }
296
297 ::std::unique_ptr<Talon> talon1_, talon2_;
298};
299
300class WPILibRobot : public SampleRobot {
301 public:
302 ::std::unique_ptr<Encoder> make_encoder(int index) {
303 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
304 Encoder::k4X);
305 }
306
307 virtual void RobotMain() {
308 ::aos::InitNRT();
309 ::aos::SetCurrentThreadName("StartCompetition");
310
311 ::frc971::wpilib::JoystickSender joystick_sender;
312 ::std::thread joystick_thread(::std::ref(joystick_sender));
313
314 SensorReader reader;
315
316 reader.set_drivetrain_left_encoder(make_encoder(0));
317 reader.set_drivetrain_right_encoder(make_encoder(1));
318
319 ::std::thread reader_thread(::std::ref(reader));
320
321 DrivetrainWriter drivetrain_writer;
322 drivetrain_writer.set_left_drivetrain_talon(
323 ::std::unique_ptr<Talon>(new Talon(3)));
324 drivetrain_writer.set_right_drivetrain_talon(
325 ::std::unique_ptr<Talon>(new Talon(4)));
326 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
327
328 ::y2012::wpilib::AccessoriesWriter accessories_writer;
329 accessories_writer.set_talon1(::std::unique_ptr<Talon>(new Talon(5)));
330 accessories_writer.set_talon2(::std::unique_ptr<Talon>(new Talon(6)));
331 ::std::thread accessories_writer_thread(::std::ref(accessories_writer));
332
333 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
334 new ::frc971::wpilib::BufferedPcm());
335 SolenoidWriter solenoid_writer(pcm);
336 solenoid_writer.set_drivetrain_high(pcm->MakeSolenoid(0));
337 solenoid_writer.set_drivetrain_low(pcm->MakeSolenoid(2));
338 solenoid_writer.set_s1(pcm->MakeSolenoid(1));
339 solenoid_writer.set_s2(pcm->MakeSolenoid(3));
340 solenoid_writer.set_s3(pcm->MakeSolenoid(4));
341
342 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
343
344 // Wait forever. Not much else to do...
345 PCHECK(select(0, nullptr, nullptr, nullptr, nullptr));
346
347 LOG(ERROR, "Exiting WPILibRobot\n");
348
349 joystick_sender.Quit();
350 joystick_thread.join();
351 reader.Quit();
352 reader_thread.join();
353
354 drivetrain_writer.Quit();
355 drivetrain_writer_thread.join();
356 accessories_writer.Quit();
357 accessories_writer_thread.join();
358
359 ::aos::Cleanup();
360 }
361};
362
363} // namespace wpilib
364} // namespace y2012
365
366
367START_ROBOT_CLASS(::y2012::wpilib::WPILibRobot);