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