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