Smartcar Shield
InfraredAnalogSensor.cpp
Go to the documentation of this file.
2 #include "../../../../utilities/Utilities.hpp"
3 
4 namespace
5 {
6 const unsigned long kMedianMeasurementDelay = 15;
7 }
8 
10 using namespace smartcarlib::utils;
11 
13  : mRuntime(runtime)
14 {
15 }
16 
17 unsigned 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 }
Runtime::delayMillis
virtual void delayMillis(unsigned long milliseconds)=0
Block the execution for the specified number of milliseconds, equivalent of delay in Arduino.
Runtime
Definition: Runtime.hpp:35
smartcarlib::utils::getMedian
AnyNumber getMedian(AnyNumber unsortedNumbers[], const unsigned int &arraySize)
Gets the median value out of the supplied number array.
Definition: Utilities.hpp:66
DistanceSensor::getDistance
virtual unsigned int getDistance()=0
Gets the distance measured by the sensor in centimeters.
smartcarlib::utils
Definition: Utilities.hpp:9
InfraredAnalogSensor::InfraredAnalogSensor
InfraredAnalogSensor(Runtime &runtime)
Definition: InfraredAnalogSensor.cpp:12
smartcarlib::constants::distanceSensor
Definition: DistanceSensor.hpp:13
InfraredAnalogSensor::getMedianDistance
unsigned int getMedianDistance(uint8_t iterations=smartcarlib::constants::analogInfraredSensor::kDefaultIterations) override
Gets the median distance from the specified number of measurements.
Definition: InfraredAnalogSensor.cpp:17
smartcarlib::constants::distanceSensor::kMaxMedianMeasurements
const int kMaxMedianMeasurements
Definition: DistanceSensor.hpp:15
InfraredAnalogSensor.hpp