Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

12. Twist

We already know how to make PiCrawler assume a specific pose, the next step is to combine the poses to form a continuous action.

Here, PiCrawler’s four feet are up and down in twos, jumping with the music.

Run the Code

cd ~/picrawler/examples
sudo python3 12_twist.py

After the program starts, the robot first stands up slowly to reach a stable posture.

Once standing, background music begins to play. At the same time, the robot performs a continuous twisting dance movement. During this motion, the four legs alternately rise and lower, creating a rhythmic twisting effect. The legs move in coordinated pairs so the body appears to sway from side to side.

A short delay between each step makes the motion smoother and more stable rather than abrupt or overly fast.

The robot continues dancing while the music plays. When Ctrl+C is pressed, the program stops and the robot safely returns to a sitting position before exiting.

Code

Note

You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like picrawler\examples. After modifying the code, you can run it directly to see the effect.

from picrawler import Picrawler
from robot_hat import Music
from time import sleep

music = Music()
crawler = Picrawler()

def twist(speed):
    new_step = [[50, 50, -80], [50, 50, -80], [50, 50, -80], [50, 50, -80]]

    for i in range(4):
        for inc in range(30, 60, 5):
            rise = [50, 50, (-80 + inc * 0.5)]
            drop = [50, 50, (-80 - inc)]

            new_step[i] = rise
            new_step[(i + 2) % 4] = drop
            new_step[(i + 1) % 4] = rise
            new_step[(i - 1) % 4] = drop

            crawler.do_step(new_step, speed)
            sleep(0.03)  # small delay to make motion smoother and less "crazy"

def main():
    try:
        # Stand up slowly first
        crawler.do_step('stand', 40)
        sleep(1.0)

        # Start music
        music.music_play('./musics/sports-Ahjay_Stelino.mp3')
        music.music_set_volume(20)

        while True:
            twist(speed=100)

    except KeyboardInterrupt:
        print("\nCtrl+C detected, exiting...")

    finally:
        # Sit down safely before exit
        try:
            crawler.do_step('sit', 40)
            sleep(1.0)
        except Exception:
            pass

if __name__ == "__main__":
    main()

How it works?

In this code, you need to pay attention to this part:

def twist(speed):
    new_step = [[50, 50, -80], [50, 50, -80], [50, 50, -80], [50, 50, -80]]

    for i in range(4):
        for inc in range(30, 60, 5):
            rise = [50, 50, (-80 + inc * 0.5)]
            drop = [50, 50, (-80 - inc)]

            new_step[i] = rise
            new_step[(i + 2) % 4] = drop
            new_step[(i + 1) % 4] = rise
            new_step[(i - 1) % 4] = drop

            crawler.do_step(new_step, speed)
            sleep(0.03)  # small delay to make motion smoother and less "crazy"

Simply put, it uses two layers of for loops to make the new_step array produce continuous and regular changes, and at the same time, crawler.do_step() executes the posture to form a continuous action.

You can intuitively get the coordinate value array corresponding to each pose from 10. Adjust Posture.

In addition, the example also played background music. The implementation method is as follows.

Play music by importing the following libraries.

from robot_hat import Music

Declare a Music object.

music = Music()

Play the background music in the picrawler/examples/musics directory and set the volume to 20. You can also add music to the musics folder via FileZilla Software.

music.music_play('./musics/sports-Ahjay_Stelino.mp3')
music.music_set_volume(20)