TinyRobotics
Loading...
Searching...
No Matches
CameraObstacleDetector.h
1
2#pragma once
3#include <stddef.h>
4#include <stdint.h>
5
6#include "TinyRobotics/communication/Message.h"
7#include "TinyRobotics/communication/MessageSource.h"
8
9namespace tinyrobotics {
10
11/**
12 * @class CameraObstacleDetector
13 * @ingroup sensors
14 * @brief Simple obstacle detection algorithm for grayscale camera images.
15 *
16 * This class scans a central region of a grayscale image (e.g., from a camera
17 * or vision sensor) to detect the presence of dark obstacles based on pixel
18 * intensity and density. It is useful for basic obstacle avoidance in robotics.
19 *
20 * Features:
21 * - Scans the center region of the image for dark pixels
22 * - Triggers detection if the density of dark pixels exceeds a threshold
23 * - Returns detection status and density (fraction of dark pixels)
24 * - Configurable pixel threshold and trigger density
25 * - Lightweight, suitable for embedded/Arduino use
26 *
27 * @note Expected format:
28 * - The input image should be a grayscale buffer (uint8_t*)
29 * - The pixel intensity is expected to be in the range 0 (black) to 255
30 * (white).
31 * - The layout of the image buffer should be row-major (i.e., pixel at (x,y) is
32 * at index y*width + x).
33 * - on ESP32 e.g. PIXFORMAT_GRAYSCALE, FRAMESIZE_QVGA (320 x 240)
34 *
35 * Usage Example:
36 * @code
37 * CameraObstacleDetector detector(60, 0.2f); // threshold=60, trigger=20%
38 * auto result = detector.process(image, width, height);
39 * if (result.detected) {
40 * // Obstacle detected, take action
41 * }
42 * @endcode
43 */
44
46 public:
47 struct Result {
48 bool detected;
49 float density; // 0.0 → 1.0
50 };
51
52 CameraObstacleDetector(
53 uint8_t threshold = 60, // pixel threshold (0–255)
54 float trigger = 0.15f // % of pixels to trigger detection
55 )
56 : _threshold(threshold), _trigger(trigger) {}
57
58 // image: grayscale buffer (width * height)
59 Result process(const uint8_t* image, size_t width, size_t height) {
60 if (!image || width == 0 || height == 0) {
61 return {false, 0.0f};
62 }
63
64 // Focus on center region (ignore edges)
65 size_t x_start = width / 4;
66 size_t x_end = 3 * width / 4;
67 size_t y_start = height / 3;
68 size_t y_end = height;
69
70 size_t total = 0;
71 size_t hits = 0;
72
73 for (size_t y = y_start; y < y_end; y++) {
74 for (size_t x = x_start; x < x_end; x++) {
75 uint8_t pixel = image[y * width + x];
76
77 // Detect dark object
78 if (pixel < _threshold) {
79 hits++;
80 }
81 total++;
82 }
83 }
84
85 float density = (total > 0) ? (float)hits / total : 0.0f;
86
87 // publish angle to direction of movement
88 Message<float> msgError(MessageContent::Obstacle, density * 100.0f, Unit::Percent, MessageOrigin::Camera);
89 sendMessage(msgError);
90
91 return {density > _trigger, density};
92 }
93
94 protected:
95 uint8_t _threshold;
96 float _trigger;
97};
98
99} // namespace tinyrobotics
Simple obstacle detection algorithm for grayscale camera images.
Definition: CameraObstacleDetector.h:45
Base class for message sources in the TinyRobotics communication framework.
Definition: MessageSource.h:35
Definition: CameraObstacleDetector.h:47
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72