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 | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame^] | 7 | #include "aos/logging/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" |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 13 | #include "frc971/wpilib/fpga_time_conversion.h" |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 14 | #include "frc971/wpilib/wpilib_interface.h" |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 15 | #include "hal/PWM.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 16 | |
| 17 | namespace frc971 { |
| 18 | namespace wpilib { |
| 19 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 20 | SensorReader::SensorReader(::aos::ShmEventLoop *event_loop) |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 21 | : event_loop_(event_loop), |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 22 | robot_state_sender_(event_loop_->MakeSender<::aos::RobotState>("/aos")), |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 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; |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 65 | const auto new_fpga_time = |
| 66 | hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status)); |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 67 | |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 68 | if (!ds_->IsSysActive()) { |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 69 | return monotonic_clock::min_time; |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 70 | } |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 71 | |
| 72 | const auto fpga_offset = CalculateFpgaOffset(); |
| 73 | // If we failed to sample the offset, just ignore this reading. |
| 74 | if (!fpga_offset) { |
| 75 | return monotonic_clock::min_time; |
| 76 | } |
| 77 | |
| 78 | return monotonic_clock::epoch() + (new_fpga_time + *fpga_offset); |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 81 | void SensorReader::DoStart() { |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 82 | Start(); |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 83 | if (dma_synchronizer_) { |
| 84 | dma_synchronizer_->Start(); |
| 85 | } |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 86 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 87 | period_ = |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 88 | pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 89 | if (pwm_trigger_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 90 | 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] | 91 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 92 | AOS_LOG(INFO, "Defaulting to open loop pwm synchronization\n"); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 93 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 94 | |
| 95 | // Now that we are configured, actually fill in the defaults. |
| 96 | phased_loop_handler_->set_interval_and_offset( |
| 97 | period_, |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 98 | pwm_trigger_ ? ::std::chrono::milliseconds(3) : chrono::milliseconds(4)); |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 99 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 100 | last_monotonic_now_ = monotonic_clock::now(); |
| 101 | } |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 102 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 103 | void SensorReader::Loop(const int iterations) { |
| 104 | if (iterations != 1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 105 | AOS_LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | const monotonic_clock::time_point monotonic_now = |
| 109 | event_loop_->monotonic_now(); |
| 110 | |
| 111 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 112 | auto builder = robot_state_sender_.MakeBuilder(); |
| 113 | builder.Send(::frc971::wpilib::PopulateRobotState(&builder, my_pid_)); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 114 | } |
| 115 | RunIteration(); |
| 116 | if (dma_synchronizer_) { |
| 117 | dma_synchronizer_->RunIteration(); |
| 118 | RunDmaIteration(); |
| 119 | } |
| 120 | |
| 121 | if (pwm_trigger_) { |
James Kuszmaul | 1bce577 | 2020-03-08 22:13:50 -0700 | [diff] [blame] | 122 | // TODO(austin): Put this in a status message. |
| 123 | VLOG(1) << "PWM wakeup delta: " |
| 124 | << (monotonic_now - last_monotonic_now_).count(); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 125 | last_monotonic_now_ = monotonic_now; |
| 126 | |
| 127 | monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime(); |
| 128 | if (last_tick_timepoint == monotonic_clock::min_time) { |
| 129 | return; |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 130 | } |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 131 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 132 | last_tick_timepoint += |
| 133 | ((monotonic_now - last_tick_timepoint) / period_) * period_; |
| 134 | // If it's over 1/2 of a period back in time, that's wrong. Move it |
| 135 | // forwards to now. |
| 136 | if (last_tick_timepoint - monotonic_now < -period_ / 2) { |
| 137 | last_tick_timepoint += period_; |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 138 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 139 | |
| 140 | // We should be sampling our sensors to kick off the control cycle 50 uS |
| 141 | // after the falling edge. This gives us a little bit of buffer for |
| 142 | // errors in waking up. The PWM cycle starts at the falling edge of the |
| 143 | // PWM pulse. |
| 144 | chrono::nanoseconds new_offset = |
| 145 | ::aos::time::PhasedLoop::OffsetFromIntervalAndTime( |
| 146 | period_, last_tick_timepoint + chrono::microseconds(50)); |
| 147 | |
| 148 | phased_loop_handler_->set_interval_and_offset(period_, new_offset); |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 149 | } |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 152 | } // namespace wpilib |
| 153 | } // namespace frc971 |