Smartcar Shield
SR04.cpp
Go to the documentation of this file.
1 #include "SR04.hpp"
2 #include "../../../../utilities/Utilities.hpp"
3 namespace
4 {
5 const float kTimeToTravelOneCmAndBack = 29.15F * 2.0F; // In microseconds
6 const unsigned long kTimeToMeasureOneCm = 120; // Empirically determined
7 // We should wait long enough between two consecutive measurements
8 // so to avoid getting parasitic readings from old returning waves.
9 const unsigned long kMedianMeasurementDelay = 15; // In milliseconds
10 } // namespace
11 
12 using namespace smartcarlib::constants::sr04;
14 using namespace smartcarlib::utils;
15 
16 SR04::SR04(Runtime& runtime, uint8_t triggerPin, uint8_t echoPin, unsigned int maxDistance)
17  : kTriggerPin{ triggerPin }
18  , kEchoPin{ echoPin }
19  , kMaxDistance{ maxDistance > 0 ? maxDistance : kDefaultMaxDistance }
20  , kTimeout{ kMaxDistance * kTimeToMeasureOneCm }
21  , mRuntime(runtime)
22  , kOutput{ mRuntime.getOutputState() }
23  , kInput{ mRuntime.getInputState() }
24  , kLow{ mRuntime.getLowState() }
25  , kHigh{ mRuntime.getHighState() }
26 
27 {
28 }
29 
30 void SR04::attach()
31 {
32  if (!mAttached)
33  {
34  mRuntime.setPinDirection(kTriggerPin, kOutput);
35  mRuntime.setPinDirection(kEchoPin, kInput);
36  mAttached = true;
37  }
38 }
39 
40 unsigned int SR04::getDistance()
41 {
42  attach();
43 
44  // Generate the pulse
45  mRuntime.setPinState(kTriggerPin, kLow);
46  static constexpr auto kSetPinToKnownStateDelay = 5;
47  mRuntime.delayMicros(kSetPinToKnownStateDelay);
48  mRuntime.setPinState(kTriggerPin, kHigh);
49  static constexpr auto kTenMicrosecondPulse = 10;
50  mRuntime.delayMicros(kTenMicrosecondPulse); // 10us pulse
51  mRuntime.setPinState(kTriggerPin, kLow);
52  // Wait for the pulse to arrive and measure its duration
53  auto duration = mRuntime.getPulseDuration(kEchoPin, kHigh, kTimeout);
54  // Calculate how much far out the object is
55  auto calculatedDistance
56  = static_cast<unsigned int>(static_cast<float>(duration) / kTimeToTravelOneCmAndBack);
57 
58  return calculatedDistance <= kMaxDistance ? calculatedDistance : kError;
59 }
60 
61 unsigned int SR04::getMedianDistance(uint8_t iterations)
62 {
63  if (iterations == 0 || iterations > kMaxMedianMeasurements)
64  {
65  return kError;
66  }
67 
68  unsigned int measurements[kMaxMedianMeasurements];
69  for (auto i = 0; i < iterations; i++)
70  {
71  measurements[i] = getDistance();
72  mRuntime.delayMillis(kMedianMeasurementDelay);
73  }
74 
75  return getMedian(measurements, iterations);
76 }
Runtime::delayMicros
virtual void delayMicros(unsigned int microseconds)=0
Block the execution for the specified number of microseconds, equivalent of delayMicroseconds in Ardu...
smartcarlib::constants::sr04
Definition: SR04.hpp:20
Runtime::setPinState
virtual void setPinState(uint8_t pin, uint8_t state)=0
Set pin state, equivalent of digitalWrite in Arduino.
SR04::SR04
SR04(Runtime &runtime, uint8_t triggerPin, uint8_t echoPin, unsigned int maxDistance=smartcarlib::constants::sr04::kDefaultMaxDistance)
Constructs an SR04 ultrasonic sensor.
Definition: SR04.cpp:16
SR04.hpp
smartcarlib::constants::sr04::kDefaultMaxDistance
const unsigned int kDefaultMaxDistance
Definition: SR04.hpp:23
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
SR04::getMedianDistance
unsigned int getMedianDistance(uint8_t iterations=smartcarlib::constants::sr04::kDefaultIterations) override
Gets the median distance from the specified number of measurements.
Definition: SR04.cpp:61
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
Runtime::getPulseDuration
virtual unsigned long getPulseDuration(uint8_t pin, uint8_t state, unsigned long timeout)=0
Gets the incomming pulse length in microseconds starting from the nearest state, equivalent to pulseI...
smartcarlib::utils
Definition: Utilities.hpp:9
SR04::getDistance
unsigned int getDistance() override
Gets the distance measured by the sensor in centimeters.
Definition: SR04.cpp:40
smartcarlib::constants::distanceSensor
Definition: DistanceSensor.hpp:13
Runtime::setPinDirection
virtual void setPinDirection(uint8_t pin, uint8_t direction)=0
Set pin direction, equivalent of pinMode in Arduino.
smartcarlib::constants::sr04::kError
const unsigned int kError
Definition: SR04.hpp:24
smartcarlib::constants::distanceSensor::kMaxMedianMeasurements
const int kMaxMedianMeasurements
Definition: DistanceSensor.hpp:15