blob: 2a40128615ca58e4e4b6287b39ce935ba99ed66d [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"
Sabina Davisadc58542019-02-01 22:23:00 -08009#include "aos/util/compiler_memory_barrier.h"
10#include "frc971/wpilib/ahal/DigitalInput.h"
Austin Schuh3b010bc2019-02-24 17:25:37 -080011#include "frc971/wpilib/ahal/DriverStation.h"
Brian Silverman7be68ba2020-01-08 22:08:40 -080012#include "frc971/wpilib/fpga_time_conversion.h"
Austin Schuh45a549f2019-02-02 15:43:56 -080013#include "frc971/wpilib/wpilib_interface.h"
Austin Schuh3b010bc2019-02-24 17:25:37 -080014#include "hal/PWM.h"
Sabina Davisadc58542019-02-01 22:23:00 -080015
16namespace frc971 {
17namespace wpilib {
18
Austin Schuh217a9782019-12-21 23:02:50 -080019SensorReader::SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuhdf6cbb12019-02-02 13:46:52 -080020 : event_loop_(event_loop),
Austin Schuh217a9782019-12-21 23:02:50 -080021 robot_state_sender_(event_loop_->MakeSender<::aos::RobotState>("/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070022 my_pid_(getpid()) {
Austin Schuh45a549f2019-02-02 15:43:56 -080023 // Set some defaults. We don't tend to exceed these, so old robots should
24 // just work with them.
25 UpdateFastEncoderFilterHz(500000);
26 UpdateMediumEncoderFilterHz(100000);
Austin Schuh3b010bc2019-02-24 17:25:37 -080027 ds_ = &::frc::DriverStation::GetInstance();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070028
29 event_loop->SetRuntimeRealtimePriority(40);
30
31 // Fill in the no pwm trigger defaults.
milind-u61227f22021-08-29 15:58:33 -070032 timer_handler_ = event_loop_->AddTimer([this]() { Loop(); });
33 timer_handler_->set_name("SensorReader Loop");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070034
35 event_loop->set_name("SensorReader");
36 event_loop->OnRun([this]() { DoStart(); });
Austin Schuh45a549f2019-02-02 15:43:56 -080037}
38
39void SensorReader::UpdateFastEncoderFilterHz(int hz) {
40 fast_encoder_filter_.SetPeriodHz(::std::max(hz, 100000));
41}
42
43void SensorReader::UpdateMediumEncoderFilterHz(int hz) {
44 medium_encoder_filter_.SetPeriodHz(::std::max(hz, 50000));
45}
Sabina Davisadc58542019-02-01 22:23:00 -080046
Sabina Davisb6317b72019-02-01 22:53:23 -080047void SensorReader::set_drivetrain_left_encoder(
48 ::std::unique_ptr<frc::Encoder> encoder) {
49 fast_encoder_filter_.Add(encoder.get());
50 drivetrain_left_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080051 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080052}
53
54void SensorReader::set_drivetrain_right_encoder(
55 ::std::unique_ptr<frc::Encoder> encoder) {
56 fast_encoder_filter_.Add(encoder.get());
57 drivetrain_right_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080058 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080059}
60
Austin Schuh3b010bc2019-02-24 17:25:37 -080061monotonic_clock::time_point SensorReader::GetPWMStartTime() {
62 int32_t status = 0;
Brian Silverman7be68ba2020-01-08 22:08:40 -080063 const auto new_fpga_time =
64 hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status));
Sabina Davisadc58542019-02-01 22:23:00 -080065
Brian Silverman7be68ba2020-01-08 22:08:40 -080066 if (!ds_->IsSysActive()) {
Austin Schuh3b010bc2019-02-24 17:25:37 -080067 return monotonic_clock::min_time;
Sabina Davisadc58542019-02-01 22:23:00 -080068 }
Brian Silverman7be68ba2020-01-08 22:08:40 -080069
70 const auto fpga_offset = CalculateFpgaOffset();
71 // If we failed to sample the offset, just ignore this reading.
72 if (!fpga_offset) {
73 return monotonic_clock::min_time;
74 }
75
76 return monotonic_clock::epoch() + (new_fpga_time + *fpga_offset);
Sabina Davisadc58542019-02-01 22:23:00 -080077}
78
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070079void SensorReader::DoStart() {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080080 Start();
Sabina Davis6292bec2019-02-06 22:53:14 -080081 if (dma_synchronizer_) {
82 dma_synchronizer_->Start();
83 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080084
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070085 period_ =
Austin Schuh3b010bc2019-02-24 17:25:37 -080086 pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000);
Austin Schuh45a549f2019-02-02 15:43:56 -080087 if (pwm_trigger_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070088 AOS_LOG(INFO, "Using PWM trigger and a 5.05 ms period\n");
Austin Schuh45a549f2019-02-02 15:43:56 -080089 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -070090 AOS_LOG(INFO, "Defaulting to open loop pwm synchronization\n");
Austin Schuh45a549f2019-02-02 15:43:56 -080091 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070092
93 // Now that we are configured, actually fill in the defaults.
milind-u61227f22021-08-29 15:58:33 -070094 timer_handler_->Setup(
95 event_loop_->monotonic_now() +
96 (pwm_trigger_ ? chrono::milliseconds(3) : chrono::milliseconds(4)),
97 period_);
Sabina Davis399dbd82019-02-01 23:06:08 -080098
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070099 last_monotonic_now_ = monotonic_clock::now();
100}
Austin Schuh45a549f2019-02-02 15:43:56 -0800101
milind-u61227f22021-08-29 15:58:33 -0700102void SensorReader::Loop() {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700103 const monotonic_clock::time_point monotonic_now =
104 event_loop_->monotonic_now();
105
106 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700107 auto builder = robot_state_sender_.MakeBuilder();
108 builder.Send(::frc971::wpilib::PopulateRobotState(&builder, my_pid_));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700109 }
110 RunIteration();
111 if (dma_synchronizer_) {
112 dma_synchronizer_->RunIteration();
113 RunDmaIteration();
114 }
115
116 if (pwm_trigger_) {
James Kuszmaul1bce5772020-03-08 22:13:50 -0700117 // TODO(austin): Put this in a status message.
118 VLOG(1) << "PWM wakeup delta: "
119 << (monotonic_now - last_monotonic_now_).count();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700120 last_monotonic_now_ = monotonic_now;
121
122 monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime();
123 if (last_tick_timepoint == monotonic_clock::min_time) {
124 return;
Sabina Davis6292bec2019-02-06 22:53:14 -0800125 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800126
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700127 last_tick_timepoint +=
128 ((monotonic_now - last_tick_timepoint) / period_) * period_;
129 // If it's over 1/2 of a period back in time, that's wrong. Move it
130 // forwards to now.
131 if (last_tick_timepoint - monotonic_now < -period_ / 2) {
132 last_tick_timepoint += period_;
Sabina Davis399dbd82019-02-01 23:06:08 -0800133 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700134
135 // We should be sampling our sensors to kick off the control cycle 50 uS
136 // after the falling edge. This gives us a little bit of buffer for
137 // errors in waking up. The PWM cycle starts at the falling edge of the
138 // PWM pulse.
milind-u61227f22021-08-29 15:58:33 -0700139 const auto next_time =
140 last_tick_timepoint + period_ + chrono::microseconds(50);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700141
milind-u61227f22021-08-29 15:58:33 -0700142 timer_handler_->Setup(next_time, period_);
Sabina Davis399dbd82019-02-01 23:06:08 -0800143 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800144}
145
Sabina Davisadc58542019-02-01 22:23:00 -0800146} // namespace wpilib
147} // namespace frc971