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()); |
| 35 | InterruptableSensorBase::WaitResult result = InterruptableSensorBase::kBoth; |
| 36 | while (should_run()) { |
| 37 | result = input_->WaitForInterrupt( |
| 38 | 0.1, result != InterruptableSensorBase::kTimeout); |
| 39 | if (result == InterruptableSensorBase::kTimeout) { |
| 40 | continue; |
| 41 | } |
| 42 | interrupt_received(); |
| 43 | |
| 44 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(*mutex()); |
| 45 | int32_t encoder_value = encoder_->GetRaw(); |
| 46 | bool hall_value = input_->Get(); |
| 47 | if (current_value_ != hall_value) { |
| 48 | if (hall_value) { |
| 49 | ++shadow_values_.positive_interrupt_count; |
| 50 | shadow_values_.last_positive_encoder_value = encoder_value; |
| 51 | } else { |
| 52 | ++shadow_values_.negative_interrupt_count; |
| 53 | shadow_values_.last_negative_encoder_value = encoder_value; |
| 54 | } |
| 55 | current_value_ = hall_value; |
| 56 | } else { |
| 57 | LOG(WARNING, "Detected spurious edge on %d. Dropping it.\n", |
Brian Silverman | e48dbc1 | 2017-02-04 20:06:29 -0800 | [diff] [blame] | 58 | input_->GetChannel()); |
Brian Silverman | 70ec719 | 2015-01-26 17:52:40 -0500 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void InterruptSynchronizer::RunIteration() { |
| 64 | while (true) { |
| 65 | if (!TryStartIteration()) continue; |
| 66 | |
| 67 | // Wait more than the amount of time it takes for a digital input change |
| 68 | // to go from visible to software to having triggered an interrupt. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 69 | ::std::this_thread::sleep_for(::std::chrono::microseconds(120)); |
Brian Silverman | 70ec719 | 2015-01-26 17:52:40 -0500 | [diff] [blame] | 70 | |
| 71 | if (TryFinishingIteration()) return; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | bool InterruptSynchronizer::TryStartIteration() { |
| 76 | for (auto &c : handlers_) { |
| 77 | c->save_interrupt_count(); |
| 78 | } |
| 79 | |
| 80 | { |
| 81 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(mutex_); |
| 82 | for (auto &c : handlers_) { |
| 83 | c->GatherPolledValue(); |
| 84 | } |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool InterruptSynchronizer::TryFinishingIteration() { |
| 90 | // Make sure no interrupts have occurred while we were waiting. If they |
| 91 | // have, we are in an inconsistent state and need to try again. |
| 92 | ::std::unique_lock<::aos::stl_mutex> mutex_guard(mutex_); |
| 93 | for (auto &c : handlers_) { |
| 94 | if (c->interrupt_count_changed()) { |
| 95 | LOG(WARNING, "got an interrupt while sampling. retrying\n"); |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | for (auto &c : handlers_) { |
| 100 | c->CommitValue(); |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | } // namespace wpilib |
| 106 | } // namespace frc971 |