blob: d25db27c312ec580047c811e553c1c4dbaae4cad [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 Schuh45a549f2019-02-02 15:43:56 -080018SensorReader::SensorReader() {
19 // Set some defaults. We don't tend to exceed these, so old robots should
20 // just work with them.
21 UpdateFastEncoderFilterHz(500000);
22 UpdateMediumEncoderFilterHz(100000);
Austin Schuh3b010bc2019-02-24 17:25:37 -080023 ds_ = &::frc::DriverStation::GetInstance();
Austin Schuh45a549f2019-02-02 15:43:56 -080024}
25
26void SensorReader::UpdateFastEncoderFilterHz(int hz) {
27 fast_encoder_filter_.SetPeriodHz(::std::max(hz, 100000));
28}
29
30void SensorReader::UpdateMediumEncoderFilterHz(int hz) {
31 medium_encoder_filter_.SetPeriodHz(::std::max(hz, 50000));
32}
Sabina Davisadc58542019-02-01 22:23:00 -080033
Sabina Davisb6317b72019-02-01 22:53:23 -080034void SensorReader::set_drivetrain_left_encoder(
35 ::std::unique_ptr<frc::Encoder> encoder) {
36 fast_encoder_filter_.Add(encoder.get());
37 drivetrain_left_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080038 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080039}
40
41void SensorReader::set_drivetrain_right_encoder(
42 ::std::unique_ptr<frc::Encoder> encoder) {
43 fast_encoder_filter_.Add(encoder.get());
44 drivetrain_right_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080045 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080046}
47
Austin Schuh3b010bc2019-02-24 17:25:37 -080048monotonic_clock::time_point SensorReader::GetPWMStartTime() {
49 int32_t status = 0;
50 const hal::fpga_clock::time_point new_fpga_time = hal::fpga_clock::time_point(
51 hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status)));
Sabina Davisadc58542019-02-01 22:23:00 -080052
Austin Schuh3b010bc2019-02-24 17:25:37 -080053 aos_compiler_memory_barrier();
54 const hal::fpga_clock::time_point fpga_time_before = hal::fpga_clock::now();
55 aos_compiler_memory_barrier();
56 const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
57 aos_compiler_memory_barrier();
58 const hal::fpga_clock::time_point fpga_time_after = hal::fpga_clock::now();
59 aos_compiler_memory_barrier();
Sabina Davisadc58542019-02-01 22:23:00 -080060
Austin Schuh3b010bc2019-02-24 17:25:37 -080061 const chrono::nanoseconds fpga_sample_length =
62 fpga_time_after - fpga_time_before;
63 const chrono::nanoseconds fpga_offset =
64 hal::fpga_clock::time_point((fpga_time_after.time_since_epoch() +
65 fpga_time_before.time_since_epoch()) /
66 2) -
67 new_fpga_time;
Sabina Davisadc58542019-02-01 22:23:00 -080068
Austin Schuh3b010bc2019-02-24 17:25:37 -080069 // Make sure that there wasn't a context switch while we were sampling the
70 // clocks. If there was, we are better off rejecting the sample than using
71 // it.
72 if (ds_->IsSysActive() && fpga_sample_length <= chrono::microseconds(20) &&
73 fpga_sample_length >= chrono::microseconds(0)) {
74 // Compute when the edge was.
75 return monotonic_now - fpga_offset;
76 } else {
77 return monotonic_clock::min_time;
Sabina Davisadc58542019-02-01 22:23:00 -080078 }
Sabina Davisadc58542019-02-01 22:23:00 -080079}
80
Sabina Davis399dbd82019-02-01 23:06:08 -080081void SensorReader::operator()() {
82 ::aos::SetCurrentThreadName("SensorReader");
83
Austin Schuh45a549f2019-02-02 15:43:56 -080084 int32_t my_pid = getpid();
Sabina Davis399dbd82019-02-01 23:06:08 -080085
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080086 Start();
Sabina Davis6292bec2019-02-06 22:53:14 -080087 if (dma_synchronizer_) {
88 dma_synchronizer_->Start();
89 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -080090
Austin Schuh3b010bc2019-02-24 17:25:37 -080091 const chrono::microseconds period =
92 pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000);
Austin Schuh45a549f2019-02-02 15:43:56 -080093 if (pwm_trigger_) {
Austin Schuh45a549f2019-02-02 15:43:56 -080094 LOG(INFO, "Using PWM trigger and a 5.05 ms period\n");
95 } else {
96 LOG(INFO, "Defaulting to open loop pwm synchronization\n");
Austin Schuh45a549f2019-02-02 15:43:56 -080097 }
98 ::aos::time::PhasedLoop phased_loop(
Austin Schuh3b010bc2019-02-24 17:25:37 -080099 period,
Austin Schuh45a549f2019-02-02 15:43:56 -0800100 pwm_trigger_ ? ::std::chrono::milliseconds(3) : chrono::milliseconds(4));
Sabina Davis399dbd82019-02-01 23:06:08 -0800101
Sabina Davis399dbd82019-02-01 23:06:08 -0800102 ::aos::SetCurrentThreadRealtimePriority(40);
Austin Schuh3b010bc2019-02-24 17:25:37 -0800103 monotonic_clock::time_point last_monotonic_now = monotonic_clock::now();
Sabina Davis399dbd82019-02-01 23:06:08 -0800104 while (run_) {
105 {
106 const int iterations = phased_loop.SleepUntilNext();
107 if (iterations != 1) {
108 LOG(WARNING, "SensorReader skipped %d iterations\n", iterations - 1);
109 }
110 }
Austin Schuh3b010bc2019-02-24 17:25:37 -0800111 const monotonic_clock::time_point monotonic_now = monotonic_clock::now();
Austin Schuh45a549f2019-02-02 15:43:56 -0800112
113 ::frc971::wpilib::SendRobotState(my_pid);
Sabina Davis399dbd82019-02-01 23:06:08 -0800114 RunIteration();
Sabina Davis6292bec2019-02-06 22:53:14 -0800115 if (dma_synchronizer_) {
116 dma_synchronizer_->RunIteration();
117 RunDmaIteration();
118 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800119
Austin Schuh45a549f2019-02-02 15:43:56 -0800120 if (pwm_trigger_) {
Austin Schuh3b010bc2019-02-24 17:25:37 -0800121 LOG(DEBUG, "PWM wakeup delta: %lld\n",
122 (monotonic_now - last_monotonic_now).count());
123 last_monotonic_now = monotonic_now;
Austin Schuh45a549f2019-02-02 15:43:56 -0800124
Austin Schuh3b010bc2019-02-24 17:25:37 -0800125 monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime();
Austin Schuh45a549f2019-02-02 15:43:56 -0800126 if (last_tick_timepoint == monotonic_clock::min_time) {
127 continue;
128 }
Austin Schuh45a549f2019-02-02 15:43:56 -0800129
Austin Schuh3b010bc2019-02-24 17:25:37 -0800130 last_tick_timepoint +=
131 (monotonic_now - last_tick_timepoint) / period * period;
132 // If it's over 1/2 of a period back in time, that's wrong. Move it
133 // forwards to now.
134 if (last_tick_timepoint - monotonic_now < -period / 2) {
135 last_tick_timepoint += period;
136 }
137
138 // We should be sampling our sensors to kick off the control cycle 50 uS
139 // after the falling edge. This gives us a little bit of buffer for
140 // errors in waking up. The PWM cycle starts at the falling edge of the
141 // PWM pulse.
142 chrono::nanoseconds new_offset = phased_loop.OffsetFromIntervalAndTime(
143 period, last_tick_timepoint + chrono::microseconds(50));
Austin Schuh45a549f2019-02-02 15:43:56 -0800144
145 phased_loop.set_interval_and_offset(period, new_offset);
Sabina Davis399dbd82019-02-01 23:06:08 -0800146 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800147 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800148}
149
Sabina Davisadc58542019-02-01 22:23:00 -0800150} // namespace wpilib
151} // namespace frc971