Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <unistd.h> |
| 4 | #include <inttypes.h> |
| 5 | |
| 6 | #include <thread> |
| 7 | #include <mutex> |
| 8 | #include <functional> |
| 9 | #include <array> |
| 10 | |
| 11 | #include "Encoder.h" |
| 12 | #include "Talon.h" |
| 13 | #include "Relay.h" |
| 14 | #include "DriverStation.h" |
| 15 | #include "AnalogInput.h" |
| 16 | #include "Compressor.h" |
| 17 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 18 | #include "DigitalGlitchFilter.h" |
| 19 | #undef ERROR |
| 20 | |
| 21 | #include "aos/common/logging/logging.h" |
| 22 | #include "aos/common/logging/queue_logging.h" |
| 23 | #include "aos/common/time.h" |
| 24 | #include "aos/common/util/log_interval.h" |
| 25 | #include "aos/common/util/phased_loop.h" |
| 26 | #include "aos/common/util/wrapping_counter.h" |
| 27 | #include "aos/common/stl_mutex.h" |
| 28 | #include "aos/linux_code/init.h" |
| 29 | #include "aos/common/messages/robot_state.q.h" |
| 30 | #include "aos/common/commonmath.h" |
| 31 | |
| 32 | #include "frc971/control_loops/control_loops.q.h" |
| 33 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 34 | #include "y2016_bot3/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
| 35 | #include "y2016_bot3/control_loops/intake/intake.q.h" |
| 36 | #include "y2016_bot3/queues/ball_detector.q.h" |
| 37 | #include "y2016_bot3/actors/autonomous_action.q.h" |
| 38 | |
| 39 | #include "frc971/wpilib/joystick_sender.h" |
| 40 | #include "frc971/wpilib/loop_output_handler.h" |
| 41 | #include "frc971/wpilib/buffered_solenoid.h" |
| 42 | #include "frc971/wpilib/buffered_pcm.h" |
| 43 | #include "frc971/wpilib/gyro_sender.h" |
| 44 | #include "frc971/wpilib/dma_edge_counting.h" |
| 45 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 46 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 47 | #include "frc971/wpilib/logging.q.h" |
| 48 | #include "frc971/wpilib/wpilib_interface.h" |
| 49 | #include "frc971/wpilib/pdp_fetcher.h" |
| 50 | #include "frc971/wpilib/dma.h" |
| 51 | |
| 52 | #include "y2016_bot3/control_loops/intake/intake.h" |
| 53 | #include "y2016_bot3/control_loops/drivetrain/drivetrain_base.h" |
| 54 | |
| 55 | #ifndef M_PI |
| 56 | #define M_PI 3.14159265358979323846 |
| 57 | #endif |
| 58 | |
| 59 | using ::frc971::control_loops::drivetrain_queue; |
| 60 | using ::y2016_bot3::control_loops::intake_queue; |
| 61 | |
| 62 | namespace y2016_bot3 { |
| 63 | namespace constants { |
| 64 | IntakeZero intake_zero; |
| 65 | } |
| 66 | namespace wpilib { |
| 67 | namespace { |
| 68 | constexpr double kMaxBringupPower = 12.0; |
| 69 | } // namespace |
| 70 | |
| 71 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 72 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 73 | // The low bit is direction. |
| 74 | |
| 75 | // TODO(brian): Replace this with ::std::make_unique once all our toolchains |
| 76 | // have support. |
| 77 | template <class T, class... U> |
| 78 | std::unique_ptr<T> make_unique(U &&... u) { |
| 79 | return std::unique_ptr<T>(new T(std::forward<U>(u)...)); |
| 80 | } |
| 81 | |
| 82 | // TODO(Campbell): Update values |
| 83 | // Translates for the sensor values to convert raw index pulses into something |
| 84 | // with proper units. |
| 85 | |
| 86 | double drivetrain_translate(int32_t in) { |
Campbell Crowley | 051b462 | 2016-11-05 15:07:54 -0700 | [diff] [blame] | 87 | return -static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) * |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 88 | ::y2016_bot3::constants::kDrivetrainEncoderRatio * |
| 89 | control_loops::drivetrain::kWheelRadius * 2.0 * M_PI; |
| 90 | } |
| 91 | |
| 92 | double drivetrain_velocity_translate(double in) { |
Campbell Crowley | 051b462 | 2016-11-05 15:07:54 -0700 | [diff] [blame] | 93 | return (1.0 / in) / 512.0 /*cpr*/ * |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 94 | ::y2016_bot3::constants::kDrivetrainEncoderRatio * |
| 95 | control_loops::drivetrain::kWheelRadius * 2.0 * M_PI; |
| 96 | } |
| 97 | |
| 98 | double intake_translate(int32_t in) { |
| 99 | return static_cast<double>(in) / (512.0 /*cpr*/ * 4.0 /*4x*/) * |
| 100 | ::y2016_bot3::constants::kIntakeEncoderRatio * (2 * M_PI /*radians*/); |
| 101 | } |
| 102 | |
| 103 | double intake_pot_translate(double voltage) { |
| 104 | return voltage * ::y2016_bot3::constants::kIntakePotRatio * |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 105 | (5.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | constexpr double kMaxDrivetrainEncoderPulsesPerSecond = |
| 109 | 5600.0 /* CIM free speed RPM */ * 14.0 / 48.0 /* 1st reduction */ * 28.0 / |
| 110 | 50.0 /* 2nd reduction (high gear) */ * 30.0 / 44.0 /* encoder gears */ / |
| 111 | 60.0 /* seconds per minute */ * 256.0 /* CPR */ * 4 /* edges per cycle */; |
| 112 | |
| 113 | // Class to send position messages with sensor readings to our loops. |
| 114 | class SensorReader { |
| 115 | public: |
| 116 | SensorReader() { |
| 117 | // Set it to filter out anything shorter than 1/4 of the minimum pulse width |
| 118 | // we should ever see. |
| 119 | drivetrain_encoder_filter_.SetPeriodNanoSeconds( |
| 120 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 121 | kMaxDrivetrainEncoderPulsesPerSecond * 1e9 + |
| 122 | 0.5)); |
| 123 | } |
| 124 | |
| 125 | // Drivetrain setters. |
| 126 | void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) { |
| 127 | drivetrain_encoder_filter_.Add(encoder.get()); |
| 128 | drivetrain_left_encoder_ = ::std::move(encoder); |
| 129 | } |
| 130 | |
| 131 | void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) { |
| 132 | drivetrain_encoder_filter_.Add(encoder.get()); |
| 133 | drivetrain_right_encoder_ = ::std::move(encoder); |
| 134 | } |
| 135 | |
| 136 | // Intake setters. |
| 137 | void set_intake_encoder(::std::unique_ptr<Encoder> encoder) { |
| 138 | intake_encoder_filter_.Add(encoder.get()); |
| 139 | intake_encoder_.set_encoder(::std::move(encoder)); |
| 140 | } |
| 141 | |
| 142 | void set_intake_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) { |
| 143 | intake_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 144 | } |
| 145 | |
| 146 | void set_intake_index(::std::unique_ptr<DigitalInput> index) { |
| 147 | intake_encoder_filter_.Add(index.get()); |
| 148 | intake_encoder_.set_index(::std::move(index)); |
| 149 | } |
| 150 | |
| 151 | // Ball detector setter. |
| 152 | void set_ball_detector(::std::unique_ptr<AnalogInput> analog) { |
| 153 | ball_detector_ = ::std::move(analog); |
| 154 | } |
| 155 | |
| 156 | // All of the DMA-related set_* calls must be made before this, and it doesn't |
| 157 | // hurt to do all of them. |
| 158 | |
| 159 | void set_dma(::std::unique_ptr<DMA> dma) { |
| 160 | dma_synchronizer_.reset( |
| 161 | new ::frc971::wpilib::DMASynchronizer(::std::move(dma))); |
| 162 | dma_synchronizer_->Add(&intake_encoder_); |
| 163 | } |
| 164 | |
| 165 | void operator()() { |
| 166 | ::aos::SetCurrentThreadName("SensorReader"); |
| 167 | |
| 168 | my_pid_ = getpid(); |
| 169 | ds_ = &DriverStation::GetInstance(); |
| 170 | |
| 171 | dma_synchronizer_->Start(); |
| 172 | |
Campbell Crowley | abe04c4 | 2016-10-23 14:42:27 -0700 | [diff] [blame] | 173 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(5), |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 174 | ::std::chrono::milliseconds(0)); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 175 | |
| 176 | ::aos::SetCurrentThreadRealtimePriority(40); |
| 177 | while (run_) { |
| 178 | { |
| 179 | const int iterations = phased_loop.SleepUntilNext(); |
| 180 | if (iterations != 1) { |
| 181 | LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1); |
| 182 | } |
| 183 | } |
| 184 | RunIteration(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void RunIteration() { |
| 189 | ::frc971::wpilib::SendRobotState(my_pid_, ds_); |
| 190 | |
| 191 | const auto intake_pot_offset = |
| 192 | y2016_bot3::constants::intake_zero.pot_offset; |
| 193 | |
| 194 | { |
| 195 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 196 | drivetrain_message->right_encoder = |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 197 | drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 198 | drivetrain_message->left_encoder = |
| 199 | -drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 200 | drivetrain_message->left_speed = |
| 201 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 202 | drivetrain_message->right_speed = |
| 203 | drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
| 204 | |
| 205 | drivetrain_message.Send(); |
| 206 | } |
| 207 | |
| 208 | dma_synchronizer_->RunIteration(); |
| 209 | |
| 210 | { |
| 211 | auto intake_message = intake_queue.position.MakeMessage(); |
| 212 | CopyPotAndIndexPosition(intake_encoder_, &intake_message->intake, |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 213 | intake_translate, intake_pot_translate, true, |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 214 | intake_pot_offset); |
| 215 | |
| 216 | intake_message.Send(); |
| 217 | } |
| 218 | |
| 219 | { |
| 220 | auto ball_detector_message = |
| 221 | ::y2016_bot3::sensors::ball_detector.MakeMessage(); |
| 222 | ball_detector_message->voltage = ball_detector_->GetVoltage(); |
| 223 | LOG_STRUCT(DEBUG, "ball detector", *ball_detector_message); |
| 224 | ball_detector_message.Send(); |
| 225 | } |
| 226 | |
| 227 | { |
| 228 | auto auto_mode_message = ::y2016_bot3::actors::auto_mode.MakeMessage(); |
| 229 | auto_mode_message->mode = 0; |
| 230 | LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message); |
| 231 | auto_mode_message.Send(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void Quit() { run_ = false; } |
| 236 | |
| 237 | private: |
| 238 | void CopyPotAndIndexPosition( |
| 239 | const ::frc971::wpilib::DMAEncoderAndPotentiometer &encoder, |
| 240 | ::frc971::PotAndIndexPosition *position, |
| 241 | ::std::function<double(int32_t)> encoder_translate, |
| 242 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 243 | double pot_offset) { |
| 244 | const double multiplier = reverse ? -1.0 : 1.0; |
| 245 | position->encoder = |
| 246 | multiplier * encoder_translate(encoder.polled_encoder_value()); |
| 247 | position->pot = multiplier * potentiometer_translate( |
| 248 | encoder.polled_potentiometer_voltage()) + |
| 249 | pot_offset; |
| 250 | position->latched_encoder = |
| 251 | multiplier * encoder_translate(encoder.last_encoder_value()); |
| 252 | position->latched_pot = |
| 253 | multiplier * |
| 254 | potentiometer_translate(encoder.last_potentiometer_voltage()) + |
| 255 | pot_offset; |
| 256 | position->index_pulses = encoder.index_posedge_count(); |
| 257 | } |
| 258 | |
| 259 | int32_t my_pid_; |
| 260 | DriverStation *ds_; |
| 261 | |
| 262 | ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_; |
| 263 | |
| 264 | ::std::unique_ptr<Encoder> drivetrain_left_encoder_, |
| 265 | drivetrain_right_encoder_; |
| 266 | |
| 267 | ::frc971::wpilib::DMAEncoderAndPotentiometer intake_encoder_; |
| 268 | ::std::unique_ptr<AnalogInput> ball_detector_; |
| 269 | |
| 270 | ::std::atomic<bool> run_{true}; |
| 271 | DigitalGlitchFilter drivetrain_encoder_filter_, intake_encoder_filter_; |
| 272 | }; |
| 273 | |
| 274 | class SolenoidWriter { |
| 275 | public: |
| 276 | SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm) |
| 277 | : pcm_(pcm), |
| 278 | drivetrain_(".frc971.control_loops.drivetrain_queue.output"), |
| 279 | intake_(".y2016_bot3.control_loops.intake_queue.output") {} |
| 280 | |
| 281 | void set_compressor(::std::unique_ptr<Compressor> compressor) { |
| 282 | compressor_ = ::std::move(compressor); |
| 283 | } |
| 284 | |
| 285 | void set_traverse(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 286 | traverse_ = ::std::move(s); |
| 287 | } |
| 288 | |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 289 | void operator()() { |
| 290 | compressor_->Start(); |
| 291 | ::aos::SetCurrentThreadName("Solenoids"); |
| 292 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 293 | |
Campbell Crowley | abe04c4 | 2016-10-23 14:42:27 -0700 | [diff] [blame] | 294 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 295 | ::std::chrono::milliseconds(1)); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 296 | |
| 297 | while (run_) { |
| 298 | { |
| 299 | const int iterations = phased_loop.SleepUntilNext(); |
| 300 | if (iterations != 1) { |
| 301 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | { |
| 306 | intake_.FetchLatest(); |
| 307 | if (intake_.get()) { |
| 308 | LOG_STRUCT(DEBUG, "solenoids", *intake_); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 309 | traverse_->Set(intake_->traverse_down); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | { |
| 314 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 315 | { to_log.compressor_on = compressor_->Enabled(); } |
| 316 | |
| 317 | pcm_->Flush(); |
| 318 | to_log.read_solenoids = pcm_->GetAll(); |
| 319 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void Quit() { run_ = false; } |
| 325 | |
| 326 | private: |
| 327 | const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_; |
| 328 | |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 329 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> traverse_; |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 330 | ::std::unique_ptr<Compressor> compressor_; |
| 331 | |
| 332 | ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_; |
| 333 | ::aos::Queue<::y2016_bot3::control_loops::IntakeQueue::Output> intake_; |
| 334 | |
| 335 | ::std::atomic<bool> run_{true}; |
| 336 | }; |
| 337 | |
| 338 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 339 | public: |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 340 | void set_drivetrain_left_talon(::std::unique_ptr<Talon> t0, |
| 341 | ::std::unique_ptr<Talon> t1) { |
| 342 | drivetrain_left_talon_0_ = ::std::move(t0); |
| 343 | drivetrain_left_talon_1_ = ::std::move(t1); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 346 | void set_drivetrain_right_talon(::std::unique_ptr<Talon> t0, |
| 347 | ::std::unique_ptr<Talon> t1) { |
| 348 | drivetrain_right_talon_0_ = ::std::move(t0); |
| 349 | drivetrain_right_talon_1_ = ::std::move(t1); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | private: |
| 353 | virtual void Read() override { |
| 354 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 355 | } |
| 356 | |
| 357 | virtual void Write() override { |
| 358 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 359 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame^] | 360 | drivetrain_left_talon_0_->SetSpeed(queue->left_voltage / 12.0); |
| 361 | drivetrain_left_talon_1_->SetSpeed(queue->left_voltage / 12.0); |
| 362 | drivetrain_right_talon_0_->SetSpeed(-queue->right_voltage / 12.0); |
| 363 | drivetrain_right_talon_1_->SetSpeed(-queue->right_voltage / 12.0); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | virtual void Stop() override { |
| 367 | LOG(WARNING, "drivetrain output too old\n"); |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame^] | 368 | drivetrain_left_talon_0_->SetDisabled(); |
| 369 | drivetrain_right_talon_0_->SetDisabled(); |
| 370 | drivetrain_left_talon_1_->SetDisabled(); |
| 371 | drivetrain_right_talon_1_->SetDisabled(); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 374 | ::std::unique_ptr<Talon> drivetrain_left_talon_0_, drivetrain_right_talon_0_, |
| 375 | drivetrain_right_talon_1_, drivetrain_left_talon_1_; |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | class IntakeWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 379 | public: |
| 380 | void set_intake_talon(::std::unique_ptr<Talon> t) { |
| 381 | intake_talon_ = ::std::move(t); |
| 382 | } |
| 383 | |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 384 | void set_intake_rollers_talon(::std::unique_ptr<Talon> t) { |
| 385 | intake_rollers_talon_ = ::std::move(t); |
| 386 | } |
| 387 | |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 388 | void set_top_rollers_talon(::std::unique_ptr<Talon> t) { |
| 389 | top_rollers_talon_ = ::std::move(t); |
| 390 | } |
| 391 | |
| 392 | void set_bottom_rollers_talon(::std::unique_ptr<Talon> t) { |
| 393 | bottom_rollers_talon_ = ::std::move(t); |
| 394 | } |
| 395 | |
| 396 | private: |
| 397 | virtual void Read() override { |
| 398 | ::y2016_bot3::control_loops::intake_queue.output.FetchAnother(); |
| 399 | } |
| 400 | |
| 401 | virtual void Write() override { |
| 402 | auto &queue = ::y2016_bot3::control_loops::intake_queue.output; |
| 403 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame^] | 404 | intake_talon_->SetSpeed(::aos::Clip(queue->voltage_intake, -kMaxBringupPower, |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 405 | kMaxBringupPower) / |
| 406 | 12.0); |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame^] | 407 | top_rollers_talon_->SetSpeed(-queue->voltage_top_rollers / 12.0); |
| 408 | intake_rollers_talon_->SetSpeed(-queue->voltage_intake_rollers / 12.0); |
| 409 | bottom_rollers_talon_->SetSpeed(-queue->voltage_bottom_rollers / 12.0); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | virtual void Stop() override { |
| 413 | LOG(WARNING, "Intake output too old.\n"); |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame^] | 414 | intake_talon_->SetDisabled(); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | ::std::unique_ptr<Talon> intake_talon_, top_rollers_talon_, |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 418 | bottom_rollers_talon_, intake_rollers_talon_; |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 422 | public: |
| 423 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 424 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 425 | Encoder::k4X); |
| 426 | } |
| 427 | |
| 428 | void Run() override { |
| 429 | ::aos::InitNRT(); |
| 430 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 431 | |
| 432 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 433 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 434 | |
| 435 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 436 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 437 | SensorReader reader; |
| 438 | |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 439 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 440 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 441 | |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 442 | reader.set_intake_encoder(make_encoder(2)); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 443 | reader.set_intake_index(make_unique<DigitalInput>(0)); |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 444 | reader.set_intake_potentiometer(make_unique<AnalogInput>(4)); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 445 | |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 446 | reader.set_ball_detector(make_unique<AnalogInput>(5)); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 447 | |
| 448 | reader.set_dma(make_unique<DMA>()); |
| 449 | ::std::thread reader_thread(::std::ref(reader)); |
| 450 | |
| 451 | ::frc971::wpilib::GyroSender gyro_sender; |
| 452 | ::std::thread gyro_thread(::std::ref(gyro_sender)); |
| 453 | |
| 454 | DrivetrainWriter drivetrain_writer; |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 455 | // 2 and 3 are right. 0 and 1 are left |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 456 | drivetrain_writer.set_drivetrain_left_talon( |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 457 | ::std::unique_ptr<Talon>(new Talon(0)), |
| 458 | ::std::unique_ptr<Talon>(new Talon(1))); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 459 | drivetrain_writer.set_drivetrain_right_talon( |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 460 | ::std::unique_ptr<Talon>(new Talon(2)), |
| 461 | ::std::unique_ptr<Talon>(new Talon(3))); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 462 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 463 | |
| 464 | IntakeWriter intake_writer; |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 465 | intake_writer.set_intake_talon(::std::unique_ptr<Talon>(new Talon(7))); |
| 466 | intake_writer.set_top_rollers_talon(::std::unique_ptr<Talon>(new Talon(6))); |
| 467 | intake_writer.set_intake_rollers_talon( |
| 468 | ::std::unique_ptr<Talon>(new Talon(5))); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 469 | intake_writer.set_bottom_rollers_talon( |
Campbell Crowley | 483d627 | 2016-11-05 14:11:34 -0700 | [diff] [blame] | 470 | ::std::unique_ptr<Talon>(new Talon(4))); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 471 | ::std::thread intake_writer_thread(::std::ref(intake_writer)); |
| 472 | |
| 473 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 474 | new ::frc971::wpilib::BufferedPcm()); |
| 475 | SolenoidWriter solenoid_writer(pcm); |
Adam Snaider | 18f4417 | 2016-10-22 15:30:21 -0700 | [diff] [blame] | 476 | solenoid_writer.set_traverse(pcm->MakeSolenoid(3)); |
| 477 | |
| 478 | solenoid_writer.set_compressor(make_unique<Compressor>()); |
| 479 | |
| 480 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 481 | |
| 482 | // Wait forever. Not much else to do... |
| 483 | while (true) { |
| 484 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 485 | if (r != 0) { |
| 486 | PLOG(WARNING, "infinite select failed"); |
| 487 | } else { |
| 488 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 493 | |
| 494 | joystick_sender.Quit(); |
| 495 | joystick_thread.join(); |
| 496 | pdp_fetcher.Quit(); |
| 497 | pdp_fetcher_thread.join(); |
| 498 | reader.Quit(); |
| 499 | reader_thread.join(); |
| 500 | gyro_sender.Quit(); |
| 501 | gyro_thread.join(); |
| 502 | |
| 503 | drivetrain_writer.Quit(); |
| 504 | drivetrain_writer_thread.join(); |
| 505 | intake_writer.Quit(); |
| 506 | intake_writer_thread.join(); |
| 507 | solenoid_writer.Quit(); |
| 508 | solenoid_thread.join(); |
| 509 | |
| 510 | ::aos::Cleanup(); |
| 511 | } |
| 512 | }; |
| 513 | |
| 514 | } // namespace wpilib |
| 515 | } // namespace y2016_bot3 |
| 516 | |
| 517 | AOS_ROBOT_CLASS(::y2016_bot3::wpilib::WPILibRobot); |