Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame^] | 1 | #include "frc971/wpilib/sensor_reader.h" |
| 2 | |
| 3 | #include "aos/init.h" |
| 4 | #include "aos/util/compiler_memory_barrier.h" |
| 5 | #include "frc971/wpilib/ahal/DigitalInput.h" |
| 6 | #include "frc971/wpilib/ahal/Utility.h" |
| 7 | |
| 8 | namespace frc971 { |
| 9 | namespace wpilib { |
| 10 | |
| 11 | SensorReader::SensorReader() {} |
| 12 | |
| 13 | void SensorReader::set_pwm_trigger( |
| 14 | ::std::unique_ptr<frc::DigitalInput> pwm_trigger) { |
| 15 | medium_encoder_filter_.Add(pwm_trigger.get()); |
| 16 | pwm_trigger_ = ::std::move(pwm_trigger); |
| 17 | } |
| 18 | |
| 19 | void SensorReader::RunPWMDetecter() { |
| 20 | ::aos::SetCurrentThreadRealtimePriority(41); |
| 21 | |
| 22 | pwm_trigger_->RequestInterrupts(); |
| 23 | // Rising edge only. |
| 24 | pwm_trigger_->SetUpSourceEdge(true, false); |
| 25 | |
| 26 | monotonic_clock::time_point last_posedge_monotonic = |
| 27 | monotonic_clock::min_time; |
| 28 | |
| 29 | while (run_) { |
| 30 | auto ret = pwm_trigger_->WaitForInterrupt(1.0, true); |
| 31 | if (ret == frc::InterruptableSensorBase::WaitResult::kRisingEdge) { |
| 32 | // Grab all the clocks. |
| 33 | const double pwm_fpga_time = pwm_trigger_->ReadRisingTimestamp(); |
| 34 | |
| 35 | aos_compiler_memory_barrier(); |
| 36 | const double fpga_time_before = frc::GetFPGATime() * 1e-6; |
| 37 | aos_compiler_memory_barrier(); |
| 38 | const monotonic_clock::time_point monotonic_now = monotonic_clock::now(); |
| 39 | aos_compiler_memory_barrier(); |
| 40 | const double fpga_time_after = frc::GetFPGATime() * 1e-6; |
| 41 | aos_compiler_memory_barrier(); |
| 42 | |
| 43 | const double fpga_offset = |
| 44 | (fpga_time_after + fpga_time_before) / 2.0 - pwm_fpga_time; |
| 45 | |
| 46 | // Compute when the edge was. |
| 47 | const monotonic_clock::time_point monotonic_edge = |
| 48 | monotonic_now - chrono::duration_cast<chrono::nanoseconds>( |
| 49 | chrono::duration<double>(fpga_offset)); |
| 50 | |
| 51 | LOG(DEBUG, "Got PWM pulse %f spread, %f offset, %lld trigger\n", |
| 52 | fpga_time_after - fpga_time_before, fpga_offset, |
| 53 | monotonic_edge.time_since_epoch().count()); |
| 54 | |
| 55 | // Compute bounds on the timestep and sampling times. |
| 56 | const double fpga_sample_length = fpga_time_after - fpga_time_before; |
| 57 | const chrono::nanoseconds elapsed_time = |
| 58 | monotonic_edge - last_posedge_monotonic; |
| 59 | |
| 60 | last_posedge_monotonic = monotonic_edge; |
| 61 | |
| 62 | // Verify that the values are sane. |
| 63 | if (fpga_sample_length > 2e-5 || fpga_sample_length < 0) { |
| 64 | continue; |
| 65 | } |
| 66 | if (fpga_offset < 0 || fpga_offset > 0.00015) { |
| 67 | continue; |
| 68 | } |
| 69 | if (elapsed_time > chrono::microseconds(5050) + chrono::microseconds(4) || |
| 70 | elapsed_time < chrono::microseconds(5050) - chrono::microseconds(4)) { |
| 71 | continue; |
| 72 | } |
| 73 | // Good edge! |
| 74 | { |
| 75 | ::std::unique_lock<::aos::stl_mutex> locker(tick_time_mutex_); |
| 76 | last_tick_time_monotonic_timepoint_ = last_posedge_monotonic; |
| 77 | last_period_ = elapsed_time; |
| 78 | } |
| 79 | } else { |
| 80 | LOG(INFO, "PWM triggered %d\n", ret); |
| 81 | } |
| 82 | } |
| 83 | pwm_trigger_->CancelInterrupts(); |
| 84 | } |
| 85 | |
| 86 | } // namespace wpilib |
| 87 | } // namespace frc971 |