blob: 1c6bddfca2b8f2aaaacadeb8a872ac5e56f03e1f [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;
Brian Silvermanf819b442019-01-20 16:51:04 -080059using aos::make_unique;
Comran Morshed41ed7c22015-11-04 21:03:37 +000060
Austin Schuh4d11f9a2019-02-02 16:30:06 -080061namespace y2014_bot3 {
Comran Morshed41ed7c22015-11-04 21:03:37 +000062namespace wpilib {
63
64double drivetrain_translate(int32_t in) {
65 return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) *
Adam Snaider83eae562016-09-10 16:47:33 -070066 ::y2014_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio *
Comran Morshed41ed7c22015-11-04 21:03:37 +000067 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
68}
69
Brian Silverman51091a02015-12-26 15:56:58 -080070double drivetrain_velocity_translate(double in) {
71 return (1.0 / in) / 256.0 /*cpr*/ *
Adam Snaider83eae562016-09-10 16:47:33 -070072 ::y2014_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio *
Brian Silverman51091a02015-12-26 15:56:58 -080073 (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI);
74}
75
Comran Morshed41ed7c22015-11-04 21:03:37 +000076// Reads in our inputs. (sensors, voltages, etc.)
Austin Schuh4d11f9a2019-02-02 16:30:06 -080077class SensorReader : public ::frc971::wpilib::SensorReader {
Comran Morshed41ed7c22015-11-04 21:03:37 +000078 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -080079 SensorReader(::aos::EventLoop *event_loop)
Austin Schuh30020d92019-05-27 13:07:02 -070080 : ::frc971::wpilib::SensorReader(event_loop),
81 rollers_position_sender_(
82 event_loop->MakeSender<
83 ::y2014_bot3::control_loops::RollersQueue::Position>(
84 ".y2014_bot3.control_loops.rollers_queue.position")) {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -080085
Comran Morshed41ed7c22015-11-04 21:03:37 +000086 void RunIteration() {
Comran Morshed41ed7c22015-11-04 21:03:37 +000087 // Drivetrain
88 {
89 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
90 drivetrain_message->right_encoder =
Austin Schuhba32a9e2018-11-03 15:28:13 -070091 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Comran Morshed41ed7c22015-11-04 21:03:37 +000092 drivetrain_message->left_encoder =
Austin Schuhba32a9e2018-11-03 15:28:13 -070093 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -080094 drivetrain_message->left_speed =
95 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
96 drivetrain_message->right_speed =
97 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Comran Morshed41ed7c22015-11-04 21:03:37 +000098
99 drivetrain_message.Send();
100 }
101
102 // Rollers
103 {
Austin Schuh30020d92019-05-27 13:07:02 -0700104 auto rollers_message = rollers_position_sender_.MakeMessage();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000105 rollers_message.Send();
106 }
107 }
Austin Schuh30020d92019-05-27 13:07:02 -0700108
109 private:
110 ::aos::Sender<::y2014_bot3::control_loops::RollersQueue::Position>
111 rollers_position_sender_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000112};
113
114// Writes out our pneumatic outputs.
115class SolenoidWriter {
116 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700117 SolenoidWriter(::aos::EventLoop *event_loop,
118 const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
Comran Morshed41ed7c22015-11-04 21:03:37 +0000119 : pcm_(pcm),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700120 drivetrain_(
121 event_loop
122 ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Output>(
123 ".frc971.control_loops.drivetrain_queue.output")),
124 rollers_(event_loop->MakeFetcher<
125 ::y2014_bot3::control_loops::RollersQueue::Output>(
126 ".y2014_bot3.control_loops.rollers_queue.output")) {
127 event_loop->set_name("Solenoids");
128 event_loop->SetRuntimeRealtimePriority(27);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000129
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700130 event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); },
131 ::std::chrono::milliseconds(20),
132 ::std::chrono::milliseconds(1));
133 }
134
135 void set_pressure_switch(
136 ::std::unique_ptr<::frc::DigitalInput> pressure_switch) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000137 pressure_switch_ = ::std::move(pressure_switch);
138 }
139
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700140 void set_compressor_relay(::std::unique_ptr<::frc::Relay> compressor_relay) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000141 compressor_relay_ = ::std::move(compressor_relay);
142 }
143
144 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
145 drivetrain_left_ = ::std::move(s);
146 }
147
148 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
149 drivetrain_right_ = ::std::move(s);
150 }
151
152 void set_rollers_front(::std::unique_ptr<BufferedSolenoid> s) {
153 rollers_front_ = ::std::move(s);
154 }
155
156 void set_rollers_back(::std::unique_ptr<BufferedSolenoid> s) {
157 rollers_back_ = ::std::move(s);
158 }
159
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700160 void Loop(const int iterations) {
161 if (iterations != 1) {
162 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
163 }
Brian Silverman5090c432016-01-02 14:44:26 -0800164
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700165 // Drivetrain
166 {
167 drivetrain_.Fetch();
168 if (drivetrain_.get()) {
169 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
170 drivetrain_left_->Set(drivetrain_->left_high);
171 drivetrain_right_->Set(drivetrain_->right_high);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000172 }
173 }
Comran Morshed41ed7c22015-11-04 21:03:37 +0000174
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700175 // Intake
176 {
177 rollers_.Fetch();
178 if (rollers_.get()) {
179 LOG_STRUCT(DEBUG, "solenoids", *rollers_);
180 rollers_front_->Set(rollers_->front_extended);
181 rollers_back_->Set(rollers_->back_extended);
182 }
183 }
184
185 // Compressor
186 {
187 ::frc971::wpilib::PneumaticsToLog to_log;
188 {
189 // Refill if pneumatic pressure goes too low.
190 const bool compressor_on = !pressure_switch_->Get();
191 to_log.compressor_on = compressor_on;
192 if (compressor_on) {
193 compressor_relay_->Set(::frc::Relay::kForward);
194 } else {
195 compressor_relay_->Set(::frc::Relay::kOff);
196 }
197 }
198
199 pcm_->Flush();
200 to_log.read_solenoids = pcm_->GetAll();
201 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
202 }
203 }
Comran Morshed41ed7c22015-11-04 21:03:37 +0000204
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
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700211 ::std::unique_ptr<::frc::DigitalInput> pressure_switch_;
212 ::std::unique_ptr<::frc::Relay> compressor_relay_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000213
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700214 ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
215 ::aos::Fetcher<::y2014_bot3::control_loops::RollersQueue::Output> rollers_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000216};
217
Comran Morshed41ed7c22015-11-04 21:03:37 +0000218// Writes out rollers voltages.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700219class RollersWriter : public LoopOutputHandler<
220 ::y2014_bot3::control_loops::RollersQueue::Output> {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000221 public:
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800222 RollersWriter(::aos::EventLoop *event_loop)
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700223 : ::frc971::wpilib::LoopOutputHandler<
224 ::y2014_bot3::control_loops::RollersQueue::Output>(
225 event_loop, ".y2014_bot3.control_loops.rollers_queue.output") {}
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800226
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700227 void set_rollers_front_intake_talon(::std::unique_ptr<::frc::Talon> t_left,
228 ::std::unique_ptr<::frc::Talon> t_right) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000229 rollers_front_left_intake_talon_ = ::std::move(t_left);
230 rollers_front_right_intake_talon_ = ::std::move(t_right);
231 }
232
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700233 void set_rollers_back_intake_talon(::std::unique_ptr<::frc::Talon> t_left,
234 ::std::unique_ptr<::frc::Talon> t_right) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000235 rollers_back_left_intake_talon_ = ::std::move(t_left);
236 rollers_back_right_intake_talon_ = ::std::move(t_right);
237 }
238
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700239 void set_rollers_low_goal_talon(::std::unique_ptr<::frc::Talon> t) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000240 rollers_low_goal_talon_ = ::std::move(t);
241 }
242
243 private:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700244 virtual void Write(const ::y2014_bot3::control_loops::RollersQueue::Output
245 &output) override {
246 LOG_STRUCT(DEBUG, "will output", output);
247 rollers_front_left_intake_talon_->SetSpeed(output.front_intake_voltage /
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800248 12.0);
249 rollers_front_right_intake_talon_->SetSpeed(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700250 -(output.front_intake_voltage / 12.0));
251 rollers_back_left_intake_talon_->SetSpeed(output.back_intake_voltage /
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800252 12.0);
253 rollers_back_right_intake_talon_->SetSpeed(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700254 -(output.back_intake_voltage / 12.0));
255 rollers_low_goal_talon_->SetSpeed(output.low_goal_voltage / 12.0);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000256 }
257
258 virtual void Stop() override {
259 LOG(WARNING, "Intake output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800260 rollers_front_left_intake_talon_->SetDisabled();
261 rollers_front_right_intake_talon_->SetDisabled();
262 rollers_back_left_intake_talon_->SetDisabled();
263 rollers_back_right_intake_talon_->SetDisabled();
264 rollers_low_goal_talon_->SetDisabled();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000265 }
266
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700267 ::std::unique_ptr<::frc::Talon> rollers_front_left_intake_talon_,
Comran Morshed41ed7c22015-11-04 21:03:37 +0000268 rollers_back_left_intake_talon_, rollers_front_right_intake_talon_,
269 rollers_back_right_intake_talon_, rollers_low_goal_talon_;
270};
271
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800272class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000273 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700274 ::std::unique_ptr<::frc::Encoder> make_encoder(int index) {
275 return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false,
276 ::frc::Encoder::k4X);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000277 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800278 void Run() override {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700279 // Thread 1.
280 ::aos::ShmEventLoop joystick_sender_event_loop;
281 ::frc971::wpilib::JoystickSender joystick_sender(
282 &joystick_sender_event_loop);
283 AddLoop(&joystick_sender_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000284
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700285 // Thread 2.
286 ::aos::ShmEventLoop pdp_fetcher_event_loop;
287 ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop);
288 AddLoop(&pdp_fetcher_event_loop);
Austin Schuhdf6cbb12019-02-02 13:46:52 -0800289
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700290 // Thread 3.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000291 // Sensors
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700292 ::aos::ShmEventLoop sensor_reader_event_loop;
293 SensorReader sensor_reader(&sensor_reader_event_loop);
294 sensor_reader.set_drivetrain_left_encoder(make_encoder(4));
295 sensor_reader.set_drivetrain_right_encoder(make_encoder(5));
296 AddLoop(&sensor_reader_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000297
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700298 // Thread 4.
299 ::aos::ShmEventLoop gyro_event_loop;
300 GyroSender gyro_sender(&gyro_event_loop);
301 AddLoop(&gyro_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000302
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700303 // Thread 5.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000304 // Outputs
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700305 ::aos::ShmEventLoop output_event_loop;
306 ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop);
Sabina Davis5ba275b2019-02-03 01:18:04 -0800307 drivetrain_writer.set_left_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700308 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(5)), true);
Sabina Davis5ba275b2019-02-03 01:18:04 -0800309 drivetrain_writer.set_right_controller0(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700310 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(2)), false);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000311
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700312 RollersWriter rollers_writer(&output_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000313 rollers_writer.set_rollers_front_intake_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700314 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(3)),
315 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(7)));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000316 rollers_writer.set_rollers_back_intake_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700317 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(1)),
318 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(6)));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000319
320 rollers_writer.set_rollers_low_goal_talon(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700321 ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(4)));
322 AddLoop(&output_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000323
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700324 // Thread 6.
325 ::aos::ShmEventLoop solenoid_writer_event_loop;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000326 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
327 new ::frc971::wpilib::BufferedPcm());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700328 SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000329 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
330 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(5));
331 solenoid_writer.set_rollers_front(pcm->MakeSolenoid(2));
332 solenoid_writer.set_rollers_back(pcm->MakeSolenoid(4));
333
334 // Don't change the following IDs.
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700335 solenoid_writer.set_pressure_switch(make_unique<::frc::DigitalInput>(9));
336 solenoid_writer.set_compressor_relay(make_unique<::frc::Relay>(0));
337 AddLoop(&solenoid_writer_event_loop);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000338
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700339 RunLoops();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000340 }
341};
342
343} // namespace wpilib
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800344} // namespace y2014_bot3
Comran Morshed41ed7c22015-11-04 21:03:37 +0000345
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800346AOS_ROBOT_CLASS(::y2014_bot3::wpilib::WPILibRobot);