FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ > Class Template Reference

#include <fastarduino/devices/sonar.h>

Inheritance diagram for devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >:
Collaboration diagram for devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >:

Public Types

using RTT = timer::RTT< NTIMER_ >
 The type of timer::RTT used by this sonar instance. More...
 
- Public Types inherited from devices::sonar::AbstractSonar< NTIMER_ >
using RTT = timer::RTT< NTIMER_ >
 The type of timer::RTT used by this sonar instance. More...
 

Public Member Functions

 HCSR04 (const RTT &rtt)
 Construct a new a sonar sensor handler. More...
 
uint16_t echo_us (uint16_t timeout_ms)
 Send a trigger pulse on this sonar and wait until an echo pulse is received, or timeout_ms has elapsed. More...
 
void async_echo (uint16_t timeout_ms, bool trigger=true)
 Send a trigger pulse on this sonar and return immediately, without waiting for the echo pulse. More...
 
uint16_t await_echo_us (uint16_t timeout_ms)
 Wait until an echo pulse is received, or timeout_ms has elapsed. More...
 
- Public Member Functions inherited from devices::sonar::AbstractSonar< NTIMER_ >
bool ready () const
 Indicate if an echo pulse measure is ready to read. More...
 
uint16_t latest_echo_us () const
 Get the latest measured echo pulse duration. More...
 

Static Public Attributes

static constexpr const board::DigitalPin TRIGGER = TRIGGER_
 The board::DigitalPin connected to the sensor trigger pin. More...
 
static constexpr const board::DigitalPin ECHO = ECHO_
 The board::DigitalPin connected to the sensor echo pin. More...
 
static constexpr const SonarType SONAR_TYPE = SONAR_TYPE_
 The mode used by this class to calculate the echo pin pulse duration. More...
 
static constexpr const uint16_t MAX_RANGE_M = 4
 The approximate maximum range, in meters, that this sonar sensor supports. More...
 
static constexpr const uint16_t DEFAULT_TIMEOUT_MS = MAX_RANGE_M * 2 * ONE_MILLI_32 / SPEED_OF_SOUND + 1
 The default timeout duration, in milliseconds, to use if you want to cover the maximum range of the sensor. More...
 

Friends

struct isr_handler
 

Detailed Description

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
class devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >
See also
BLOCKING_HCSR04
ASYNC_INT_HCSR04
ASYNC_PCINT_HCSR04

Definition at line 756 of file sonar.h.

Member Typedef Documentation

◆ RTT

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
using devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >::RTT = timer::RTT<NTIMER_>

The type of timer::RTT used by this sonar instance.

Definition at line 760 of file sonar.h.

Constructor & Destructor Documentation

◆ HCSR04()

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >::HCSR04 ( const RTT rtt)
inlineexplicit

Construct a new a sonar sensor handler.

Parameters
rtta reference to an existing timer::RTT for echo pulse duration counting; this RTT shall be started before using any other methods of this sonar.

Definition at line 802 of file sonar.h.

Member Function Documentation

◆ echo_us()

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
uint16_t devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >::echo_us ( uint16_t  timeout_ms)
inline

Send a trigger pulse on this sonar and wait until an echo pulse is received, or timeout_ms has elapsed.

This method is blocking, whatever the value of SONAR_TYPE for this sonar. If you want to start a sonar ranging asynchronously, then you should use async_echo instead.

Parameters
timeout_msthe timeout, in milliseconds, after which the method will return if no echo pulse has been received
Returns
the echo pulse duration in microseconds
Return values
0if no echo pulse was received before timeout_ms elapsed
See also
async_echo()

Definition at line 822 of file sonar.h.

◆ async_echo()

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
void devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >::async_echo ( uint16_t  timeout_ms,
bool  trigger = true 
)
inline

Send a trigger pulse on this sonar and return immediately, without waiting for the echo pulse.

There are several ways then to get the echo pulse duration:

  • call await_echo_us() and then wait for the echo pulse to be received
  • call ready() to check if echo pulse has been received already and then call latest_echo_us() to get the echo pulse duration
  • use callbacks to be notified when the echo pulse is received, then latest_echo_us() can be called to obtain the pulse duration
Parameters
timeout_msthe timeout, in milliseconds, after which the ranging will stop if no echo pulse has been received
triggerindicate if the method should generate a trigger pulse on the TRIGGER pin; by default it is true, but you may want to use false if you have several HCSR04 sensors, which you want to trigger all at the same time (i.e. all their trigger pins are connected to the same MCU pin).
See also
await_echo_us()
ready()
latest_echo_us()

Definition at line 850 of file sonar.h.

◆ await_echo_us()

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
uint16_t devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >::await_echo_us ( uint16_t  timeout_ms)
inline

Wait until an echo pulse is received, or timeout_ms has elapsed.

You must call async_echo() before calling this method. This method is blocking, whatever the value of SONAR_TYPE for this sonar.

Parameters
timeout_msthe timeout, in milliseconds, after which the method will return if no echo pulse has been received
Returns
the echo pulse duration in microseconds
Return values
0if no echo pulse was received before timeout_ms elapsed
See also
async_echo()

Definition at line 869 of file sonar.h.

Friends And Related Function Documentation

◆ isr_handler

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
friend struct isr_handler
friend

Definition at line 904 of file sonar.h.

Member Data Documentation

◆ TRIGGER

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
constexpr const board::DigitalPin devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >::TRIGGER = TRIGGER_
staticconstexpr

The board::DigitalPin connected to the sensor trigger pin.

Definition at line 762 of file sonar.h.

◆ ECHO

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
constexpr const board::DigitalPin devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >::ECHO = ECHO_
staticconstexpr

The board::DigitalPin connected to the sensor echo pin.

Definition at line 764 of file sonar.h.

◆ SONAR_TYPE

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
constexpr const SonarType devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >::SONAR_TYPE = SONAR_TYPE_
staticconstexpr

The mode used by this class to calculate the echo pin pulse duration.

Definition at line 766 of file sonar.h.

◆ MAX_RANGE_M

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
constexpr const uint16_t devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >::MAX_RANGE_M = 4
staticconstexpr

The approximate maximum range, in meters, that this sonar sensor supports.

Any obstacle beyond this distance will generate no echo pulse from the sensor.

Definition at line 783 of file sonar.h.

◆ DEFAULT_TIMEOUT_MS

template<board::Timer NTIMER_, board::DigitalPin TRIGGER_, board::DigitalPin ECHO_, SonarType SONAR_TYPE_ = SonarType::BLOCKING>
constexpr const uint16_t devices::sonar::HCSR04< NTIMER_, TRIGGER_, ECHO_, SONAR_TYPE_ >::DEFAULT_TIMEOUT_MS = MAX_RANGE_M * 2 * ONE_MILLI_32 / SPEED_OF_SOUND + 1
staticconstexpr

The default timeout duration, in milliseconds, to use if you want to cover the maximum range of the sensor.

Using any greater timeout value would be pointless.

See also
MAX_RANGE_M
echo_us()
await_echo_us()
async_echo()

Definition at line 794 of file sonar.h.


The documentation for this class was generated from the following file: