Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 1 | #include "frc971/wpilib/encoder_and_potentiometer.h" |
| 2 | |
| 3 | #include "aos/linux_code/init.h" |
| 4 | #include "aos/common/logging/logging.h" |
| 5 | |
| 6 | namespace frc971 { |
| 7 | namespace wpilib { |
| 8 | |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 9 | bool DMAEncoder::DoUpdateFromSample(const DMASample &sample) { |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 10 | 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 Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 18 | return true; |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 19 | } |
Brian Silverman | 7cce2d3 | 2017-02-19 21:48:48 -0800 | [diff] [blame] | 20 | return false; |
Brian Silverman | 4da5807 | 2015-01-26 20:18:52 -0500 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | void InterruptEncoderAndPotentiometer::Start() { |
| 24 | CHECK_NE(nullptr, encoder_); |
| 25 | CHECK_NE(nullptr, index_); |
| 26 | CHECK_NE(nullptr, potentiometer_); |
| 27 | CHECK_NE(0, priority_); |
| 28 | thread_ = ::std::thread(::std::ref(*this)); |
| 29 | } |
| 30 | |
| 31 | void 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 | |
| 40 | InterruptableSensorBase::WaitResult result = InterruptableSensorBase::kBoth; |
| 41 | while (run_) { |
| 42 | result = index_->WaitForInterrupt( |
| 43 | 0.1, result != InterruptableSensorBase::kTimeout); |
| 44 | if (result == InterruptableSensorBase::kTimeout) { |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | { |
| 49 | ::aos::MutexLocker locker(&mutex_); |
| 50 | last_potentiometer_voltage_ = potentiometer_->GetVoltage(); |
| 51 | last_encoder_value_ = encoder_->GetRaw(); |
| 52 | ++index_posedge_count_; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | } // namespace wpilib |
| 58 | } // namespace frc971 |