blob: 8610105402eec05f38a0b4d4cfdbf65ce678fb6f [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 <inttypes.h>
4#include <unistd.h>
5
Sabina Davisadc58542019-02-01 22:23:00 -08006#include "aos/init.h"
7#include "aos/util/compiler_memory_barrier.h"
Sabina Davis399dbd82019-02-01 23:06:08 -08008#include "aos/util/phased_loop.h"
Sabina Davisadc58542019-02-01 22:23:00 -08009#include "frc971/wpilib/ahal/DigitalInput.h"
Austin Schuh3b010bc2019-02-24 17:25:37 -080010#include "frc971/wpilib/ahal/DriverStation.h"
Sabina Davisadc58542019-02-01 22:23:00 -080011#include "frc971/wpilib/ahal/Utility.h"
Austin Schuh45a549f2019-02-02 15:43:56 -080012#include "frc971/wpilib/wpilib_interface.h"
Austin Schuh3b010bc2019-02-24 17:25:37 -080013#include "hal/PWM.h"
Sabina Davisadc58542019-02-01 22:23:00 -080014
15namespace frc971 {
16namespace wpilib {
17
Austin Schuhdf6cbb12019-02-02 13:46:52 -080018SensorReader::SensorReader(::aos::EventLoop *event_loop)
19 : event_loop_(event_loop),
20 robot_state_sender_(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070021 event_loop_->MakeSender<::aos::RobotState>(".aos.robot_state")),
22 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.
32 phased_loop_handler_ =
33 event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); },
34 period_, chrono::milliseconds(4));
35
36 event_loop->set_name("SensorReader");
37 event_loop->OnRun([this]() { DoStart(); });
Austin Schuh45a549f2019-02-02 15:43:56 -080038}
39
40void SensorReader::UpdateFastEncoderFilterHz(int hz) {
41 fast_encoder_filter_.SetPeriodHz(::std::max(hz, 100000));
42}
43
44void SensorReader::UpdateMediumEncoderFilterHz(int hz) {
45 medium_encoder_filter_.SetPeriodHz(::std::max(hz, 50000));
46}
Sabina Davisadc58542019-02-01 22:23:00 -080047
Sabina Davisb6317b72019-02-01 22:53:23 -080048void SensorReader::set_drivetrain_left_encoder(
49 ::std::unique_ptr<frc::Encoder> encoder) {
50 fast_encoder_filter_.Add(encoder.get());
51 drivetrain_left_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080052 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080053}
54
55void SensorReader::set_drivetrain_right_encoder(
56 ::std::unique_ptr<frc::Encoder> encoder) {
57 fast_encoder_filter_.Add(encoder.get());
58 drivetrain_right_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080059 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080060}
61
Austin Schuh3b010bc2019-02-24 17:25:37 -080062monotonic_clock::time_point SensorReader::GetPWMStartTime() {
63 int32_t status = 0;
64 const hal::fpga_clock::time_point new_fpga_time = hal::fpga_clock::time_point(
65 hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status)));
Sabina Davisadc58542019-02-01 22:23:00 -080066
Austin Schuh3b010bc2019-02-24 17:25:37 -080067 aos_compiler_memory_barrier();
68 const hal::fpga_clock::time_point fpga_time_before = hal::fpga_clock::now();
69 aos_compiler_memory_barrier();
70 const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
71 aos_compiler_memory_barrier();
72 const hal::fpga_clock::time_point fpga_time_after = hal::fpga_clock::now();
73 aos_compiler_memory_barrier();
Sabina Davisadc58542019-02-01 22:23:00 -080074
Austin Schuh3b010bc2019-02-24 17:25:37 -080075 const chrono::nanoseconds fpga_sample_length =
76 fpga_time_after - fpga_time_before;
77 const chrono::nanoseconds fpga_offset =
78 hal::fpga_clock::time_point((fpga_time_after.time_since_epoch() +
79 fpga_time_before.time_since_epoch()) /
80 2) -
81 new_fpga_time;
Sabina Davisadc58542019-02-01 22:23:00 -080082
Austin Schuh3b010bc2019-02-24 17:25:37 -080083 // Make sure that there wasn't a context switch while we were sampling the
84 // clocks. If there was, we are better off rejecting the sample than using
85 // it.
86 if (ds_->IsSysActive() && fpga_sample_length <= chrono::microseconds(20) &&
87 fpga_sample_length >= chrono::microseconds(0)) {
88 // Compute when the edge was.
89 return monotonic_now - fpga_offset;
90 } else {
91 return monotonic_clock::min_time;
Sabina Davisadc58542019-02-01 22:23:00 -080092 }
Sabina Davisadc58542019-02-01 22:23:00 -080093}
94
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070095void SensorReader::DoStart() {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080096 Start();
Sabina Davis6292bec2019-02-06 22:53:14 -080097 if (dma_synchronizer_) {
98 dma_synchronizer_->Start();
99 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800100
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700101 period_ =
Austin Schuh3b010bc2019-02-24 17:25:37 -0800102 pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000);
Austin Schuh45a549f2019-02-02 15:43:56 -0800103 if (pwm_trigger_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700104 AOS_LOG(INFO, "Using PWM trigger and a 5.05 ms period\n");
Austin Schuh45a549f2019-02-02 15:43:56 -0800105 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700106 AOS_LOG(INFO, "Defaulting to open loop pwm synchronization\n");
Austin Schuh45a549f2019-02-02 15:43:56 -0800107 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700108
109 // Now that we are configured, actually fill in the defaults.
110 phased_loop_handler_->set_interval_and_offset(
111 period_,
Austin Schuh45a549f2019-02-02 15:43:56 -0800112 pwm_trigger_ ? ::std::chrono::milliseconds(3) : chrono::milliseconds(4));
Sabina Davis399dbd82019-02-01 23:06:08 -0800113
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700114 last_monotonic_now_ = monotonic_clock::now();
115}
Austin Schuh45a549f2019-02-02 15:43:56 -0800116
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700117void SensorReader::Loop(const int iterations) {
118 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700119 AOS_LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700120 }
121
122 const monotonic_clock::time_point monotonic_now =
123 event_loop_->monotonic_now();
124
125 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700126 auto builder = robot_state_sender_.MakeBuilder();
127 builder.Send(::frc971::wpilib::PopulateRobotState(&builder, my_pid_));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700128 }
129 RunIteration();
130 if (dma_synchronizer_) {
131 dma_synchronizer_->RunIteration();
132 RunDmaIteration();
133 }
134
135 if (pwm_trigger_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700136 AOS_LOG(DEBUG, "PWM wakeup delta: %lld\n",
137 (monotonic_now - last_monotonic_now_).count());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700138 last_monotonic_now_ = monotonic_now;
139
140 monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime();
141 if (last_tick_timepoint == monotonic_clock::min_time) {
142 return;
Sabina Davis6292bec2019-02-06 22:53:14 -0800143 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800144
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700145 last_tick_timepoint +=
146 ((monotonic_now - last_tick_timepoint) / period_) * period_;
147 // If it's over 1/2 of a period back in time, that's wrong. Move it
148 // forwards to now.
149 if (last_tick_timepoint - monotonic_now < -period_ / 2) {
150 last_tick_timepoint += period_;
Sabina Davis399dbd82019-02-01 23:06:08 -0800151 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700152
153 // We should be sampling our sensors to kick off the control cycle 50 uS
154 // after the falling edge. This gives us a little bit of buffer for
155 // errors in waking up. The PWM cycle starts at the falling edge of the
156 // PWM pulse.
157 chrono::nanoseconds new_offset =
158 ::aos::time::PhasedLoop::OffsetFromIntervalAndTime(
159 period_, last_tick_timepoint + chrono::microseconds(50));
160
161 phased_loop_handler_->set_interval_and_offset(period_, new_offset);
Sabina Davis399dbd82019-02-01 23:06:08 -0800162 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800163}
164
Sabina Davisadc58542019-02-01 22:23:00 -0800165} // namespace wpilib
166} // namespace frc971