Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 1 | #include "frc971/wpilib/sensor_reader.h" |
| 2 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 3 | #include <inttypes.h> |
| 4 | #include <unistd.h> |
| 5 | |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 6 | #include "aos/init.h" |
| 7 | #include "aos/util/compiler_memory_barrier.h" |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 8 | #include "aos/util/phased_loop.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 9 | #include "frc971/wpilib/ahal/DigitalInput.h" |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 10 | #include "frc971/wpilib/ahal/DriverStation.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 11 | #include "frc971/wpilib/ahal/Utility.h" |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 12 | #include "frc971/wpilib/wpilib_interface.h" |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 13 | #include "hal/PWM.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 14 | |
| 15 | namespace frc971 { |
| 16 | namespace wpilib { |
| 17 | |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 18 | SensorReader::SensorReader() { |
| 19 | // Set some defaults. We don't tend to exceed these, so old robots should |
| 20 | // just work with them. |
| 21 | UpdateFastEncoderFilterHz(500000); |
| 22 | UpdateMediumEncoderFilterHz(100000); |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 23 | ds_ = &::frc::DriverStation::GetInstance(); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void SensorReader::UpdateFastEncoderFilterHz(int hz) { |
| 27 | fast_encoder_filter_.SetPeriodHz(::std::max(hz, 100000)); |
| 28 | } |
| 29 | |
| 30 | void SensorReader::UpdateMediumEncoderFilterHz(int hz) { |
| 31 | medium_encoder_filter_.SetPeriodHz(::std::max(hz, 50000)); |
| 32 | } |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 33 | |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 34 | void SensorReader::set_drivetrain_left_encoder( |
| 35 | ::std::unique_ptr<frc::Encoder> encoder) { |
| 36 | fast_encoder_filter_.Add(encoder.get()); |
| 37 | drivetrain_left_encoder_ = ::std::move(encoder); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 38 | drivetrain_left_encoder_->SetMaxPeriod(0.005); |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void SensorReader::set_drivetrain_right_encoder( |
| 42 | ::std::unique_ptr<frc::Encoder> encoder) { |
| 43 | fast_encoder_filter_.Add(encoder.get()); |
| 44 | drivetrain_right_encoder_ = ::std::move(encoder); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 45 | drivetrain_right_encoder_->SetMaxPeriod(0.005); |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 48 | monotonic_clock::time_point SensorReader::GetPWMStartTime() { |
| 49 | int32_t status = 0; |
| 50 | const hal::fpga_clock::time_point new_fpga_time = hal::fpga_clock::time_point( |
| 51 | hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status))); |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 52 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 53 | aos_compiler_memory_barrier(); |
| 54 | const hal::fpga_clock::time_point fpga_time_before = hal::fpga_clock::now(); |
| 55 | aos_compiler_memory_barrier(); |
| 56 | const monotonic_clock::time_point monotonic_now = monotonic_clock::now(); |
| 57 | aos_compiler_memory_barrier(); |
| 58 | const hal::fpga_clock::time_point fpga_time_after = hal::fpga_clock::now(); |
| 59 | aos_compiler_memory_barrier(); |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 60 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 61 | const chrono::nanoseconds fpga_sample_length = |
| 62 | fpga_time_after - fpga_time_before; |
| 63 | const chrono::nanoseconds fpga_offset = |
| 64 | hal::fpga_clock::time_point((fpga_time_after.time_since_epoch() + |
| 65 | fpga_time_before.time_since_epoch()) / |
| 66 | 2) - |
| 67 | new_fpga_time; |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 68 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 69 | // Make sure that there wasn't a context switch while we were sampling the |
| 70 | // clocks. If there was, we are better off rejecting the sample than using |
| 71 | // it. |
| 72 | if (ds_->IsSysActive() && fpga_sample_length <= chrono::microseconds(20) && |
| 73 | fpga_sample_length >= chrono::microseconds(0)) { |
| 74 | // Compute when the edge was. |
| 75 | return monotonic_now - fpga_offset; |
| 76 | } else { |
| 77 | return monotonic_clock::min_time; |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 78 | } |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 81 | void SensorReader::operator()() { |
| 82 | ::aos::SetCurrentThreadName("SensorReader"); |
| 83 | |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 84 | int32_t my_pid = getpid(); |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 85 | |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 86 | Start(); |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 87 | if (dma_synchronizer_) { |
| 88 | dma_synchronizer_->Start(); |
| 89 | } |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 90 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 91 | const chrono::microseconds period = |
| 92 | pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 93 | if (pwm_trigger_) { |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 94 | LOG(INFO, "Using PWM trigger and a 5.05 ms period\n"); |
| 95 | } else { |
| 96 | LOG(INFO, "Defaulting to open loop pwm synchronization\n"); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 97 | } |
| 98 | ::aos::time::PhasedLoop phased_loop( |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 99 | period, |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 100 | pwm_trigger_ ? ::std::chrono::milliseconds(3) : chrono::milliseconds(4)); |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 101 | |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 102 | ::aos::SetCurrentThreadRealtimePriority(40); |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 103 | monotonic_clock::time_point last_monotonic_now = monotonic_clock::now(); |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 104 | while (run_) { |
| 105 | { |
| 106 | const int iterations = phased_loop.SleepUntilNext(); |
| 107 | if (iterations != 1) { |
| 108 | LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1); |
| 109 | } |
| 110 | } |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 111 | const monotonic_clock::time_point monotonic_now = monotonic_clock::now(); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 112 | |
| 113 | ::frc971::wpilib::SendRobotState(my_pid); |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 114 | RunIteration(); |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 115 | if (dma_synchronizer_) { |
| 116 | dma_synchronizer_->RunIteration(); |
| 117 | RunDmaIteration(); |
| 118 | } |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 119 | |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 120 | if (pwm_trigger_) { |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 121 | LOG(DEBUG, "PWM wakeup delta: %lld\n", |
| 122 | (monotonic_now - last_monotonic_now).count()); |
| 123 | last_monotonic_now = monotonic_now; |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 124 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 125 | monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime(); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 126 | if (last_tick_timepoint == monotonic_clock::min_time) { |
| 127 | continue; |
| 128 | } |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 129 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame^] | 130 | last_tick_timepoint += |
| 131 | (monotonic_now - last_tick_timepoint) / period * period; |
| 132 | // If it's over 1/2 of a period back in time, that's wrong. Move it |
| 133 | // forwards to now. |
| 134 | if (last_tick_timepoint - monotonic_now < -period / 2) { |
| 135 | last_tick_timepoint += period; |
| 136 | } |
| 137 | |
| 138 | // We should be sampling our sensors to kick off the control cycle 50 uS |
| 139 | // after the falling edge. This gives us a little bit of buffer for |
| 140 | // errors in waking up. The PWM cycle starts at the falling edge of the |
| 141 | // PWM pulse. |
| 142 | chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime( |
| 143 | period, last_tick_timepoint + chrono::microseconds(50)); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 144 | |
| 145 | phased_loop.set_interval_and_offset(period, new_offset); |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 146 | } |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 147 | } |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 150 | } // namespace wpilib |
| 151 | } // namespace frc971 |