blob: 70d5c3531f4a01f6752488f74b3c28bb9bf1d962 [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
Maxwell Hendersond5ae85b2024-01-08 16:14:28 -080087void 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 Schuhbd1fe9c2019-06-29 16:35:48 -0700113void SensorReader::DoStart() {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800114 Start();
Sabina Davis6292bec2019-02-06 22:53:14 -0800115 if (dma_synchronizer_) {
116 dma_synchronizer_->Start();
117 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800118
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700119 period_ =
Austin Schuh3b010bc2019-02-24 17:25:37 -0800120 pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000);
Austin Schuh45a549f2019-02-02 15:43:56 -0800121 if (pwm_trigger_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700122 AOS_LOG(INFO, "Using PWM trigger and a 5.05 ms period\n");
Austin Schuh45a549f2019-02-02 15:43:56 -0800123 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700124 AOS_LOG(INFO, "Defaulting to open loop pwm synchronization\n");
Austin Schuh45a549f2019-02-02 15:43:56 -0800125 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700126
127 // Now that we are configured, actually fill in the defaults.
Philipp Schradera6712522023-07-05 20:25:11 -0700128 timer_handler_->Schedule(
milind-u61227f22021-08-29 15:58:33 -0700129 event_loop_->monotonic_now() +
130 (pwm_trigger_ ? chrono::milliseconds(3) : chrono::milliseconds(4)),
131 period_);
Sabina Davis399dbd82019-02-01 23:06:08 -0800132
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700133 last_monotonic_now_ = monotonic_clock::now();
134}
Austin Schuh45a549f2019-02-02 15:43:56 -0800135
milind-u61227f22021-08-29 15:58:33 -0700136void SensorReader::Loop() {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700137 const monotonic_clock::time_point monotonic_now =
138 event_loop_->monotonic_now();
139
140 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700141 auto builder = robot_state_sender_.MakeBuilder();
milind1f1dca32021-07-03 13:50:07 -0700142 (void)builder.Send(::frc971::wpilib::PopulateRobotState(&builder, my_pid_));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700143 }
144 RunIteration();
145 if (dma_synchronizer_) {
146 dma_synchronizer_->RunIteration();
147 RunDmaIteration();
148 }
149
150 if (pwm_trigger_) {
James Kuszmaul1bce5772020-03-08 22:13:50 -0700151 // TODO(austin): Put this in a status message.
152 VLOG(1) << "PWM wakeup delta: "
153 << (monotonic_now - last_monotonic_now_).count();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700154 last_monotonic_now_ = monotonic_now;
155
156 monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime();
Philipp Schrader790cb542023-07-05 21:06:52 -0700157 VLOG(1) << "Start time " << last_tick_timepoint << " period "
158 << period_.count();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700159 if (last_tick_timepoint == monotonic_clock::min_time) {
160 return;
Sabina Davis6292bec2019-02-06 22:53:14 -0800161 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800162
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700163 last_tick_timepoint +=
Austin Schuh028d81d2022-03-26 15:11:42 -0700164 ((monotonic_now - chrono::microseconds(FLAGS_pwm_offset) -
165 last_tick_timepoint) /
166 period_) *
Philipp Schrader790cb542023-07-05 21:06:52 -0700167 period_ +
168 chrono::microseconds(FLAGS_pwm_offset);
Austin Schuh028d81d2022-03-26 15:11:42 -0700169 VLOG(1) << "Now " << monotonic_now << " tick " << last_tick_timepoint;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700170 // 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 Davis399dbd82019-02-01 23:06:08 -0800174 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700175
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 Schrader790cb542023-07-05 21:06:52 -0700180 const auto next_time = last_tick_timepoint + period_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700181
Philipp Schradera6712522023-07-05 20:25:11 -0700182 timer_handler_->Schedule(next_time, period_);
Sabina Davis399dbd82019-02-01 23:06:08 -0800183 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800184}
185
Sabina Davisadc58542019-02-01 22:23:00 -0800186} // namespace wpilib
187} // namespace frc971