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