SunFounder pisloth
www.sunfounder.com
Aug 02, 2022
CONTENTS
1
Component List and Assembly Instructions
3
2
About Robot HAT
5
3
Play with Ezblock
3.1 Quick Guide on EzBlock
3.2 Move . . . . . . . . . .
3.3 Don’t Touch Me . . . .
3.4 Obstacle Avoidance . .
3.5 Emotional PiSloth . . .
3.6 Dance . . . . . . . . . .
3.7 Let’s Fight! Warrior! . .
3.8 Remote Control . . . .
3.9 Custom Step . . . . . .
3.10 Custom Action . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
7
7
10
12
15
17
21
26
31
34
40
Play with Python
4.1 Quick Guide on Python
4.2 Move . . . . . . . . . .
4.3 Dance . . . . . . . . . .
4.4 Obstacle Avoidance . .
4.5 Don’t Touch Me . . . .
4.6 Let’s Fight! Warrior! . .
4.7 Emotional PiSloth . . .
4.8 Remote Control . . . .
4.9 Custom Step . . . . . .
4.10 Custom Action . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
45
45
66
67
70
72
74
76
79
82
85
Appendix
5.1 I2C Configuration
5.2 Remote Desktop .
5.3 About the Battery
5.4 Filezilla Software .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
89
. 89
. 91
. 100
. 101
4
5
.
.
.
.
.
.
.
.
.
.
.
.
6
Thank You
105
7
Copyright Notice
107
i
ii
SunFounder pisloth
Thanks for choosing our PiSloth.
PiSloth is a Raspberry Pi Bionic robot with an aluminum alloy structure. It can talk, dance, and even express emotions,
such as happiness and excitement.
It has 22 different actions, such as: Stomp, Swing and MoonWalk, and you can customize the actions according to
your needs. PiSloth’s eyes consist of an ultrasonic sensor module that can be used to detect distance for obstacle
avoidance and following functions.
In this tutorial, a list and assembly pdf, introduction to Robot HAT, and programming of PiSloth are included.
CONTENTS
1
SunFounder pisloth
The programming part is divided into two chapters: Play with Ezblock & Play with Python, and each of them can get
you stated on making PiSloth work in way you want.
EzBlock Studio is a development platform developed by SunFounder designed for beginners to lower the barriers to
getting started with Raspberry Pi. It has two programming languages: Graphical and Python, and available on almost
all different types of devices. With Bluetooth and Wi-Fi support, you can download code, remote control a Raspberry
Pi, on Ezblock Studio.
More experienced makers can use the popular programming language - Python.
Content
2
CONTENTS
CHAPTER
ONE
COMPONENT LIST AND ASSEMBLY INSTRUCTIONS
You need to check whether there are missing or damaged components according to the list first. If there are any
problems, please contact us and we will solve them as soon as possible.
Please follow the steps on the PDF to assemble.
Note:
1. Before assembling, you need to buy 2 18650 batteries and fully charge them, refer to About the Battery.
2. Robot HAT cannot charge the battery, so you need to buy a battery charger at the same time.
• Component List and Assembly Instructions.
3
SunFounder pisloth
4
Chapter 1. Component List and Assembly Instructions
CHAPTER
TWO
ABOUT ROBOT HAT
Robot HAT is a multifunctional expansion board that allows Raspberry Pi to be quickly turned into a robot. An MCU
is on board to extend the PWM output and ADC input for the Raspberry Pi, as well as a motor driver chip, Bluetooth
module, I2S audio module and mono speaker. As well as the GPIOs that lead out of the Raspberry Pi itself.
It also comes with a Speaker, which can be used to play background music, sound effects and implement TTS functions
to make your project more interesting.
5
SunFounder pisloth
Accepts 7-12V PH2.0 2pin power input with 2 power indicators. The board also has a user available LED and a button
for you to quickly test some effects.
Note: You can see more details in the Robot HAT Documentation.
6
Chapter 2. About Robot HAT
CHAPTER
THREE
PLAY WITH EZBLOCK
For beginners and novices, EzBlock is a software development platform offered by SunFounder for Raspberry Pi.
Ezbock offers two programming environments: a graphical environment and a Python environment.
It is available for almost all types of devices, including Mac, PC, and Android.
Here is a tutorial to help you complete EzBlock installation, download, and use.
3.1 Quick Guide on EzBlock
There are 2 parts here:
• Servo Adjust allows you to keep all the servos at 0 degrees to complete a proper and safe assembly (otherwise
you will probably damage the servos).
• Install and Configure EzBlock Studio will guide you to download EzBlock Studio to play with your robot.
3.1.1 Servo Adjust
When assembling to the part with the servo, you need to keep the servo at 0° and secure it with the servo screw. Please
follow the tutorial below to do this.
1. Firstly, Install EzBlock OS(3.1) onto a Micro SD card, once the installation is complete, insert it into the Raspberry Pi.
2. To ensure that the servo has been properly set to 0°, first insert the rocker arm into the servo shaft and then
gently rotate the rocker arm to a different angle.
7
SunFounder pisloth
3. Follow the instructions on the assembly foldout, insert the battery holder cable and turn the power switch to the
ON. Wait for 1-2 minutes, there will be a sound to indicate that the Raspberry Pi boots successfully.
4. Next, plug the servo cable into the P11 port as follows.
8
Chapter 3. Play with Ezblock
SunFounder pisloth
5. At this point you will see the servo arm rotate to a specific position (0°). If the servo arm does not return to 0°,
press the RST button to restart the Robot HAT.
6. Now you can continue the installation as instructed on the assembly foldout.
Note:
• Do not unplug this servo cable before fastening this servo with the servo screw, you can unplug it after fastening.
• Do not turn the servo while it is powered on to avoid damage; if the servo shaft is inserted at the wrong angle,
pull out the servo and reinsert it.
• Before assembling each servo, you need to plug the servo cable into P11 and turn on the power to set its angle
to 0°.
• This zeroing function will be disabled if you download a program to the robot later with the EzBlock APP.
3.1.2 Install and Configure EzBlock Studio
As soon as the robot is assembled, you will need to carry out some basic operations.
• Install EzBlock Studio(3.1): Download and install EzBlock Studio on your device or use the web-based version.
• Connect the Product and EzBlock(3.1): Configure Wi-Fi, Bluetooth and calibrate before use.
• Open and Run Examples(3.1): View or run the related example directly.
Note: After you connect the PiSloth, there will be a calibration step. This is because of possible deviations in
the installation process or limitations of the servos themselves, making some servo angles slightly tilted, so you can
3.1. Quick Guide on EzBlock
9
SunFounder pisloth
calibrate them in this step.
But if you think the assembly is perfect and no calibration is needed, you can also skip this step.
Projects
Here, we show you the projects of playing PiSloth on EzBlock Studio. If you are new to these, you can refer to the
code images inside each project to program, and can learn the use of blocks according to TIPS.
If you don’t want to write these projects one by one, we have uploaded them to EzBlock Studio’s Examples page and
you can run them directly or edit them and run them later.
3.2 Move
This is the first project. PiSloth has woken up, and it moves freely.
Before programming, you need to learn the basic usage of EzBlock Studio from here.
• How to Create a New Project?
TIPS
This is the basic structure of the program, the Start block is used to do some initialization (even if no block is placed,
it cannot be deleted) and the Forever block is, as the name suggests, a continuous loop that allows your program to
change and respond.
10
Chapter 3. Play with Ezblock
SunFounder pisloth
This block is used to make PiSloth do a specific action several steps at a speed (%), for example, let PiSloth go forward
1 step at 50% speed.
Different actions can be selected from the drop down options, there are 22 in total.
This is a block that sets the duration of the previous block, unit: ms.
EXAMPLE
Note:
• You can write the program according to the following picture, please refer to the tutorial: How to Create a New
Project?
• Or find the code with the same name on the Examples page of the EzBlock Studio and click Run or Edit directly.
After writing the code according to the following figure, click the download icon in the bottom right corner, you will
see PiSloth move forward 3 steps, backward 4 steps, left 3 steps, right 5 steps, and finally stop. Since the whole code
is placed inside the Forever block, PiSloth will repeat the above actions after stopping for a while.
You can try putting the code from the Forever block into the Start block and see what happens.
3.2. Move
11
SunFounder pisloth
3.3 Don’t Touch Me
If you don’t meet PiSloth’s needs, it will get angry and stay away from your touch.
TIPS
You can directly use this block to read the distance to the obstacle right ahead.
Note: When assembling, Trig and Echo are connected to D2 and D3 respectively, you also need to change them
simultaneously when programming.
12
Chapter 3. Play with Ezblock
SunFounder pisloth
To achieve conditional judgment of “if” type, you need to use an if do block.
When you need to implement multiple conditional judgments, you will have to change if do into if else do. This can
be achieved by clicking on the setting icon.
You need to use a conditional statements block in conjunction with if do. Judging conditions can be “=”, “>”, “ Raspberry Pi OS(other).
Scroll down to the end of the newly opened page and you will see Raspberry Pi OS(Legacy) and Raspberry Pi
OS Lite(Legacy), these are security updates for Debian Buster, the difference between them is with or without the
desktop. It is recommended to install Raspberry Pi OS(Legacy), the system with the desktop.
48
Chapter 4. Play with Python
SunFounder pisloth
Step 5
Select the SD card you are using.
Step 6
Press Ctrl+Shift+X or click the setting button to open the Advanced options page to enable SSH and configure wifi,
these 2 items must be set, the others depend on your choice . You can choose to always use this image customization
options.
4.1. Quick Guide on Python
49
SunFounder pisloth
Then scroll down to complete the wifi configuration and click SAVE.
Note: wifi country should be set the two-letter ISO/IEC alpha2 code for the country in which you are using
your Raspberry Pi, please refer to the following link: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_
assigned_code_elements
50
Chapter 4. Play with Python
SunFounder pisloth
Step 7
Click the WRITE button.
4.1. Quick Guide on Python
51
SunFounder pisloth
Step 8
If your SD card currently has any files on it, you may wish to back up these files first to prevent you from permanently
losing them. If there is no file to be backed up, click Yes.
52
Chapter 4. Play with Python
SunFounder pisloth
Step 9
After waiting for a period of time, the following window will appear to represent the completion of writing.
4.1. Quick Guide on Python
53
SunFounder pisloth
4.1.3 Set up Your Raspberry Pi
If You Have a Screen
If you have a screen, it will be easy for you to operate on the Raspberry Pi.
Required Components
Any Raspberry Pi
1 * Micro SD card
1 * HDMI cable
1 * Mouse
1 * Power Adapter
1 * Screen Power Adapter
1 * Screen
1 * Keyboard
1. Insert the SD card you’ve set up with Raspberry Pi OS into the micro SD card slot on the underside of your
Raspberry Pi.
2. Plug in the Mouse and Keyboard.
3. Connect the screen to Raspberry Pi’s HDMI port and make sure your screen is plugged into a wall socket and
switched on.
Note: If you use a Raspberry Pi 4, you need to connect the screen to the HDMI0 (nearest the power
in port).
4. Use the power adapter to power the Raspberry Pi. After a few seconds, the Raspberry Pi OS desktop will be
displayed.
54
Chapter 4. Play with Python
SunFounder pisloth
If You Have No Screen
If you don’t have a display, you can log in to the Raspberry Pi remotely, but before that, you need to get the IP of the
Raspberry Pi.
Get the IP Address
After the Raspberry Pi is connected to WIFI, we need to get the IP address of it. There are many ways to know the IP
address, and two of them are listed as follows.
1. Checking via the router
If you have permission to log in the router(such as a home network), you can check the addresses assigned to Raspberry
Pi on the admin interface of router.
The default hostname of the Raspberry Pi OS is raspberrypi, and you need to find it. (If you are using ArchLinuxARM
system, please find alarmpi.)
2. Network Segment Scanning
You can also use network scanning to look up the IP address of Raspberry Pi. You can apply the software, Advanced
IP scanner and so on.
Scan the IP range set, and the name of all connected devices will be displayed. Similarly, the default hostname of the
Raspberry Pi OS is raspberrypi, if you haven’t modified it.
4.1. Quick Guide on Python
55
SunFounder pisloth
Use the SSH Remote Control
We can open the Bash Shell of Raspberry Pi by applying SSH. Bash is the standard default shell of Linux. The Shell
itself is a program written in C that is the bridge linking the customers and Unix/Linux. Moreover, it can help to
complete most of the work needed.
For Linux or/Mac OS X Users
Step 1
Go to Applications->Utilities, find the Terminal, and open it.
Step 2
Type in ssh pi@ip_address . “pi” is your username and “ip_address” is your IP address. For example:
ssh pi@192.168.18.197
Step 3
Input “yes”.
56
Chapter 4. Play with Python
SunFounder pisloth
Step 4
Input the passcode and the default password is raspberry.
Step 5
We now get the Raspberry Pi connected and are ready to go to the next step.
4.1. Quick Guide on Python
57
SunFounder pisloth
Note: When you input the password, the characters do not display on window accordingly, which is normal. What
you need is to input the correct password.
For Windows Users
If you’re a Windows user, you can use SSH with the application of some software. Here, we recommend PuTTY.
Step 1
Download PuTTY.
Step 2
Open PuTTY and click Session on the left tree-alike structure. Enter the IP address of the RPi in the text box under
Host Name (or IP address) and 22 under Port (by default it is 22).
58
Chapter 4. Play with Python
SunFounder pisloth
Step 3
Click Open. Note that when you first log in to the Raspberry Pi with the IP address, there prompts a security reminder.
Just click Yes.
Step 4
When the PuTTY window prompts “login as:”, type in “pi” (the user name of the RPi), and password: “raspberry”
(the default one, if you haven’t changed it).
Note: When you input the password, the characters do not display on window accordingly, which is normal. What
you need is to input the correct password.
If inactive appears next to PuTTY, it means that the connection has been broken and needs to be reconnected.
4.1. Quick Guide on Python
59
SunFounder pisloth
Step 5
Here, we get the Raspberry Pi connected and it is time to conduct the next steps.
Note: If you are not satisfied with using the command window to control the Raspberry Pi, you can also use the
remote desktop function, which can help us manage the files in the Raspberry Pi easily.
For details on how to do this, please refer to Remote Desktop.
4.1.4 Download and Run the Code
We can download the files by using git clone in the command line.
Install robot-hat library first.
cd /home/pi/
git clone https://github.com/sunfounder/robot-hat.git
cd robot-hat
sudo python3 setup.py install
Note: Running setup.py will download some necessary components. You may fail to download due to network
problems. You may need to download again at this time. In the following cases, enter Y and press Enter.
60
Chapter 4. Play with Python
SunFounder pisloth
Then download the code and install pisloth library.
cd /home/pi/
git clone -b v2.0 https://github.com/sunfounder/pisloth.git
cd pisloth
sudo python3 setup.py install
This step will take a little time, so please be patient.
Finally, you need to run the script i2samp.sh to install the components required by the i2s amplifier, otherwise the
pislot will have no sound.
cd /home/pi/pisloth
sudo bash i2samp.sh
4.1. Quick Guide on Python
61
SunFounder pisloth
Type y and press Enter to continue running the script.
Type y and press Enter to run /dev/zero in the background.
62
Chapter 4. Play with Python
SunFounder pisloth
Type y and press Enter to restart the machine.
Note: If there is no sound after restarting, you may need to run the i2samp.sh script multiple times.
4.1.5 Servo Adjust
To ensure that the servo has been properly set to 0°, first insert the rocker arm into the servo shaft and then gently
rotate the rocker arm to a different angle.
Follow the instructions on the assembly foldout, insert the battery holder cable and turn the power switch to the ON.
4.1. Quick Guide on Python
63
SunFounder pisloth
Wait for 1-2 minutes, there will be a sound to indicate that the Raspberry Pi boots successfully.
Now, run servo_zeroing.py in the examples/ folder.
cd /home/pi/piarm/examples
sudo python3 servo_zeroing.py
Note: If you get an error, try re-enabling the Raspberry Pi’s I2C port, see: I2C Configuration.
Next, plug the servo cable into the P11 port as follows.
64
Chapter 4. Play with Python
SunFounder pisloth
At this point you will see the servo arm rotate to a specific position (0°). If the servo arm does not return to 0°, press
the RST button to restart the Robot HAT.
Now you can continue the installation as instructed on the assembly foldout.
Note:
• Do not unplug this servo cable before fixing it with the servo screw, you can unplug it after fixing it.
• Do not rotate the servo while it is powered on to avoid damage; if the servo shaft is not inserted at the right
angle, pull the servo out and reinsert it.
• Before assembling each servo, you need to plug the servo cable into P11 and turn on the power to set its angle
to 0°.
After the assembly is complete, you can try to run the projects below.
4.1. Quick Guide on Python
65
SunFounder pisloth
4.2 Move
This is the first project. PiSloth has woken up, and it moves freely.
Run the Code
cd /home/pi/pisloth/examples
sudo python3 move.py
After running the code, you will see PiSloth move left 7 steps, forward 5 steps, right 7 steps, and forward 5 steps.
Code
Note: You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path
like pisloth\examples. After modifying the code, you can run it directly to see the effect.
from pisloth import Sloth
sloth = Sloth([1,2,3,4])
sloth.set_offset([0,0,0,0])
def main():
sloth.do_action('turn left', 7, 90)
sloth.do_action('forward', 5, 90)
sloth.do_action('turn right', 7, 90)
sloth.do_action('forward', 5, 90)
(continues on next page)
66
Chapter 4. Play with Python
SunFounder pisloth
(continued from previous page)
if __name__ == "__main__":
while True:
main()
How it works?
First, import the Sloth class from the pisloth library you have installed, which contains all of PiSloth’s actions
and the functions that implement them.
from pisloth import Sloth
Then instantiate the Sloth class.
sloth = Sloth([1,2,3,4])
sloth.set_offset([0,0,0,0])
Finally use the sloth.do_action() function to make PiSloth move.
sloth.do_action('turn left', 7, 90)
sloth.do_action('forward', 5, 90)
sloth.do_action('turn right', 7, 90)
sloth.do_action('forward', 5, 90)
In general, all actions of PiSloth can be implemented with the sloth.do_action() function. It has four parameters:
• motion_name is the name of specific actions, including: forward, turn right, turn left,
backward, stand, moon walk left, moon walk right, hook, big swing, swing, walk
boldly, walk backward boldly, walk shyly, walk backward shyly, stomp rihgt,
stomp left, close, open, tiptoe left, tiptoe right, fall left, fall right.
• step represents the number of each action is done, the default is 1.
• speed indicates the speed of the action, the default is 50 and the range is 0~100.
• bpm means rhythm, we will use it later in the Dance project.
Note: You can add different sound effects or music to musics or sounds folder via Filezilla Software.
4.3 Dance
Now, PiSltoh will show you its newly learned dance.
4.3. Dance
67
SunFounder pisloth
Note: You can download and print the PDF Cartoon Mask for your PiSloth.
Run the Code
cd /home/pi/pisloth/examples
sudo python3 dancing.py
The whole dance is divided into 2 parts, and PiSloth will finish these 2 parts with the music. If you don’t stop the
code, it will repeat the dance.
Code
Note: You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path
like pisloth\examples. After modifying the code, you can run it directly to see the effect.
from pisloth import Sloth
from robot_hat import Music
from robot_hat import Ultrasonic
from robot_hat import Pin
import time
import os
music = Music()
sloth = Sloth([1,2,3,4])
(continues on next page)
68
Chapter 4. Play with Python
SunFounder pisloth
(continued from previous page)
sloth.set_offset([0,0,0,0])
def main():
music.background_music('./musics/india-Arulo.mp3')
music.music_set_volume(20)
sloth.do_action('stomp left',3,bpm=129)
sloth.do_action('stomp right',3,bpm=129)
sloth.do_action('moon walk left',3,bpm=129)
sloth.do_action('moon walk right',3,bpm=129)
for i in range(3):
sloth.do_action('swing',1,bpm=129)
sloth.do_action('stand',1,bpm=129)
for i in range(3):
sloth.do_action('close',1,bpm=129)
sloth.do_action('stand',1,bpm=129)
sloth.do_action('open',1,bpm=129)
sloth.do_action('stand',1,bpm=129)
sloth.do_action('tiptoe left',2,bpm=129)
sloth.do_action('tiptoe right',2,bpm=129)
sloth.do_action('stomp left',3,bpm=129)
sloth.do_action('stomp rihgt',3,bpm=129)
sloth.do_action('moon walk left',3,bpm=129)
sloth.do_action('moon walk right',3,bpm=129)
for i in range(3):
sloth.do_action('hook',1,bpm=129)
sloth.do_action('stand',1,bpm=129)
for i in range(4):
sloth.do_action('swing',1,bpm=129)
sloth.do_action('big swing',1,bpm=129)
sloth.do_action('swing',1,bpm=129)
sloth.do_action('stand',1,bpm=129)
sloth.do_action('tiptoe right',2,bpm=129)
sloth.do_action('stand',2,bpm=129)
music.music_stop()
time.sleep(10)
if __name__ == "__main__":
while True:
main()
How it works?
You can make PiSloth play music by importing the following libraries.
from robot_hat import TTS, Music
Play the background music in the pisloth/examples/musics directory and set the volume to 20. You can also
add music to the musics folder via Filezilla Software.
4.3. Dance
69
SunFounder pisloth
music.background_music('./musics/india-Arulo.mp3')
music.music_set_volume(20)
In general, all actions of PiSloth can be implemented with the sloth.do_action() function. It has four parameters:
• motion_name is the name of specific actions, including: forward, turn right, turn left,
backward, stand, moon walk left, moon walk right, hook, big swing, swing, walk
boldly, walk backward boldly, walk shyly, walk backward shyly, stomp rihgt,
stomp left, close, open, tiptoe left, tiptoe right, fall left, fall right.
• step represents the number of each action is done, the default is 1.
• speed indicates the speed of the action, the default is 50 and the range is 0~100.
• bpm means rhythm, the bpm parameter here affects the interval time of PiSloth movement. The higher the
value, the shorter the interval time. When we know the beat of a song through the bpm calculator, we can
make PiSloth dance to the music.
For music bmp, if you want to know more, you can refer to: https://en.wikipedia.org/wiki/Tempo
Note: You can add different sound effects or music to musics or sounds folder via Filezilla Software.
4.4 Obstacle Avoidance
In this project, PiSloth will use an ultrasonic module to detect obstacles in front. When PiSloth detects an obstacle, it
will send a signal and look for another direction to move forward.
Run the Code
cd /home/pi/pisloth/examples
sudo python3 avoid.py
After the code runs, PiSloth will walk forward. If it detects that the distance of the obstacle ahead is less than 10cm, it
will stop and sound a warning, then turn left and stop. If there is no obstacle in the direction after turning left or the
obstacle distance is greater than 10, it will continue to move forward.
Code
Note: You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path
like pisloth\examples. After modifying the code, you can run it directly to see the effect.
from pisloth import Sloth
from robot_hat import TTS, Music
from robot_hat import Ultrasonic
from robot_hat import Pin
import time
import os
tts = TTS()
music = Music()
sloth = Sloth([1,2,3,4])
(continues on next page)
70
Chapter 4. Play with Python
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