Brian Silverman | 70ec719 | 2015-01-26 17:52:40 -0500 | [diff] [blame] | 1 | #include "frc971/wpilib/interrupt_edge_counting.h" |
| 2 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 3 | #include <chrono> |
| 4 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/time/time.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 6 | #include "aos/init.h" |
Brian Silverman | 70ec719 | 2015-01-26 17:52:40 -0500 | [diff] [blame] | 7 | |
| 8 | namespace frc971 { |
| 9 | namespace wpilib { |
| 10 | |
| 11 | void EdgeCounter::GatherPolledValue() { |
| 12 | shadow_values_.polled_value = input_->Get(); |
| 13 | bool miss_match = (shadow_values_.polled_value != current_value_); |
| 14 | if (miss_match && last_miss_match_) { |
| 15 | current_value_ = shadow_values_.polled_value; |
| 16 | last_miss_match_ = false; |
| 17 | } else { |
| 18 | last_miss_match_ = miss_match; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | void EdgeCounter::operator()() { |
| 23 | ::aos::SetCurrentThreadName("EdgeCounter_" + |
Brian Silverman | e48dbc1 | 2017-02-04 20:06:29 -0800 | [diff] [blame] | 24 | ::std::to_string(input_->GetChannel())); |
Brian Silverman | 70ec719 | 2015-01-26 17:52:40 -0500 | [diff] [blame] | 25 | |
| 26 | input_->RequestInterrupts(); |
| 27 | input_->SetUpSourceEdge(true, true); |
| 28 | |
| 29 | { |
| 30 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(*mutex()); |
| 31 | current_value_ = input_->Get(); |
| 32 | } |
| 33 | |
| 34 | ::aos::SetCurrentThreadRealtimePriority(priority()); |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 35 | frc::InterruptableSensorBase::WaitResult result = |
| 36 | frc::InterruptableSensorBase::kBoth; |
Brian Silverman | 70ec719 | 2015-01-26 17:52:40 -0500 | [diff] [blame] | 37 | while (should_run()) { |
| 38 | result = input_->WaitForInterrupt( |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 39 | 0.1, result != frc::InterruptableSensorBase::kTimeout); |
| 40 | if (result == frc::InterruptableSensorBase::kTimeout) { |
Brian Silverman | 70ec719 | 2015-01-26 17:52:40 -0500 | [diff] [blame] | 41 | continue; |
| 42 | } |
| 43 | interrupt_received(); |
| 44 | |
| 45 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(*mutex()); |
| 46 | int32_t encoder_value = encoder_->GetRaw(); |
| 47 | bool hall_value = input_->Get(); |
| 48 | if (current_value_ != hall_value) { |
| 49 | if (hall_value) { |
| 50 | ++shadow_values_.positive_interrupt_count; |
| 51 | shadow_values_.last_positive_encoder_value = encoder_value; |
| 52 | } else { |
| 53 | ++shadow_values_.negative_interrupt_count; |
| 54 | shadow_values_.last_negative_encoder_value = encoder_value; |
| 55 | } |
| 56 | current_value_ = hall_value; |
| 57 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 58 | AOS_LOG(WARNING, "Detected spurious edge on %d. Dropping it.\n", |
| 59 | input_->GetChannel()); |
Brian Silverman | 70ec719 | 2015-01-26 17:52:40 -0500 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void InterruptSynchronizer::RunIteration() { |
| 65 | while (true) { |
| 66 | if (!TryStartIteration()) continue; |
| 67 | |
| 68 | // Wait more than the amount of time it takes for a digital input change |
| 69 | // to go from visible to software to having triggered an interrupt. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 70 | ::std::this_thread::sleep_for(::std::chrono::microseconds(120)); |
Brian Silverman | 70ec719 | 2015-01-26 17:52:40 -0500 | [diff] [blame] | 71 | |
| 72 | if (TryFinishingIteration()) return; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool InterruptSynchronizer::TryStartIteration() { |
| 77 | for (auto &c : handlers_) { |
| 78 | c->save_interrupt_count(); |
| 79 | } |
| 80 | |
| 81 | { |
| 82 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(mutex_); |
| 83 | for (auto &c : handlers_) { |
| 84 | c->GatherPolledValue(); |
| 85 | } |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool InterruptSynchronizer::TryFinishingIteration() { |
| 91 | // Make sure no interrupts have occurred while we were waiting. If they |
| 92 | // have, we are in an inconsistent state and need to try again. |
| 93 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(mutex_); |
| 94 | for (auto &c : handlers_) { |
| 95 | if (c->interrupt_count_changed()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 96 | AOS_LOG(WARNING, "got an interrupt while sampling. retrying\n"); |
Brian Silverman | 70ec719 | 2015-01-26 17:52:40 -0500 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | for (auto &c : handlers_) { |
| 101 | c->CommitValue(); |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | } // namespace wpilib |
| 107 | } // namespace frc971 |