mirror of https://github.com/joan2937/pigpio
310 lines
10 KiB
HTML
310 lines
10 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/stepper.jpg" width="250"></td>
|
|
<td><img src="images/transistors.jpg" width="250"></td>
|
|
<td><img src="images/wires.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>Arduino Motor Shield</h2></center>
|
|
The following example demonstrates the use of an Arduino shield
|
|
from the Rasperry Pi.<br>
|
|
<br>
|
|
The shield used is a clone of the Adafruit motor shield. See
|
|
<a href="http://shieldlist.org/adafruit/motor">shieldlist.org</a>
|
|
for details.<br>
|
|
<br>
|
|
For the demonstration DC motors 3 and 4 are being driven forwards
|
|
and backwards with changing speeds (speeds are controlled via
|
|
PWM).<br>
|
|
<br>
|
|
Seven connections are made between the Pi and the shield.
|
|
Four to latch the motor states (latch, enable, data, clock); Two to
|
|
control motor speed (PWM 3 and 4); and ground.<br>
|
|
<br>
|
|
The code used was ported from the Adafruit Arduino code and
|
|
converted to use the pigpio library. Only the DC motor code
|
|
was ported.<br>
|
|
<br>
|
|
A video of the shield in use is available at <a href=
|
|
"http://youtu.be/Z_l3C246oVI">youtube.com</a><br>
|
|
<code><br>
|
|
#include <stdio.h></code> <code><br>
|
|
<br>
|
|
#include <pigpio.h><br>
|
|
<br>
|
|
/*<br>
|
|
This code may be used to drive the Adafruit (or
|
|
clones) Motor Shield.<br>
|
|
<br>
|
|
The code as written only supports DC motors.<br>
|
|
<br>
|
|
http://shieldlist.org/adafruit/motor<br>
|
|
<br>
|
|
The shield pinouts are<br>
|
|
<br>
|
|
D12 MOTORLATCH<br>
|
|
D11 PMW motor 1<br>
|
|
D10 Servo 1<br>
|
|
D9 Servo 2<br>
|
|
D8 MOTORDATA<br>
|
|
<br>
|
|
D7 MOTORENABLE<br>
|
|
D6 PWM motor 4<br>
|
|
D5 PWM motor 3<br>
|
|
D4 MOTORCLK<br>
|
|
D3 PWM motor 2<br>
|
|
<br>
|
|
The motor states (forward, backward, brake, release)
|
|
are encoded using the<br>
|
|
MOTOR_ latch pins. This saves four gpios.<br>
|
|
*/<br>
|
|
<br>
|
|
typedef unsigned char uint8_t;<br>
|
|
<br>
|
|
#define BIT(bit) (1 << (bit))<br>
|
|
<br>
|
|
/* assign gpios to drive the shield pins */<br>
|
|
<br>
|
|
/*
|
|
Shield Pi */<br>
|
|
<br>
|
|
#define MOTORLATCH 14<br>
|
|
#define MOTORCLK 24<br>
|
|
#define MOTORENABLE 25<br>
|
|
#define MOTORDATA 15<br>
|
|
<br>
|
|
#define MOTOR_3_PWM 7<br>
|
|
#define MOTOR_4_PWM 8<br>
|
|
<br>
|
|
/*<br>
|
|
The only other connection needed between the Pi and
|
|
the shield<br>
|
|
is ground to ground. I used Pi P1-6 to shield gnd
|
|
(next to D13).<br>
|
|
*/<br>
|
|
<br>
|
|
/* assignment of motor states to latch */<br>
|
|
<br>
|
|
#define MOTOR1_A 2<br>
|
|
#define MOTOR1_B 3<br>
|
|
#define MOTOR2_A 1<br>
|
|
#define MOTOR2_B 4<br>
|
|
#define MOTOR4_A 0<br>
|
|
#define MOTOR4_B 6<br>
|
|
#define MOTOR3_A 5<br>
|
|
#define MOTOR3_B 7<br>
|
|
<br>
|
|
#define FORWARD 1<br>
|
|
#define BACKWARD 2<br>
|
|
#define BRAKE 3<br>
|
|
#define RELEASE 4<br>
|
|
<br>
|
|
static uint8_t latch_state;<br>
|
|
<br>
|
|
void latch_tx(void)<br>
|
|
{<br>
|
|
unsigned char i;<br>
|
|
<br>
|
|
gpioWrite(MOTORLATCH, PI_LOW);<br>
|
|
<br>
|
|
gpioWrite(MOTORDATA, PI_LOW);<br>
|
|
<br>
|
|
for (i=0; i<8; i++)<br>
|
|
{<br>
|
|
gpioDelay(10); // 10 micros
|
|
delay<br>
|
|
<br>
|
|
gpioWrite(MOTORCLK, PI_LOW);<br>
|
|
<br>
|
|
if (latch_state & BIT(7-i))
|
|
gpioWrite(MOTORDATA, PI_HIGH);<br>
|
|
|
|
else
|
|
gpioWrite(MOTORDATA, PI_LOW);<br>
|
|
<br>
|
|
gpioDelay(10); // 10 micros
|
|
delay<br>
|
|
<br>
|
|
gpioWrite(MOTORCLK, PI_HIGH);<br>
|
|
}<br>
|
|
<br>
|
|
gpioWrite(MOTORLATCH, PI_HIGH);<br>
|
|
}<br>
|
|
<br>
|
|
void init(void)<br>
|
|
{<br>
|
|
latch_state = 0;<br>
|
|
<br>
|
|
latch_tx();<br>
|
|
<br>
|
|
gpioWrite(MOTORENABLE, PI_LOW);<br>
|
|
}<br>
|
|
<br>
|
|
void DCMotorInit(uint8_t num)<br>
|
|
{<br>
|
|
switch (num)<br>
|
|
{<br>
|
|
case 1: latch_state &=
|
|
~BIT(MOTOR1_A) & ~BIT(MOTOR1_B); break;<br>
|
|
case 2: latch_state &=
|
|
~BIT(MOTOR2_A) & ~BIT(MOTOR2_B); break;<br>
|
|
case 3: latch_state &=
|
|
~BIT(MOTOR3_A) & ~BIT(MOTOR3_B); break;<br>
|
|
case 4: latch_state &=
|
|
~BIT(MOTOR4_A) & ~BIT(MOTOR4_B); break;<br>
|
|
default: return;<br>
|
|
}<br>
|
|
<br>
|
|
latch_tx();<br>
|
|
<br>
|
|
printf("Latch=%08X\ ", latch_state);<br>
|
|
}<br>
|
|
<br>
|
|
void DCMotorRun(uint8_t motornum, uint8_t cmd)<br>
|
|
{<br>
|
|
uint8_t a, b;<br>
|
|
<br>
|
|
switch (motornum)<br>
|
|
{<br>
|
|
case 1: a = MOTOR1_A; b = MOTOR1_B;
|
|
break;<br>
|
|
case 2: a = MOTOR2_A; b = MOTOR2_B;
|
|
break;<br>
|
|
case 3: a = MOTOR3_A; b = MOTOR3_B;
|
|
break;<br>
|
|
case 4: a = MOTOR4_A; b = MOTOR4_B;
|
|
break;<br>
|
|
default: return;<br>
|
|
}<br>
|
|
<br>
|
|
switch (cmd)<br>
|
|
{<br>
|
|
case FORWARD: latch_state
|
|
|= BIT(a); latch_state &= ~BIT(b); break;<br>
|
|
case BACKWARD: latch_state &=
|
|
~BIT(a); latch_state |= BIT(b); break;<br>
|
|
case RELEASE: latch_state
|
|
&= ~BIT(a); latch_state &= ~BIT(b); break;<br>
|
|
default: return;<br>
|
|
}<br>
|
|
<br>
|
|
latch_tx();<br>
|
|
<br>
|
|
printf("Latch=%08X\ ", latch_state);<br>
|
|
}<br>
|
|
<br>
|
|
int main (int argc, char *argv[])<br>
|
|
{<br>
|
|
int i;<br>
|
|
<br>
|
|
if (gpioInitialise()<0) return 1;<br>
|
|
<br>
|
|
gpioSetMode(MOTORLATCH, PI_OUTPUT);<br>
|
|
gpioSetMode(MOTORENABLE, PI_OUTPUT);<br>
|
|
gpioSetMode(MOTORDATA, PI_OUTPUT);<br>
|
|
gpioSetMode(MOTORCLK,
|
|
PI_OUTPUT);<br>
|
|
<br>
|
|
gpioSetMode(MOTOR_3_PWM, PI_OUTPUT);<br>
|
|
gpioSetMode(MOTOR_4_PWM, PI_OUTPUT);<br>
|
|
<br>
|
|
gpioPWM(MOTOR_3_PWM, 0);<br>
|
|
gpioPWM(MOTOR_4_PWM, 0);<br>
|
|
<br>
|
|
init();<br>
|
|
<br>
|
|
for (i=60; i<160; i+=20)<br>
|
|
{<br>
|
|
gpioPWM(MOTOR_3_PWM, i);<br>
|
|
gpioPWM(MOTOR_4_PWM, 220-i);<br>
|
|
<br>
|
|
DCMotorRun(3, FORWARD);<br>
|
|
DCMotorRun(4, BACKWARD);<br>
|
|
<br>
|
|
sleep(2);<br>
|
|
<br>
|
|
DCMotorRun(3, RELEASE);<br>
|
|
DCMotorRun(4, RELEASE);<br>
|
|
<br>
|
|
sleep(2);<br>
|
|
<br>
|
|
gpioPWM(MOTOR_4_PWM, i);<br>
|
|
gpioPWM(MOTOR_3_PWM, 220-i);<br>
|
|
<br>
|
|
DCMotorRun(3, BACKWARD);<br>
|
|
DCMotorRun(4, FORWARD);<br>
|
|
<br>
|
|
sleep(2);<br>
|
|
<br>
|
|
DCMotorRun(3, RELEASE);<br>
|
|
DCMotorRun(4, RELEASE);<br>
|
|
<br>
|
|
sleep(2);<br>
|
|
}<br>
|
|
<br>
|
|
gpioPWM(MOTOR_4_PWM, 0);<br>
|
|
gpioPWM(MOTOR_3_PWM, 0);<br>
|
|
<br>
|
|
DCMotorRun(3, RELEASE);<br>
|
|
DCMotorRun(4, RELEASE);<br>
|
|
<br>
|
|
gpioTerminate();<br>
|
|
}<br></code>
|
|
</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: 29/04/2020</small></div></td>
|
|
</tr></table>
|
|
</td>
|
|
</table>
|
|
</body>
|
|
</html>
|