
SunFounder pisloth
(continued from previous page)
sloth
.
set_offset([
0
,
0
,
0
,
0
])
sonar
=
Ultrasonic(Pin(
"D2"
) ,Pin(
"D3"
))
alert_distance
=
10
def
main
():
distance
=
sonar
.
read()
if
distance
<
0
:
pass
elif
distance
<=
alert_distance:
try
:
music
.
sound_effect_threading(
'./sounds/sign.wav'
)
except
Exception
as
e:
(e)
sloth
.
do_action(
'hook'
,
1
,
95
)
time
.
sleep(
0.5
)
sloth
.
do_action(
'stand'
,
1
,
95
)
time
.
sleep(
0.5
)
sloth
.
do_action(
'turn left'
,
7
,
90
)
sloth
.
do_action(
'stand'
,
1
,
95
)
time
.
sleep(
0.2
)
else
:
sloth
.
do_action(
'forward'
,
1
,
90
)
if
__name__
==
"__main__"
:
while
True
:
main()
How it works?
You can get the distance by importing the
Ultrasonic
class.
from
robot_hat
import
Ultrasonic
Then initialize the ultrasonic pins.
sonar
=
Ultrasonic(Pin(
"D2"
) ,Pin(
"D3"
))
Here is the main program.
• Read the
distance
detected by ultrasonic module and filter out the values less than 0 (When the ultrasonic
module is too far from the obstacle or cannot read the data correctly,
distance<0
will appear).
• When the
distance
is less than or equal to
alert_distance
(the threshold value set earlier, which is 10),
play the sound effect
sign.wav
. PiSloth does
hook
,
stand
,
left turn
and
stand
in sequence.
• When the
distance
is greater than
alert_distance
, PiSloth will move
forward
.
distance
=
sonar
.
read()
if
distance
<
0
:
pass
elif
distance
<=
alert_distance:
try
:
music
.
sound_effect_threading(
'./sounds/sign.wav'
)
except
Exception
as
e:
(e)
sloth
.
do_action(
'hook'
,
1
,
95
)
(continues on next page)
66
Chapter 4. Play with Python
Содержание PiSloth
Страница 1: ...SunFounder pisloth www sunfounder com Oct 19 2021 ...
Страница 2: ......
Страница 4: ...ii ...
Страница 6: ...SunFounder pisloth 2 CONTENTS ...
Страница 8: ...SunFounder pisloth 4 Chapter 1 Component List and Assembly Instructions ...
Страница 18: ...SunFounder pisloth EXAMPLE 14 Chapter 3 Play with Ezblock ...
Страница 20: ...SunFounder pisloth 16 Chapter 3 Play with Ezblock ...
Страница 22: ...SunFounder pisloth Confuse Happy 18 Chapter 3 Play with Ezblock ...
Страница 23: ...SunFounder pisloth Fear Sad 3 5 Emotional PiSloth 19 ...
Страница 24: ...SunFounder pisloth Fall Call all custom functions in the Forever block 20 Chapter 3 Play with Ezblock ...
Страница 25: ...SunFounder pisloth 3 6 Dance Now PiSltoh will show you his newly learned dance 3 6 Dance 21 ...
Страница 28: ...SunFounder pisloth 24 Chapter 3 Play with Ezblock ...
Страница 32: ...SunFounder pisloth 28 Chapter 3 Play with Ezblock ...
Страница 35: ...SunFounder pisloth 3 8 Remote Control 31 ...
Страница 39: ...SunFounder pisloth 3 9 Custom Step 35 ...
Страница 41: ...SunFounder pisloth The complete code is as follows 3 9 Custom Step 37 ...
Страница 50: ...SunFounder pisloth Step 7 Click the WRITE button 46 Chapter 4 Play with Python ...
Страница 86: ...SunFounder pisloth 82 Chapter 4 Play with Python ...
Страница 90: ...SunFounder pisloth Yes then Ok Finish 86 Chapter 5 Appendix ...
Страница 94: ...SunFounder pisloth Step 4 Double click the connection just created 90 Chapter 5 Appendix ...
Страница 95: ...SunFounder pisloth Step 5 Enter Username pi and Password raspberry by default 5 3 Remote Desktop 91 ...
Страница 96: ...SunFounder pisloth Step 6 Now you can see the desktop of the Raspberry Pi 92 Chapter 5 Appendix ...
Страница 102: ...SunFounder pisloth 98 Chapter 5 Appendix ...
Страница 104: ...SunFounder pisloth 100 Chapter 6 Thank You ...