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