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