Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 1 | #include <inttypes.h> |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 5 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 6 | #include <chrono> |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 7 | #include <functional> |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 8 | #include <mutex> |
| 9 | #include <thread> |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 10 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 11 | #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 Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 18 | #include "frc971/wpilib/wpilib_robot_base.h" |
Austin Schuh | bc9a2ab | 2015-11-26 16:20:17 -0800 | [diff] [blame] | 19 | #undef ERROR |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 20 | |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 21 | #include "aos/events/shm-event-loop.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 22 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 23 | #include "aos/logging/logging.h" |
| 24 | #include "aos/logging/queue_logging.h" |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 25 | #include "aos/make_unique.h" |
| 26 | #include "aos/robot_state/robot_state.q.h" |
| 27 | #include "aos/stl_mutex/stl_mutex.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 28 | #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 Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 32 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 33 | #include "frc971/wpilib/buffered_pcm.h" |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 34 | #include "frc971/wpilib/buffered_solenoid.h" |
Brian Silverman | b5b46ca | 2016-03-13 01:14:17 -0500 | [diff] [blame] | 35 | #include "frc971/wpilib/dma.h" |
Sabina Davis | 5ba275b | 2019-02-03 01:18:04 -0800 | [diff] [blame] | 36 | #include "frc971/wpilib/drivetrain_writer.h" |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 37 | #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 Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 46 | |
| 47 | #ifndef M_PI |
| 48 | #define M_PI 3.14159265358979323846 |
| 49 | #endif |
| 50 | |
| 51 | using ::aos::util::SimpleLogInterval; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 52 | using ::frc971::wpilib::BufferedPcm; |
| 53 | using ::frc971::wpilib::BufferedSolenoid; |
| 54 | using ::frc971::wpilib::LoopOutputHandler; |
| 55 | using ::frc971::wpilib::JoystickSender; |
| 56 | using ::frc971::wpilib::GyroSender; |
Brian Silverman | f819b44 | 2019-01-20 16:51:04 -0800 | [diff] [blame] | 57 | using aos::make_unique; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 58 | |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 59 | namespace y2014_bot3 { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 60 | namespace wpilib { |
| 61 | |
| 62 | double drivetrain_translate(int32_t in) { |
| 63 | return static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) * |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 64 | ::y2014_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio * |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 65 | (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI); |
| 66 | } |
| 67 | |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 68 | double drivetrain_velocity_translate(double in) { |
| 69 | return (1.0 / in) / 256.0 /*cpr*/ * |
Adam Snaider | 83eae56 | 2016-09-10 16:47:33 -0700 | [diff] [blame] | 70 | ::y2014_bot3::control_loops::drivetrain::kDrivetrainEncoderRatio * |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 71 | (4 /*wheel diameter*/ * 2.54 / 100.0 * M_PI); |
| 72 | } |
| 73 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 74 | // Reads in our inputs. (sensors, voltages, etc.) |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 75 | class SensorReader : public ::frc971::wpilib::SensorReader { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 76 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 77 | SensorReader(::aos::EventLoop *event_loop) |
Austin Schuh | 30020d9 | 2019-05-27 13:07:02 -0700 | [diff] [blame] | 78 | : ::frc971::wpilib::SensorReader(event_loop), |
| 79 | rollers_position_sender_( |
| 80 | event_loop->MakeSender< |
| 81 | ::y2014_bot3::control_loops::RollersQueue::Position>( |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame^] | 82 | ".y2014_bot3.control_loops.rollers_queue.position")), |
| 83 | drivetrain_position_sender_( |
| 84 | event_loop->MakeSender< |
| 85 | ::frc971::control_loops::DrivetrainQueue::Position>( |
| 86 | ".frc971.control_loops.drivetrain_queue.position")) {} |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 87 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 88 | void RunIteration() { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 89 | // Drivetrain |
| 90 | { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame^] | 91 | auto drivetrain_message = drivetrain_position_sender_.MakeMessage(); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 92 | drivetrain_message->right_encoder = |
Austin Schuh | ba32a9e | 2018-11-03 15:28:13 -0700 | [diff] [blame] | 93 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 94 | drivetrain_message->left_encoder = |
Austin Schuh | ba32a9e | 2018-11-03 15:28:13 -0700 | [diff] [blame] | 95 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
Brian Silverman | 51091a0 | 2015-12-26 15:56:58 -0800 | [diff] [blame] | 96 | drivetrain_message->left_speed = |
| 97 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 98 | drivetrain_message->right_speed = |
| 99 | drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 100 | |
| 101 | drivetrain_message.Send(); |
| 102 | } |
| 103 | |
| 104 | // Rollers |
| 105 | { |
Austin Schuh | 30020d9 | 2019-05-27 13:07:02 -0700 | [diff] [blame] | 106 | auto rollers_message = rollers_position_sender_.MakeMessage(); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 107 | rollers_message.Send(); |
| 108 | } |
| 109 | } |
Austin Schuh | 30020d9 | 2019-05-27 13:07:02 -0700 | [diff] [blame] | 110 | |
| 111 | private: |
| 112 | ::aos::Sender<::y2014_bot3::control_loops::RollersQueue::Position> |
| 113 | rollers_position_sender_; |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame^] | 114 | ::aos::Sender<::frc971::control_loops::DrivetrainQueue::Position> |
| 115 | drivetrain_position_sender_; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // Writes out our pneumatic outputs. |
| 119 | class SolenoidWriter { |
| 120 | public: |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 121 | SolenoidWriter(::aos::EventLoop *event_loop, |
| 122 | const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm) |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 123 | : pcm_(pcm), |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 124 | drivetrain_( |
| 125 | event_loop |
| 126 | ->MakeFetcher<::frc971::control_loops::DrivetrainQueue::Output>( |
| 127 | ".frc971.control_loops.drivetrain_queue.output")), |
| 128 | rollers_(event_loop->MakeFetcher< |
| 129 | ::y2014_bot3::control_loops::RollersQueue::Output>( |
| 130 | ".y2014_bot3.control_loops.rollers_queue.output")) { |
| 131 | event_loop->set_name("Solenoids"); |
| 132 | event_loop->SetRuntimeRealtimePriority(27); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 133 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 134 | event_loop->AddPhasedLoop([this](int iterations) { Loop(iterations); }, |
| 135 | ::std::chrono::milliseconds(20), |
| 136 | ::std::chrono::milliseconds(1)); |
| 137 | } |
| 138 | |
| 139 | void set_pressure_switch( |
| 140 | ::std::unique_ptr<::frc::DigitalInput> pressure_switch) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 141 | pressure_switch_ = ::std::move(pressure_switch); |
| 142 | } |
| 143 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 144 | void set_compressor_relay(::std::unique_ptr<::frc::Relay> compressor_relay) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 145 | compressor_relay_ = ::std::move(compressor_relay); |
| 146 | } |
| 147 | |
| 148 | void set_drivetrain_left(::std::unique_ptr<BufferedSolenoid> s) { |
| 149 | drivetrain_left_ = ::std::move(s); |
| 150 | } |
| 151 | |
| 152 | void set_drivetrain_right(::std::unique_ptr<BufferedSolenoid> s) { |
| 153 | drivetrain_right_ = ::std::move(s); |
| 154 | } |
| 155 | |
| 156 | void set_rollers_front(::std::unique_ptr<BufferedSolenoid> s) { |
| 157 | rollers_front_ = ::std::move(s); |
| 158 | } |
| 159 | |
| 160 | void set_rollers_back(::std::unique_ptr<BufferedSolenoid> s) { |
| 161 | rollers_back_ = ::std::move(s); |
| 162 | } |
| 163 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 164 | void Loop(const int iterations) { |
| 165 | if (iterations != 1) { |
| 166 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 167 | } |
Brian Silverman | 5090c43 | 2016-01-02 14:44:26 -0800 | [diff] [blame] | 168 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 169 | // Drivetrain |
| 170 | { |
| 171 | drivetrain_.Fetch(); |
| 172 | if (drivetrain_.get()) { |
| 173 | LOG_STRUCT(DEBUG, "solenoids", *drivetrain_); |
| 174 | drivetrain_left_->Set(drivetrain_->left_high); |
| 175 | drivetrain_right_->Set(drivetrain_->right_high); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 178 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 179 | // Intake |
| 180 | { |
| 181 | rollers_.Fetch(); |
| 182 | if (rollers_.get()) { |
| 183 | LOG_STRUCT(DEBUG, "solenoids", *rollers_); |
| 184 | rollers_front_->Set(rollers_->front_extended); |
| 185 | rollers_back_->Set(rollers_->back_extended); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // Compressor |
| 190 | { |
| 191 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 192 | { |
| 193 | // Refill if pneumatic pressure goes too low. |
| 194 | const bool compressor_on = !pressure_switch_->Get(); |
| 195 | to_log.compressor_on = compressor_on; |
| 196 | if (compressor_on) { |
| 197 | compressor_relay_->Set(::frc::Relay::kForward); |
| 198 | } else { |
| 199 | compressor_relay_->Set(::frc::Relay::kOff); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | pcm_->Flush(); |
| 204 | to_log.read_solenoids = pcm_->GetAll(); |
| 205 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 206 | } |
| 207 | } |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 208 | |
| 209 | private: |
| 210 | const ::std::unique_ptr<BufferedPcm> &pcm_; |
| 211 | |
| 212 | ::std::unique_ptr<BufferedSolenoid> drivetrain_left_, drivetrain_right_; |
| 213 | ::std::unique_ptr<BufferedSolenoid> rollers_front_, rollers_back_; |
| 214 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 215 | ::std::unique_ptr<::frc::DigitalInput> pressure_switch_; |
| 216 | ::std::unique_ptr<::frc::Relay> compressor_relay_; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 217 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 218 | ::aos::Fetcher<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_; |
| 219 | ::aos::Fetcher<::y2014_bot3::control_loops::RollersQueue::Output> rollers_; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 222 | // Writes out rollers voltages. |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 223 | class RollersWriter : public LoopOutputHandler< |
| 224 | ::y2014_bot3::control_loops::RollersQueue::Output> { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 225 | public: |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 226 | RollersWriter(::aos::EventLoop *event_loop) |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 227 | : ::frc971::wpilib::LoopOutputHandler< |
| 228 | ::y2014_bot3::control_loops::RollersQueue::Output>( |
| 229 | event_loop, ".y2014_bot3.control_loops.rollers_queue.output") {} |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 230 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 231 | void set_rollers_front_intake_talon(::std::unique_ptr<::frc::Talon> t_left, |
| 232 | ::std::unique_ptr<::frc::Talon> t_right) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 233 | rollers_front_left_intake_talon_ = ::std::move(t_left); |
| 234 | rollers_front_right_intake_talon_ = ::std::move(t_right); |
| 235 | } |
| 236 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 237 | void set_rollers_back_intake_talon(::std::unique_ptr<::frc::Talon> t_left, |
| 238 | ::std::unique_ptr<::frc::Talon> t_right) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 239 | rollers_back_left_intake_talon_ = ::std::move(t_left); |
| 240 | rollers_back_right_intake_talon_ = ::std::move(t_right); |
| 241 | } |
| 242 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 243 | void set_rollers_low_goal_talon(::std::unique_ptr<::frc::Talon> t) { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 244 | rollers_low_goal_talon_ = ::std::move(t); |
| 245 | } |
| 246 | |
| 247 | private: |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 248 | virtual void Write(const ::y2014_bot3::control_loops::RollersQueue::Output |
| 249 | &output) override { |
| 250 | LOG_STRUCT(DEBUG, "will output", output); |
| 251 | rollers_front_left_intake_talon_->SetSpeed(output.front_intake_voltage / |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 252 | 12.0); |
| 253 | rollers_front_right_intake_talon_->SetSpeed( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 254 | -(output.front_intake_voltage / 12.0)); |
| 255 | rollers_back_left_intake_talon_->SetSpeed(output.back_intake_voltage / |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 256 | 12.0); |
| 257 | rollers_back_right_intake_talon_->SetSpeed( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 258 | -(output.back_intake_voltage / 12.0)); |
| 259 | rollers_low_goal_talon_->SetSpeed(output.low_goal_voltage / 12.0); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | virtual void Stop() override { |
| 263 | LOG(WARNING, "Intake output too old\n"); |
Brian Silverman | 6eaa2d8 | 2017-02-04 20:48:30 -0800 | [diff] [blame] | 264 | rollers_front_left_intake_talon_->SetDisabled(); |
| 265 | rollers_front_right_intake_talon_->SetDisabled(); |
| 266 | rollers_back_left_intake_talon_->SetDisabled(); |
| 267 | rollers_back_right_intake_talon_->SetDisabled(); |
| 268 | rollers_low_goal_talon_->SetDisabled(); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 271 | ::std::unique_ptr<::frc::Talon> rollers_front_left_intake_talon_, |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 272 | rollers_back_left_intake_talon_, rollers_front_right_intake_talon_, |
| 273 | rollers_back_right_intake_talon_, rollers_low_goal_talon_; |
| 274 | }; |
| 275 | |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 276 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 277 | public: |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 278 | ::std::unique_ptr<::frc::Encoder> make_encoder(int index) { |
| 279 | return make_unique<::frc::Encoder>(10 + index * 2, 11 + index * 2, false, |
| 280 | ::frc::Encoder::k4X); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 281 | } |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 282 | void Run() override { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 283 | // Thread 1. |
| 284 | ::aos::ShmEventLoop joystick_sender_event_loop; |
| 285 | ::frc971::wpilib::JoystickSender joystick_sender( |
| 286 | &joystick_sender_event_loop); |
| 287 | AddLoop(&joystick_sender_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 288 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 289 | // Thread 2. |
| 290 | ::aos::ShmEventLoop pdp_fetcher_event_loop; |
| 291 | ::frc971::wpilib::PDPFetcher pdp_fetcher(&pdp_fetcher_event_loop); |
| 292 | AddLoop(&pdp_fetcher_event_loop); |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 293 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 294 | // Thread 3. |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 295 | // Sensors |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 296 | ::aos::ShmEventLoop sensor_reader_event_loop; |
| 297 | SensorReader sensor_reader(&sensor_reader_event_loop); |
| 298 | sensor_reader.set_drivetrain_left_encoder(make_encoder(4)); |
| 299 | sensor_reader.set_drivetrain_right_encoder(make_encoder(5)); |
| 300 | AddLoop(&sensor_reader_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 301 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 302 | // Thread 4. |
| 303 | ::aos::ShmEventLoop gyro_event_loop; |
| 304 | GyroSender gyro_sender(&gyro_event_loop); |
| 305 | AddLoop(&gyro_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 306 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 307 | // Thread 5. |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 308 | // Outputs |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 309 | ::aos::ShmEventLoop output_event_loop; |
| 310 | ::frc971::wpilib::DrivetrainWriter drivetrain_writer(&output_event_loop); |
Sabina Davis | 5ba275b | 2019-02-03 01:18:04 -0800 | [diff] [blame] | 311 | drivetrain_writer.set_left_controller0( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 312 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(5)), true); |
Sabina Davis | 5ba275b | 2019-02-03 01:18:04 -0800 | [diff] [blame] | 313 | drivetrain_writer.set_right_controller0( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 314 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(2)), false); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 315 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 316 | RollersWriter rollers_writer(&output_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 317 | rollers_writer.set_rollers_front_intake_talon( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 318 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(3)), |
| 319 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(7))); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 320 | rollers_writer.set_rollers_back_intake_talon( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 321 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(1)), |
| 322 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(6))); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 323 | |
| 324 | rollers_writer.set_rollers_low_goal_talon( |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 325 | ::std::unique_ptr<::frc::Talon>(new ::frc::Talon(4))); |
| 326 | AddLoop(&output_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 327 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 328 | // Thread 6. |
| 329 | ::aos::ShmEventLoop solenoid_writer_event_loop; |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 330 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 331 | new ::frc971::wpilib::BufferedPcm()); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 332 | SolenoidWriter solenoid_writer(&solenoid_writer_event_loop, pcm); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 333 | solenoid_writer.set_drivetrain_left(pcm->MakeSolenoid(6)); |
| 334 | solenoid_writer.set_drivetrain_right(pcm->MakeSolenoid(5)); |
| 335 | solenoid_writer.set_rollers_front(pcm->MakeSolenoid(2)); |
| 336 | solenoid_writer.set_rollers_back(pcm->MakeSolenoid(4)); |
| 337 | |
| 338 | // Don't change the following IDs. |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 339 | solenoid_writer.set_pressure_switch(make_unique<::frc::DigitalInput>(9)); |
| 340 | solenoid_writer.set_compressor_relay(make_unique<::frc::Relay>(0)); |
| 341 | AddLoop(&solenoid_writer_event_loop); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 342 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 343 | RunLoops(); |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 344 | } |
| 345 | }; |
| 346 | |
| 347 | } // namespace wpilib |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 348 | } // namespace y2014_bot3 |
Comran Morshed | 41ed7c2 | 2015-11-04 21:03:37 +0000 | [diff] [blame] | 349 | |
Austin Schuh | 4d11f9a | 2019-02-02 16:30:06 -0800 | [diff] [blame] | 350 | AOS_ROBOT_CLASS(::y2014_bot3::wpilib::WPILibRobot); |