blob: 8173d208de12f2b82411c9579831b2a883508d25 [file] [log] [blame]
Brian Silverman4da58072015-01-26 20:18:52 -05001#include "frc971/wpilib/encoder_and_potentiometer.h"
2
John Park33858a32018-09-28 23:05:48 -07003#include "aos/logging/logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07004#include "aos/realtime.h"
Brian Silverman4da58072015-01-26 20:18:52 -05005
6namespace frc971 {
7namespace wpilib {
8
Brian Silverman7cce2d32017-02-19 21:48:48 -08009bool DMAEncoder::DoUpdateFromSample(const DMASample &sample) {
Brian Silverman4da58072015-01-26 20:18:52 -050010 if (index_last_value_) {
11 // It was already true last time, so check if it's reset back to false yet.
12 index_last_value_ = sample.Get(index_.get());
13 } else if (sample.Get(index_.get())) {
14 // This sample is posedge, so record all the values.
15 index_last_value_ = true;
16 ++index_posedge_count_;
17 last_encoder_value_ = sample.GetRaw(encoder_.get());
Brian Silverman7cce2d32017-02-19 21:48:48 -080018 return true;
Brian Silverman4da58072015-01-26 20:18:52 -050019 }
Brian Silverman7cce2d32017-02-19 21:48:48 -080020 return false;
Brian Silverman4da58072015-01-26 20:18:52 -050021}
22
23void InterruptEncoderAndPotentiometer::Start() {
Austin Schuhf257f3c2019-10-27 21:00:43 -070024 AOS_CHECK_NE(nullptr, encoder_);
25 AOS_CHECK_NE(nullptr, index_);
26 AOS_CHECK_NE(nullptr, potentiometer_);
27 AOS_CHECK_NE(0, priority_);
Brian Silverman4da58072015-01-26 20:18:52 -050028 thread_ = ::std::thread(::std::ref(*this));
29}
30
31void InterruptEncoderAndPotentiometer::operator()() {
32 ::aos::SetCurrentThreadName("IntEncPot_" +
33 ::std::to_string(potentiometer_->GetChannel()));
34
35 index_->RequestInterrupts();
36 index_->SetUpSourceEdge(true, false);
37
38 ::aos::SetCurrentThreadRealtimePriority(priority_);
39
Parker Schuhd3b7a8872018-02-19 16:42:27 -080040 frc::InterruptableSensorBase::WaitResult result =
41 frc::InterruptableSensorBase::kBoth;
Brian Silverman4da58072015-01-26 20:18:52 -050042 while (run_) {
43 result = index_->WaitForInterrupt(
Parker Schuhd3b7a8872018-02-19 16:42:27 -080044 0.1, result != frc::InterruptableSensorBase::kTimeout);
45 if (result == frc::InterruptableSensorBase::kTimeout) {
Brian Silverman4da58072015-01-26 20:18:52 -050046 continue;
47 }
48
49 {
50 ::aos::MutexLocker locker(&mutex_);
51 last_potentiometer_voltage_ = potentiometer_->GetVoltage();
52 last_encoder_value_ = encoder_->GetRaw();
53 ++index_posedge_count_;
54 }
55 }
56}
57
58} // namespace wpilib
59} // namespace frc971