ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
SHARPGP2Y.h
Go to the documentation of this file.
1/**
2 * @file SHARPGP2Y.h
3 * @brief A class for interfacing with the SHARP GP2Y distance sensor.
4 *
5 * This file defines the SHARPGP2Y class, which provides methods for initializing and retrieving
6 * distance measurements from the SHARP GP2Y sensor using a running average filter for smoothing.
7 *
8 * @date 22/12/2024
9 * @author Coela Can't
10 */
11
12#pragma once
13
14#include <Arduino.h> // Include for Arduino compatibility.
15#include "../../Utils/Filter/RunningAverageFilter.h" // Include for running average filtering.
16
17/**
18 * @class SHARPGP2Y
19 * @brief A class for managing the SHARP GP2Y distance sensor.
20 *
21 * The SHARPGP2Y class provides methods for reading distance measurements from the sensor and
22 * applying filtering for noise reduction.
23 */
24class SHARPGP2Y {
25private:
26 RunningAverageFilter<25> rAF = RunningAverageFilter<25>(0.1f); ///< Running average filter for smoothing sensor readings.
27 float distance = 0.0f; ///< Current distance measurement.
28 uint8_t pin; ///< Analog pin connected to the sensor.
29
30 /**
31 * @brief Converts a raw sensor reading to a distance value.
32 *
33 * @param reading The raw analog reading from the sensor.
34 * @return The calculated distance in appropriate units (e.g., cm).
35 */
36 float ReadingToDistance(uint8_t reading);
37
38public:
39 /**
40 * @brief Constructs a SHARPGP2Y instance with the specified pin.
41 *
42 * @param pin The analog pin connected to the SHARP GP2Y sensor.
43 */
44 SHARPGP2Y(uint8_t pin);
45
46 /**
47 * @brief Retrieves the current distance measurement.
48 *
49 * @return The current distance as a float.
50 */
51 float GetDistance();
52
53 /**
54 * @brief Updates the distance measurement by reading the sensor.
55 *
56 * @return The updated distance as a float.
57 */
58 float Update();
59};
Smooths data values using a weighted running average.
A class for managing the SHARP GP2Y distance sensor.
Definition SHARPGP2Y.h:24
float distance
Current distance measurement.
Definition SHARPGP2Y.h:27
RunningAverageFilter< 25 > rAF
Running average filter for smoothing sensor readings.
Definition SHARPGP2Y.h:26
float Update()
Updates the distance measurement by reading the sensor.
Definition SHARPGP2Y.cpp:20
uint8_t pin
Analog pin connected to the sensor.
Definition SHARPGP2Y.h:28
float GetDistance()
Retrieves the current distance measurement.
Definition SHARPGP2Y.cpp:16
float ReadingToDistance(uint8_t reading)
Converts a raw sensor reading to a distance value.
Definition SHARPGP2Y.cpp:7