Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 1 | #include <inttypes.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include <array> |
| 7 | #include <chrono> |
| 8 | #include <cmath> |
| 9 | #include <functional> |
| 10 | #include <mutex> |
| 11 | #include <thread> |
| 12 | |
| 13 | #include "AnalogInput.h" |
| 14 | #include "Counter.h" |
| 15 | #include "DigitalGlitchFilter.h" |
| 16 | #include "DriverStation.h" |
| 17 | #include "Encoder.h" |
| 18 | #include "Relay.h" |
| 19 | #include "Servo.h" |
| 20 | #include "VictorSP.h" |
| 21 | #undef ERROR |
| 22 | |
| 23 | #include "aos/common/commonmath.h" |
| 24 | #include "aos/common/logging/logging.h" |
| 25 | #include "aos/common/logging/queue_logging.h" |
| 26 | #include "aos/common/messages/robot_state.q.h" |
| 27 | #include "aos/common/stl_mutex.h" |
| 28 | #include "aos/common/time.h" |
| 29 | #include "aos/common/util/compiler_memory_barrier.h" |
| 30 | #include "aos/common/util/log_interval.h" |
| 31 | #include "aos/common/util/phased_loop.h" |
| 32 | #include "aos/common/util/wrapping_counter.h" |
| 33 | #include "aos/linux_code/init.h" |
| 34 | |
| 35 | #include "frc971/autonomous/auto.q.h" |
| 36 | #include "frc971/control_loops/control_loops.q.h" |
| 37 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 38 | #include "frc971/wpilib/ADIS16448.h" |
| 39 | #include "frc971/wpilib/buffered_pcm.h" |
| 40 | #include "frc971/wpilib/buffered_solenoid.h" |
| 41 | #include "frc971/wpilib/dma.h" |
| 42 | #include "frc971/wpilib/dma_edge_counting.h" |
| 43 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 44 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 45 | #include "frc971/wpilib/joystick_sender.h" |
| 46 | #include "frc971/wpilib/logging.q.h" |
| 47 | #include "frc971/wpilib/loop_output_handler.h" |
| 48 | #include "frc971/wpilib/pdp_fetcher.h" |
| 49 | #include "frc971/wpilib/wpilib_interface.h" |
| 50 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 51 | #include "y2018/constants.h" |
| 52 | #include "y2018/control_loops/superstructure/superstructure.q.h" |
| 53 | |
| 54 | #ifndef M_PI |
| 55 | #define M_PI 3.14159265358979323846 |
| 56 | #endif |
| 57 | |
| 58 | using ::frc971::control_loops::drivetrain_queue; |
| 59 | using ::y2018::control_loops::superstructure_queue; |
| 60 | using ::y2018::constants::Values; |
| 61 | using ::aos::monotonic_clock; |
| 62 | namespace chrono = ::std::chrono; |
| 63 | |
| 64 | namespace y2018 { |
| 65 | namespace wpilib { |
| 66 | namespace { |
| 67 | |
| 68 | constexpr double kMaxBringupPower = 12.0; |
| 69 | |
| 70 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 71 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 72 | // The low bit is direction. |
| 73 | |
| 74 | // TODO(brian): Replace this with ::std::make_unique once all our toolchains |
| 75 | // have support. |
| 76 | |
| 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(brian): Use ::std::max instead once we have C++14 so that can be |
| 83 | // constexpr. |
| 84 | |
| 85 | template <typename T> |
| 86 | constexpr T max(T a, T b) { |
| 87 | return (a > b) ? a : b; |
| 88 | } |
| 89 | |
| 90 | template <typename T, typename... Rest> |
| 91 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 92 | return max(max(a, b), c, rest...); |
| 93 | } |
| 94 | |
| 95 | double drivetrain_translate(int32_t in) { |
| 96 | return static_cast<double>(in) / |
| 97 | Values::kDrivetrainEncoderCountsPerRevolution() * |
| 98 | Values::kDrivetrainEncoderRatio() * control_loops::drivetrain::kWheelRadius; |
| 99 | } |
| 100 | |
| 101 | double drivetrain_velocity_translate(double in) { |
| 102 | return (1.0 / in) / Values::kDrivetrainCyclesPerRevolution() * |
| 103 | Values::kDrivetrainEncoderRatio() * control_loops::drivetrain::kWheelRadius; |
| 104 | } |
| 105 | |
| 106 | double proximal_pot_translate(double voltage) { |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 107 | return -voltage * Values::kProximalPotRatio() * |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 108 | (3.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 109 | } |
| 110 | |
| 111 | double distal_pot_translate(double voltage) { |
| 112 | return voltage * Values::kDistalPotRatio() * |
| 113 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 114 | } |
| 115 | |
| 116 | double intake_pot_translate(double voltage) { |
| 117 | return voltage * Values::kIntakeMotorPotRatio() * |
| 118 | (10.0 /*turns*/ / 5.0 /*volts*/) * (2 * M_PI /*radians*/); |
| 119 | } |
| 120 | |
| 121 | double intake_spring_translate(double voltage) { |
| 122 | return voltage * Values::kIntakeSpringRatio() * (2 * M_PI /*radians*/) / |
| 123 | (5.0 /*volts*/); |
| 124 | } |
| 125 | |
| 126 | // TODO() figure out differnce between max and min voltages on shifter pots. |
| 127 | // Returns value from 0.0 to 1.0, with 0.0 being close to low gear so it can be |
| 128 | // passed drectly into the drivetrain position queue. |
| 129 | double drivetrain_shifter_pot_translate(double voltage) { |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 130 | return (voltage - Values::kDrivetrainShifterPotMinVoltage()) / |
| 131 | (Values::kDrivetrainShifterPotMaxVoltage() - |
| 132 | Values::kDrivetrainShifterPotMinVoltage()); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 136 | max(Values::kMaxDrivetrainEncoderPulsesPerSecond(), |
| 137 | Values::kMaxIntakeMotorEncoderPulsesPerSecond()); |
| 138 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 139 | "fast encoders are too fast"); |
| 140 | |
| 141 | constexpr double kMaxMediumEncoderPulsesPerSecond = |
| 142 | max(Values::kMaxProximalEncoderPulsesPerSecond(), |
| 143 | Values::kMaxDistalEncoderPulsesPerSecond()); |
| 144 | static_assert(kMaxMediumEncoderPulsesPerSecond <= 400000, |
| 145 | "medium encoders are too fast"); |
| 146 | |
| 147 | // Class to send position messages with sensor readings to our loops. |
| 148 | class SensorReader { |
| 149 | public: |
| 150 | SensorReader() { |
| 151 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 152 | // we should ever see. |
| 153 | fast_encoder_filter_.SetPeriodNanoSeconds( |
| 154 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 155 | kMaxFastEncoderPulsesPerSecond * 1e9 + |
| 156 | 0.5)); |
| 157 | medium_encoder_filter_.SetPeriodNanoSeconds( |
| 158 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 159 | kMaxMediumEncoderPulsesPerSecond * 1e9 + |
| 160 | 0.5)); |
| 161 | hall_filter_.SetPeriodNanoSeconds(100000); |
| 162 | } |
| 163 | |
| 164 | // Left drivetrain side. |
| 165 | void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) { |
| 166 | fast_encoder_filter_.Add(encoder.get()); |
| 167 | drivetrain_left_encoder_ = ::std::move(encoder); |
| 168 | } |
| 169 | |
| 170 | void set_left_drivetrain_shifter_potentiometer( |
| 171 | ::std::unique_ptr<AnalogInput> potentiometer) { |
| 172 | left_drivetrain_shifter_ = ::std::move(potentiometer); |
| 173 | } |
| 174 | |
| 175 | // Right drivetrain side. |
| 176 | void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) { |
| 177 | fast_encoder_filter_.Add(encoder.get()); |
| 178 | drivetrain_right_encoder_ = ::std::move(encoder); |
| 179 | } |
| 180 | |
| 181 | void set_right_drivetrain_shifter_potentiometer( |
| 182 | ::std::unique_ptr<AnalogInput> potentiometer) { |
| 183 | right_drivetrain_shifter_ = ::std::move(potentiometer); |
| 184 | } |
| 185 | |
| 186 | // Proximal joint. |
| 187 | void set_proximal_encoder(::std::unique_ptr<Encoder> encoder) { |
| 188 | medium_encoder_filter_.Add(encoder.get()); |
| 189 | proximal_encoder_.set_encoder(::std::move(encoder)); |
| 190 | } |
| 191 | |
| 192 | void set_proximal_absolute_pwm(::std::unique_ptr<DigitalInput> absolute_pwm) { |
| 193 | proximal_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 194 | } |
| 195 | |
| 196 | void set_proximal_potentiometer( |
| 197 | ::std::unique_ptr<AnalogInput> potentiometer) { |
| 198 | proximal_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 199 | } |
| 200 | |
| 201 | // Distal joint. |
| 202 | void set_distal_encoder(::std::unique_ptr<Encoder> encoder) { |
| 203 | medium_encoder_filter_.Add(encoder.get()); |
| 204 | distal_encoder_.set_encoder(::std::move(encoder)); |
| 205 | } |
| 206 | |
| 207 | void set_distal_absolute_pwm(::std::unique_ptr<DigitalInput> absolute_pwm) { |
| 208 | fast_encoder_filter_.Add(absolute_pwm.get()); |
| 209 | distal_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 210 | } |
| 211 | |
| 212 | void set_distal_potentiometer(::std::unique_ptr<AnalogInput> potentiometer) { |
| 213 | distal_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 214 | } |
| 215 | |
| 216 | // Left intake side. |
| 217 | void set_left_intake_encoder(::std::unique_ptr<Encoder> encoder) { |
| 218 | fast_encoder_filter_.Add(encoder.get()); |
| 219 | left_intake_encoder_.set_encoder(::std::move(encoder)); |
| 220 | } |
| 221 | |
| 222 | void set_left_intake_absolute_pwm( |
| 223 | ::std::unique_ptr<DigitalInput> absolute_pwm) { |
| 224 | fast_encoder_filter_.Add(absolute_pwm.get()); |
| 225 | left_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 226 | } |
| 227 | |
| 228 | void set_left_intake_potentiometer( |
| 229 | ::std::unique_ptr<AnalogInput> potentiometer) { |
| 230 | left_intake_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 231 | } |
| 232 | |
| 233 | void set_left_intake_spring_angle(::std::unique_ptr<AnalogInput> encoder) { |
| 234 | left_intake_spring_angle_ = ::std::move(encoder); |
| 235 | } |
| 236 | |
| 237 | void set_left_intake_cube_detector(::std::unique_ptr<DigitalInput> input) { |
| 238 | left_intake_cube_detector_ = ::std::move(input); |
| 239 | } |
| 240 | |
| 241 | // Right intake side. |
| 242 | void set_right_intake_encoder(::std::unique_ptr<Encoder> encoder) { |
| 243 | fast_encoder_filter_.Add(encoder.get()); |
| 244 | right_intake_encoder_.set_encoder(::std::move(encoder)); |
| 245 | } |
| 246 | |
| 247 | void set_right_intake_absolute_pwm( |
| 248 | ::std::unique_ptr<DigitalInput> absolute_pwm) { |
| 249 | fast_encoder_filter_.Add(absolute_pwm.get()); |
| 250 | right_intake_encoder_.set_absolute_pwm(::std::move(absolute_pwm)); |
| 251 | } |
| 252 | |
| 253 | void set_right_intake_potentiometer( |
| 254 | ::std::unique_ptr<AnalogInput> potentiometer) { |
| 255 | right_intake_encoder_.set_potentiometer(::std::move(potentiometer)); |
| 256 | } |
| 257 | |
| 258 | void set_right_intake_spring_angle(::std::unique_ptr<AnalogInput> encoder) { |
| 259 | right_intake_spring_angle_ = ::std::move(encoder); |
| 260 | } |
| 261 | |
| 262 | void set_right_intake_cube_detector(::std::unique_ptr<DigitalInput> input) { |
| 263 | right_intake_cube_detector_ = ::std::move(input); |
| 264 | } |
| 265 | |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame^] | 266 | void set_claw_beambreak(::std::unique_ptr<DigitalInput> input) { |
| 267 | claw_beambreak_ = ::std::move(input); |
| 268 | } |
| 269 | |
| 270 | void set_box_back_beambreak(::std::unique_ptr<DigitalInput> input) { |
| 271 | box_back_beambreak_ = ::std::move(input); |
| 272 | } |
| 273 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 274 | // Auto mode switches. |
| 275 | void set_autonomous_mode(int i, ::std::unique_ptr<DigitalInput> sensor) { |
| 276 | autonomous_modes_.at(i) = ::std::move(sensor); |
| 277 | } |
| 278 | |
| 279 | void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) { |
| 280 | medium_encoder_filter_.Add(pwm_trigger.get()); |
| 281 | pwm_trigger_ = ::std::move(pwm_trigger); |
| 282 | } |
| 283 | |
| 284 | // All of the DMA-related set_* calls must be made before this, and it |
| 285 | // doesn't hurt to do all of them. |
| 286 | void set_dma(::std::unique_ptr<DMA> dma) { |
| 287 | dma_synchronizer_.reset( |
| 288 | new ::frc971::wpilib::DMASynchronizer(::std::move(dma))); |
| 289 | } |
| 290 | |
| 291 | void RunPWMDetecter() { |
| 292 | ::aos::SetCurrentThreadRealtimePriority(41); |
| 293 | |
| 294 | pwm_trigger_->RequestInterrupts(); |
| 295 | // Rising edge only. |
| 296 | pwm_trigger_->SetUpSourceEdge(true, false); |
| 297 | |
| 298 | monotonic_clock::time_point last_posedge_monotonic = |
| 299 | monotonic_clock::min_time; |
| 300 | |
| 301 | while (run_) { |
| 302 | auto ret = pwm_trigger_->WaitForInterrupt(1.0, true); |
| 303 | if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) { |
| 304 | // Grab all the clocks. |
| 305 | const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp(); |
| 306 | |
| 307 | aos_compiler_memory_barrier(); |
| 308 | const double fpga_time_before = GetFPGATime() * 1e-6; |
| 309 | aos_compiler_memory_barrier(); |
| 310 | const monotonic_clock::time_point monotonic_now = |
| 311 | monotonic_clock::now(); |
| 312 | aos_compiler_memory_barrier(); |
| 313 | const double fpga_time_after = GetFPGATime() * 1e-6; |
| 314 | aos_compiler_memory_barrier(); |
| 315 | |
| 316 | const double fpga_offset = |
| 317 | (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time; |
| 318 | |
| 319 | // Compute when the edge was. |
| 320 | const monotonic_clock::time_point monotonic_edge = |
| 321 | monotonic_now - chrono::duration_cast<chrono::nanoseconds>( |
| 322 | chrono::duration<double>(fpga_offset)); |
| 323 | |
| 324 | LOG(DEBUG, "Got PWM pulse %f spread, %f offset, %lld trigger\n", |
| 325 | fpga_time_after - fpga_time_before, fpga_offset, |
| 326 | monotonic_edge.time_since_epoch().count()); |
| 327 | |
| 328 | // Compute bounds on the timestep and sampling times. |
| 329 | const double fpga_sample_length = fpga_time_after - fpga_time_before; |
| 330 | const chrono::nanoseconds elapsed_time = |
| 331 | monotonic_edge - last_posedge_monotonic; |
| 332 | |
| 333 | last_posedge_monotonic = monotonic_edge; |
| 334 | |
| 335 | // Verify that the values are sane. |
| 336 | if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) { |
| 337 | continue; |
| 338 | } |
| 339 | if (fpga_offset < 0 || fpga_offset > 0.00015) { |
| 340 | continue; |
| 341 | } |
| 342 | if (elapsed_time > |
| 343 | chrono::microseconds(5050) + chrono::microseconds(4) || |
| 344 | elapsed_time < |
| 345 | chrono::microseconds(5050) - chrono::microseconds(4)) { |
| 346 | continue; |
| 347 | } |
| 348 | // Good edge! |
| 349 | { |
| 350 | ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_); |
| 351 | last_tick_time_monotonic_timepoint_ = last_posedge_monotonic; |
| 352 | last_period_ = elapsed_time; |
| 353 | } |
| 354 | } else { |
| 355 | LOG(INFO, "PWM triggered %d\n", ret); |
| 356 | } |
| 357 | } |
| 358 | pwm_trigger_->CancelInterrupts(); |
| 359 | } |
| 360 | |
| 361 | void operator()() { |
| 362 | ::aos::SetCurrentThreadName("SensorReader"); |
| 363 | |
| 364 | my_pid_ = getpid(); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 365 | |
| 366 | dma_synchronizer_->Start(); |
| 367 | |
| 368 | ::aos::time::PhasedLoop phased_loop(last_period_, |
| 369 | ::std::chrono::milliseconds(3)); |
| 370 | chrono::nanoseconds filtered_period = last_period_; |
| 371 | |
| 372 | ::std::thread pwm_detecter_thread( |
| 373 | ::std::bind(&SensorReader::RunPWMDetecter, this)); |
| 374 | |
| 375 | ::aos::SetCurrentThreadRealtimePriority(40); |
| 376 | while (run_) { |
| 377 | { |
| 378 | const int iterations = phased_loop.SleepUntilNext(); |
| 379 | if (iterations != 1) { |
| 380 | LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1); |
| 381 | } |
| 382 | } |
| 383 | RunIteration(); |
| 384 | |
| 385 | monotonic_clock::time_point last_tick_timepoint; |
| 386 | chrono::nanoseconds period; |
| 387 | { |
| 388 | ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_); |
| 389 | last_tick_timepoint = last_tick_time_monotonic_timepoint_; |
| 390 | period = last_period_; |
| 391 | } |
| 392 | |
| 393 | if (last_tick_timepoint == monotonic_clock::min_time) { |
| 394 | continue; |
| 395 | } |
| 396 | chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime( |
| 397 | period, last_tick_timepoint + chrono::microseconds(2050)); |
| 398 | |
| 399 | // TODO(austin): If this is the first edge in a while, skip to it (plus |
| 400 | // an offset). Otherwise, slowly drift time to line up. |
| 401 | |
| 402 | phased_loop.set_interval_and_offset(period, new_offset); |
| 403 | } |
| 404 | pwm_detecter_thread.join(); |
| 405 | } |
| 406 | |
| 407 | void RunIteration() { |
Austin Schuh | 94f51e9 | 2017-10-30 19:25:32 -0700 | [diff] [blame] | 408 | ::frc971::wpilib::SendRobotState(my_pid_); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 409 | |
| 410 | const auto values = constants::GetValues(); |
| 411 | |
| 412 | { |
| 413 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 414 | drivetrain_message->left_encoder = |
| 415 | drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 416 | drivetrain_message->left_speed = |
| 417 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 418 | drivetrain_message->left_shifter_position = |
| 419 | drivetrain_shifter_pot_translate( |
| 420 | left_drivetrain_shifter_->GetVoltage()); |
| 421 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 422 | drivetrain_message->right_encoder = |
| 423 | -drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 424 | drivetrain_message->right_speed = |
| 425 | -drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 426 | drivetrain_message->right_shifter_position = |
| 427 | drivetrain_shifter_pot_translate( |
| 428 | right_drivetrain_shifter_->GetVoltage()); |
| 429 | |
| 430 | drivetrain_message.Send(); |
| 431 | } |
| 432 | |
| 433 | dma_synchronizer_->RunIteration(); |
| 434 | |
| 435 | { |
| 436 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 437 | |
| 438 | CopyPosition(proximal_encoder_, &superstructure_message->arm.proximal, |
| 439 | Values::kProximalEncoderCountsPerRevolution(), |
| 440 | Values::kProximalEncoderRatio(), proximal_pot_translate, |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 441 | true, values.arm_proximal.potentiometer_offset); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 442 | |
| 443 | CopyPosition(distal_encoder_, &superstructure_message->arm.distal, |
| 444 | Values::kDistalEncoderCountsPerRevolution(), |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 445 | Values::kDistalEncoderRatio(), distal_pot_translate, true, |
| 446 | values.arm_distal.potentiometer_offset); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 447 | |
| 448 | CopyPosition(left_intake_encoder_, |
| 449 | &superstructure_message->intake.left.motor_position, |
| 450 | Values::kIntakeMotorEncoderCountsPerRevolution(), |
| 451 | Values::kIntakeMotorEncoderRatio(), intake_pot_translate, |
Sabina Davis | 8d20ca8 | 2018-02-19 13:17:45 -0800 | [diff] [blame] | 452 | false, values.left_intake.potentiometer_offset); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 453 | |
| 454 | CopyPosition(right_intake_encoder_, |
| 455 | &superstructure_message->intake.right.motor_position, |
| 456 | Values::kIntakeMotorEncoderCountsPerRevolution(), |
| 457 | Values::kIntakeMotorEncoderRatio(), intake_pot_translate, |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 458 | true, values.right_intake.potentiometer_offset); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 459 | |
| 460 | superstructure_message->intake.left.spring_angle = |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 461 | intake_spring_translate(left_intake_spring_angle_->GetVoltage()) + |
| 462 | values.left_intake.spring_offset; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 463 | superstructure_message->intake.left.beam_break = |
| 464 | left_intake_cube_detector_->Get(); |
| 465 | |
| 466 | superstructure_message->intake.right.spring_angle = |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 467 | -intake_spring_translate(right_intake_spring_angle_->GetVoltage()) + |
| 468 | values.right_intake.spring_offset; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 469 | superstructure_message->intake.right.beam_break = |
| 470 | right_intake_cube_detector_->Get(); |
| 471 | |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame^] | 472 | superstructure_message->claw_beambreak_triggered = claw_beambreak_->Get(); |
| 473 | superstructure_message->box_back_beambreak_triggered = |
| 474 | !box_back_beambreak_->Get(); |
| 475 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 476 | superstructure_message.Send(); |
| 477 | } |
| 478 | |
| 479 | { |
| 480 | auto auto_mode_message = ::frc971::autonomous::auto_mode.MakeMessage(); |
| 481 | auto_mode_message->mode = 0; |
| 482 | for (size_t i = 0; i < autonomous_modes_.size(); ++i) { |
| 483 | if (autonomous_modes_[i] && autonomous_modes_[i]->Get()) { |
| 484 | auto_mode_message->mode |= 1 << i; |
| 485 | } |
| 486 | } |
| 487 | LOG_STRUCT(DEBUG, "auto mode", *auto_mode_message); |
| 488 | auto_mode_message.Send(); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | void Quit() { run_ = false; } |
| 493 | |
| 494 | private: |
| 495 | double encoder_translate(int32_t value, double counts_per_revolution, |
| 496 | double ratio) { |
| 497 | return static_cast<double>(value) / counts_per_revolution * ratio * |
| 498 | (2.0 * M_PI); |
| 499 | } |
| 500 | |
| 501 | void CopyPosition( |
| 502 | const ::frc971::wpilib::AbsoluteEncoderAndPotentiometer &encoder, |
| 503 | ::frc971::PotAndAbsolutePosition *position, |
| 504 | double encoder_counts_per_revolution, double encoder_ratio, |
| 505 | ::std::function<double(double)> potentiometer_translate, bool reverse, |
| 506 | double pot_offset) { |
| 507 | const double multiplier = reverse ? -1.0 : 1.0; |
| 508 | position->pot = multiplier * potentiometer_translate( |
| 509 | encoder.ReadPotentiometerVoltage()) + |
| 510 | pot_offset; |
| 511 | position->encoder = |
| 512 | multiplier * encoder_translate(encoder.ReadRelativeEncoder(), |
| 513 | encoder_counts_per_revolution, |
| 514 | encoder_ratio); |
| 515 | |
| 516 | position->absolute_encoder = |
| 517 | (reverse ? (1.0 - encoder.ReadAbsoluteEncoder()) |
| 518 | : encoder.ReadAbsoluteEncoder()) * |
| 519 | encoder_ratio * (2.0 * M_PI); |
| 520 | } |
| 521 | |
| 522 | int32_t my_pid_; |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 523 | |
| 524 | // Mutex to manage access to the period and tick time variables. |
| 525 | ::aos::stl_mutex tick_time_mutex_; |
| 526 | monotonic_clock::time_point last_tick_time_monotonic_timepoint_ = |
| 527 | monotonic_clock::min_time; |
| 528 | chrono::nanoseconds last_period_ = chrono::microseconds(5050); |
| 529 | |
| 530 | ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_; |
| 531 | |
| 532 | DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_, |
| 533 | hall_filter_; |
| 534 | |
| 535 | ::std::unique_ptr<Encoder> drivetrain_left_encoder_, |
| 536 | drivetrain_right_encoder_; |
| 537 | |
| 538 | ::std::unique_ptr<AnalogInput> left_drivetrain_shifter_, |
| 539 | right_drivetrain_shifter_; |
| 540 | |
| 541 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer proximal_encoder_, |
| 542 | distal_encoder_; |
| 543 | |
| 544 | ::frc971::wpilib::AbsoluteEncoderAndPotentiometer left_intake_encoder_, |
| 545 | right_intake_encoder_; |
| 546 | |
| 547 | ::std::unique_ptr<AnalogInput> left_intake_spring_angle_, |
| 548 | right_intake_spring_angle_; |
| 549 | ::std::unique_ptr<DigitalInput> left_intake_cube_detector_, |
| 550 | right_intake_cube_detector_; |
| 551 | |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame^] | 552 | ::std::unique_ptr<DigitalInput> claw_beambreak_; |
| 553 | ::std::unique_ptr<DigitalInput> box_back_beambreak_; |
| 554 | |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 555 | ::std::unique_ptr<DigitalInput> pwm_trigger_; |
| 556 | |
| 557 | ::std::array<::std::unique_ptr<DigitalInput>, 4> autonomous_modes_; |
| 558 | |
| 559 | ::std::atomic<bool> run_{true}; |
| 560 | }; |
| 561 | |
| 562 | class SolenoidWriter { |
| 563 | public: |
| 564 | SolenoidWriter(::frc971::wpilib::BufferedPcm *pcm) |
| 565 | : pcm_(pcm), |
| 566 | drivetrain_(".frc971.control_loops.drivetrain_queue.output"), |
| 567 | superstructure_(".y2018.control_loops.superstructure_queue.output") {} |
| 568 | |
| 569 | // left drive |
| 570 | // right drive |
| 571 | // |
| 572 | // claw |
| 573 | // arm brakes |
| 574 | // hook release |
| 575 | // fork release |
| 576 | void set_left_drivetrain_shifter( |
| 577 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 578 | left_drivetrain_shifter_ = ::std::move(s); |
| 579 | } |
| 580 | void set_right_drivetrain_shifter( |
| 581 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 582 | right_drivetrain_shifter_ = ::std::move(s); |
| 583 | } |
| 584 | |
| 585 | void set_claw(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 586 | claw_ = ::std::move(s); |
| 587 | } |
| 588 | |
| 589 | void set_arm_brakes(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 590 | arm_brakes_ = ::std::move(s); |
| 591 | } |
| 592 | |
| 593 | void set_hook(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 594 | hook_ = ::std::move(s); |
| 595 | } |
| 596 | |
| 597 | void set_forks(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 598 | forks_ = ::std::move(s); |
| 599 | } |
| 600 | |
| 601 | void operator()() { |
| 602 | ::aos::SetCurrentThreadName("Solenoids"); |
| 603 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 604 | |
| 605 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 606 | ::std::chrono::milliseconds(1)); |
| 607 | |
| 608 | while (run_) { |
| 609 | { |
| 610 | const int iterations = phased_loop.SleepUntilNext(); |
| 611 | if (iterations != 1) { |
| 612 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | { |
| 617 | drivetrain_.FetchLatest(); |
| 618 | if (drivetrain_.get()) { |
| 619 | LOG_STRUCT(DEBUG, "solenoids", *drivetrain_); |
| 620 | left_drivetrain_shifter_->Set(!drivetrain_->left_high); |
| 621 | right_drivetrain_shifter_->Set(!drivetrain_->right_high); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | { |
| 626 | superstructure_.FetchLatest(); |
| 627 | if (superstructure_.get()) { |
| 628 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 629 | |
| 630 | claw_->Set(superstructure_->claw_grabbed); |
| 631 | arm_brakes_->Set(superstructure_->release_arm_brake); |
| 632 | hook_->Set(superstructure_->hook_release); |
| 633 | forks_->Set(superstructure_->forks_release); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | { |
| 638 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 639 | |
| 640 | pcm_->Flush(); |
| 641 | to_log.read_solenoids = pcm_->GetAll(); |
| 642 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | void Quit() { run_ = false; } |
| 648 | |
| 649 | private: |
| 650 | ::frc971::wpilib::BufferedPcm *pcm_; |
| 651 | |
| 652 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> |
| 653 | left_drivetrain_shifter_, right_drivetrain_shifter_, claw_, arm_brakes_, |
| 654 | hook_, forks_; |
| 655 | |
| 656 | ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_; |
| 657 | ::aos::Queue<::y2018::control_loops::SuperstructureQueue::Output> |
| 658 | superstructure_; |
| 659 | |
| 660 | ::std::atomic<bool> run_{true}; |
| 661 | }; |
| 662 | |
| 663 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 664 | public: |
| 665 | void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 666 | drivetrain_left_victor_ = ::std::move(t); |
| 667 | } |
| 668 | |
| 669 | void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 670 | drivetrain_right_victor_ = ::std::move(t); |
| 671 | } |
| 672 | |
| 673 | private: |
| 674 | virtual void Read() override { |
| 675 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 676 | } |
| 677 | |
| 678 | virtual void Write() override { |
| 679 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 680 | LOG_STRUCT(DEBUG, "will output", *queue); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 681 | drivetrain_left_victor_->SetSpeed(queue->left_voltage / 12.0); |
| 682 | drivetrain_right_victor_->SetSpeed(-queue->right_voltage / 12.0); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | virtual void Stop() override { |
| 686 | LOG(WARNING, "drivetrain output too old\n"); |
| 687 | drivetrain_left_victor_->SetDisabled(); |
| 688 | drivetrain_right_victor_->SetDisabled(); |
| 689 | } |
| 690 | |
| 691 | ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_, |
| 692 | drivetrain_right_victor_; |
| 693 | }; |
| 694 | |
| 695 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 696 | public: |
| 697 | void set_proximal_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 698 | proximal_victor_ = ::std::move(t); |
| 699 | } |
| 700 | void set_distal_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 701 | distal_victor_ = ::std::move(t); |
| 702 | } |
| 703 | |
| 704 | void set_left_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 705 | left_intake_elastic_victor_ = ::std::move(t); |
| 706 | } |
| 707 | void set_right_intake_elastic_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 708 | right_intake_elastic_victor_ = ::std::move(t); |
| 709 | } |
| 710 | |
| 711 | void set_left_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 712 | left_intake_rollers_victor_ = ::std::move(t); |
| 713 | } |
| 714 | |
| 715 | void set_right_intake_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 716 | right_intake_rollers_victor_ = ::std::move(t); |
| 717 | } |
| 718 | |
| 719 | private: |
| 720 | virtual void Read() override { |
| 721 | ::y2018::control_loops::superstructure_queue.output.FetchAnother(); |
| 722 | } |
| 723 | |
| 724 | virtual void Write() override { |
| 725 | auto &queue = ::y2018::control_loops::superstructure_queue.output; |
| 726 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 727 | |
| 728 | left_intake_elastic_victor_->SetSpeed( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 729 | ::aos::Clip(-queue->intake.left.voltage_elastic, -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 730 | kMaxBringupPower) / |
| 731 | 12.0); |
| 732 | |
| 733 | right_intake_elastic_victor_->SetSpeed( |
| 734 | ::aos::Clip(queue->intake.right.voltage_elastic, -kMaxBringupPower, |
| 735 | kMaxBringupPower) / |
| 736 | 12.0); |
| 737 | |
| 738 | left_intake_rollers_victor_->SetSpeed( |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 739 | ::aos::Clip(-queue->intake.left.voltage_rollers, -kMaxBringupPower, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 740 | kMaxBringupPower) / |
| 741 | 12.0); |
| 742 | |
| 743 | right_intake_rollers_victor_->SetSpeed( |
| 744 | ::aos::Clip(queue->intake.right.voltage_rollers, -kMaxBringupPower, |
| 745 | kMaxBringupPower) / |
| 746 | 12.0); |
| 747 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 748 | proximal_victor_->SetSpeed(::aos::Clip(-queue->voltage_proximal, |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 749 | -kMaxBringupPower, |
| 750 | kMaxBringupPower) / |
| 751 | 12.0); |
| 752 | |
| 753 | distal_victor_->SetSpeed(::aos::Clip(queue->voltage_distal, |
| 754 | -kMaxBringupPower, kMaxBringupPower) / |
| 755 | 12.0); |
| 756 | } |
| 757 | |
| 758 | virtual void Stop() override { |
| 759 | LOG(WARNING, "Superstructure output too old.\n"); |
| 760 | |
| 761 | left_intake_rollers_victor_->SetDisabled(); |
| 762 | right_intake_rollers_victor_->SetDisabled(); |
| 763 | left_intake_elastic_victor_->SetDisabled(); |
| 764 | right_intake_elastic_victor_->SetDisabled(); |
| 765 | |
| 766 | proximal_victor_->SetDisabled(); |
| 767 | distal_victor_->SetDisabled(); |
| 768 | } |
| 769 | |
| 770 | ::std::unique_ptr<::frc::VictorSP> left_intake_rollers_victor_, |
| 771 | right_intake_rollers_victor_, left_intake_elastic_victor_, |
| 772 | right_intake_elastic_victor_, proximal_victor_, distal_victor_; |
| 773 | }; |
| 774 | |
| 775 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 776 | public: |
| 777 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 778 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 779 | Encoder::k4X); |
| 780 | } |
| 781 | |
| 782 | void Run() override { |
| 783 | ::aos::InitNRT(); |
| 784 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 785 | |
| 786 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 787 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 788 | |
| 789 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 790 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 791 | SensorReader reader; |
| 792 | |
| 793 | // TODO(Sabina): Update port numbers(Sensors and Victors) |
| 794 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 795 | reader.set_left_drivetrain_shifter_potentiometer( |
| 796 | make_unique<AnalogInput>(6)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 797 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 798 | reader.set_right_drivetrain_shifter_potentiometer( |
| 799 | make_unique<AnalogInput>(7)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 800 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 801 | reader.set_proximal_encoder(make_encoder(4)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 802 | reader.set_proximal_absolute_pwm(make_unique<DigitalInput>(2)); |
| 803 | reader.set_proximal_potentiometer(make_unique<AnalogInput>(2)); |
| 804 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 805 | reader.set_distal_encoder(make_encoder(2)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 806 | reader.set_distal_absolute_pwm(make_unique<DigitalInput>(3)); |
| 807 | reader.set_distal_potentiometer(make_unique<AnalogInput>(3)); |
| 808 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 809 | reader.set_right_intake_encoder(make_encoder(5)); |
| 810 | reader.set_right_intake_absolute_pwm(make_unique<DigitalInput>(7)); |
| 811 | reader.set_right_intake_potentiometer(make_unique<AnalogInput>(1)); |
| 812 | reader.set_right_intake_spring_angle(make_unique<AnalogInput>(5)); |
| 813 | reader.set_right_intake_cube_detector(make_unique<DigitalInput>(1)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 814 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 815 | reader.set_left_intake_encoder(make_encoder(3)); |
| 816 | reader.set_left_intake_absolute_pwm(make_unique<DigitalInput>(4)); |
| 817 | reader.set_left_intake_potentiometer(make_unique<AnalogInput>(0)); |
| 818 | reader.set_left_intake_spring_angle(make_unique<AnalogInput>(4)); |
| 819 | reader.set_left_intake_cube_detector(make_unique<DigitalInput>(0)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 820 | |
Austin Schuh | 4ef51af | 2018-03-04 01:08:45 -0800 | [diff] [blame^] | 821 | reader.set_claw_beambreak(make_unique<DigitalInput>(8)); |
| 822 | reader.set_box_back_beambreak(make_unique<DigitalInput>(9)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 823 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 824 | reader.set_pwm_trigger(make_unique<DigitalInput>(25)); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 825 | |
| 826 | reader.set_dma(make_unique<DMA>()); |
| 827 | ::std::thread reader_thread(::std::ref(reader)); |
| 828 | |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 829 | auto imu_trigger = make_unique<DigitalInput>(5); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 830 | ::frc971::wpilib::ADIS16448 imu(SPI::Port::kOnboardCS1, imu_trigger.get()); |
| 831 | imu.SetDummySPI(SPI::Port::kOnboardCS2); |
| 832 | auto imu_reset = make_unique<DigitalOutput>(6); |
| 833 | imu.set_reset(imu_reset.get()); |
| 834 | ::std::thread imu_thread(::std::ref(imu)); |
| 835 | |
| 836 | // While as of 2/9/18 the drivetrain Victors are SPX, it appears as though they |
| 837 | // are identical, as far as DrivetrainWriter is concerned, to the SP variety |
| 838 | // so all the Victors are written as SPs. |
| 839 | |
| 840 | DrivetrainWriter drivetrain_writer; |
| 841 | drivetrain_writer.set_drivetrain_left_victor( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 842 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 843 | drivetrain_writer.set_drivetrain_right_victor( |
| 844 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
| 845 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 846 | |
| 847 | SuperstructureWriter superstructure_writer; |
| 848 | superstructure_writer.set_left_intake_elastic_victor( |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 849 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(4))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 850 | superstructure_writer.set_left_intake_rollers_victor( |
| 851 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(5))); |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 852 | superstructure_writer.set_right_intake_elastic_victor( |
| 853 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(7))); |
| 854 | superstructure_writer.set_right_intake_rollers_victor( |
| 855 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(6))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 856 | superstructure_writer.set_proximal_victor( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 857 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0))); |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 858 | superstructure_writer.set_distal_victor( |
Austin Schuh | 6829f76 | 2018-03-02 21:36:01 -0800 | [diff] [blame] | 859 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1))); |
| 860 | |
| 861 | // Hanger: victor 8 |
Austin Schuh | 2a3e063 | 2018-02-19 16:24:49 -0800 | [diff] [blame] | 862 | |
| 863 | ::std::thread superstructure_writer_thread( |
| 864 | ::std::ref(superstructure_writer)); |
| 865 | |
| 866 | ::frc971::wpilib::BufferedPcm *pcm = new ::frc971::wpilib::BufferedPcm(); |
| 867 | SolenoidWriter solenoid_writer(pcm); |
| 868 | solenoid_writer.set_left_drivetrain_shifter(pcm->MakeSolenoid(0)); |
| 869 | solenoid_writer.set_right_drivetrain_shifter(pcm->MakeSolenoid(1)); |
| 870 | solenoid_writer.set_claw(pcm->MakeSolenoid(2)); |
| 871 | solenoid_writer.set_arm_brakes(pcm->MakeSolenoid(3)); |
| 872 | solenoid_writer.set_hook(pcm->MakeSolenoid(4)); |
| 873 | solenoid_writer.set_forks(pcm->MakeSolenoid(5)); |
| 874 | |
| 875 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 876 | |
| 877 | // Wait forever. Not much else to do... |
| 878 | while (true) { |
| 879 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 880 | if (r != 0) { |
| 881 | PLOG(WARNING, "infinite select failed"); |
| 882 | } else { |
| 883 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 888 | |
| 889 | joystick_sender.Quit(); |
| 890 | joystick_thread.join(); |
| 891 | pdp_fetcher.Quit(); |
| 892 | pdp_fetcher_thread.join(); |
| 893 | reader.Quit(); |
| 894 | reader_thread.join(); |
| 895 | imu.Quit(); |
| 896 | imu_thread.join(); |
| 897 | |
| 898 | drivetrain_writer.Quit(); |
| 899 | drivetrain_writer_thread.join(); |
| 900 | superstructure_writer.Quit(); |
| 901 | superstructure_writer_thread.join(); |
| 902 | |
| 903 | ::aos::Cleanup(); |
| 904 | } |
| 905 | }; |
| 906 | |
| 907 | } // namespace |
| 908 | } // namespace wpilib |
| 909 | } // namespace y2018 |
| 910 | |
| 911 | AOS_ROBOT_CLASS(::y2018::wpilib::WPILibRobot); |