Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
hcsr04.hpp
1 /*
2  Title --- sensor/hcsr04.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_SENSOR_HCSR04_HPP_
23 #define _RPI_HW_SENSOR_HCSR04_HPP_
24 
25 #include <vector>
26 #include <chrono>
27 
28 #include <sys/time.h>
29 
30 #include <rpi-hw/types.hpp>
31 #include <rpi-hw/exception.hpp>
32 #include <rpi-hw/math.hpp>
33 #include <rpi-hw/time.hpp>
34 
35 #include <rpi-hw/iface/base.hpp>
36 #include <rpi-hw/iface/output.hpp>
37 #include <rpi-hw/iface/input.hpp>
38 
39 // Use some external namespaces
40 using namespace std::chrono;
41 
42 namespace rpihw { // Begin main namespace
43 
44 /*!
45  @namespace rpihw::sensor
46  @brief Namespace of the sensors.
47 */
48 
49 namespace sensor { // Begin sensors namespace
50 
51 /*!
52  @class hcsr04
53  @brief Ultrasonic distance sensor HC-SR04.
54 
55  @example sensor/hcsr04.cpp
56 */
57 class hcsr04 {
58 
59 public:
60 
61  //! Used timer to measure the delay of pulse.
62  using timer = high_resolution_clock;
63 
64  /*!
65  @brief Constructor method.
66  @param[in] trigger The GPIO pin connected to the trigger.
67  @param[in] echo The GPIO pin connected to the echo pin.
68  */
69  hcsr04( uint8_t trigger, uint8_t echo );
70 
71  //! Destructor method.
72  virtual ~hcsr04();
73 
74  /*!
75  @brief Returns the elapsed time between sending and receiving back the pulse.
76  @return The elapsed time in milliseconds.
77  */
78  double timing();
79 
80  /*!
81  @brief Returns the distance from the sensor to an object or surface.
82  @return The distance in centimeters.
83  */
84  double ranging();
85 
86 protected:
87 
88  //! Output interface to the trigger.
90 
91  //! Input interface connected to the echo pin.
93 
94  //! Time of last pulse sending.
95  std::chrono::time_point< timer > m_last_sending;
96 };
97 
98 } // End of sensors namespace
99 
100 } // End of main namespace
101 
102 
103 // Include inline methods
104 #include <rpi-hw/sensor/hcsr04-inl.hpp>
105 
106 #endif /* _RPI_HW_SENSOR_HCSR04_HPP_ */
Output interface.
Definition: output.hpp:43
iface::output * m_trigger
Output interface to the trigger.
Definition: hcsr04.hpp:89
iface::input * m_echo
Input interface connected to the echo pin.
Definition: hcsr04.hpp:92
Input interface.
Definition: input.hpp:43
Ultrasonic distance sensor HC-SR04.
Definition: hcsr04.hpp:57
high_resolution_clock timer
Used timer to measure the delay of pulse.
Definition: hcsr04.hpp:62
std::chrono::time_point< timer > m_last_sending
Time of last pulse sending.
Definition: hcsr04.hpp:95