Skip to content

Commit 6d0fd1b

Browse files
authored
Add sonar example (#102)
1 parent d5c93cd commit 6d0fd1b

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

APIs_RTOS/Sonar/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Sonar example
2+
3+
This example shows the how to make use of HC-SR04 Sonar.
4+
5+
6+
**Note:** By default `D5` and `D6` pins are used to control Sonar.

APIs_RTOS/Sonar/main.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
/**
8+
* Sonar class for the HC-SR04
9+
*/
10+
class Sonar {
11+
DigitalOut trigger;
12+
InterruptIn echo; // calls a callback when a pin changes
13+
Timer timer;
14+
Timeout timeout; // calls a callback once when a timeout expires
15+
Ticker ticker; // calls a callback repeatedly with a timeout
16+
int32_t begin;
17+
int32_t end;
18+
float distance;
19+
20+
public:
21+
/**
22+
* Sonar constructor
23+
* Creates a sonar object on a set of provided pins
24+
* @param trigger_pin Pin used to trigger reads from the sonar device
25+
* @param echo_pin Pin used to receive the sonar's distance measurement
26+
*/
27+
Sonar(PinName trigger_pin, PinName echo_pin) : trigger(trigger_pin), echo(echo_pin)
28+
{
29+
trigger = 0;
30+
distance = -1;
31+
32+
echo.rise(callback(this, &Sonar::echo_in)); // Attach handler to the rising interruptIn edge
33+
echo.fall(callback(this, &Sonar::echo_fall)); // Attach handler to the falling interruptIn edge
34+
}
35+
36+
/**
37+
* Start the background task to trigger sonar reads every 100ms
38+
*/
39+
void start(void)
40+
{
41+
ticker.attach(callback(this, &Sonar::background_read), 0.01f);
42+
}
43+
44+
/**
45+
* Stop the background task that triggers sonar reads
46+
*/
47+
void stop(void)
48+
{
49+
ticker.detach();
50+
}
51+
52+
/**
53+
* Interrupt pin rising edge interrupt handler. Reset and start timer
54+
*/
55+
void echo_in(void)
56+
{
57+
timer.reset();
58+
timer.start();
59+
begin = timer.read_us();
60+
}
61+
62+
/**
63+
* Interrupt pin falling edge interrupt handler. Read and disengage timer.
64+
* Calculate raw echo pulse length
65+
*/
66+
void echo_fall(void)
67+
{
68+
end = timer.read_us();
69+
timer.stop();
70+
distance = end - begin;
71+
}
72+
73+
/**
74+
* Wrapper function to set the trigger pin low. Callbacks cannot take in both object and argument pointers.
75+
* See use of this function in background_read().
76+
*/
77+
void trigger_toggle(void)
78+
{
79+
trigger = 0;
80+
}
81+
82+
/**
83+
* Background callback task attached to the periodic ticker that kicks off sonar reads
84+
*/
85+
void background_read(void)
86+
{
87+
trigger = 1;
88+
timeout.attach(callback(this, &Sonar::trigger_toggle), 10.0e-6);
89+
}
90+
91+
/**
92+
* Public read function that returns the scaled distance result in cm
93+
*/
94+
float read(void)
95+
{
96+
return distance / 58.0f;
97+
}
98+
};
99+
100+
101+
int main()
102+
{
103+
// Create sonar object on pins D5 and D6
104+
Sonar sonar(D5, D6);
105+
// Begin background thread sonar acquires
106+
sonar.start();
107+
108+
while (1) {
109+
ThisThread::sleep_for(100);
110+
// Periodically print results from sonar object
111+
printf("%f\r\n", sonar.read());
112+
}
113+
}

0 commit comments

Comments
 (0)