mirror of https://github.com/joan2937/pigpio
220 lines
7.8 KiB
HTML
220 lines
7.8 KiB
HTML
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
<head>
|
|
<meta name="description" content="Raspberry Pi Reg. C GPIO library and Python GPIO module and shell command utilities to control the GPIO, including SPI, I2C, and serial links." />
|
|
<meta name="keywords" content="raspberry, pi, C, Python, GPIO, library, shell, command, utilities, module, SPI, I2C, serial" />
|
|
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
|
|
<title>pigpio library</title>
|
|
<link rel="stylesheet" type="text/css" href="scripts/index.css">
|
|
<link rel="icon" href="favicon.ico" type="image/x-icon">
|
|
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
|
|
</head>
|
|
<body>
|
|
|
|
<table style="padding:0px; border:0px; margin:0px; width:780px; background-color:#e0e0e0;">
|
|
<td style="background:#EAF2E6 url('images/sidebar.gif') repeat-y; width:35px; height:100%"></td>
|
|
<td>
|
|
<table>
|
|
<div style="background:url('images/topbar.gif') repeat-x; height: 70px; font-size:1.5em; vertical-align: top;"><a href="index.html"><img src="images/pigpio-logo.gif" border="0" /></a>pigpio library</div>
|
|
</table>
|
|
<table><div>
|
|
<td><img src="images/nano.jpg" width="250"></td>
|
|
<td><img src="images/pot.jpg" width="250"></td>
|
|
<td><img src="images/remote-1.jpg" width="250"></td>
|
|
</div></table>
|
|
<table>
|
|
<td style="vertical-align: top; background-color: #98bf21;"><a class="l1" href="index.html">pigpio</a>
|
|
<a class="l1" href="cif.html">pigpio C I/F</a>
|
|
<a class="l1" href="pigpiod.html">pigpiod</a>
|
|
<a class="l1" href="pdif2.html">pigpiod C I/F</a>
|
|
<a class="l1" href="python.html">Python</a>
|
|
<a class="l1" href="pigs.html">pigs</a>
|
|
<a class="l1" href="piscope.html">piscope</a>
|
|
<a class="l1" href="misc.html">Misc</a>
|
|
<a class="l1" href="examples.html">Examples</a>
|
|
<a class="l1" href="download.html">Download</a>
|
|
<a class="l1" href="faq.html">FAQ</a>
|
|
<a class="l1" href="sitemap.html">Site Map</a>
|
|
</td>
|
|
<td><center><h2>Sonar Ranger Example</h2></center>
|
|
<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>
|
|
</td>
|
|
</table>
|
|
<div style="vertical-align: center; text-align: center; background-color:#98bf21; font-size:0.8em; height:30px"><a class="l2" href="index.html">[pigpio]</a>
|
|
<a class="l2" href="cif.html">[pigpio C I/F]</a>
|
|
<a class="l2" href="pigpiod.html">[pigpiod]</a>
|
|
<a class="l2" href="pdif2.html">[pigpiod C I/F]</a>
|
|
<a class="l2" href="python.html">[Python]</a>
|
|
<a class="l2" href="pigs.html">[pigs]</a>
|
|
<a class="l2" href="piscope.html">[piscope]</a>
|
|
<a class="l2" href="misc.html">[Misc]</a>
|
|
<a class="l2" href="examples.html">[Examples]</a>
|
|
<a class="l2" href="download.html">[Download]</a>
|
|
<a class="l2" href="faq.html">[FAQ]</a>
|
|
<a class="l2" href="sitemap.html">[Site Map]</a>
|
|
</div>
|
|
<table><tr>
|
|
<td style="width: 200px"><div style="text-align: left;"><small>© 2012-2020</small></div></td>
|
|
<td style="width: 350px"><div style="text-align: center;">e-mail: pigpio @ abyz.me.uk</div></td>
|
|
<td style="width: 200px"><div style="text-align: right;"><small>Updated: 30/04/2020</small></div></td>
|
|
</tr></table>
|
|
</td>
|
|
</table>
|
|
</body>
|
|
</html>
|