blob: 56d72f1e79ad9efee15dd69feea12195d66d8dea [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
Brian Silvermanf819b442019-01-20 16:51:04 -080021#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -070022#include "aos/logging/logging.h"
23#include "aos/logging/queue_logging.h"
Brian Silvermanf819b442019-01-20 16:51:04 -080024#include "aos/make_unique.h"
25#include "aos/robot_state/robot_state.q.h"
26#include "aos/stl_mutex/stl_mutex.h"
John Park33858a32018-09-28 23:05:48 -070027#include "aos/time/time.h"
28#include "aos/util/log_interval.h"
29#include "aos/util/phased_loop.h"
30#include "aos/util/wrapping_counter.h"
Adam Snaider83eae562016-09-10 16:47:33 -070031#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Comran Morshed41ed7c22015-11-04 21:03:37 +000032#include "frc971/wpilib/buffered_pcm.h"
Austin Schuh4d11f9a2019-02-02 16:30:06 -080033#include "frc971/wpilib/buffered_solenoid.h"
Brian Silvermanb5b46ca2016-03-13 01:14:17 -050034#include "frc971/wpilib/dma.h"
Sabina Davis5ba275b2019-02-03 01:18:04 -080035#include "frc971/wpilib/drivetrain_writer.h"
Austin Schuh4d11f9a2019-02-02 16:30:06 -080036#include "frc971/wpilib/gyro_sender.h"
37#include "frc971/wpilib/joystick_sender.h"
38#include "frc971/wpilib/logging.q.h"
39#include "frc971/wpilib/loop_output_handler.h"
40#include "frc971/wpilib/pdp_fetcher.h"
41#include "frc971/wpilib/sensor_reader.h"
42#include "y2014_bot3/control_loops/drivetrain/drivetrain_base.h"
43#include "y2014_bot3/control_loops/rollers/rollers.h"
44#include "y2014_bot3/control_loops/rollers/rollers.q.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;
Adam Snaider83eae562016-09-10 16:47:33 -070051using ::frc971::control_loops::drivetrain_queue;
Comran Morshed41ed7c22015-11-04 21:03:37 +000052using ::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;
Parker Schuhd3b7a8872018-02-19 16:42:27 -080058using namespace frc;
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:
Comran Morshed41ed7c22015-11-04 21:03:37 +000079 void RunIteration() {
Comran Morshed41ed7c22015-11-04 21:03:37 +000080 // Drivetrain
81 {
82 auto drivetrain_message = drivetrain_queue.position.MakeMessage();
83 drivetrain_message->right_encoder =
Austin Schuhba32a9e2018-11-03 15:28:13 -070084 -drivetrain_translate(drivetrain_right_encoder_->GetRaw());
Comran Morshed41ed7c22015-11-04 21:03:37 +000085 drivetrain_message->left_encoder =
Austin Schuhba32a9e2018-11-03 15:28:13 -070086 drivetrain_translate(drivetrain_left_encoder_->GetRaw());
Brian Silverman51091a02015-12-26 15:56:58 -080087 drivetrain_message->left_speed =
88 drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod());
89 drivetrain_message->right_speed =
90 drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod());
Comran Morshed41ed7c22015-11-04 21:03:37 +000091
92 drivetrain_message.Send();
93 }
94
95 // Rollers
96 {
97 auto rollers_message = rollers_queue.position.MakeMessage();
98 rollers_message.Send();
99 }
100 }
Comran Morshed41ed7c22015-11-04 21:03:37 +0000101};
102
103// Writes out our pneumatic outputs.
104class SolenoidWriter {
105 public:
106 SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm)
107 : pcm_(pcm),
Adam Snaider83eae562016-09-10 16:47:33 -0700108 drivetrain_(".frc971.control_loops.drivetrain_queue.output"),
Comran Morshed41ed7c22015-11-04 21:03:37 +0000109 rollers_(".y2014_bot3.control_loops.rollers_queue.output") {}
110
111 void set_pressure_switch(::std::unique_ptr<DigitalInput> pressure_switch) {
112 pressure_switch_ = ::std::move(pressure_switch);
113 }
114
115 void set_compressor_relay(::std::unique_ptr<Relay> compressor_relay) {
116 compressor_relay_ = ::std::move(compressor_relay);
117 }
118
119 void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) {
120 drivetrain_left_ = ::std::move(s);
121 }
122
123 void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) {
124 drivetrain_right_ = ::std::move(s);
125 }
126
127 void set_rollers_front(::std::unique_ptr<BufferedSolenoid> s) {
128 rollers_front_ = ::std::move(s);
129 }
130
131 void set_rollers_back(::std::unique_ptr<BufferedSolenoid> s) {
132 rollers_back_ = ::std::move(s);
133 }
134
135 void operator()() {
136 ::aos::SetCurrentThreadName("Solenoids");
Brian Silverman5090c432016-01-02 14:44:26 -0800137 ::aos::SetCurrentThreadRealtimePriority(27);
138
Austin Schuh8aec1ed2016-05-01 13:29:20 -0700139 ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20),
140 ::std::chrono::milliseconds(1));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000141
142 while (run_) {
Brian Silverman5090c432016-01-02 14:44:26 -0800143 {
144 const int iterations = phased_loop.SleepUntilNext();
145 if (iterations != 1) {
146 LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1);
147 }
148 }
149
Comran Morshed41ed7c22015-11-04 21:03:37 +0000150 // Drivetrain
151 {
152 drivetrain_.FetchLatest();
153 if (drivetrain_.get()) {
154 LOG_STRUCT(DEBUG, "solenoids", *drivetrain_);
155 drivetrain_left_->Set(drivetrain_->left_high);
156 drivetrain_right_->Set(drivetrain_->right_high);
157 }
158 }
159
160 // Intake
161 {
162 rollers_.FetchLatest();
163 if (rollers_.get()) {
164 LOG_STRUCT(DEBUG, "solenoids", *rollers_);
165 rollers_front_->Set(rollers_->front_extended);
166 rollers_back_->Set(rollers_->back_extended);
167 }
168 }
169
170 // Compressor
171 ::aos::joystick_state.FetchLatest();
172 {
173 ::frc971::wpilib::PneumaticsToLog to_log;
174 {
175 // Refill if pneumatic pressure goes too low.
176 const bool compressor_on = !pressure_switch_->Get();
177 to_log.compressor_on = compressor_on;
178 if (compressor_on) {
179 compressor_relay_->Set(Relay::kForward);
180 } else {
181 compressor_relay_->Set(Relay::kOff);
182 }
183 }
184
185 pcm_->Flush();
186 to_log.read_solenoids = pcm_->GetAll();
187 LOG_STRUCT(DEBUG, "pneumatics info", to_log);
188 }
189 }
190 }
191
192 void Quit() { run_ = false; }
193
194 private:
195 const ::std::unique_ptr<BufferedPcm> &pcm_;
196
197 ::std::unique_ptr<BufferedSolenoid> drivetrain_left_, drivetrain_right_;
198 ::std::unique_ptr<BufferedSolenoid> rollers_front_, rollers_back_;
199
200 ::std::unique_ptr<DigitalInput> pressure_switch_;
201 ::std::unique_ptr<Relay> compressor_relay_;
202
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800203 ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000204 ::aos::Queue<::y2014_bot3::control_loops::RollersQueue::Output> rollers_;
205
206 ::std::atomic<bool> run_{true};
207};
208
Comran Morshed41ed7c22015-11-04 21:03:37 +0000209// Writes out rollers voltages.
210class RollersWriter : public LoopOutputHandler {
211 public:
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800212 void set_rollers_front_intake_talon(::std::unique_ptr<Talon> t_left,
213 ::std::unique_ptr<Talon> t_right) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000214 rollers_front_left_intake_talon_ = ::std::move(t_left);
215 rollers_front_right_intake_talon_ = ::std::move(t_right);
216 }
217
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800218 void set_rollers_back_intake_talon(::std::unique_ptr<Talon> t_left,
219 ::std::unique_ptr<Talon> t_right) {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000220 rollers_back_left_intake_talon_ = ::std::move(t_left);
221 rollers_back_right_intake_talon_ = ::std::move(t_right);
222 }
223
224 void set_rollers_low_goal_talon(::std::unique_ptr<Talon> t) {
225 rollers_low_goal_talon_ = ::std::move(t);
226 }
227
228 private:
229 virtual void Read() override {
230 ::y2014_bot3::control_loops::rollers_queue.output.FetchAnother();
231 }
232
233 virtual void Write() override {
234 auto &queue = ::y2014_bot3::control_loops::rollers_queue.output;
235 LOG_STRUCT(DEBUG, "will output", *queue);
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800236 rollers_front_left_intake_talon_->SetSpeed(queue->front_intake_voltage /
237 12.0);
238 rollers_front_right_intake_talon_->SetSpeed(
239 -(queue->front_intake_voltage / 12.0));
240 rollers_back_left_intake_talon_->SetSpeed(queue->back_intake_voltage /
241 12.0);
242 rollers_back_right_intake_talon_->SetSpeed(
243 -(queue->back_intake_voltage / 12.0));
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800244 rollers_low_goal_talon_->SetSpeed(queue->low_goal_voltage / 12.0);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000245 }
246
247 virtual void Stop() override {
248 LOG(WARNING, "Intake output too old\n");
Brian Silverman6eaa2d82017-02-04 20:48:30 -0800249 rollers_front_left_intake_talon_->SetDisabled();
250 rollers_front_right_intake_talon_->SetDisabled();
251 rollers_back_left_intake_talon_->SetDisabled();
252 rollers_back_right_intake_talon_->SetDisabled();
253 rollers_low_goal_talon_->SetDisabled();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000254 }
255
256 ::std::unique_ptr<Talon> rollers_front_left_intake_talon_,
257 rollers_back_left_intake_talon_, rollers_front_right_intake_talon_,
258 rollers_back_right_intake_talon_, rollers_low_goal_talon_;
259};
260
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800261class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000262 public:
263 ::std::unique_ptr<Encoder> make_encoder(int index) {
264 return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false,
265 Encoder::k4X);
266 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -0800267 void Run() override {
Comran Morshed41ed7c22015-11-04 21:03:37 +0000268 ::aos::InitNRT();
269 ::aos::SetCurrentThreadName("StartCompetition");
270
271 JoystickSender joystick_sender;
272 ::std::thread joystick_thread(::std::ref(joystick_sender));
273
Brian Silverman425492b2015-12-30 15:23:55 -0800274 ::frc971::wpilib::PDPFetcher pdp_fetcher;
275 ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher));
276
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800277 // TODO(comran): IO ports are placeholders at the moment, so match them to
Comran Morshed41ed7c22015-11-04 21:03:37 +0000278 // the robot before turning on.
279
280 // Sensors
Brian Silverman39b339e2016-01-03 13:24:22 -0800281 SensorReader reader;
Comran Morshed41ed7c22015-11-04 21:03:37 +0000282 reader.set_drivetrain_left_encoder(make_encoder(4));
283 reader.set_drivetrain_right_encoder(make_encoder(5));
284
285 ::std::thread reader_thread(::std::ref(reader));
286 GyroSender gyro_sender;
287 ::std::thread gyro_thread(::std::ref(gyro_sender));
288
289 // Outputs
Sabina Davis5ba275b2019-02-03 01:18:04 -0800290 ::frc971::wpilib::DrivetrainWriter drivetrain_writer;
291 drivetrain_writer.set_left_controller0(
292 ::std::unique_ptr<Talon>(new Talon(5)), true);
293 drivetrain_writer.set_right_controller0(
294 ::std::unique_ptr<Talon>(new Talon(2)), false);
Comran Morshed41ed7c22015-11-04 21:03:37 +0000295 ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer));
296
297 RollersWriter rollers_writer;
298 rollers_writer.set_rollers_front_intake_talon(
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800299 ::std::unique_ptr<Talon>(new Talon(3)),
300 ::std::unique_ptr<Talon>(new Talon(7)));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000301 rollers_writer.set_rollers_back_intake_talon(
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800302 ::std::unique_ptr<Talon>(new Talon(1)),
303 ::std::unique_ptr<Talon>(new Talon(6)));
Comran Morshed41ed7c22015-11-04 21:03:37 +0000304
305 rollers_writer.set_rollers_low_goal_talon(
306 ::std::unique_ptr<Talon>(new Talon(4)));
307 ::std::thread rollers_writer_thread(::std::ref(rollers_writer));
308
309 ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm(
310 new ::frc971::wpilib::BufferedPcm());
311 SolenoidWriter solenoid_writer(pcm);
312 solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6));
313 solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(5));
314 solenoid_writer.set_rollers_front(pcm->MakeSolenoid(2));
315 solenoid_writer.set_rollers_back(pcm->MakeSolenoid(4));
316
317 // Don't change the following IDs.
318 solenoid_writer.set_pressure_switch(make_unique<DigitalInput>(9));
319 solenoid_writer.set_compressor_relay(make_unique<Relay>(0));
320 ::std::thread solenoid_thread(::std::ref(solenoid_writer));
321
322 // Wait forever. Not much else to do...
Brian Silverman5090c432016-01-02 14:44:26 -0800323 while (true) {
324 const int r = select(0, nullptr, nullptr, nullptr, nullptr);
325 if (r != 0) {
326 PLOG(WARNING, "infinite select failed");
327 } else {
328 PLOG(WARNING, "infinite select succeeded??\n");
329 }
330 }
Comran Morshed41ed7c22015-11-04 21:03:37 +0000331
332 LOG(ERROR, "Exiting WPILibRobot\n");
333
334 joystick_sender.Quit();
335 joystick_thread.join();
Brian Silverman425492b2015-12-30 15:23:55 -0800336 pdp_fetcher.Quit();
337 pdp_fetcher_thread.join();
Comran Morshed41ed7c22015-11-04 21:03:37 +0000338 reader.Quit();
339 reader_thread.join();
340 gyro_sender.Quit();
341 gyro_thread.join();
342
343 drivetrain_writer.Quit();
344 drivetrain_writer_thread.join();
345
346 rollers_writer.Quit();
347 rollers_writer_thread.join();
348
349 solenoid_writer.Quit();
350 solenoid_thread.join();
351
352 ::aos::Cleanup();
353 }
354};
355
356} // namespace wpilib
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800357} // namespace y2014_bot3
Comran Morshed41ed7c22015-11-04 21:03:37 +0000358
Austin Schuh4d11f9a2019-02-02 16:30:06 -0800359AOS_ROBOT_CLASS(::y2014_bot3::wpilib::WPILibRobot);