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