blob: f1413fb72c279551dee2cc19e34e5de6c7838524 [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 ::frc971::wpilib::BufferedPcm;
54using ::frc971::wpilib::BufferedSolenoid;
55using ::frc971::wpilib::LoopOutputHandler;
56using ::frc971::wpilib::JoystickSender;
57using ::frc971::wpilib::GyroSender;
Brian Silvermanf819b442019-01-20 16:51:04 -080058using aos::make_unique;
Comran Morshed41ed7c22015-11-04 21:03:37 +000059
Austin Schuh4d11f9a2019-02-02 16:30:06 -080060namespace y2014_bot3 {
Comran Morshed41ed7c22015-11-04 21:03:37 +000061namespace wpilib {
62
63double drivetrain_translate(int32_t in) {
64 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Adam Snaider83eae562016-09-10 16:47:33 -070065 ::y2014_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio *
Comran Morshed41ed7c22015-11-04 21:03:37 +000066 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
67}
68
Brian Silverman51091a02015-12-26 15:56:58 -080069double drivetrain_velocity_translate(double in) {
70 return (1.0 / in) / 256.0 /*cpr*/ *
Adam Snaider83eae562016-09-10 16:47:33 -070071 ::y2014_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio *
Brian Silverman51091a02015-12-26 15:56:58 -080072 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
73}
74
Comran Morshed41ed7c22015-11-04 21:03:37 +000075// Reads in our inputs. (sensors, voltages, etc.)
Austin Schuh4d11f9a2019-02-02 16:30:06 -080076class SensorReader : public ::frc971::wpilib::SensorReader {
Comran Morshed41ed7c22015-11-04 21:03:37 +000077 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -080078 SensorReader(::aos::EventLoop *event_loop)
Austin Schuh30020d92019-05-27 13:07:02 -070079 : ::frc971::wpilib::SensorReader(event_loop),
80 rollers_position_sender_(
81 event_loop->MakeSender<
82 ::y2014_bot3::control_loops::RollersQueue::Position>(
83 ".y2014_bot3.control_loops.rollers_queue.position")) {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -080084
Comran Morshed41ed7c22015-11-04 21:03:37 +000085 void RunIteration() {
Comran Morshed41ed7c22015-11-04 21:03:37 +000086 // Drivetrain
87 {
88 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
89 drivetrain_message->right_encoder =
Austin Schuhba32a9e2018-11-03 15:28:13 -070090 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Comran Morshed41ed7c22015-11-04 21:03:37 +000091 drivetrain_message->left_encoder =
Austin Schuhba32a9e2018-11-03 15:28:13 -070092 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -080093 drivetrain_message->left_speed =
94 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
95 drivetrain_message->right_speed =
96 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Comran Morshed41ed7c22015-11-04 21:03:37 +000097
98 drivetrain_message.Send();
99 }
100
101 // Rollers
102 {
Austin Schuh30020d92019-05-27 13:07:02 -0700103 auto rollers_message = rollers_position_sender_.MakeMessage();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000104 rollers_message.Send();
105 }
106 }
Austin Schuh30020d92019-05-27 13:07:02 -0700107
108 private:
109 ::aos::Sender<::y2014_bot3::control_loops::RollersQueue::Position>
110 rollers_position_sender_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000111};
112
113// Writes out our pneumatic outputs.
114class SolenoidWriter {
115 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700116 SolenoidWriter(::aos::EventLoop *event_loop,
117 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Comran Morshed41ed7c22015-11-04 21:03:37 +0000118 : pcm_(pcm),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700119 drivetrain_(
120 event_loop
121 ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Output>(
122 ".frc971.control_loops.drivetrain_queue.output")),
123 rollers_(event_loop->MakeFetcher<
124 ::y2014_bot3::control_loops::RollersQueue::Output>(
125 ".y2014_bot3.control_loops.rollers_queue.output")) {
126 event_loop->set_name("Solenoids");
127 event_loop->SetRuntimeRealtimePriority(27);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000128
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700129 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
130 ::std::chrono::milliseconds(20),
131 ::std::chrono::milliseconds(1));
132 }
133
134 void set_pressure_switch(
135 ::std::unique_ptr<::frc::DigitalInput> pressure_switch) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000136 pressure_switch_ = ::std::move(pressure_switch);
137 }
138
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700139 void set_compressor_relay(::std::unique_ptr<::frc::Relay> compressor_relay) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000140 compressor_relay_ = ::std::move(compressor_relay);
141 }
142
143 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
144 drivetrain_left_ = ::std::move(s);
145 }
146
147 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
148 drivetrain_right_ = ::std::move(s);
149 }
150
151 void set_rollers_front(::std::unique_ptr<BufferedSolenoid> s) {
152 rollers_front_ = ::std::move(s);
153 }
154
155 void set_rollers_back(::std::unique_ptr<BufferedSolenoid> s) {
156 rollers_back_ = ::std::move(s);
157 }
158
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700159 void Loop(const int iterations) {
160 if (iterations != 1) {
161 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
162 }
Brian Silverman5090c432016-01-02 14:44:26 -0800163
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700164 // Drivetrain
165 {
166 drivetrain_.Fetch();
167 if (drivetrain_.get()) {
168 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
169 drivetrain_left_->Set(drivetrain_->left_high);
170 drivetrain_right_->Set(drivetrain_->right_high);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000171 }
172 }
Comran Morshed41ed7c22015-11-04 21:03:37 +0000173
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700174 // Intake
175 {
176 rollers_.Fetch();
177 if (rollers_.get()) {
178 LOG_STRUCT(DEBUG, "solenoids", *rollers_);
179 rollers_front_->Set(rollers_->front_extended);
180 rollers_back_->Set(rollers_->back_extended);
181 }
182 }
183
184 // Compressor
185 {
186 ::frc971::wpilib::PneumaticsToLog to_log;
187 {
188 // Refill if pneumatic pressure goes too low.
189 const bool compressor_on = !pressure_switch_->Get();
190 to_log.compressor_on = compressor_on;
191 if (compressor_on) {
192 compressor_relay_->Set(::frc::Relay::kForward);
193 } else {
194 compressor_relay_->Set(::frc::Relay::kOff);
195 }
196 }
197
198 pcm_->Flush();
199 to_log.read_solenoids = pcm_->GetAll();
200 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
201 }
202 }
Comran Morshed41ed7c22015-11-04 21:03:37 +0000203
204 private:
205 const ::std::unique_ptr<BufferedPcm> &pcm_;
206
207 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_, drivetrain_right_;
208 ::std::unique_ptr<BufferedSolenoid> rollers_front_, rollers_back_;
209
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700210 ::std::unique_ptr<::frc::DigitalInput> pressure_switch_;
211 ::std::unique_ptr<::frc::Relay> compressor_relay_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000212
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700213 ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
214 ::aos::Fetcher<::y2014_bot3::control_loops::RollersQueue::Output> rollers_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000215};
216
Comran Morshed41ed7c22015-11-04 21:03:37 +0000217// Writes out rollers voltages.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700218class RollersWriter : public LoopOutputHandler<
219 ::y2014_bot3::control_loops::RollersQueue::Output> {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000220 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800221 RollersWriter(::aos::EventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700222 : ::frc971::wpilib::LoopOutputHandler<
223 ::y2014_bot3::control_loops::RollersQueue::Output>(
224 event_loop, ".y2014_bot3.control_loops.rollers_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800225
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700226 void set_rollers_front_intake_talon(::std::unique_ptr<::frc::Talon> t_left,
227 ::std::unique_ptr<::frc::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 Schuhbd1fe9c2019-06-29 16:35:48 -0700232 void set_rollers_back_intake_talon(::std::unique_ptr<::frc::Talon> t_left,
233 ::std::unique_ptr<::frc::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
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700238 void set_rollers_low_goal_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000239 rollers_low_goal_talon_ = ::std::move(t);
240 }
241
242 private:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700243 virtual void Write(const ::y2014_bot3::control_loops::RollersQueue::Output
244 &output) override {
245 LOG_STRUCT(DEBUG, "will output", output);
246 rollers_front_left_intake_talon_->SetSpeed(output.front_intake_voltage /
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800247 12.0);
248 rollers_front_right_intake_talon_->SetSpeed(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700249 -(output.front_intake_voltage / 12.0));
250 rollers_back_left_intake_talon_->SetSpeed(output.back_intake_voltage /
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800251 12.0);
252 rollers_back_right_intake_talon_->SetSpeed(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700253 -(output.back_intake_voltage / 12.0));
254 rollers_low_goal_talon_->SetSpeed(output.low_goal_voltage / 12.0);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000255 }
256
257 virtual void Stop() override {
258 LOG(WARNING, "Intake output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800259 rollers_front_left_intake_talon_->SetDisabled();
260 rollers_front_right_intake_talon_->SetDisabled();
261 rollers_back_left_intake_talon_->SetDisabled();
262 rollers_back_right_intake_talon_->SetDisabled();
263 rollers_low_goal_talon_->SetDisabled();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000264 }
265
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700266 ::std::unique_ptr<::frc::Talon> rollers_front_left_intake_talon_,
Comran Morshed41ed7c22015-11-04 21:03:37 +0000267 rollers_back_left_intake_talon_, rollers_front_right_intake_talon_,
268 rollers_back_right_intake_talon_, rollers_low_goal_talon_;
269};
270
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800271class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000272 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700273 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
274 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
275 ::frc::Encoder::k4X);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000276 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800277 void Run() override {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700278 // Thread 1.
279 ::aos::ShmEventLoop joystick_sender_event_loop;
280 ::frc971::wpilib::JoystickSender joystick_sender(
281 &joystick_sender_event_loop);
282 AddLoop(&joystick_sender_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000283
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700284 // Thread 2.
285 ::aos::ShmEventLoop pdp_fetcher_event_loop;
286 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
287 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800288
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700289 // Thread 3.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000290 // Sensors
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700291 ::aos::ShmEventLoop sensor_reader_event_loop;
292 SensorReader sensor_reader(&sensor_reader_event_loop);
293 sensor_reader.set_drivetrain_left_encoder(make_encoder(4));
294 sensor_reader.set_drivetrain_right_encoder(make_encoder(5));
295 AddLoop(&sensor_reader_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000296
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700297 // Thread 4.
298 ::aos::ShmEventLoop gyro_event_loop;
299 GyroSender gyro_sender(&gyro_event_loop);
300 AddLoop(&gyro_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000301
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700302 // Thread 5.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000303 // Outputs
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700304 ::aos::ShmEventLoop output_event_loop;
305 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Davis5ba275b2019-02-03 01:18:04 -0800306 drivetrain_writer.set_left_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700307 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(5)), true);
Sabina Davis5ba275b2019-02-03 01:18:04 -0800308 drivetrain_writer.set_right_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700309 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(2)), false);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000310
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700311 RollersWriter rollers_writer(&output_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000312 rollers_writer.set_rollers_front_intake_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700313 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(3)),
314 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(7)));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000315 rollers_writer.set_rollers_back_intake_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700316 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(1)),
317 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(6)));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000318
319 rollers_writer.set_rollers_low_goal_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700320 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(4)));
321 AddLoop(&output_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000322
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700323 // Thread 6.
324 ::aos::ShmEventLoop solenoid_writer_event_loop;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000325 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
326 new ::frc971::wpilib::BufferedPcm());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700327 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000328 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.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700334 solenoid_writer.set_pressure_switch(make_unique<::frc::DigitalInput>(9));
335 solenoid_writer.set_compressor_relay(make_unique<::frc::Relay>(0));
336 AddLoop(&solenoid_writer_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000337
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700338 RunLoops();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000339 }
340};
341
342} // namespace wpilib
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800343} // namespace y2014_bot3
Comran Morshed41ed7c22015-11-04 21:03:37 +0000344
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800345AOS_ROBOT_CLASS(::y2014_bot3::wpilib::WPILibRobot);