|
| 1 | +#include "GreySensor.hpp" |
| 2 | + |
| 3 | +#include "libxr_def.hpp" |
| 4 | +#include "thread.hpp" |
| 5 | + |
| 6 | +uint8_t GreySensor::BuildBit(size_t channel) |
| 7 | +{ |
| 8 | + return static_cast<uint8_t>(1U << channel); |
| 9 | +} |
| 10 | + |
| 11 | +int16_t GreySensor::BuildPosition(size_t channel, size_t channel_count) |
| 12 | +{ |
| 13 | + const int32_t centered = |
| 14 | + (static_cast<int32_t>(channel) * 2) - static_cast<int32_t>(channel_count - 1); |
| 15 | + return static_cast<int16_t>((centered * POSITION_SCALE) / 2); |
| 16 | +} |
| 17 | + |
| 18 | +uint8_t GreySensor::GetLostSide(int16_t position) |
| 19 | +{ |
| 20 | + if (position < 0) |
| 21 | + { |
| 22 | + return LOST_SIDE_LEFT; |
| 23 | + } |
| 24 | + |
| 25 | + if (position > 0) |
| 26 | + { |
| 27 | + return LOST_SIDE_RIGHT; |
| 28 | + } |
| 29 | + |
| 30 | + return LOST_SIDE_UNKNOWN; |
| 31 | +} |
| 32 | + |
| 33 | +GreySensor::GreySensor(LibXR::HardwareContainer& hw, LibXR::ApplicationManager& app, |
| 34 | + std::initializer_list<const char*> channel_names, |
| 35 | + bool active_low, const char* topic_name, |
| 36 | + uint32_t publish_period_ms) |
| 37 | + : channel_count_(channel_names.size()), |
| 38 | + topic_(LibXR::Topic::CreateTopic<Sample>(topic_name, nullptr, false, true, |
| 39 | + true)), |
| 40 | + active_low_(active_low), |
| 41 | + publish_period_ms_(publish_period_ms) |
| 42 | +{ |
| 43 | + ASSERT(channel_count_ > 0); |
| 44 | + ASSERT(channel_count_ <= MAX_CHANNEL_COUNT); |
| 45 | + |
| 46 | + size_t i = 0; |
| 47 | + for (const char* name : channel_names) |
| 48 | + { |
| 49 | + ASSERT(name != nullptr); |
| 50 | + channels_[i] = hw.FindOrExit<LibXR::GPIO>({name}); |
| 51 | + const LibXR::ErrorCode err = channels_[i]->SetConfig( |
| 52 | + {LibXR::GPIO::Direction::INPUT, LibXR::GPIO::Pull::NONE}); |
| 53 | + ASSERT(err == LibXR::ErrorCode::OK); |
| 54 | + i++; |
| 55 | + } |
| 56 | + |
| 57 | + last_active_mask_ = ReadActiveMask(); |
| 58 | + app.Register(*this); |
| 59 | +} |
| 60 | + |
| 61 | +GreySensor::Sample GreySensor::ReadDigital() const |
| 62 | +{ |
| 63 | + Sample sample; |
| 64 | + sample.channel_count = static_cast<uint8_t>(channel_count_); |
| 65 | + |
| 66 | + for (size_t i = 0; i < channel_count_; i++) |
| 67 | + { |
| 68 | + const bool raw_high = channels_[i]->Read(); |
| 69 | + const bool active = active_low_ ? !raw_high : raw_high; |
| 70 | + |
| 71 | + sample.raw[i] = raw_high ? 1U : 0U; |
| 72 | + sample.active[i] = active ? 1U : 0U; |
| 73 | + |
| 74 | + if (raw_high) |
| 75 | + { |
| 76 | + sample.raw_mask |= BuildBit(i); |
| 77 | + } |
| 78 | + |
| 79 | + if (active) |
| 80 | + { |
| 81 | + sample.active_mask |= BuildBit(i); |
| 82 | + sample.active_count++; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return sample; |
| 87 | +} |
| 88 | + |
| 89 | +void GreySensor::UpdatePositionState(Sample& sample) |
| 90 | +{ |
| 91 | + int32_t weighted_sum = 0; |
| 92 | + |
| 93 | + for (size_t i = 0; i < channel_count_; i++) |
| 94 | + { |
| 95 | + if (sample.active[i] != 0U) |
| 96 | + { |
| 97 | + weighted_sum += BuildPosition(i, channel_count_); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (sample.active_count != 0U) |
| 102 | + { |
| 103 | + sample.line_detected = 1; |
| 104 | + sample.line_lost = 0; |
| 105 | + sample.weighted_position = |
| 106 | + static_cast<int16_t>(weighted_sum / sample.active_count); |
| 107 | + sample.position = sample.weighted_position; |
| 108 | + |
| 109 | + remembered_position_ = sample.position; |
| 110 | + has_position_memory_ = true; |
| 111 | + lost_count_ = 0; |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + sample.line_detected = 0; |
| 116 | + sample.line_lost = 1; |
| 117 | + lost_count_++; |
| 118 | + |
| 119 | + sample.position = has_position_memory_ ? remembered_position_ : 0; |
| 120 | + sample.lost_side = |
| 121 | + has_position_memory_ ? GetLostSide(remembered_position_) : LOST_SIDE_UNKNOWN; |
| 122 | + sample.lost_count = lost_count_; |
| 123 | + } |
| 124 | + |
| 125 | + sample.remembered_position = has_position_memory_ ? remembered_position_ : 0; |
| 126 | +} |
| 127 | + |
| 128 | +GreySensor::Sample GreySensor::Read() |
| 129 | +{ |
| 130 | + Sample sample = ReadDigital(); |
| 131 | + UpdatePositionState(sample); |
| 132 | + return sample; |
| 133 | +} |
| 134 | + |
| 135 | +uint8_t GreySensor::ReadRawMask() const { return ReadDigital().raw_mask; } |
| 136 | + |
| 137 | +uint8_t GreySensor::ReadActiveMask() const { return ReadDigital().active_mask; } |
| 138 | + |
| 139 | +int16_t GreySensor::ReadPosition() { return Read().position; } |
| 140 | + |
| 141 | +size_t GreySensor::ChannelCount() const { return channel_count_; } |
| 142 | + |
| 143 | +void GreySensor::OnMonitor() |
| 144 | +{ |
| 145 | + const uint32_t now_ms = LibXR::Thread::GetTime(); |
| 146 | + if (has_published_ && publish_period_ms_ != 0U && |
| 147 | + static_cast<uint32_t>(now_ms - last_publish_ms_) < publish_period_ms_) |
| 148 | + { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + Sample sample = Read(); |
| 153 | + sample.changed_mask = static_cast<uint8_t>(sample.active_mask ^ last_active_mask_); |
| 154 | + sample.sequence = sequence_++; |
| 155 | + |
| 156 | + last_active_mask_ = sample.active_mask; |
| 157 | + last_publish_ms_ = now_ms; |
| 158 | + has_published_ = true; |
| 159 | + |
| 160 | + topic_.Publish(sample); |
| 161 | +} |
0 commit comments