mirror of https://github.com/joan2937/pigpio
156 lines
4.7 KiB
Plaintext
156 lines
4.7 KiB
Plaintext
|
|
<p>The following code shows a method of reading a class of sonar
|
|
rangers. These rangers requires a trigger pulse.
|
|
Shortly after receiving a trigger they transmit a noise pulse and
|
|
set the echo line high. When the echo is received the echo
|
|
line is set low.<br></p>
|
|
<h3>SETUP</h3>
|
|
<img src="images/son-fritz.png" alt="fritzing diagram" style=
|
|
"width: 200px; height: 600px;" align="left" hspace="10"><br>
|
|
<br>
|
|
<br>
|
|
<br>
|
|
<br>
|
|
The ranger used is a SRF05 (check the pinouts, there are many
|
|
variants).<br>
|
|
<br>
|
|
The fritzing diagram shows the back of the ranger, i.e. pin 1 is
|
|
the rightmost.<br>
|
|
<br>
|
|
Pin 1 is 5V.<br>
|
|
Pin 2 is the trigger line.<br>
|
|
Pin 3 is the echo line.<br>
|
|
Pin 4 is out (unused).<br>
|
|
Pin 5 is ground.<br>
|
|
<br clear="all">
|
|
<p><img src="images/son-photo.jpg" style=
|
|
"width: 500px; height: 376px;" alt="photo of set-up"></p>
|
|
<h3>CODE</h3>
|
|
<code>#include <stdio.h><br>
|
|
<br>
|
|
#include <pigpio.h><br>
|
|
<br>
|
|
/*<br>
|
|
<br>
|
|
P1 Name gpio used for<br>
|
|
<br>
|
|
2 5V ---
|
|
5V<br>
|
|
6 GND ---
|
|
Ground<br>
|
|
24 CE0 8
|
|
Sonar echo<br>
|
|
26 CE1 7
|
|
Sonar trigger<br>
|
|
<br>
|
|
*/<br>
|
|
<br>
|
|
#define SONAR_TRIGGER 7<br>
|
|
#define SONAR_ECHO 8<br>
|
|
<br>
|
|
/* forward prototypes */<br>
|
|
<br>
|
|
void sonarTrigger(void);<br>
|
|
<br>
|
|
void sonarEcho(int gpio, int level, uint32_t tick);<br>
|
|
<br>
|
|
int main(int argc, char *argv[])<br>
|
|
{<br>
|
|
if (gpioInitialise()<0) return 1;<br>
|
|
<br>
|
|
gpioSetMode(SONAR_TRIGGER, PI_OUTPUT);<br>
|
|
gpioWrite (SONAR_TRIGGER, PI_OFF);<br>
|
|
<br>
|
|
gpioSetMode(SONAR_ECHO,
|
|
PI_INPUT);<br>
|
|
<br>
|
|
/* update sonar 20 times a second, timer #0 */<br>
|
|
<br>
|
|
gpioSetTimerFunc(0, 50, sonarTrigger); /* every 50ms
|
|
*/<br>
|
|
<br>
|
|
/* monitor sonar echos */<br>
|
|
<br>
|
|
gpioSetAlertFunc(SONAR_ECHO, sonarEcho);<br>
|
|
<br>
|
|
while (1) sleep(1);<br>
|
|
<br>
|
|
gpioTerminate();<br>
|
|
<br>
|
|
return 0;<br>
|
|
}<br>
|
|
<br>
|
|
void sonarTrigger(void)<br>
|
|
{<br>
|
|
/* trigger a sonar reading */<br>
|
|
<br>
|
|
gpioWrite(SONAR_TRIGGER, PI_ON);<br>
|
|
<br>
|
|
gpioDelay(10); /* 10us trigger pulse */<br>
|
|
<br>
|
|
gpioWrite(SONAR_TRIGGER, PI_OFF);<br>
|
|
}<br>
|
|
<br>
|
|
void sonarEcho(int gpio, int level, uint32_t tick)<br>
|
|
{<br>
|
|
static uint32_t startTick, firstTick=0;<br>
|
|
<br>
|
|
int diffTick;<br>
|
|
<br>
|
|
if (!firstTick) firstTick = tick;<br>
|
|
<br>
|
|
if (level == PI_ON)<br>
|
|
{<br>
|
|
startTick = tick;<br>
|
|
}<br>
|
|
else if (level == PI_OFF)<br>
|
|
{<br>
|
|
diffTick = tick - startTick;<br>
|
|
<br>
|
|
printf("%u %u\ ", tick-firstTick,
|
|
diffTick);<br>
|
|
}<br>
|
|
}<br></code>
|
|
<h3>BUILD</h3>
|
|
<code>cc -o sonar sonar.c -lpigpio -lrt -lpthread<br></code>
|
|
<h3>RUN</h3>
|
|
<code>sudo ./sonar >sonar.dat &</code><br>
|
|
<br>
|
|
While the program is running you can capture the waveform using the
|
|
notification feature built in to pigpio. Issue the following
|
|
commands on the Pi.<br>
|
|
<br>
|
|
<code>pigs no<br>
|
|
pig2vcd </dev/pigpio0 >sonar.vcd &<br>
|
|
pigs nb 0 0x180 # set bits for gpios 7 and 8<br></code>
|
|
<p>Move an object in front of the sonar ranger for a few
|
|
seconds.<br></p>
|
|
<code>pigs nc 0</code><br>
|
|
<p>The file sonar.vcd will contain the captured waveform, which can
|
|
be viewed using GTKWave.</p>
|
|
<p>Overview</p>
|
|
<img src="images/son-wave-1.png" style=
|
|
"width: 600px; height: 100px;" alt="LDR waveform 1"><br>
|
|
<p>Reading circa every 10ms<br></p>
|
|
<img src="images/son-wave-2.png" style=
|
|
"width: 600px; height: 100px;" alt="Sonar waveform 2"><br>
|
|
<p>One reading, circa 400us<br></p>
|
|
<img src="images/son-wave-3.png" style=
|
|
"width: 600px; height: 100px;" alt="Sonar waveform 3"><br>
|
|
<p>another</p>
|
|
<img style="width: 600px; height: 100px;" alt="Sonar waveform 4"
|
|
src="images/son-wave-4.png"><br>
|
|
<p>The file sonar.dat will contain pairs of timestamps and echo
|
|
length (in us). The following script will convert the
|
|
timestamps into seconds.<span style=
|
|
"font-style: italic;"><br></span></p>
|
|
<p><code>awk '{print $1/1000000, $2}' sonar.dat
|
|
>sonar-secs.dat</code></p>
|
|
<p>Gnuplot is a useful tool to graph data.<br></p>
|
|
plot 'sonar-secs.dat' title 'Sonar'<br>
|
|
<p><img src="images/son-gnup-1.png" style=
|
|
"width: 600px; height: 321px;" alt="gnuplot 1"><br>
|
|
plot [10:25] 'sonar-secs.dat' title 'Sonar'<br></p>
|
|
<p><img src="images/son-gnup-2.png" style=
|
|
"width: 600px; height: 321px;" alt="gnuplot 1"></p>
|