blob: f8bfb5a638128ecd750704f8f07ef7ce1a73dc14 [file] [log] [blame]
Sabina Davisadc58542019-02-01 22:23:00 -08001#include "frc971/wpilib/sensor_reader.h"
2
Sabina Davis399dbd82019-02-01 23:06:08 -08003#include <unistd.h>
4
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <cinttypes>
6
Sabina Davisadc58542019-02-01 22:23:00 -08007#include "aos/init.h"
Austin Schuha0c41ba2020-09-10 22:59:14 -07008#include "aos/logging/logging.h"
Austin Schuh719d6802021-11-05 23:46:20 -07009#include "aos/realtime.h"
Sabina Davisadc58542019-02-01 22:23:00 -080010#include "aos/util/compiler_memory_barrier.h"
11#include "frc971/wpilib/ahal/DigitalInput.h"
Austin Schuh3b010bc2019-02-24 17:25:37 -080012#include "frc971/wpilib/ahal/DriverStation.h"
Brian Silverman7be68ba2020-01-08 22:08:40 -080013#include "frc971/wpilib/fpga_time_conversion.h"
Austin Schuh45a549f2019-02-02 15:43:56 -080014#include "frc971/wpilib/wpilib_interface.h"
Austin Schuh3b010bc2019-02-24 17:25:37 -080015#include "hal/PWM.h"
Sabina Davisadc58542019-02-01 22:23:00 -080016
Austin Schuh028d81d2022-03-26 15:11:42 -070017DEFINE_int32(pwm_offset, 5050 / 2,
18 "Offset of reading the sensors from the start of the PWM cycle");
19
Sabina Davisadc58542019-02-01 22:23:00 -080020namespace frc971 {
21namespace wpilib {
22
Austin Schuh217a9782019-12-21 23:02:50 -080023SensorReader::SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuhdf6cbb12019-02-02 13:46:52 -080024 : event_loop_(event_loop),
Austin Schuh217a9782019-12-21 23:02:50 -080025 robot_state_sender_(event_loop_->MakeSender<::aos::RobotState>("/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070026 my_pid_(getpid()) {
Austin Schuh45a549f2019-02-02 15:43:56 -080027 // 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 Schuh3b010bc2019-02-24 17:25:37 -080031 ds_ = &::frc::DriverStation::GetInstance();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070032
33 event_loop->SetRuntimeRealtimePriority(40);
Austin Schuh719d6802021-11-05 23:46:20 -070034 // 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 Schuhbd1fe9c2019-06-29 16:35:48 -070038
39 // Fill in the no pwm trigger defaults.
milind-u61227f22021-08-29 15:58:33 -070040 timer_handler_ = event_loop_->AddTimer([this]() { Loop(); });
41 timer_handler_->set_name("SensorReader Loop");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070042
43 event_loop->set_name("SensorReader");
44 event_loop->OnRun([this]() { DoStart(); });
Austin Schuh45a549f2019-02-02 15:43:56 -080045}
46
47void SensorReader::UpdateFastEncoderFilterHz(int hz) {
48 fast_encoder_filter_.SetPeriodHz(::std::max(hz, 100000));
49}
50
51void SensorReader::UpdateMediumEncoderFilterHz(int hz) {
52 medium_encoder_filter_.SetPeriodHz(::std::max(hz, 50000));
53}
Sabina Davisadc58542019-02-01 22:23:00 -080054
Sabina Davisb6317b72019-02-01 22:53:23 -080055void 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 Schuh45a549f2019-02-02 15:43:56 -080059 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080060}
61
62void 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 Schuh45a549f2019-02-02 15:43:56 -080066 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080067}
68
Austin Schuh3b010bc2019-02-24 17:25:37 -080069monotonic_clock::time_point SensorReader::GetPWMStartTime() {
70 int32_t status = 0;
Brian Silverman7be68ba2020-01-08 22:08:40 -080071 const auto new_fpga_time =
72 hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status));
Sabina Davisadc58542019-02-01 22:23:00 -080073
Brian Silverman7be68ba2020-01-08 22:08:40 -080074 if (!ds_->IsSysActive()) {
Austin Schuh3b010bc2019-02-24 17:25:37 -080075 return monotonic_clock::min_time;
Sabina Davisadc58542019-02-01 22:23:00 -080076 }
Brian Silverman7be68ba2020-01-08 22:08:40 -080077
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 Davisadc58542019-02-01 22:23:00 -080085}
86
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070087void SensorReader::DoStart() {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080088 Start();
Sabina Davis6292bec2019-02-06 22:53:14 -080089 if (dma_synchronizer_) {
90 dma_synchronizer_->Start();
91 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080092
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070093 period_ =
Austin Schuh3b010bc2019-02-24 17:25:37 -080094 pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000);
Austin Schuh45a549f2019-02-02 15:43:56 -080095 if (pwm_trigger_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070096 AOS_LOG(INFO, "Using PWM trigger and a 5.05 ms period\n");
Austin Schuh45a549f2019-02-02 15:43:56 -080097 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -070098 AOS_LOG(INFO, "Defaulting to open loop pwm synchronization\n");
Austin Schuh45a549f2019-02-02 15:43:56 -080099 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700100
101 // Now that we are configured, actually fill in the defaults.
milind-u61227f22021-08-29 15:58:33 -0700102 timer_handler_->Setup(
103 event_loop_->monotonic_now() +
104 (pwm_trigger_ ? chrono::milliseconds(3) : chrono::milliseconds(4)),
105 period_);
Sabina Davis399dbd82019-02-01 23:06:08 -0800106
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700107 last_monotonic_now_ = monotonic_clock::now();
108}
Austin Schuh45a549f2019-02-02 15:43:56 -0800109
milind-u61227f22021-08-29 15:58:33 -0700110void SensorReader::Loop() {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700111 const monotonic_clock::time_point monotonic_now =
112 event_loop_->monotonic_now();
113
114 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700115 auto builder = robot_state_sender_.MakeBuilder();
milind1f1dca32021-07-03 13:50:07 -0700116 (void)builder.Send(::frc971::wpilib::PopulateRobotState(&builder, my_pid_));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700117 }
118 RunIteration();
119 if (dma_synchronizer_) {
120 dma_synchronizer_->RunIteration();
121 RunDmaIteration();
122 }
123
124 if (pwm_trigger_) {
James Kuszmaul1bce5772020-03-08 22:13:50 -0700125 // TODO(austin): Put this in a status message.
126 VLOG(1) << "PWM wakeup delta: "
127 << (monotonic_now - last_monotonic_now_).count();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700128 last_monotonic_now_ = monotonic_now;
129
130 monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime();
Philipp Schrader790cb542023-07-05 21:06:52 -0700131 VLOG(1) << "Start time " << last_tick_timepoint << " period "
132 << period_.count();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700133 if (last_tick_timepoint == monotonic_clock::min_time) {
134 return;
Sabina Davis6292bec2019-02-06 22:53:14 -0800135 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800136
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700137 last_tick_timepoint +=
Austin Schuh028d81d2022-03-26 15:11:42 -0700138 ((monotonic_now - chrono::microseconds(FLAGS_pwm_offset) -
139 last_tick_timepoint) /
140 period_) *
Philipp Schrader790cb542023-07-05 21:06:52 -0700141 period_ +
142 chrono::microseconds(FLAGS_pwm_offset);
Austin Schuh028d81d2022-03-26 15:11:42 -0700143 VLOG(1) << "Now " << monotonic_now << " tick " << last_tick_timepoint;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700144 // If it's over 1/2 of a period back in time, that's wrong. Move it
145 // forwards to now.
146 if (last_tick_timepoint - monotonic_now < -period_ / 2) {
147 last_tick_timepoint += period_;
Sabina Davis399dbd82019-02-01 23:06:08 -0800148 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700149
150 // We should be sampling our sensors to kick off the control cycle 50 uS
151 // after the falling edge. This gives us a little bit of buffer for
152 // errors in waking up. The PWM cycle starts at the falling edge of the
153 // PWM pulse.
Philipp Schrader790cb542023-07-05 21:06:52 -0700154 const auto next_time = last_tick_timepoint + period_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700155
milind-u61227f22021-08-29 15:58:33 -0700156 timer_handler_->Setup(next_time, period_);
Sabina Davis399dbd82019-02-01 23:06:08 -0800157 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800158}
159
Sabina Davisadc58542019-02-01 22:23:00 -0800160} // namespace wpilib
161} // namespace frc971