Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Distance_HC_SR04 Class Reference
Distance_HC_SR04 class. More...
#include <Distance_HC_SR04.h>
Public Member Functions | |
| Distance_HC_SR04 (PinName trig, PinName echo, uint32_t tout_us=TIMEOUT_DELAY_US, float coeff=CALC_COEFF, uint32_t tmin_us=TICKS_RANGE_MIN, uint32_t tmax_us=TICKS_RANGE_MAX) | |
| Applied rules: 0, 1,. | |
| void | trigger (void) |
| Start the measurement in asynchronous non-blocking mode. | |
| Distance_HC_SR04_state | getState (void) |
| Returns a state of measurement FSM in asynchronous non-blocking mode. | |
| void | reset (void) |
| Reset whole device and prepare for triggering of the next measurement in asynchronous non-blocking mode. | |
| uint32_t | getTicks (void) |
| Return measured duration of "echo" pulse in "COMPLETED" state. | |
| float | getDistance (void) |
| Return distance of the obstacle. | |
| float | getCoeff (void) |
| Return actual value of coefficient used to calculate distance from "echo" pulse duration. | |
| void | setCoeff (float coeff) |
| Set the actual value of coefficient used to calculate distance from "echo" pulse duration. | |
| float | measureDistance (void) |
| Measure (synchronous) distance of the object by reflected ultrasonic signal. | |
| uint32_t | measureTicks (void) |
| Measure (synchronous) ultrasonic signal travel time. | |
Detailed Description
Distance_HC_SR04 class.
Library for interfacing ultrasonic distance measurement device HC-SR04. Works in 2 basic modes:
- blocking (via measureTicks() or measureDistance())
- non-blocking asynchronous (via reset() + trigger() + getState() + getTicks() or getDistance())
Functionality includes detection of error in echo signal, detection of timeout and limits of reliable measurement.
Definition at line 48 of file Distance_HC_SR04.h.
Constructor & Destructor Documentation
| Distance_HC_SR04 | ( | PinName | trig, |
| PinName | echo, | ||
| uint32_t | tout_us = TIMEOUT_DELAY_US, |
||
| float | coeff = CALC_COEFF, |
||
| uint32_t | tmin_us = TICKS_RANGE_MIN, |
||
| uint32_t | tmax_us = TICKS_RANGE_MAX |
||
| ) |
Applied rules: 0, 1,.
Create Distance_HC_SR04 instance. Set FSM to IDLE state.
Definition at line 10 of file Distance_HC_SR04.cpp.
Member Function Documentation
| float getCoeff | ( | void | ) |
Return actual value of coefficient used to calculate distance from "echo" pulse duration.
- Returns:
- float Value of the coefficient used to multiply time in microseconds to get distance in mm.
Definition at line 155 of file Distance_HC_SR04.cpp.
| float getDistance | ( | void | ) |
Return distance of the obstacle.
Use in asynchronous non-blocking mode.
New measurement is not started. Uses result from last measurement.
- Returns:
- float Distance in milimiters calculated from measured duration of "echo" pulse.
Definition at line 145 of file Distance_HC_SR04.cpp.
| Distance_HC_SR04_state getState | ( | void | ) |
Returns a state of measurement FSM in asynchronous non-blocking mode.
This function is used in asynchronous non-blocking mode.
- Returns:
- Distance_HC_SR04_state FSM state value.
Definition at line 121 of file Distance_HC_SR04.cpp.
| uint32_t getTicks | ( | void | ) |
Return measured duration of "echo" pulse in "COMPLETED" state.
Use in asynchronous non-blocking mode.
New measurement is not started. Uses result from last measurement.
- Returns:
- uint32_t Measured duration of "echo" pulse in microseconds.
Definition at line 133 of file Distance_HC_SR04.cpp.
| float measureDistance | ( | void | ) |
Measure (synchronous) distance of the object by reflected ultrasonic signal.
Measure and return the distance in synchronous blocking mode.
Measure the distance from the object by reflected ultrasonic signal in synchronous (blocking) mode. In case of any error, returns 0. Error information can be retrieved via getState() method.
Example of usage:
Distance_HC_SR04 dist(PIN_TRIG, PIN_ECHO); float distance; while (true) { distance = dist.getDistance(); lcd.cls(); switch (dist.getState()) { case COMPLETED: lcd.printf("Dist.: %.3f", dist); lcd.locate(0, 1); lcd.printf("COMPLETED"); break; case TIMEOUT: lcd.printf("Dist.: ---"); lcd.locate(0, 1); lcd.printf("TIMEOUR"); break; case ERROR_SIG: lcd.printf("Dist.: ---"); lcd.locate(0, 1); lcd.printf("ERROR_SIG"); break; case OUT_OF_RANGE_MIN: lcd.printf("Dist.: ---"); lcd.locate(0, 1); lcd.printf("OUT_OF_RANGE_MIN"); break; case OUT_OF_RANGE_MAX: lcd.printf("Dist.: ---"); lcd.locate(0, 1); lcd.printf("OUT_OF_RANGE_MAX"); break; default: lcd.printf("Dist.: ---"); lcd.locate(0, 1); lcd.printf("OTHER"); break; } wait_ms(100); }
- Parameters:
-
-
- Returns:
- Object distance in milimeters. 0 in case of error.
- See also:
- -
- Note:
- - Calculation can be influenced by changing _coeff via setCoeff() or Distance_HC_SR04().
- Warning:
- -
New measurement is started and the code waits for measurement completion or timeout.
- Parameters:
-
void -
- Returns:
- float value of distance > 0.0f in case of a success, 0.0f in case of an error
Definition at line 61 of file Distance_HC_SR04.cpp.
| uint32_t measureTicks | ( | void | ) |
Measure (synchronous) ultrasonic signal travel time.
Measure and return "echo" pulse duration in synchronous blocking mode.
Measure the ultrasonic signal travel time in microseconds in synchronous (blocking) mode. In case of any error, returns 0. Error information can be retrieved via getState() method.
Example of usage:
Distance_HC_SR04 dist(PIN_TRIG, PIN_ECHO); uint32_t ticks_us; while (true) { ticks_us = dist.measureTicks(); lcd.cls(); switch (dist.getState()) { case COMPLETED: lcd.printf("Dist.: %u", ticks_us); lcd.locate(0, 1); lcd.printf("COMPLETED"); break; case TIMEOUT: lcd.printf("Dist.: ---"); lcd.locate(0, 1); lcd.printf("TIMEOUR"); break; case ERROR_SIG: lcd.printf("Dist.: ---"); lcd.locate(0, 1); lcd.printf("ERROR_SIG"); break; case OUT_OF_RANGE_MIN: lcd.printf("Dist.: ---"); lcd.locate(0, 1); lcd.printf("OUT_OF_RANGE_MIN"); break; case OUT_OF_RANGE_MAX: lcd.printf("Dist.: ---"); lcd.locate(0, 1); lcd.printf("OUT_OF_RANGE_MAX"); break; default: lcd.printf("Dist.: ---"); lcd.locate(0, 1); lcd.printf("OTHER"); break; } wait_ms(100); }
- Parameters:
-
-
- Returns:
- Ultrasonic signal total travel time (including reflected signal) in microseconds. 0 in case of error.
- See also:
- -
- Note:
- -
- Warning:
- -
New measurement is started and the code waits for measurement completion or timeout.
- Parameters:
-
void -
- Returns:
- uint32_t value of distance > 0 in case of a success, 0 in case of an error
Definition at line 26 of file Distance_HC_SR04.cpp.
| void reset | ( | void | ) |
Reset whole device and prepare for triggering of the next measurement in asynchronous non-blocking mode.
FSM set to IDLE state.
Definition at line 71 of file Distance_HC_SR04.cpp.
| void setCoeff | ( | float | coeff ) |
Set the actual value of coefficient used to calculate distance from "echo" pulse duration.
- Parameters:
-
coeff Coeficient for multiplication with pulse duration in microseconds
- Returns:
- void -
Definition at line 166 of file Distance_HC_SR04.cpp.
| void trigger | ( | void | ) |
Start the measurement in asynchronous non-blocking mode.
Generates pulse on "trig" output and checks proper value of "echo" signal. Sets FSM STATE and exits.
If "echo" is low right after "trig" pulse is generated, sets FSM state to "STARTED". In other case FSM sate is set to "ERROR_SIG".
Definition at line 89 of file Distance_HC_SR04.cpp.
Generated on Mon Jul 18 2022 21:55:06 by
1.7.2