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