blob: f476e71ce1426e4e09e3e3eebe615be1a2a45f0e [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
17namespace frc971 {
18namespace wpilib {
19
Austin Schuh217a9782019-12-21 23:02:50 -080020SensorReader::SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuhdf6cbb12019-02-02 13:46:52 -080021 : event_loop_(event_loop),
Austin Schuh217a9782019-12-21 23:02:50 -080022 robot_state_sender_(event_loop_->MakeSender<::aos::RobotState>("/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070023 my_pid_(getpid()) {
Austin Schuh45a549f2019-02-02 15:43:56 -080024 // 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 Schuh3b010bc2019-02-24 17:25:37 -080028 ds_ = &::frc::DriverStation::GetInstance();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070029
30 event_loop->SetRuntimeRealtimePriority(40);
Austin Schuh719d6802021-11-05 23:46:20 -070031 // The timer interrupt fires on CPU1. Since nothing else is pinned, it will
32 // be cheapest to pin this there so it transitions directly and doesn't
33 // need to ever migrate.
34 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1}));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070035
36 // Fill in the no pwm trigger defaults.
milind-u61227f22021-08-29 15:58:33 -070037 timer_handler_ = event_loop_->AddTimer([this]() { Loop(); });
38 timer_handler_->set_name("SensorReader Loop");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070039
40 event_loop->set_name("SensorReader");
41 event_loop->OnRun([this]() { DoStart(); });
Austin Schuh45a549f2019-02-02 15:43:56 -080042}
43
44void SensorReader::UpdateFastEncoderFilterHz(int hz) {
45 fast_encoder_filter_.SetPeriodHz(::std::max(hz, 100000));
46}
47
48void SensorReader::UpdateMediumEncoderFilterHz(int hz) {
49 medium_encoder_filter_.SetPeriodHz(::std::max(hz, 50000));
50}
Sabina Davisadc58542019-02-01 22:23:00 -080051
Sabina Davisb6317b72019-02-01 22:53:23 -080052void SensorReader::set_drivetrain_left_encoder(
53 ::std::unique_ptr<frc::Encoder> encoder) {
54 fast_encoder_filter_.Add(encoder.get());
55 drivetrain_left_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080056 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080057}
58
59void SensorReader::set_drivetrain_right_encoder(
60 ::std::unique_ptr<frc::Encoder> encoder) {
61 fast_encoder_filter_.Add(encoder.get());
62 drivetrain_right_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080063 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080064}
65
Austin Schuh3b010bc2019-02-24 17:25:37 -080066monotonic_clock::time_point SensorReader::GetPWMStartTime() {
67 int32_t status = 0;
Brian Silverman7be68ba2020-01-08 22:08:40 -080068 const auto new_fpga_time =
69 hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status));
Sabina Davisadc58542019-02-01 22:23:00 -080070
Brian Silverman7be68ba2020-01-08 22:08:40 -080071 if (!ds_->IsSysActive()) {
Austin Schuh3b010bc2019-02-24 17:25:37 -080072 return monotonic_clock::min_time;
Sabina Davisadc58542019-02-01 22:23:00 -080073 }
Brian Silverman7be68ba2020-01-08 22:08:40 -080074
75 const auto fpga_offset = CalculateFpgaOffset();
76 // If we failed to sample the offset, just ignore this reading.
77 if (!fpga_offset) {
78 return monotonic_clock::min_time;
79 }
80
81 return monotonic_clock::epoch() + (new_fpga_time + *fpga_offset);
Sabina Davisadc58542019-02-01 22:23:00 -080082}
83
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070084void SensorReader::DoStart() {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080085 Start();
Sabina Davis6292bec2019-02-06 22:53:14 -080086 if (dma_synchronizer_) {
87 dma_synchronizer_->Start();
88 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080089
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070090 period_ =
Austin Schuh3b010bc2019-02-24 17:25:37 -080091 pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000);
Austin Schuh45a549f2019-02-02 15:43:56 -080092 if (pwm_trigger_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070093 AOS_LOG(INFO, "Using PWM trigger and a 5.05 ms period\n");
Austin Schuh45a549f2019-02-02 15:43:56 -080094 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -070095 AOS_LOG(INFO, "Defaulting to open loop pwm synchronization\n");
Austin Schuh45a549f2019-02-02 15:43:56 -080096 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070097
98 // Now that we are configured, actually fill in the defaults.
milind-u61227f22021-08-29 15:58:33 -070099 timer_handler_->Setup(
100 event_loop_->monotonic_now() +
101 (pwm_trigger_ ? chrono::milliseconds(3) : chrono::milliseconds(4)),
102 period_);
Sabina Davis399dbd82019-02-01 23:06:08 -0800103
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700104 last_monotonic_now_ = monotonic_clock::now();
105}
Austin Schuh45a549f2019-02-02 15:43:56 -0800106
milind-u61227f22021-08-29 15:58:33 -0700107void SensorReader::Loop() {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700108 const monotonic_clock::time_point monotonic_now =
109 event_loop_->monotonic_now();
110
111 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700112 auto builder = robot_state_sender_.MakeBuilder();
113 builder.Send(::frc971::wpilib::PopulateRobotState(&builder, my_pid_));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700114 }
115 RunIteration();
116 if (dma_synchronizer_) {
117 dma_synchronizer_->RunIteration();
118 RunDmaIteration();
119 }
120
121 if (pwm_trigger_) {
James Kuszmaul1bce5772020-03-08 22:13:50 -0700122 // TODO(austin): Put this in a status message.
123 VLOG(1) << "PWM wakeup delta: "
124 << (monotonic_now - last_monotonic_now_).count();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700125 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 Davis6292bec2019-02-06 22:53:14 -0800130 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800131
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700132 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 Davis399dbd82019-02-01 23:06:08 -0800138 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700139
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.
milind-u61227f22021-08-29 15:58:33 -0700144 const auto next_time =
145 last_tick_timepoint + period_ + chrono::microseconds(50);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700146
milind-u61227f22021-08-29 15:58:33 -0700147 timer_handler_->Setup(next_time, period_);
Sabina Davis399dbd82019-02-01 23:06:08 -0800148 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800149}
150
Sabina Davisadc58542019-02-01 22:23:00 -0800151} // namespace wpilib
152} // namespace frc971