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 <unistd.h> |
| 4 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <cinttypes> |
| 6 | |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 7 | #include "aos/init.h" |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 8 | #include "aos/logging/logging.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 9 | #include "aos/util/compiler_memory_barrier.h" |
| 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" |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 12 | #include "frc971/wpilib/fpga_time_conversion.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 | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 19 | SensorReader::SensorReader(::aos::ShmEventLoop *event_loop) |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 20 | : event_loop_(event_loop), |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 21 | robot_state_sender_(event_loop_->MakeSender<::aos::RobotState>("/aos")), |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 22 | my_pid_(getpid()) { |
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 | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 28 | |
| 29 | event_loop->SetRuntimeRealtimePriority(40); |
| 30 | |
| 31 | // Fill in the no pwm trigger defaults. |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 32 | timer_handler_ = event_loop_->AddTimer([this]() { Loop(); }); |
| 33 | timer_handler_->set_name("SensorReader Loop"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 34 | |
| 35 | event_loop->set_name("SensorReader"); |
| 36 | event_loop->OnRun([this]() { DoStart(); }); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void SensorReader::UpdateFastEncoderFilterHz(int hz) { |
| 40 | fast_encoder_filter_.SetPeriodHz(::std::max(hz, 100000)); |
| 41 | } |
| 42 | |
| 43 | void SensorReader::UpdateMediumEncoderFilterHz(int hz) { |
| 44 | medium_encoder_filter_.SetPeriodHz(::std::max(hz, 50000)); |
| 45 | } |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 46 | |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 47 | void SensorReader::set_drivetrain_left_encoder( |
| 48 | ::std::unique_ptr<frc::Encoder> encoder) { |
| 49 | fast_encoder_filter_.Add(encoder.get()); |
| 50 | drivetrain_left_encoder_ = ::std::move(encoder); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 51 | drivetrain_left_encoder_->SetMaxPeriod(0.005); |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void SensorReader::set_drivetrain_right_encoder( |
| 55 | ::std::unique_ptr<frc::Encoder> encoder) { |
| 56 | fast_encoder_filter_.Add(encoder.get()); |
| 57 | drivetrain_right_encoder_ = ::std::move(encoder); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 58 | drivetrain_right_encoder_->SetMaxPeriod(0.005); |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 61 | monotonic_clock::time_point SensorReader::GetPWMStartTime() { |
| 62 | int32_t status = 0; |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 63 | const auto new_fpga_time = |
| 64 | hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status)); |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 65 | |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 66 | if (!ds_->IsSysActive()) { |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 67 | return monotonic_clock::min_time; |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 68 | } |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 69 | |
| 70 | const auto fpga_offset = CalculateFpgaOffset(); |
| 71 | // If we failed to sample the offset, just ignore this reading. |
| 72 | if (!fpga_offset) { |
| 73 | return monotonic_clock::min_time; |
| 74 | } |
| 75 | |
| 76 | return monotonic_clock::epoch() + (new_fpga_time + *fpga_offset); |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 79 | void SensorReader::DoStart() { |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 80 | Start(); |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 81 | if (dma_synchronizer_) { |
| 82 | dma_synchronizer_->Start(); |
| 83 | } |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 84 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 85 | period_ = |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 86 | pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 87 | if (pwm_trigger_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 88 | AOS_LOG(INFO, "Using PWM trigger and a 5.05 ms period\n"); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 89 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 90 | AOS_LOG(INFO, "Defaulting to open loop pwm synchronization\n"); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 91 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 92 | |
| 93 | // Now that we are configured, actually fill in the defaults. |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 94 | timer_handler_->Setup( |
| 95 | event_loop_->monotonic_now() + |
| 96 | (pwm_trigger_ ? chrono::milliseconds(3) : chrono::milliseconds(4)), |
| 97 | period_); |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 98 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 99 | last_monotonic_now_ = monotonic_clock::now(); |
| 100 | } |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 101 | |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 102 | void SensorReader::Loop() { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 103 | const monotonic_clock::time_point monotonic_now = |
| 104 | event_loop_->monotonic_now(); |
| 105 | |
| 106 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 107 | auto builder = robot_state_sender_.MakeBuilder(); |
| 108 | builder.Send(::frc971::wpilib::PopulateRobotState(&builder, my_pid_)); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 109 | } |
| 110 | RunIteration(); |
| 111 | if (dma_synchronizer_) { |
| 112 | dma_synchronizer_->RunIteration(); |
| 113 | RunDmaIteration(); |
| 114 | } |
| 115 | |
| 116 | if (pwm_trigger_) { |
James Kuszmaul | 1bce577 | 2020-03-08 22:13:50 -0700 | [diff] [blame] | 117 | // TODO(austin): Put this in a status message. |
| 118 | VLOG(1) << "PWM wakeup delta: " |
| 119 | << (monotonic_now - last_monotonic_now_).count(); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 120 | last_monotonic_now_ = monotonic_now; |
| 121 | |
| 122 | monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime(); |
| 123 | if (last_tick_timepoint == monotonic_clock::min_time) { |
| 124 | return; |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 125 | } |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 126 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 127 | last_tick_timepoint += |
| 128 | ((monotonic_now - last_tick_timepoint) / period_) * period_; |
| 129 | // If it's over 1/2 of a period back in time, that's wrong. Move it |
| 130 | // forwards to now. |
| 131 | if (last_tick_timepoint - monotonic_now < -period_ / 2) { |
| 132 | last_tick_timepoint += period_; |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 133 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 134 | |
| 135 | // We should be sampling our sensors to kick off the control cycle 50 uS |
| 136 | // after the falling edge. This gives us a little bit of buffer for |
| 137 | // errors in waking up. The PWM cycle starts at the falling edge of the |
| 138 | // PWM pulse. |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 139 | const auto next_time = |
| 140 | last_tick_timepoint + period_ + chrono::microseconds(50); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 141 | |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 142 | timer_handler_->Setup(next_time, period_); |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 143 | } |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 146 | } // namespace wpilib |
| 147 | } // namespace frc971 |