blob: 63d3992d848868997de365d852de8319c843b261 [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"
Austin Schuhdf6cbb12019-02-02 13:46:52 -08007#include "aos/logging/queue_logging.h"
Sabina Davisadc58542019-02-01 22:23:00 -08008#include "aos/util/compiler_memory_barrier.h"
Sabina Davis399dbd82019-02-01 23:06:08 -08009#include "aos/util/phased_loop.h"
Sabina Davisadc58542019-02-01 22:23:00 -080010#include "frc971/wpilib/ahal/DigitalInput.h"
Austin Schuh3b010bc2019-02-24 17:25:37 -080011#include "frc971/wpilib/ahal/DriverStation.h"
Sabina Davisadc58542019-02-01 22:23:00 -080012#include "frc971/wpilib/ahal/Utility.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 Schuhdf6cbb12019-02-02 13:46:52 -080019SensorReader::SensorReader(::aos::EventLoop *event_loop)
20 : event_loop_(event_loop),
21 robot_state_sender_(
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070022 event_loop_->MakeSender<::aos::RobotState>(".aos.robot_state")),
23 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);
31
32 // Fill in the no pwm trigger defaults.
33 phased_loop_handler_ =
34 event_loop_->AddPhasedLoop([this](int iterations) { Loop(iterations); },
35 period_, chrono::milliseconds(4));
36
37 event_loop->set_name("SensorReader");
38 event_loop->OnRun([this]() { DoStart(); });
Austin Schuh45a549f2019-02-02 15:43:56 -080039}
40
41void SensorReader::UpdateFastEncoderFilterHz(int hz) {
42 fast_encoder_filter_.SetPeriodHz(::std::max(hz, 100000));
43}
44
45void SensorReader::UpdateMediumEncoderFilterHz(int hz) {
46 medium_encoder_filter_.SetPeriodHz(::std::max(hz, 50000));
47}
Sabina Davisadc58542019-02-01 22:23:00 -080048
Sabina Davisb6317b72019-02-01 22:53:23 -080049void SensorReader::set_drivetrain_left_encoder(
50 ::std::unique_ptr<frc::Encoder> encoder) {
51 fast_encoder_filter_.Add(encoder.get());
52 drivetrain_left_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080053 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080054}
55
56void SensorReader::set_drivetrain_right_encoder(
57 ::std::unique_ptr<frc::Encoder> encoder) {
58 fast_encoder_filter_.Add(encoder.get());
59 drivetrain_right_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080060 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080061}
62
Austin Schuh3b010bc2019-02-24 17:25:37 -080063monotonic_clock::time_point SensorReader::GetPWMStartTime() {
64 int32_t status = 0;
65 const hal::fpga_clock::time_point new_fpga_time = hal::fpga_clock::time_point(
66 hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status)));
Sabina Davisadc58542019-02-01 22:23:00 -080067
Austin Schuh3b010bc2019-02-24 17:25:37 -080068 aos_compiler_memory_barrier();
69 const hal::fpga_clock::time_point fpga_time_before = hal::fpga_clock::now();
70 aos_compiler_memory_barrier();
71 const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
72 aos_compiler_memory_barrier();
73 const hal::fpga_clock::time_point fpga_time_after = hal::fpga_clock::now();
74 aos_compiler_memory_barrier();
Sabina Davisadc58542019-02-01 22:23:00 -080075
Austin Schuh3b010bc2019-02-24 17:25:37 -080076 const chrono::nanoseconds fpga_sample_length =
77 fpga_time_after - fpga_time_before;
78 const chrono::nanoseconds fpga_offset =
79 hal::fpga_clock::time_point((fpga_time_after.time_since_epoch() +
80 fpga_time_before.time_since_epoch()) /
81 2) -
82 new_fpga_time;
Sabina Davisadc58542019-02-01 22:23:00 -080083
Austin Schuh3b010bc2019-02-24 17:25:37 -080084 // Make sure that there wasn't a context switch while we were sampling the
85 // clocks. If there was, we are better off rejecting the sample than using
86 // it.
87 if (ds_->IsSysActive() && fpga_sample_length <= chrono::microseconds(20) &&
88 fpga_sample_length >= chrono::microseconds(0)) {
89 // Compute when the edge was.
90 return monotonic_now - fpga_offset;
91 } else {
92 return monotonic_clock::min_time;
Sabina Davisadc58542019-02-01 22:23:00 -080093 }
Sabina Davisadc58542019-02-01 22:23:00 -080094}
95
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070096void SensorReader::DoStart() {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080097 Start();
Sabina Davis6292bec2019-02-06 22:53:14 -080098 if (dma_synchronizer_) {
99 dma_synchronizer_->Start();
100 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800101
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700102 period_ =
Austin Schuh3b010bc2019-02-24 17:25:37 -0800103 pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000);
Austin Schuh45a549f2019-02-02 15:43:56 -0800104 if (pwm_trigger_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700105 AOS_LOG(INFO, "Using PWM trigger and a 5.05 ms period\n");
Austin Schuh45a549f2019-02-02 15:43:56 -0800106 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700107 AOS_LOG(INFO, "Defaulting to open loop pwm synchronization\n");
Austin Schuh45a549f2019-02-02 15:43:56 -0800108 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700109
110 // Now that we are configured, actually fill in the defaults.
111 phased_loop_handler_->set_interval_and_offset(
112 period_,
Austin Schuh45a549f2019-02-02 15:43:56 -0800113 pwm_trigger_ ? ::std::chrono::milliseconds(3) : chrono::milliseconds(4));
Sabina Davis399dbd82019-02-01 23:06:08 -0800114
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700115 last_monotonic_now_ = monotonic_clock::now();
116}
Austin Schuh45a549f2019-02-02 15:43:56 -0800117
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700118void SensorReader::Loop(const int iterations) {
119 if (iterations != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700120 AOS_LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700121 }
122
123 const monotonic_clock::time_point monotonic_now =
124 event_loop_->monotonic_now();
125
126 {
127 auto new_state = robot_state_sender_.MakeMessage();
128 ::frc971::wpilib::PopulateRobotState(new_state.get(), my_pid_);
Austin Schuhf257f3c2019-10-27 21:00:43 -0700129 AOS_LOG_STRUCT(DEBUG, "robot_state", *new_state);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700130 new_state.Send();
131 }
132 RunIteration();
133 if (dma_synchronizer_) {
134 dma_synchronizer_->RunIteration();
135 RunDmaIteration();
136 }
137
138 if (pwm_trigger_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700139 AOS_LOG(DEBUG, "PWM wakeup delta: %lld\n",
140 (monotonic_now - last_monotonic_now_).count());
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700141 last_monotonic_now_ = monotonic_now;
142
143 monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime();
144 if (last_tick_timepoint == monotonic_clock::min_time) {
145 return;
Sabina Davis6292bec2019-02-06 22:53:14 -0800146 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800147
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700148 last_tick_timepoint +=
149 ((monotonic_now - last_tick_timepoint) / period_) * period_;
150 // If it's over 1/2 of a period back in time, that's wrong. Move it
151 // forwards to now.
152 if (last_tick_timepoint - monotonic_now < -period_ / 2) {
153 last_tick_timepoint += period_;
Sabina Davis399dbd82019-02-01 23:06:08 -0800154 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700155
156 // We should be sampling our sensors to kick off the control cycle 50 uS
157 // after the falling edge. This gives us a little bit of buffer for
158 // errors in waking up. The PWM cycle starts at the falling edge of the
159 // PWM pulse.
160 chrono::nanoseconds new_offset =
161 ::aos::time::PhasedLoop::OffsetFromIntervalAndTime(
162 period_, last_tick_timepoint + chrono::microseconds(50));
163
164 phased_loop_handler_->set_interval_and_offset(period_, new_offset);
Sabina Davis399dbd82019-02-01 23:06:08 -0800165 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800166}
167
Sabina Davisadc58542019-02-01 22:23:00 -0800168} // namespace wpilib
169} // namespace frc971