Smartcar Shield
Loading...
Searching...
No Matches
SR04.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <stdint.h> // NOLINT(modernize-deprecated-headers)
12
13#include "../../../../runtime/Runtime.hpp"
14#include "../../DistanceSensor.hpp"
15
16namespace smartcarlib
17{
18namespace constants
19{
20namespace sr04
21{
22const uint8_t kDefaultIterations = 5;
23const unsigned int kDefaultMaxDistance = 70;
24const unsigned int kError = 0;
25} // namespace sr04
26} // namespace constants
27} // namespace smartcarlib
28
29class SR04 : public DistanceSensor
30{
31public:
48 SR04(Runtime& runtime,
49 uint8_t triggerPin,
50 uint8_t echoPin,
51 unsigned int maxDistance = smartcarlib::constants::sr04::kDefaultMaxDistance);
52
53 /* Check `DistanceSensor` interface for documentation */
54 unsigned int getDistance() override;
55
56 /* Check `DistanceSensor` interface for documentation */
57 unsigned int getMedianDistance(uint8_t iterations
59
60private:
61 const uint8_t kTriggerPin;
62 const uint8_t kEchoPin;
63 const unsigned int kMaxDistance;
64 const unsigned long kTimeout;
65 Runtime& mRuntime;
66 bool mAttached{ false };
67 const uint8_t kOutput;
68 const uint8_t kInput;
69 const uint8_t kLow;
70 const uint8_t kHigh;
71
72 void attach();
73};
74
A parent class for all sensors that conduct distance measurements.
An interface to serve as a wrapper for the native runtime environment, greatly inspired by the Arduin...
Definition Runtime.hpp:36
The SR04 (aka SRF05) is an inexpensive ultrasonic sensor controllable over two digital pins.
Definition SR04.hpp:30
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
unsigned int getDistance() override
Gets the distance measured by the sensor in centimeters.
Definition SR04.cpp:40
const uint8_t kDefaultIterations
Definition SR04.hpp:22
const unsigned int kError
Definition SR04.hpp:24
const unsigned int kDefaultMaxDistance
Definition SR04.hpp:23