blob: bd5ae3d78bc4fd59c78ca0dfcb5a509e8e611265 [file] [log] [blame]
Austin Schuh4d11f9a2019-02-02 16:30:06 -08001#include <inttypes.h>
Comran Morshed41ed7c22015-11-04 21:03:37 +00002#include <stdio.h>
3#include <string.h>
4#include <unistd.h>
Comran Morshed41ed7c22015-11-04 21:03:37 +00005
Austin Schuh8aec1ed2016-05-01 13:29:20 -07006#include <chrono>
Comran Morshed41ed7c22015-11-04 21:03:37 +00007#include <functional>
Austin Schuh4d11f9a2019-02-02 16:30:06 -08008#include <mutex>
9#include <thread>
Comran Morshed41ed7c22015-11-04 21:03:37 +000010
Parker Schuhd3b7a8872018-02-19 16:42:27 -080011#include "frc971/wpilib/ahal/AnalogInput.h"
12#include "frc971/wpilib/ahal/Compressor.h"
13#include "frc971/wpilib/ahal/DigitalInput.h"
14#include "frc971/wpilib/ahal/DriverStation.h"
15#include "frc971/wpilib/ahal/Encoder.h"
16#include "frc971/wpilib/ahal/Relay.h"
17#include "frc971/wpilib/ahal/Talon.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080018#include "frc971/wpilib/wpilib_robot_base.h"
Austin Schuhbc9a2ab2015-11-26 16:20:17 -080019#undef ERROR
Comran Morshed41ed7c22015-11-04 21:03:37 +000020
Austin Schuhdf6cbb12019-02-02 13:46:52 -080021#include "aos/events/shm-event-loop.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080022#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070023#include "aos/logging/logging.h"
24#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080025#include "aos/make_unique.h"
26#include "aos/robot_state/robot_state.q.h"
27#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -070028#include "aos/time/time.h"
29#include "aos/util/log_interval.h"
30#include "aos/util/phased_loop.h"
31#include "aos/util/wrapping_counter.h"
Adam Snaider83eae562016-09-10 16:47:33 -070032#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000033#include "frc971/wpilib/buffered_pcm.h"
Austin Schuh4d11f9a2019-02-02 16:30:06 -080034#include "frc971/wpilib/buffered_solenoid.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050035#include "frc971/wpilib/dma.h"
Sabina Davis5ba275b2019-02-03 01:18:04 -080036#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh4d11f9a2019-02-02 16:30:06 -080037#include "frc971/wpilib/gyro_sender.h"
38#include "frc971/wpilib/joystick_sender.h"
39#include "frc971/wpilib/logging.q.h"
40#include "frc971/wpilib/loop_output_handler.h"
41#include "frc971/wpilib/pdp_fetcher.h"
42#include "frc971/wpilib/sensor_reader.h"
43#include "y2014_bot3/control_loops/drivetrain/drivetrain_base.h"
44#include "y2014_bot3/control_loops/rollers/rollers.h"
45#include "y2014_bot3/control_loops/rollers/rollers.q.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000046
47#ifndef M_PI
48#define M_PI 3.14159265358979323846
49#endif
50
51using ::aos::util::SimpleLogInterval;
Adam Snaider83eae562016-09-10 16:47:33 -070052using ::frc971::control_loops::drivetrain_queue;
Comran Morshed41ed7c22015-11-04 21:03:37 +000053using ::y2014_bot3::control_loops::rollers_queue;
54using ::frc971::wpilib::BufferedPcm;
55using ::frc971::wpilib::BufferedSolenoid;
56using ::frc971::wpilib::LoopOutputHandler;
57using ::frc971::wpilib::JoystickSender;
58using ::frc971::wpilib::GyroSender;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080059using namespace frc;
Brian Silvermanf819b442019-01-20 16:51:04 -080060using aos::make_unique;
Comran Morshed41ed7c22015-11-04 21:03:37 +000061
Austin Schuh4d11f9a2019-02-02 16:30:06 -080062namespace y2014_bot3 {
Comran Morshed41ed7c22015-11-04 21:03:37 +000063namespace wpilib {
64
65double drivetrain_translate(int32_t in) {
66 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Adam Snaider83eae562016-09-10 16:47:33 -070067 ::y2014_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio *
Comran Morshed41ed7c22015-11-04 21:03:37 +000068 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
69}
70
Brian Silverman51091a02015-12-26 15:56:58 -080071double drivetrain_velocity_translate(double in) {
72 return (1.0 / in) / 256.0 /*cpr*/ *
Adam Snaider83eae562016-09-10 16:47:33 -070073 ::y2014_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio *
Brian Silverman51091a02015-12-26 15:56:58 -080074 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
75}
76
Comran Morshed41ed7c22015-11-04 21:03:37 +000077// Reads in our inputs. (sensors, voltages, etc.)
Austin Schuh4d11f9a2019-02-02 16:30:06 -080078class SensorReader : public ::frc971::wpilib::SensorReader {
Comran Morshed41ed7c22015-11-04 21:03:37 +000079 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -080080 SensorReader(::aos::EventLoop *event_loop)
Austin Schuh30020d92019-05-27 13:07:02 -070081 : ::frc971::wpilib::SensorReader(event_loop),
82 rollers_position_sender_(
83 event_loop->MakeSender<
84 ::y2014_bot3::control_loops::RollersQueue::Position>(
85 ".y2014_bot3.control_loops.rollers_queue.position")) {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -080086
Comran Morshed41ed7c22015-11-04 21:03:37 +000087 void RunIteration() {
Comran Morshed41ed7c22015-11-04 21:03:37 +000088 // Drivetrain
89 {
90 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
91 drivetrain_message->right_encoder =
Austin Schuhba32a9e2018-11-03 15:28:13 -070092 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Comran Morshed41ed7c22015-11-04 21:03:37 +000093 drivetrain_message->left_encoder =
Austin Schuhba32a9e2018-11-03 15:28:13 -070094 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -080095 drivetrain_message->left_speed =
96 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
97 drivetrain_message->right_speed =
98 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Comran Morshed41ed7c22015-11-04 21:03:37 +000099
100 drivetrain_message.Send();
101 }
102
103 // Rollers
104 {
Austin Schuh30020d92019-05-27 13:07:02 -0700105 auto rollers_message = rollers_position_sender_.MakeMessage();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000106 rollers_message.Send();
107 }
108 }
Austin Schuh30020d92019-05-27 13:07:02 -0700109
110 private:
111 ::aos::Sender<::y2014_bot3::control_loops::RollersQueue::Position>
112 rollers_position_sender_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000113};
114
115// Writes out our pneumatic outputs.
116class SolenoidWriter {
117 public:
118 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
119 : pcm_(pcm),
Adam Snaider83eae562016-09-10 16:47:33 -0700120 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Comran Morshed41ed7c22015-11-04 21:03:37 +0000121 rollers_(".y2014_bot3.control_loops.rollers_queue.output") {}
122
123 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
124 pressure_switch_ = ::std::move(pressure_switch);
125 }
126
127 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
128 compressor_relay_ = ::std::move(compressor_relay);
129 }
130
131 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
132 drivetrain_left_ = ::std::move(s);
133 }
134
135 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
136 drivetrain_right_ = ::std::move(s);
137 }
138
139 void set_rollers_front(::std::unique_ptr<BufferedSolenoid> s) {
140 rollers_front_ = ::std::move(s);
141 }
142
143 void set_rollers_back(::std::unique_ptr<BufferedSolenoid> s) {
144 rollers_back_ = ::std::move(s);
145 }
146
147 void operator()() {
148 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800149 ::aos::SetCurrentThreadRealtimePriority(27);
150
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700151 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
152 ::std::chrono::milliseconds(1));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000153
154 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800155 {
156 const int iterations = phased_loop.SleepUntilNext();
157 if (iterations != 1) {
158 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
159 }
160 }
161
Comran Morshed41ed7c22015-11-04 21:03:37 +0000162 // Drivetrain
163 {
164 drivetrain_.FetchLatest();
165 if (drivetrain_.get()) {
166 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
167 drivetrain_left_->Set(drivetrain_->left_high);
168 drivetrain_right_->Set(drivetrain_->right_high);
169 }
170 }
171
172 // Intake
173 {
174 rollers_.FetchLatest();
175 if (rollers_.get()) {
176 LOG_STRUCT(DEBUG, "solenoids", *rollers_);
177 rollers_front_->Set(rollers_->front_extended);
178 rollers_back_->Set(rollers_->back_extended);
179 }
180 }
181
182 // Compressor
Comran Morshed41ed7c22015-11-04 21:03:37 +0000183 {
184 ::frc971::wpilib::PneumaticsToLog to_log;
185 {
186 // Refill if pneumatic pressure goes too low.
187 const bool compressor_on = !pressure_switch_->Get();
188 to_log.compressor_on = compressor_on;
189 if (compressor_on) {
190 compressor_relay_->Set(Relay::kForward);
191 } else {
192 compressor_relay_->Set(Relay::kOff);
193 }
194 }
195
196 pcm_->Flush();
197 to_log.read_solenoids = pcm_->GetAll();
198 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
199 }
200 }
201 }
202
203 void Quit() { run_ = false; }
204
205 private:
206 const ::std::unique_ptr<BufferedPcm> &pcm_;
207
208 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_, drivetrain_right_;
209 ::std::unique_ptr<BufferedSolenoid> rollers_front_, rollers_back_;
210
211 ::std::unique_ptr<DigitalInput> pressure_switch_;
212 ::std::unique_ptr<Relay> compressor_relay_;
213
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800214 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000215 ::aos::Queue<::y2014_bot3::control_loops::RollersQueue::Output> rollers_;
216
217 ::std::atomic<bool> run_{true};
218};
219
Comran Morshed41ed7c22015-11-04 21:03:37 +0000220// Writes out rollers voltages.
221class RollersWriter : public LoopOutputHandler {
222 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800223 RollersWriter(::aos::EventLoop *event_loop)
224 : ::frc971::wpilib::LoopOutputHandler(event_loop) {}
225
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800226 void set_rollers_front_intake_talon(::std::unique_ptr<Talon> t_left,
227 ::std::unique_ptr<Talon> t_right) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000228 rollers_front_left_intake_talon_ = ::std::move(t_left);
229 rollers_front_right_intake_talon_ = ::std::move(t_right);
230 }
231
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800232 void set_rollers_back_intake_talon(::std::unique_ptr<Talon> t_left,
233 ::std::unique_ptr<Talon> t_right) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000234 rollers_back_left_intake_talon_ = ::std::move(t_left);
235 rollers_back_right_intake_talon_ = ::std::move(t_right);
236 }
237
238 void set_rollers_low_goal_talon(::std::unique_ptr<Talon> t) {
239 rollers_low_goal_talon_ = ::std::move(t);
240 }
241
242 private:
243 virtual void Read() override {
244 ::y2014_bot3::control_loops::rollers_queue.output.FetchAnother();
245 }
246
247 virtual void Write() override {
248 auto &queue = ::y2014_bot3::control_loops::rollers_queue.output;
249 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800250 rollers_front_left_intake_talon_->SetSpeed(queue->front_intake_voltage /
251 12.0);
252 rollers_front_right_intake_talon_->SetSpeed(
253 -(queue->front_intake_voltage / 12.0));
254 rollers_back_left_intake_talon_->SetSpeed(queue->back_intake_voltage /
255 12.0);
256 rollers_back_right_intake_talon_->SetSpeed(
257 -(queue->back_intake_voltage / 12.0));
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800258 rollers_low_goal_talon_->SetSpeed(queue->low_goal_voltage / 12.0);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000259 }
260
261 virtual void Stop() override {
262 LOG(WARNING, "Intake output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800263 rollers_front_left_intake_talon_->SetDisabled();
264 rollers_front_right_intake_talon_->SetDisabled();
265 rollers_back_left_intake_talon_->SetDisabled();
266 rollers_back_right_intake_talon_->SetDisabled();
267 rollers_low_goal_talon_->SetDisabled();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000268 }
269
270 ::std::unique_ptr<Talon> rollers_front_left_intake_talon_,
271 rollers_back_left_intake_talon_, rollers_front_right_intake_talon_,
272 rollers_back_right_intake_talon_, rollers_low_goal_talon_;
273};
274
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800275class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000276 public:
277 ::std::unique_ptr<Encoder> make_encoder(int index) {
278 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
279 Encoder::k4X);
280 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800281 void Run() override {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000282 ::aos::InitNRT();
283 ::aos::SetCurrentThreadName("StartCompetition");
284
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800285 ::aos::ShmEventLoop event_loop;
286
287 JoystickSender joystick_sender(&event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000288 ::std::thread joystick_thread(::std::ref(joystick_sender));
289
Austin Schuh0b545432019-05-12 15:46:12 -0700290 ::frc971::wpilib::PDPFetcher pdp_fetcher(&event_loop);
Brian Silverman425492b2015-12-30 15:23:55 -0800291 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
292
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800293 // TODO(comran): IO ports are placeholders at the moment, so match them to
Comran Morshed41ed7c22015-11-04 21:03:37 +0000294 // the robot before turning on.
295
296 // Sensors
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800297 SensorReader reader(&event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000298 reader.set_drivetrain_left_encoder(make_encoder(4));
299 reader.set_drivetrain_right_encoder(make_encoder(5));
300
301 ::std::thread reader_thread(::std::ref(reader));
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800302 GyroSender gyro_sender(&event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000303 ::std::thread gyro_thread(::std::ref(gyro_sender));
304
305 // Outputs
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800306 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&event_loop);
Sabina Davis5ba275b2019-02-03 01:18:04 -0800307 drivetrain_writer.set_left_controller0(
308 ::std::unique_ptr<Talon>(new Talon(5)), true);
309 drivetrain_writer.set_right_controller0(
310 ::std::unique_ptr<Talon>(new Talon(2)), false);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000311 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
312
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800313 RollersWriter rollers_writer(&event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000314 rollers_writer.set_rollers_front_intake_talon(
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800315 ::std::unique_ptr<Talon>(new Talon(3)),
316 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000317 rollers_writer.set_rollers_back_intake_talon(
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800318 ::std::unique_ptr<Talon>(new Talon(1)),
319 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000320
321 rollers_writer.set_rollers_low_goal_talon(
322 ::std::unique_ptr<Talon>(new Talon(4)));
323 ::std::thread rollers_writer_thread(::std::ref(rollers_writer));
324
325 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
326 new ::frc971::wpilib::BufferedPcm());
327 SolenoidWriter solenoid_writer(pcm);
328 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
329 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(5));
330 solenoid_writer.set_rollers_front(pcm->MakeSolenoid(2));
331 solenoid_writer.set_rollers_back(pcm->MakeSolenoid(4));
332
333 // Don't change the following IDs.
334 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
335 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
336 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
337
338 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800339 while (true) {
340 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
341 if (r != 0) {
342 PLOG(WARNING, "infinite select failed");
343 } else {
344 PLOG(WARNING, "infinite select succeeded??\n");
345 }
346 }
Comran Morshed41ed7c22015-11-04 21:03:37 +0000347
348 LOG(ERROR, "Exiting WPILibRobot\n");
349
350 joystick_sender.Quit();
351 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800352 pdp_fetcher.Quit();
353 pdp_fetcher_thread.join();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000354 reader.Quit();
355 reader_thread.join();
356 gyro_sender.Quit();
357 gyro_thread.join();
358
359 drivetrain_writer.Quit();
360 drivetrain_writer_thread.join();
361
362 rollers_writer.Quit();
363 rollers_writer_thread.join();
364
365 solenoid_writer.Quit();
366 solenoid_thread.join();
367
368 ::aos::Cleanup();
369 }
370};
371
372} // namespace wpilib
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800373} // namespace y2014_bot3
Comran Morshed41ed7c22015-11-04 21:03:37 +0000374
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800375AOS_ROBOT_CLASS(::y2014_bot3::wpilib::WPILibRobot);