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!

1. Move

This is PiCrawler’s first project. Perform its most basic function - move.

Run the Code

cd ~/picrawler/examples
sudo python3 1_move.py

When the program starts, PiCrawler stands up and waits briefly.

It then continuously performs a movement cycle: forward, backward, turn left, turn right, small left turn, and small right turn.

Each action is separated by short delays for smoother movement.

Press Ctrl+C to stop the program. Before exiting, the crawler sits down safely.

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 time import sleep

crawler = Picrawler()  # Create PiCrawler object

def main():
    speed = 80  # Movement speed

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

        while True:
            crawler.do_action('forward', 1, speed)   # Move forward
            sleep(0.25)

            crawler.do_action('backward', 1, speed)  # Move backward
            sleep(0.25)

            crawler.do_action('turn left', 1, speed)  # Turn left
            sleep(0.25)

            crawler.do_action('turn right', 1, speed)  # Turn right
            sleep(0.25)

            crawler.do_action('turn left angle', 1, speed)  # Small left turn
            sleep(0.3)

            crawler.do_action('turn right angle', 1, speed)  # Small right turn
            sleep(0.3)

            sleep(0.5)

    except KeyboardInterrupt:
        print("\nCtrl+C pressed...")

    finally:
        crawler.do_step('sit', 40)  # Sit down before exit
        sleep(1.0)

if __name__ == "__main__":
    main()

How it works?

  1. Import and Initialization

    from picrawler import Picrawler
    from time import sleep
    
    crawler = Picrawler()
    

    The script imports the required modules and creates a Picrawler object, which is used to control all robot movements.

  2. Main Function and Setup

    def main():
        speed = 80
        crawler.do_step('stand', 40)
        sleep(1.0)
    

    The main() function defines the movement speed. Before starting the loop, the robot stands up and stabilizes.

  3. Continuous Movement Loop

    while True:
        crawler.do_action('forward', 1, speed)
        crawler.do_action('backward', 1, speed)
        crawler.do_action('turn left', 1, speed)
        crawler.do_action('turn right', 1, speed)
        crawler.do_action('turn left angle', 1, speed)
        crawler.do_action('turn right angle', 1, speed)
    

    The robot continuously performs a predefined sequence of movement actions inside an infinite loop. Short delays between actions help smooth motion.

  4. Safe Exit Handling

    except KeyboardInterrupt:
        print("\nCtrl+C pressed...")
    finally:
        crawler.do_step('sit', 40)
    

    The try / except / finally structure ensures: - Ctrl+C stops the loop safely. - The robot sits down before the program exits.

  5. Program Entry

    if __name__ == "__main__":
        main()
    

    This ensures that main() runs only when the script is executed directly.