TinyRobotics
Loading...
Searching...
No Matches
AccelerometerObstacleDetector.h
1#pragma once
2#include <cmath>
3#include <functional>
4
5#include "TinyRobotics/communication/Message.h"
6#include "TinyRobotics/communication/MessageSource.h"
7#include "TinyRobotics/units/Distance.h"
8
9namespace tinyrobotics {
10
11/**
12 * @class AccelerometerObstacleDetector
13 * @ingroup sensors
14 * @brief Simple obstacle detector using accelerometer data.
15 *
16 * Detects sudden deceleration (collision/obstacle) events by monitoring the
17 * X/Y/Z acceleration. When a deceleration above a threshold is detected, a
18 * callback (with user data) or message is triggered.
19 *
20 * ## Usage Example
21 * @code
22 * AccelerometerObstacleDetector detector(3.0f); // 3 m/s^2 threshold
23 * detected!"); }, nullptr); void loop() { detector.update(ax, ay, az); }
24 * @endcode
25 */
27 public:
28 /**
29 * @brief Construct with a deceleration threshold.
30 * @param threshold Deceleration threshold (m/s^2)
31 */
32 AccelerometerObstacleDetector(float threshold = 3.0f)
33 : threshold(threshold) {}
34
35
36 /**
37 * @brief Update the detector with new acceleration values. Checks for sudden
38 * deceleration.
39 * @param ax Acceleration in X (m/s^2)
40 * @param ay Acceleration in Y (m/s^2)
41 * @param az Acceleration in Z (m/s^2)
42 */
43 void update(float ax, float ay, float az) {
44 float magnitude = std::sqrt(ax * ax + ay * ay + az * az);
45 float delta = lastMagnitude - magnitude;
46 if (delta > threshold) {
47 Message<float> msg(MessageContent::Obstacle, 0.0f, Unit::Meters);
48 msg.origin = MessageOrigin::IMU;
49 sendMessage(msg);
50 }
51 lastMagnitude = magnitude;
52 }
53
54 /**
55 * @brief Set the deceleration threshold (m/s^2).
56 */
57 void setThreshold(float t) { threshold = t; }
58
59 protected:
60 // No accelerometer reference needed
61 float threshold;
62 float lastMagnitude = 0.0f;
63};
64
65} // namespace tinyrobotics
Simple obstacle detector using accelerometer data.
Definition: AccelerometerObstacleDetector.h:26
AccelerometerObstacleDetector(float threshold=3.0f)
Construct with a deceleration threshold.
Definition: AccelerometerObstacleDetector.h:32
void setThreshold(float t)
Set the deceleration threshold (m/s^2).
Definition: AccelerometerObstacleDetector.h:57
void update(float ax, float ay, float az)
Update the detector with new acceleration values. Checks for sudden deceleration.
Definition: AccelerometerObstacleDetector.h:43
Base class for message sources in the TinyRobotics communication framework.
Definition: MessageSource.h:35
Generic message structure for communication, parameterized by value type.
Definition: Message.h:72