Chapter 13 Motor & Driver
150
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
d e f
setup
( ):
g l obal
p
GPIO
.
setmode
(
GPIO
.
BOARD
)
# set mode for pin
GPIO
.
setup
(
motoRPin1
,
GPIO
.
OUT
)
GPIO
.
setup
(
motoRPin2
,
GPIO
.
OUT
)
GPIO
.
setup
(
enablePin
,
GPIO
.
OUT
)
p
=
GPIO
.
PWM
(
enablePin
,
1000
)
# creat PWM
p
.
start
(
0
)
#mapNUM function: map the value from a range of mapping to another range.
d e f
mapNUM
(
value
,
fromLow
,
fromHigh
,
toLow
,
toHigh
):
r e turn
(
toHigh
-
toLow
)*(
value
-
fromLow
)
/
(
fromHigh
-
fromLow
)
+
toLow
#motor function: determine the direction and speed of the motor according to the ADC
value to be input.
d e f
motor
(
ADC
):
value
=
ADC
-
128
i f
(
value
>
0
) :
GPIO
.
output
(
motoRPin1
,
GPIO
.
HIGH
)
GPIO
.
output
(
motoRPin2
,
GPIO
.
LOW
)
p r int
(
'Turn Forward...'
)
e l if
(
value
<
0
) :
GPIO
.
output
(
motoRPin1
,
GPIO
.
LOW
)
GPIO
.
output
(
motoRPin2
,
GPIO
.
HIGH
)
p r int
(
'Turn Backward...'
)
e l se
:
GPIO
.
output
(
motoRPin1
,
GPIO
.
LOW
)
GPIO
.
output
(
motoRPin2
,
GPIO
.
LOW
)
p r int
(
'Motor Stop...'
)
p
.
start
(
mapNUM
(
abs
(
value
),
0
,
128
,
0
,
100
))
p r int
(
'The PWM duty cycle is %d%%\n'
%(
abs
(
value
)*
100
/
127
))
#print PMW duty cycle.
d e f
loop
( ):
w h ile
T r ue
:
value
=
analogRead
(
0
)
p r int
(
'ADC Value : %d'
% (
value
))
motor
(
value
)
time
.
sleep
(
0.01
)
d e f
destroy
():
bus
.
close
()
GPIO
.
cleanup
()
i f
__name__
= =
'__main__'
:
Содержание Ultimate Starter Kit
Страница 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Страница 116: ...Chapter 9 Potentiometer RGBLED 116 www freenove com support freenove com Circuit Schematic diagram ...
Страница 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Страница 136: ...Chapter 12 Joystick 136 www freenove com support freenove com Circuit Schematic diagram Hardware connection ...
Страница 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Страница 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Страница 182: ...Chapter 17 74HC595 LEDBar Graph 182 www freenove com support freenove com Circuit Schematic diagram Hardware connection ...
Страница 197: ...197 Chapter 18 74HC595 7 segment display www freenove com support freenove com Circuit Schematic diagram ...
Страница 198: ...Chapter 18 74HC595 7 segment display 198 www freenove com support freenove com Hardware connection ...
Страница 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Страница 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...
Страница 270: ...Chapter 26 WebIOPi IOT 270 www freenove com support freenove com Circuit Schematic diagram Hardware connection ...