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 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 7 | #include "absl/flags/flag.h" |
| 8 | |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 9 | #include "aos/init.h" |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 10 | #include "aos/logging/logging.h" |
Austin Schuh | 719d680 | 2021-11-05 23:46:20 -0700 | [diff] [blame] | 11 | #include "aos/realtime.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 12 | #include "aos/util/compiler_memory_barrier.h" |
| 13 | #include "frc971/wpilib/ahal/DigitalInput.h" |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 14 | #include "frc971/wpilib/ahal/DriverStation.h" |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 15 | #include "frc971/wpilib/fpga_time_conversion.h" |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 16 | #include "frc971/wpilib/wpilib_interface.h" |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 17 | #include "hal/PWM.h" |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 18 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 19 | ABSL_FLAG(int32_t, pwm_offset, 5050 / 2, |
| 20 | "Offset of reading the sensors from the start of the PWM cycle"); |
Austin Schuh | 028d81d | 2022-03-26 15:11:42 -0700 | [diff] [blame] | 21 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 22 | namespace frc971::wpilib { |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 23 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 24 | SensorReader::SensorReader(::aos::ShmEventLoop *event_loop) |
Austin Schuh | df6cbb1 | 2019-02-02 13:46:52 -0800 | [diff] [blame] | 25 | : event_loop_(event_loop), |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 26 | robot_state_sender_(event_loop_->MakeSender<::aos::RobotState>("/aos")), |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 27 | my_pid_(getpid()) { |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 28 | // Set some defaults. We don't tend to exceed these, so old robots should |
| 29 | // just work with them. |
| 30 | UpdateFastEncoderFilterHz(500000); |
| 31 | UpdateMediumEncoderFilterHz(100000); |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 32 | ds_ = &::frc::DriverStation::GetInstance(); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 33 | |
| 34 | event_loop->SetRuntimeRealtimePriority(40); |
Austin Schuh | 719d680 | 2021-11-05 23:46:20 -0700 | [diff] [blame] | 35 | // The timer interrupt fires on CPU1. Since nothing else is pinned, it will |
| 36 | // be cheapest to pin this there so it transitions directly and doesn't |
| 37 | // need to ever migrate. |
| 38 | event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1})); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 39 | |
| 40 | // Fill in the no pwm trigger defaults. |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 41 | timer_handler_ = event_loop_->AddTimer([this]() { Loop(); }); |
| 42 | timer_handler_->set_name("SensorReader Loop"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 43 | |
| 44 | event_loop->set_name("SensorReader"); |
| 45 | event_loop->OnRun([this]() { DoStart(); }); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void SensorReader::UpdateFastEncoderFilterHz(int hz) { |
| 49 | fast_encoder_filter_.SetPeriodHz(::std::max(hz, 100000)); |
| 50 | } |
| 51 | |
| 52 | void SensorReader::UpdateMediumEncoderFilterHz(int hz) { |
| 53 | medium_encoder_filter_.SetPeriodHz(::std::max(hz, 50000)); |
| 54 | } |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 55 | |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 56 | void SensorReader::set_drivetrain_left_encoder( |
| 57 | ::std::unique_ptr<frc::Encoder> encoder) { |
| 58 | fast_encoder_filter_.Add(encoder.get()); |
| 59 | drivetrain_left_encoder_ = ::std::move(encoder); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 60 | drivetrain_left_encoder_->SetMaxPeriod(0.005); |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void SensorReader::set_drivetrain_right_encoder( |
| 64 | ::std::unique_ptr<frc::Encoder> encoder) { |
| 65 | fast_encoder_filter_.Add(encoder.get()); |
| 66 | drivetrain_right_encoder_ = ::std::move(encoder); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 67 | drivetrain_right_encoder_->SetMaxPeriod(0.005); |
Sabina Davis | b6317b7 | 2019-02-01 22:53:23 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 70 | monotonic_clock::time_point SensorReader::GetPWMStartTime() { |
| 71 | int32_t status = 0; |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 72 | const auto new_fpga_time = |
| 73 | hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status)); |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 74 | |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 75 | if (!ds_->IsSysActive()) { |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 76 | return monotonic_clock::min_time; |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 77 | } |
Brian Silverman | 7be68ba | 2020-01-08 22:08:40 -0800 | [diff] [blame] | 78 | |
| 79 | const auto fpga_offset = CalculateFpgaOffset(); |
| 80 | // If we failed to sample the offset, just ignore this reading. |
| 81 | if (!fpga_offset) { |
| 82 | return monotonic_clock::min_time; |
| 83 | } |
| 84 | |
| 85 | return monotonic_clock::epoch() + (new_fpga_time + *fpga_offset); |
Sabina Davis | adc5854 | 2019-02-01 22:23:00 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Maxwell Henderson | d5ae85b | 2024-01-08 16:14:28 -0800 | [diff] [blame] | 88 | void SensorReader::SendDrivetrainPosition( |
Maxwell Henderson | f382b4b | 2024-01-14 12:31:41 -0800 | [diff] [blame] | 89 | aos::Sender<control_loops::drivetrain::PositionStatic>::StaticBuilder |
| 90 | builder, |
Maxwell Henderson | d5ae85b | 2024-01-08 16:14:28 -0800 | [diff] [blame] | 91 | std::function<double(double input)> velocity_translate, |
| 92 | std::function<double(double input)> encoder_to_meters, bool left_inverted, |
| 93 | bool right_inverted) { |
Maxwell Henderson | f382b4b | 2024-01-14 12:31:41 -0800 | [diff] [blame] | 94 | builder->set_left_encoder( |
Maxwell Henderson | d5ae85b | 2024-01-08 16:14:28 -0800 | [diff] [blame] | 95 | (left_inverted ? -1.0 : 1.0) * |
| 96 | encoder_to_meters(drivetrain_left_encoder_->GetRaw())); |
Maxwell Henderson | f382b4b | 2024-01-14 12:31:41 -0800 | [diff] [blame] | 97 | builder->set_left_speed( |
Maxwell Henderson | d5ae85b | 2024-01-08 16:14:28 -0800 | [diff] [blame] | 98 | (left_inverted ? -1.0 : 1.0) * |
| 99 | velocity_translate(drivetrain_left_encoder_->GetPeriod())); |
| 100 | |
Maxwell Henderson | f382b4b | 2024-01-14 12:31:41 -0800 | [diff] [blame] | 101 | builder->set_right_encoder( |
Maxwell Henderson | d5ae85b | 2024-01-08 16:14:28 -0800 | [diff] [blame] | 102 | (right_inverted ? -1.0 : 1.0) * |
| 103 | encoder_to_meters(drivetrain_right_encoder_->GetRaw())); |
Maxwell Henderson | f382b4b | 2024-01-14 12:31:41 -0800 | [diff] [blame] | 104 | builder->set_right_speed( |
Maxwell Henderson | d5ae85b | 2024-01-08 16:14:28 -0800 | [diff] [blame] | 105 | (right_inverted ? -1.0 : 1.0) * |
| 106 | velocity_translate(drivetrain_right_encoder_->GetPeriod())); |
| 107 | |
Maxwell Henderson | f382b4b | 2024-01-14 12:31:41 -0800 | [diff] [blame] | 108 | builder.CheckOk(builder.Send()); |
Maxwell Henderson | d5ae85b | 2024-01-08 16:14:28 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 111 | void SensorReader::DoStart() { |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 112 | Start(); |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 113 | if (dma_synchronizer_) { |
| 114 | dma_synchronizer_->Start(); |
| 115 | } |
Austin Schuh | 2c2cc2e | 2019-02-02 20:19:45 -0800 | [diff] [blame] | 116 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 117 | period_ = |
Austin Schuh | 3b010bc | 2019-02-24 17:25:37 -0800 | [diff] [blame] | 118 | pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 119 | if (pwm_trigger_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 120 | 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] | 121 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 122 | AOS_LOG(INFO, "Defaulting to open loop pwm synchronization\n"); |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 123 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 124 | |
Austin Schuh | 8ba6c5c | 2024-03-17 16:01:45 -0700 | [diff] [blame] | 125 | if (pwm_trigger_) { |
| 126 | // Now that we are configured, actually fill in the defaults. |
| 127 | timer_handler_->Schedule( |
| 128 | event_loop_->monotonic_now() + |
| 129 | (pwm_trigger_ ? chrono::milliseconds(3) : chrono::milliseconds(4)), |
| 130 | period_); |
| 131 | } else { |
| 132 | // Synchronous CAN wakes up at round multiples of the clock. Use a phased |
| 133 | // loop to calculate it. |
| 134 | aos::time::PhasedLoop phased_loop(period_, monotonic_clock::now()); |
| 135 | timer_handler_->Schedule(phased_loop.sleep_time(), period_); |
| 136 | } |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 137 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 138 | last_monotonic_now_ = monotonic_clock::now(); |
| 139 | } |
Austin Schuh | 45a549f | 2019-02-02 15:43:56 -0800 | [diff] [blame] | 140 | |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 141 | void SensorReader::Loop() { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 142 | const monotonic_clock::time_point monotonic_now = |
| 143 | event_loop_->monotonic_now(); |
| 144 | |
| 145 | { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 146 | auto builder = robot_state_sender_.MakeBuilder(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 147 | (void)builder.Send(::frc971::wpilib::PopulateRobotState(&builder, my_pid_)); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 148 | } |
| 149 | RunIteration(); |
| 150 | if (dma_synchronizer_) { |
| 151 | dma_synchronizer_->RunIteration(); |
| 152 | RunDmaIteration(); |
| 153 | } |
| 154 | |
| 155 | if (pwm_trigger_) { |
James Kuszmaul | 1bce577 | 2020-03-08 22:13:50 -0700 | [diff] [blame] | 156 | // TODO(austin): Put this in a status message. |
| 157 | VLOG(1) << "PWM wakeup delta: " |
| 158 | << (monotonic_now - last_monotonic_now_).count(); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 159 | last_monotonic_now_ = monotonic_now; |
| 160 | |
| 161 | monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime(); |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 162 | VLOG(1) << "Start time " << last_tick_timepoint << " period " |
| 163 | << period_.count(); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 164 | if (last_tick_timepoint == monotonic_clock::min_time) { |
| 165 | return; |
Sabina Davis | 6292bec | 2019-02-06 22:53:14 -0800 | [diff] [blame] | 166 | } |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 167 | |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 168 | last_tick_timepoint += |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 169 | ((monotonic_now - |
| 170 | chrono::microseconds(absl::GetFlag(FLAGS_pwm_offset)) - |
Austin Schuh | 028d81d | 2022-03-26 15:11:42 -0700 | [diff] [blame] | 171 | last_tick_timepoint) / |
| 172 | period_) * |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 173 | period_ + |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame^] | 174 | chrono::microseconds(absl::GetFlag(FLAGS_pwm_offset)); |
Austin Schuh | 028d81d | 2022-03-26 15:11:42 -0700 | [diff] [blame] | 175 | VLOG(1) << "Now " << monotonic_now << " tick " << last_tick_timepoint; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 176 | // If it's over 1/2 of a period back in time, that's wrong. Move it |
| 177 | // forwards to now. |
| 178 | if (last_tick_timepoint - monotonic_now < -period_ / 2) { |
| 179 | last_tick_timepoint += period_; |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 180 | } |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 181 | |
| 182 | // We should be sampling our sensors to kick off the control cycle 50 uS |
| 183 | // after the falling edge. This gives us a little bit of buffer for |
| 184 | // errors in waking up. The PWM cycle starts at the falling edge of the |
| 185 | // PWM pulse. |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 186 | const auto next_time = last_tick_timepoint + period_; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 187 | |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 188 | timer_handler_->Schedule(next_time, period_); |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 189 | } |
Sabina Davis | 399dbd8 | 2019-02-01 23:06:08 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 192 | } // namespace frc971::wpilib |