Smartcar Shield
Loading...
Searching...
No Matches
InfraredAnalogSensor.cpp
Go to the documentation of this file.
2#include "../../../../utilities/Utilities.hpp"
3
4namespace
5{
6const unsigned long kMedianMeasurementDelay = 15;
7}
8
10using namespace smartcarlib::utils;
11
13 : mRuntime(runtime)
14{
15}
16
17unsigned int InfraredAnalogSensor::getMedianDistance(uint8_t iterations)
18{
19 if (iterations == 0 || iterations > kMaxMedianMeasurements)
20 {
21 return static_cast<unsigned int>(-1); // Return a large number to indicate error
22 }
23
24 unsigned int measurements[kMaxMedianMeasurements];
25 for (auto i = 0; i < iterations; i++)
26 {
27 measurements[i] = getDistance();
28 mRuntime.delayMillis(kMedianMeasurementDelay);
29 }
30
31 return getMedian(measurements, iterations);
32}
virtual unsigned int getDistance()=0
Gets the distance measured by the sensor in centimeters.
unsigned int getMedianDistance(uint8_t iterations=smartcarlib::constants::analogInfraredSensor::kDefaultIterations) override
Gets the median distance from the specified number of measurements.
InfraredAnalogSensor(Runtime &runtime)
An interface to serve as a wrapper for the native runtime environment, greatly inspired by the Arduin...
Definition Runtime.hpp:36
virtual void delayMillis(unsigned long milliseconds)=0
Block the execution for the specified number of milliseconds, equivalent of delay in Arduino.
AnyNumber getMedian(AnyNumber unsortedNumbers[], const unsigned int &arraySize)
Gets the median value out of the supplied number array.
Definition Utilities.hpp:66