5#include "TinyRobotics/communication/Message.h"
6#include "TinyRobotics/communication/MessageSource.h"
8namespace tinyrobotics {
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
52 CameraLineFollower(uint8_t threshold = 80,
55 : _threshold(threshold), _minWidth(minWidth), _lastPosition(-1) {}
57 Result process(
const uint8_t* image, size_t width, size_t height) {
58 if (!image || width == 0 || height == 0) {
63 size_t y = (height * 3) / 4;
67 int currentStart = -1;
70 for (size_t x = 0; x < width; x++) {
71 uint8_t pixel = image[y * width + x];
73 bool isLine = (pixel < _threshold);
76 if (currentStart < 0) {
80 if (currentStart >= 0) {
81 int segmentWidth = x - currentStart;
83 if (segmentWidth >= _minWidth) {
84 if (segmentWidth > (bestEnd - bestStart)) {
85 bestStart = currentStart;
95 if (currentStart >= 0) {
96 int segmentWidth = width - currentStart;
97 if (segmentWidth >= _minWidth) {
98 bestStart = currentStart;
105 if (_lastPosition >= 0) {
106 int error = _lastPosition - (
int)(width / 2);
107 return {
false, _lastPosition, error};
109 return {
false, 0, 0};
112 int position = (bestStart + bestEnd) / 2;
115 if (_lastPosition >= 0) {
116 position = (_lastPosition * 3 + position) / 4;
119 _lastPosition = position;
121 int center = width / 2;
122 int error = position - center;
125 Message<
float> msgError(MessageContent::Error, error, Unit::Pixel);
126 msgError.origin = MessageOrigin::Camera;
127 sendMessage(msgError);
129 return {
true, position, error};
Simple line-following algorithm for grayscale camera images.
Definition: CameraLineFollower.h:44
Base class for message sources in the TinyRobotics communication framework.
Definition: MessageSource.h:35
Definition: CameraLineFollower.h:46
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72