Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [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 "DigitalGlitchFilter.h" |
| 15 | #include "DriverStation.h" |
| 16 | #include "Encoder.h" |
| 17 | #include "Compressor.h" |
| 18 | #include "VictorSP.h" |
| 19 | #undef ERROR |
| 20 | |
| 21 | #include "aos/common/commonmath.h" |
| 22 | #include "aos/common/logging/logging.h" |
| 23 | #include "aos/common/logging/queue_logging.h" |
| 24 | #include "aos/common/messages/robot_state.q.h" |
| 25 | #include "aos/common/stl_mutex.h" |
| 26 | #include "aos/common/time.h" |
| 27 | #include "aos/common/util/compiler_memory_barrier.h" |
| 28 | #include "aos/common/util/log_interval.h" |
| 29 | #include "aos/common/util/phased_loop.h" |
| 30 | #include "aos/linux_code/init.h" |
| 31 | |
| 32 | #include "frc971/control_loops/control_loops.q.h" |
| 33 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
| 34 | #include "frc971/wpilib/ADIS16448.h" |
| 35 | #include "frc971/wpilib/buffered_pcm.h" |
| 36 | #include "frc971/wpilib/buffered_solenoid.h" |
| 37 | #include "frc971/wpilib/gyro_sender.h" |
| 38 | #include "frc971/wpilib/dma.h" |
| 39 | #include "frc971/wpilib/dma_edge_counting.h" |
| 40 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 41 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 42 | #include "frc971/wpilib/joystick_sender.h" |
| 43 | #include "frc971/wpilib/logging.q.h" |
| 44 | #include "frc971/wpilib/loop_output_handler.h" |
| 45 | #include "frc971/wpilib/pdp_fetcher.h" |
| 46 | #include "frc971/wpilib/wpilib_interface.h" |
| 47 | #include "frc971/wpilib/wpilib_robot_base.h" |
| 48 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 49 | #include "frc971/control_loops/drivetrain/drivetrain.q.h" |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 50 | #include "y2017_bot3/control_loops/drivetrain/drivetrain_dog_motor_plant.h" |
| 51 | #include "y2017_bot3/control_loops/superstructure/superstructure.q.h" |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 52 | |
| 53 | #ifndef M_PI |
| 54 | #define M_PI 3.14159265358979323846 |
| 55 | #endif |
| 56 | |
| 57 | using ::frc971::control_loops::drivetrain_queue; |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 58 | using ::y2017_bot3::control_loops::superstructure_queue; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 59 | using ::aos::monotonic_clock; |
| 60 | namespace chrono = ::std::chrono; |
| 61 | |
| 62 | namespace y2017_bot3 { |
| 63 | namespace wpilib { |
| 64 | namespace { |
| 65 | |
| 66 | constexpr double kMaxBringupPower = 12.0; |
| 67 | |
| 68 | constexpr double kDrivetrainCyclesPerRevolution = 256; |
| 69 | constexpr double kDrivetrainEncoderCountsPerRevolution = |
| 70 | kDrivetrainCyclesPerRevolution * 4; |
| 71 | constexpr double kDrivetrainEncoderRatio = |
| 72 | 1.0 * control_loops::drivetrain::kWheelRadius; |
| 73 | constexpr double kMaxDrivetrainEncoderPulsesPerSecond = |
| 74 | control_loops::drivetrain::kFreeSpeed * |
| 75 | control_loops::drivetrain::kHighOutputRatio / |
| 76 | kDrivetrainEncoderRatio * |
| 77 | kDrivetrainEncoderCountsPerRevolution; |
| 78 | |
| 79 | // TODO(Brian): Fix the interpretation of the result of GetRaw here and in the |
| 80 | // DMA stuff and then removing the * 2.0 in *_translate. |
| 81 | // The low bit is direction. |
| 82 | |
| 83 | // TODO(brian): Replace this with ::std::make_unique once all our toolchains |
| 84 | // have support. |
| 85 | template <class T, class... U> |
| 86 | std::unique_ptr<T> make_unique(U &&... u) { |
| 87 | return std::unique_ptr<T>(new T(std::forward<U>(u)...)); |
| 88 | } |
| 89 | |
| 90 | // TODO(brian): Use ::std::max instead once we have C++14 so that can be |
| 91 | // constexpr. |
| 92 | template <typename T> |
| 93 | constexpr T max(T a, T b) { |
| 94 | return (a > b) ? a : b; |
| 95 | } |
| 96 | template <typename T, typename... Rest> |
| 97 | constexpr T max(T a, T b, T c, Rest... rest) { |
| 98 | return max(max(a, b), c, rest...); |
| 99 | } |
| 100 | |
| 101 | double hall_translate(double in) { |
| 102 | // Turn voltage from our 3-state halls into a ratio that the loop can use. |
| 103 | return in / 5.0; |
| 104 | } |
| 105 | |
| 106 | double drivetrain_translate(int32_t in) { |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 107 | return -static_cast<double>(in) / (256.0 /*cpr*/ * 4.0 /*4x*/) * |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 108 | kDrivetrainEncoderRatio * |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 109 | control_loops::drivetrain::kWheelRadius * |
| 110 | 2.0 * M_PI; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | double drivetrain_velocity_translate(double in) { |
| 114 | return (1.0 / in) / 256.0 /*cpr*/ * |
| 115 | kDrivetrainEncoderRatio * |
| 116 | control_loops::drivetrain::kWheelRadius * 2.0 * M_PI; |
| 117 | } |
| 118 | |
| 119 | constexpr double kMaxFastEncoderPulsesPerSecond = |
| 120 | kMaxDrivetrainEncoderPulsesPerSecond; |
| 121 | static_assert(kMaxFastEncoderPulsesPerSecond <= 1300000, |
| 122 | "fast encoders are too fast"); |
| 123 | |
| 124 | // Class to send position messages with sensor readings to our loops. |
| 125 | class SensorReader { |
| 126 | public: |
| 127 | SensorReader() { |
| 128 | // Set to filter out anything shorter than 1/4 of the minimum pulse width |
| 129 | // we should ever see. |
| 130 | fast_encoder_filter_.SetPeriodNanoSeconds( |
| 131 | static_cast<int>(1 / 4.0 /* built-in tolerance */ / |
| 132 | kMaxFastEncoderPulsesPerSecond * 1e9 + |
| 133 | 0.5)); |
| 134 | hall_filter_.SetPeriodNanoSeconds(100000); |
| 135 | } |
| 136 | |
| 137 | void set_drivetrain_left_encoder(::std::unique_ptr<Encoder> encoder) { |
| 138 | fast_encoder_filter_.Add(encoder.get()); |
| 139 | drivetrain_left_encoder_ = ::std::move(encoder); |
| 140 | } |
| 141 | |
| 142 | void set_drivetrain_right_encoder(::std::unique_ptr<Encoder> encoder) { |
| 143 | fast_encoder_filter_.Add(encoder.get()); |
| 144 | drivetrain_right_encoder_ = ::std::move(encoder); |
| 145 | } |
| 146 | |
| 147 | void set_drivetrain_left_hall(::std::unique_ptr<AnalogInput> analog) { |
| 148 | drivetrain_left_hall_ = ::std::move(analog); |
| 149 | } |
| 150 | |
| 151 | void set_drivetrain_right_hall(::std::unique_ptr<AnalogInput> analog) { |
| 152 | drivetrain_right_hall_ = ::std::move(analog); |
| 153 | } |
| 154 | |
| 155 | void set_pwm_trigger(::std::unique_ptr<DigitalInput> pwm_trigger) { |
| 156 | medium_encoder_filter_.Add(pwm_trigger.get()); |
| 157 | pwm_trigger_ = ::std::move(pwm_trigger); |
| 158 | } |
| 159 | |
| 160 | // All of the DMA-related set_* calls must be made before this, and it |
| 161 | // doesn't |
| 162 | // hurt to do all of them. |
| 163 | void set_dma(::std::unique_ptr<DMA> dma) { |
| 164 | dma_synchronizer_.reset( |
| 165 | new ::frc971::wpilib::DMASynchronizer(::std::move(dma))); |
| 166 | } |
| 167 | |
| 168 | void RunPWMDetecter() { |
| 169 | ::aos::SetCurrentThreadRealtimePriority(41); |
| 170 | |
| 171 | pwm_trigger_->RequestInterrupts(); |
| 172 | // Rising edge only. |
| 173 | pwm_trigger_->SetUpSourceEdge(true, false); |
| 174 | |
| 175 | monotonic_clock::time_point last_posedge_monotonic = |
| 176 | monotonic_clock::min_time; |
| 177 | |
| 178 | while (run_) { |
| 179 | auto ret = pwm_trigger_->WaitForInterrupt(1.0, true); |
| 180 | if (ret == InterruptableSensorBase::WaitResult::kRisingEdge) { |
| 181 | // Grab all the clocks. |
| 182 | const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp(); |
| 183 | |
| 184 | aos_compiler_memory_barrier(); |
| 185 | const double fpga_time_before = GetFPGATime() * 1e-6; |
| 186 | aos_compiler_memory_barrier(); |
| 187 | const monotonic_clock::time_point monotonic_now = |
| 188 | monotonic_clock::now(); |
| 189 | aos_compiler_memory_barrier(); |
| 190 | const double fpga_time_after = GetFPGATime() * 1e-6; |
| 191 | aos_compiler_memory_barrier(); |
| 192 | |
| 193 | const double fpga_offset = |
| 194 | (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time; |
| 195 | |
| 196 | // Compute when the edge was. |
| 197 | const monotonic_clock::time_point monotonic_edge = |
| 198 | monotonic_now - chrono::duration_cast<chrono::nanoseconds>( |
| 199 | chrono::duration<double>(fpga_offset)); |
| 200 | |
| 201 | LOG(INFO, "Got PWM pulse %f spread, %f offset, %lld trigger\n", |
| 202 | fpga_time_after - fpga_time_before, fpga_offset, |
| 203 | monotonic_edge.time_since_epoch().count()); |
| 204 | |
| 205 | // Compute bounds on the timestep and sampling times. |
| 206 | const double fpga_sample_length = fpga_time_after - fpga_time_before; |
| 207 | const chrono::nanoseconds elapsed_time = |
| 208 | monotonic_edge - last_posedge_monotonic; |
| 209 | |
| 210 | last_posedge_monotonic = monotonic_edge; |
| 211 | |
| 212 | // Verify that the values are sane. |
| 213 | if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) { |
| 214 | continue; |
| 215 | } |
| 216 | if (fpga_offset < 0 || fpga_offset > 0.00015) { |
| 217 | continue; |
| 218 | } |
| 219 | if (elapsed_time > |
| 220 | chrono::microseconds(5050) + chrono::microseconds(4) || |
| 221 | elapsed_time < |
| 222 | chrono::microseconds(5050) - chrono::microseconds(4)) { |
| 223 | continue; |
| 224 | } |
| 225 | // Good edge! |
| 226 | { |
| 227 | ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_); |
| 228 | last_tick_time_monotonic_timepoint_ = last_posedge_monotonic; |
| 229 | last_period_ = elapsed_time; |
| 230 | } |
| 231 | } else { |
| 232 | LOG(INFO, "PWM triggered %d\n", ret); |
| 233 | } |
| 234 | } |
| 235 | pwm_trigger_->CancelInterrupts(); |
| 236 | } |
| 237 | |
| 238 | void operator()() { |
| 239 | ::aos::SetCurrentThreadName("SensorReader"); |
| 240 | |
| 241 | my_pid_ = getpid(); |
| 242 | ds_ = &DriverStation::GetInstance(); |
| 243 | |
| 244 | dma_synchronizer_->Start(); |
| 245 | |
| 246 | ::aos::time::PhasedLoop phased_loop(last_period_, |
| 247 | ::std::chrono::milliseconds(3)); |
| 248 | chrono::nanoseconds filtered_period = last_period_; |
| 249 | |
| 250 | ::std::thread pwm_detecter_thread( |
| 251 | ::std::bind(&SensorReader::RunPWMDetecter, this)); |
| 252 | |
| 253 | ::aos::SetCurrentThreadRealtimePriority(40); |
| 254 | while (run_) { |
| 255 | { |
| 256 | const int iterations = phased_loop.SleepUntilNext(); |
| 257 | if (iterations != 1) { |
| 258 | LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1); |
| 259 | } |
| 260 | } |
| 261 | RunIteration(); |
| 262 | |
| 263 | monotonic_clock::time_point last_tick_timepoint; |
| 264 | chrono::nanoseconds period; |
| 265 | { |
| 266 | ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_); |
| 267 | last_tick_timepoint = last_tick_time_monotonic_timepoint_; |
| 268 | period = last_period_; |
| 269 | } |
| 270 | |
| 271 | if (last_tick_timepoint == monotonic_clock::min_time) { |
| 272 | continue; |
| 273 | } |
| 274 | chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime( |
| 275 | period, last_tick_timepoint + chrono::microseconds(2050)); |
| 276 | |
| 277 | // TODO(austin): If this is the first edge in a while, skip to it (plus |
| 278 | // an offset). Otherwise, slowly drift time to line up. |
| 279 | |
| 280 | phased_loop.set_interval_and_offset(period, new_offset); |
| 281 | } |
| 282 | pwm_detecter_thread.join(); |
| 283 | } |
| 284 | |
| 285 | void RunIteration() { |
| 286 | ::frc971::wpilib::SendRobotState(my_pid_, ds_); |
| 287 | |
| 288 | { |
| 289 | auto drivetrain_message = drivetrain_queue.position.MakeMessage(); |
| 290 | drivetrain_message->right_encoder = |
| 291 | drivetrain_translate(drivetrain_right_encoder_->GetRaw()); |
| 292 | drivetrain_message->right_speed = |
| 293 | drivetrain_velocity_translate(drivetrain_right_encoder_->GetPeriod()); |
| 294 | |
| 295 | drivetrain_message->left_encoder = |
| 296 | -drivetrain_translate(drivetrain_left_encoder_->GetRaw()); |
| 297 | drivetrain_message->left_speed = |
| 298 | drivetrain_velocity_translate(drivetrain_left_encoder_->GetPeriod()); |
| 299 | |
| 300 | drivetrain_message->left_shifter_position = |
| 301 | hall_translate(drivetrain_left_hall_->GetVoltage()); |
| 302 | drivetrain_message->right_shifter_position = |
| 303 | hall_translate(drivetrain_right_hall_->GetVoltage()); |
| 304 | |
| 305 | drivetrain_message.Send(); |
| 306 | } |
| 307 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 308 | { |
| 309 | auto superstructure_message = superstructure_queue.position.MakeMessage(); |
| 310 | superstructure_message.Send(); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 311 | } |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 312 | dma_synchronizer_->RunIteration(); |
| 313 | } |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 314 | |
| 315 | void Quit() { run_ = false; } |
| 316 | |
| 317 | private: |
| 318 | double encoder_translate(int32_t value, double counts_per_revolution, |
| 319 | double ratio) { |
| 320 | return static_cast<double>(value) / counts_per_revolution * ratio * |
| 321 | (2.0 * M_PI); |
| 322 | } |
| 323 | |
| 324 | int32_t my_pid_; |
| 325 | DriverStation *ds_; |
| 326 | |
| 327 | // Mutex to manage access to the period and tick time variables. |
| 328 | ::aos::stl_mutex tick_time_mutex_; |
| 329 | monotonic_clock::time_point last_tick_time_monotonic_timepoint_ = |
| 330 | monotonic_clock::min_time; |
| 331 | chrono::nanoseconds last_period_ = chrono::microseconds(5050); |
| 332 | |
| 333 | ::std::unique_ptr<::frc971::wpilib::DMASynchronizer> dma_synchronizer_; |
| 334 | |
| 335 | ::std::unique_ptr<Encoder> drivetrain_left_encoder_, |
| 336 | drivetrain_right_encoder_; |
| 337 | |
| 338 | ::std::unique_ptr<AnalogInput> drivetrain_left_hall_, drivetrain_right_hall_; |
| 339 | |
| 340 | ::std::unique_ptr<DigitalInput> pwm_trigger_; |
| 341 | |
| 342 | ::std::atomic<bool> run_{true}; |
| 343 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 344 | DigitalGlitchFilter fast_encoder_filter_, medium_encoder_filter_, |
| 345 | hall_filter_; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | class SolenoidWriter { |
| 349 | public: |
| 350 | SolenoidWriter(const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm) |
| 351 | : pcm_(pcm), |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 352 | drivetrain_(".y2017_bot3.control_loops.drivetrain_queue.output"), |
| 353 | superstructure_( |
| 354 | ".y2017_bot3.control_loops.superstructure_queue.output") {} |
| 355 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 356 | void set_compressor(::std::unique_ptr<Compressor> compressor) { |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 357 | compressor_ = ::std::move(compressor); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void set_drivetrain_shifter( |
| 361 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 362 | drivetrain_shifter_ = ::std::move(s); |
| 363 | } |
| 364 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 365 | void set_fingers(::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> s) { |
| 366 | fingers_ = ::std::move(s); |
| 367 | } |
| 368 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 369 | void operator()() { |
| 370 | compressor_->Start(); |
| 371 | ::aos::SetCurrentThreadName("Solenoids"); |
| 372 | ::aos::SetCurrentThreadRealtimePriority(27); |
| 373 | |
| 374 | ::aos::time::PhasedLoop phased_loop(::std::chrono::milliseconds(20), |
| 375 | ::std::chrono::milliseconds(1)); |
| 376 | |
| 377 | while (run_) { |
| 378 | { |
| 379 | int iterations = phased_loop.SleepUntilNext(); |
| 380 | if (iterations != 1) { |
| 381 | LOG(DEBUG, "Solenoids skipped %d iterations\n", iterations - 1); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | { |
| 386 | drivetrain_.FetchLatest(); |
| 387 | if (drivetrain_.get()) { |
| 388 | LOG_STRUCT(DEBUG, "solenoids", *drivetrain_); |
| 389 | drivetrain_shifter_->Set( |
| 390 | !(drivetrain_->left_high || drivetrain_->right_high)); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | { |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 395 | superstructure_.FetchLatest(); |
| 396 | if (superstructure_.get()) { |
| 397 | LOG_STRUCT(DEBUG, "solenoids", *superstructure_); |
| 398 | fingers_->Set(superstructure_->fingers_out); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 399 | } |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 400 | } |
| 401 | |
| 402 | { |
| 403 | ::frc971::wpilib::PneumaticsToLog to_log; |
| 404 | { to_log.compressor_on = compressor_->Enabled(); } |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 405 | |
| 406 | pcm_->Flush(); |
| 407 | to_log.read_solenoids = pcm_->GetAll(); |
| 408 | LOG_STRUCT(DEBUG, "pneumatics info", to_log); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | void Quit() { run_ = false; } |
| 414 | |
| 415 | private: |
| 416 | const ::std::unique_ptr<::frc971::wpilib::BufferedPcm> &pcm_; |
| 417 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> drivetrain_shifter_; |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 418 | ::std::unique_ptr<::frc971::wpilib::BufferedSolenoid> fingers_; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 419 | |
| 420 | ::std::unique_ptr<Compressor> compressor_; |
| 421 | |
| 422 | ::aos::Queue<::frc971::control_loops::DrivetrainQueue::Output> drivetrain_; |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 423 | ::aos::Queue<::y2017_bot3::control_loops::SuperstructureQueue::Output> |
| 424 | superstructure_; |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 425 | |
| 426 | ::std::atomic<bool> run_{true}; |
| 427 | }; |
| 428 | |
| 429 | class DrivetrainWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 430 | public: |
| 431 | void set_drivetrain_left_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 432 | drivetrain_left_victor_ = ::std::move(t); |
| 433 | } |
| 434 | |
| 435 | void set_drivetrain_right_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 436 | drivetrain_right_victor_ = ::std::move(t); |
| 437 | }; |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 438 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 439 | private: |
| 440 | virtual void Read() override { |
| 441 | ::frc971::control_loops::drivetrain_queue.output.FetchAnother(); |
| 442 | } |
| 443 | |
| 444 | virtual void Write() override { |
| 445 | auto &queue = ::frc971::control_loops::drivetrain_queue.output; |
| 446 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 447 | drivetrain_left_victor_->SetSpeed(-queue->left_voltage / 12.0); |
| 448 | drivetrain_right_victor_->SetSpeed(queue->right_voltage / 12.0); |
| 449 | } |
| 450 | |
| 451 | virtual void Stop() override { |
| 452 | LOG(WARNING, "drivetrain output too old\n"); |
| 453 | drivetrain_left_victor_->SetDisabled(); |
| 454 | drivetrain_right_victor_->SetDisabled(); |
| 455 | } |
| 456 | |
| 457 | ::std::unique_ptr<::frc::VictorSP> drivetrain_left_victor_, |
| 458 | drivetrain_right_victor_; |
| 459 | }; |
| 460 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 461 | class SuperstructureWriter : public ::frc971::wpilib::LoopOutputHandler { |
| 462 | public: |
| 463 | void set_rollers_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 464 | rollers_victor_ = ::std::move(t); |
| 465 | } |
| 466 | |
| 467 | void set_hanger_victor(::std::unique_ptr<::frc::VictorSP> t) { |
| 468 | hanger_victor_ = ::std::move(t); |
| 469 | } |
| 470 | private: |
| 471 | virtual void Read() override { |
| 472 | ::y2017_bot3::control_loops::superstructure_queue.output.FetchAnother(); |
| 473 | } |
| 474 | |
| 475 | virtual void Write() override { |
| 476 | auto &queue = ::y2017_bot3::control_loops::superstructure_queue.output; |
| 477 | LOG_STRUCT(DEBUG, "will output", *queue); |
| 478 | rollers_victor_->SetSpeed(queue->voltage_rollers / 12.0); |
| 479 | hanger_victor_->SetSpeed(queue->hanger_voltage / 12.0); |
| 480 | } |
| 481 | |
| 482 | virtual void Stop() override { |
| 483 | LOG(WARNING, "Superstructure output too old.\n"); |
| 484 | rollers_victor_->SetDisabled(); |
| 485 | hanger_victor_->SetDisabled(); |
| 486 | } |
| 487 | |
| 488 | ::std::unique_ptr<::frc::VictorSP> rollers_victor_; |
| 489 | ::std::unique_ptr<::frc::VictorSP> hanger_victor_; |
| 490 | }; |
| 491 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 492 | class WPILibRobot : public ::frc971::wpilib::WPILibRobotBase { |
| 493 | public: |
| 494 | ::std::unique_ptr<Encoder> make_encoder(int index) { |
| 495 | return make_unique<Encoder>(10 + index * 2, 11 + index * 2, false, |
| 496 | Encoder::k4X); |
| 497 | } |
| 498 | |
| 499 | void Run() override { |
| 500 | ::aos::InitNRT(); |
| 501 | ::aos::SetCurrentThreadName("StartCompetition"); |
| 502 | |
| 503 | ::frc971::wpilib::JoystickSender joystick_sender; |
| 504 | ::std::thread joystick_thread(::std::ref(joystick_sender)); |
| 505 | |
| 506 | ::frc971::wpilib::PDPFetcher pdp_fetcher; |
| 507 | ::std::thread pdp_fetcher_thread(::std::ref(pdp_fetcher)); |
| 508 | SensorReader reader; |
| 509 | |
| 510 | // TODO(sabina): Update port numbers |
| 511 | reader.set_drivetrain_left_encoder(make_encoder(0)); |
| 512 | reader.set_drivetrain_right_encoder(make_encoder(1)); |
| 513 | reader.set_drivetrain_left_hall(make_unique<AnalogInput>(0)); |
| 514 | reader.set_drivetrain_right_hall(make_unique<AnalogInput>(1)); |
| 515 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 516 | reader.set_pwm_trigger(make_unique<DigitalInput>(0)); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 517 | reader.set_dma(make_unique<DMA>()); |
| 518 | ::std::thread reader_thread(::std::ref(reader)); |
| 519 | |
| 520 | ::frc971::wpilib::GyroSender gyro_sender; |
| 521 | ::std::thread gyro_thread(::std::ref(gyro_sender)); |
| 522 | |
| 523 | DrivetrainWriter drivetrain_writer; |
| 524 | drivetrain_writer.set_drivetrain_left_victor( |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 525 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(0))); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 526 | drivetrain_writer.set_drivetrain_right_victor( |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 527 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(1))); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 528 | ::std::thread drivetrain_writer_thread(::std::ref(drivetrain_writer)); |
| 529 | |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 530 | SuperstructureWriter superstructure_writer; |
| 531 | superstructure_writer.set_rollers_victor( |
| 532 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(2))); |
| 533 | superstructure_writer.set_hanger_victor( |
| 534 | ::std::unique_ptr<::frc::VictorSP>(new ::frc::VictorSP(3))); |
| 535 | |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 536 | ::std::unique_ptr<::frc971::wpilib::BufferedPcm> pcm( |
| 537 | new ::frc971::wpilib::BufferedPcm()); |
| 538 | SolenoidWriter solenoid_writer(pcm); |
| 539 | solenoid_writer.set_drivetrain_shifter(pcm->MakeSolenoid(0)); |
Sabina Davis | b6b987d | 2017-10-22 20:50:21 -0700 | [diff] [blame^] | 540 | solenoid_writer.set_fingers(pcm->MakeSolenoid(2)); |
Sabina Davis | 5ae0c7c | 2017-10-21 20:51:55 -0700 | [diff] [blame] | 541 | |
| 542 | solenoid_writer.set_compressor(make_unique<Compressor>()); |
| 543 | |
| 544 | ::std::thread solenoid_thread(::std::ref(solenoid_writer)); |
| 545 | |
| 546 | // Wait forever. Not much else to do... |
| 547 | while (true) { |
| 548 | const int r = select(0, nullptr, nullptr, nullptr, nullptr); |
| 549 | if (r != 0) { |
| 550 | PLOG(WARNING, "infinite select failed"); |
| 551 | } else { |
| 552 | PLOG(WARNING, "infinite select succeeded??\n"); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | LOG(ERROR, "Exiting WPILibRobot\n"); |
| 557 | |
| 558 | joystick_sender.Quit(); |
| 559 | joystick_thread.join(); |
| 560 | pdp_fetcher.Quit(); |
| 561 | pdp_fetcher_thread.join(); |
| 562 | reader.Quit(); |
| 563 | reader_thread.join(); |
| 564 | gyro_sender.Quit(); |
| 565 | gyro_thread.join(); |
| 566 | |
| 567 | drivetrain_writer.Quit(); |
| 568 | drivetrain_writer_thread.join(); |
| 569 | solenoid_writer.Quit(); |
| 570 | solenoid_thread.join(); |
| 571 | |
| 572 | ::aos::Cleanup(); |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | } // namespace |
| 577 | } // namespace wpilib |
| 578 | } // namespace y2017_bot3 |
| 579 | |
| 580 | AOS_ROBOT_CLASS(::y2017_bot3::wpilib::WPILibRobot); |