blob: 447e3b416cd0178a63e172a299c5cfb777a45f1b [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
Austin Schuh028d81d2022-03-26 15:11:42 -070017DEFINE_int32(pwm_offset, 5050 / 2,
18 "Offset of reading the sensors from the start of the PWM cycle");
19
Stephan Pleinesf63bde82024-01-13 15:59:33 -080020namespace frc971::wpilib {
Sabina Davisadc58542019-02-01 22:23:00 -080021
Austin Schuh217a9782019-12-21 23:02:50 -080022SensorReader::SensorReader(::aos::ShmEventLoop *event_loop)
Austin Schuhdf6cbb12019-02-02 13:46:52 -080023 : event_loop_(event_loop),
Austin Schuh217a9782019-12-21 23:02:50 -080024 robot_state_sender_(event_loop_->MakeSender<::aos::RobotState>("/aos")),
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070025 my_pid_(getpid()) {
Austin Schuh45a549f2019-02-02 15:43:56 -080026 // Set some defaults. We don't tend to exceed these, so old robots should
27 // just work with them.
28 UpdateFastEncoderFilterHz(500000);
29 UpdateMediumEncoderFilterHz(100000);
Austin Schuh3b010bc2019-02-24 17:25:37 -080030 ds_ = &::frc::DriverStation::GetInstance();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070031
32 event_loop->SetRuntimeRealtimePriority(40);
Austin Schuh719d6802021-11-05 23:46:20 -070033 // The timer interrupt fires on CPU1. Since nothing else is pinned, it will
34 // be cheapest to pin this there so it transitions directly and doesn't
35 // need to ever migrate.
36 event_loop->SetRuntimeAffinity(aos::MakeCpusetFromCpus({1}));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070037
38 // Fill in the no pwm trigger defaults.
milind-u61227f22021-08-29 15:58:33 -070039 timer_handler_ = event_loop_->AddTimer([this]() { Loop(); });
40 timer_handler_->set_name("SensorReader Loop");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070041
42 event_loop->set_name("SensorReader");
43 event_loop->OnRun([this]() { DoStart(); });
Austin Schuh45a549f2019-02-02 15:43:56 -080044}
45
46void SensorReader::UpdateFastEncoderFilterHz(int hz) {
47 fast_encoder_filter_.SetPeriodHz(::std::max(hz, 100000));
48}
49
50void SensorReader::UpdateMediumEncoderFilterHz(int hz) {
51 medium_encoder_filter_.SetPeriodHz(::std::max(hz, 50000));
52}
Sabina Davisadc58542019-02-01 22:23:00 -080053
Sabina Davisb6317b72019-02-01 22:53:23 -080054void SensorReader::set_drivetrain_left_encoder(
55 ::std::unique_ptr<frc::Encoder> encoder) {
56 fast_encoder_filter_.Add(encoder.get());
57 drivetrain_left_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080058 drivetrain_left_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080059}
60
61void SensorReader::set_drivetrain_right_encoder(
62 ::std::unique_ptr<frc::Encoder> encoder) {
63 fast_encoder_filter_.Add(encoder.get());
64 drivetrain_right_encoder_ = ::std::move(encoder);
Austin Schuh45a549f2019-02-02 15:43:56 -080065 drivetrain_right_encoder_->SetMaxPeriod(0.005);
Sabina Davisb6317b72019-02-01 22:53:23 -080066}
67
Austin Schuh3b010bc2019-02-24 17:25:37 -080068monotonic_clock::time_point SensorReader::GetPWMStartTime() {
69 int32_t status = 0;
Brian Silverman7be68ba2020-01-08 22:08:40 -080070 const auto new_fpga_time =
71 hal::fpga_clock::duration(HAL_GetPWMCycleStartTime(&status));
Sabina Davisadc58542019-02-01 22:23:00 -080072
Brian Silverman7be68ba2020-01-08 22:08:40 -080073 if (!ds_->IsSysActive()) {
Austin Schuh3b010bc2019-02-24 17:25:37 -080074 return monotonic_clock::min_time;
Sabina Davisadc58542019-02-01 22:23:00 -080075 }
Brian Silverman7be68ba2020-01-08 22:08:40 -080076
77 const auto fpga_offset = CalculateFpgaOffset();
78 // If we failed to sample the offset, just ignore this reading.
79 if (!fpga_offset) {
80 return monotonic_clock::min_time;
81 }
82
83 return monotonic_clock::epoch() + (new_fpga_time + *fpga_offset);
Sabina Davisadc58542019-02-01 22:23:00 -080084}
85
Maxwell Hendersond5ae85b2024-01-08 16:14:28 -080086void SensorReader::SendDrivetrainPosition(
Maxwell Hendersonf382b4b2024-01-14 12:31:41 -080087 aos::Sender<control_loops::drivetrain::PositionStatic>::StaticBuilder
88 builder,
Maxwell Hendersond5ae85b2024-01-08 16:14:28 -080089 std::function<double(double input)> velocity_translate,
90 std::function<double(double input)> encoder_to_meters, bool left_inverted,
91 bool right_inverted) {
Maxwell Hendersonf382b4b2024-01-14 12:31:41 -080092 builder->set_left_encoder(
Maxwell Hendersond5ae85b2024-01-08 16:14:28 -080093 (left_inverted ? -1.0 : 1.0) *
94 encoder_to_meters(drivetrain_left_encoder_->GetRaw()));
Maxwell Hendersonf382b4b2024-01-14 12:31:41 -080095 builder->set_left_speed(
Maxwell Hendersond5ae85b2024-01-08 16:14:28 -080096 (left_inverted ? -1.0 : 1.0) *
97 velocity_translate(drivetrain_left_encoder_->GetPeriod()));
98
Maxwell Hendersonf382b4b2024-01-14 12:31:41 -080099 builder->set_right_encoder(
Maxwell Hendersond5ae85b2024-01-08 16:14:28 -0800100 (right_inverted ? -1.0 : 1.0) *
101 encoder_to_meters(drivetrain_right_encoder_->GetRaw()));
Maxwell Hendersonf382b4b2024-01-14 12:31:41 -0800102 builder->set_right_speed(
Maxwell Hendersond5ae85b2024-01-08 16:14:28 -0800103 (right_inverted ? -1.0 : 1.0) *
104 velocity_translate(drivetrain_right_encoder_->GetPeriod()));
105
Maxwell Hendersonf382b4b2024-01-14 12:31:41 -0800106 builder.CheckOk(builder.Send());
Maxwell Hendersond5ae85b2024-01-08 16:14:28 -0800107}
108
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700109void SensorReader::DoStart() {
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800110 Start();
Sabina Davis6292bec2019-02-06 22:53:14 -0800111 if (dma_synchronizer_) {
112 dma_synchronizer_->Start();
113 }
Austin Schuh2c2cc2e2019-02-02 20:19:45 -0800114
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700115 period_ =
Austin Schuh3b010bc2019-02-24 17:25:37 -0800116 pwm_trigger_ ? chrono::microseconds(5050) : chrono::microseconds(5000);
Austin Schuh45a549f2019-02-02 15:43:56 -0800117 if (pwm_trigger_) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700118 AOS_LOG(INFO, "Using PWM trigger and a 5.05 ms period\n");
Austin Schuh45a549f2019-02-02 15:43:56 -0800119 } else {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700120 AOS_LOG(INFO, "Defaulting to open loop pwm synchronization\n");
Austin Schuh45a549f2019-02-02 15:43:56 -0800121 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700122
123 // Now that we are configured, actually fill in the defaults.
Philipp Schradera6712522023-07-05 20:25:11 -0700124 timer_handler_->Schedule(
milind-u61227f22021-08-29 15:58:33 -0700125 event_loop_->monotonic_now() +
126 (pwm_trigger_ ? chrono::milliseconds(3) : chrono::milliseconds(4)),
127 period_);
Sabina Davis399dbd82019-02-01 23:06:08 -0800128
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700129 last_monotonic_now_ = monotonic_clock::now();
130}
Austin Schuh45a549f2019-02-02 15:43:56 -0800131
milind-u61227f22021-08-29 15:58:33 -0700132void SensorReader::Loop() {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700133 const monotonic_clock::time_point monotonic_now =
134 event_loop_->monotonic_now();
135
136 {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700137 auto builder = robot_state_sender_.MakeBuilder();
milind1f1dca32021-07-03 13:50:07 -0700138 (void)builder.Send(::frc971::wpilib::PopulateRobotState(&builder, my_pid_));
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700139 }
140 RunIteration();
141 if (dma_synchronizer_) {
142 dma_synchronizer_->RunIteration();
143 RunDmaIteration();
144 }
145
146 if (pwm_trigger_) {
James Kuszmaul1bce5772020-03-08 22:13:50 -0700147 // TODO(austin): Put this in a status message.
148 VLOG(1) << "PWM wakeup delta: "
149 << (monotonic_now - last_monotonic_now_).count();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700150 last_monotonic_now_ = monotonic_now;
151
152 monotonic_clock::time_point last_tick_timepoint = GetPWMStartTime();
Philipp Schrader790cb542023-07-05 21:06:52 -0700153 VLOG(1) << "Start time " << last_tick_timepoint << " period "
154 << period_.count();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700155 if (last_tick_timepoint == monotonic_clock::min_time) {
156 return;
Sabina Davis6292bec2019-02-06 22:53:14 -0800157 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800158
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700159 last_tick_timepoint +=
Austin Schuh028d81d2022-03-26 15:11:42 -0700160 ((monotonic_now - chrono::microseconds(FLAGS_pwm_offset) -
161 last_tick_timepoint) /
162 period_) *
Philipp Schrader790cb542023-07-05 21:06:52 -0700163 period_ +
164 chrono::microseconds(FLAGS_pwm_offset);
Austin Schuh028d81d2022-03-26 15:11:42 -0700165 VLOG(1) << "Now " << monotonic_now << " tick " << last_tick_timepoint;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700166 // If it's over 1/2 of a period back in time, that's wrong. Move it
167 // forwards to now.
168 if (last_tick_timepoint - monotonic_now < -period_ / 2) {
169 last_tick_timepoint += period_;
Sabina Davis399dbd82019-02-01 23:06:08 -0800170 }
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700171
172 // We should be sampling our sensors to kick off the control cycle 50 uS
173 // after the falling edge. This gives us a little bit of buffer for
174 // errors in waking up. The PWM cycle starts at the falling edge of the
175 // PWM pulse.
Philipp Schrader790cb542023-07-05 21:06:52 -0700176 const auto next_time = last_tick_timepoint + period_;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -0700177
Philipp Schradera6712522023-07-05 20:25:11 -0700178 timer_handler_->Schedule(next_time, period_);
Sabina Davis399dbd82019-02-01 23:06:08 -0800179 }
Sabina Davis399dbd82019-02-01 23:06:08 -0800180}
181
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800182} // namespace frc971::wpilib