Smartcar Shield
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 
16 namespace smartcarlib
17 {
18 namespace constants
19 {
20 namespace sr04
21 {
22 const uint8_t kDefaultIterations = 5;
23 const unsigned int kDefaultMaxDistance = 70;
24 const unsigned int kError = 0;
25 } // namespace sr04
26 } // namespace constants
27 } // namespace smartcarlib
28 
29 class SR04 : public DistanceSensor
30 {
31 public:
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 
60 private:
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 
smartcarlib
Definition: DistanceCar.hpp:11
DistanceSensor
Definition: DistanceSensor.hpp:20
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
smartcarlib::constants::sr04::kDefaultMaxDistance
const unsigned int kDefaultMaxDistance
Definition: SR04.hpp:23
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
SR04::getDistance
unsigned int getDistance() override
Gets the distance measured by the sensor in centimeters.
Definition: SR04.cpp:40
smartcarlib::constants::sr04::kDefaultIterations
const uint8_t kDefaultIterations
Definition: SR04.hpp:22
smartcarlib::constants::sr04::kError
const unsigned int kError
Definition: SR04.hpp:24
SR04
Definition: SR04.hpp:29