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!

11. Record New Step

We use the keyboard to control PiCrawler to make several poses in turn, and record these poses. Replay them later.

Run the Code

cd ~/picrawler/examples
sudo python3 11_record_new_step.py

After the code runs, please operate according to the prompt that pops up in the terminal.

  • Press 1234 to select the feet separately, 1: right front foot, 2: left front foot, 3: left rear foot, 4: right rear foot

  • Press w, a, s, d, r, and f to slowly control the PiCrawler’s coordinate values.

  • Press space to print all coordinate values.

  • Press p to have PiCrawler replay the recorded action.

  • Press esc to exit.

Code

from picrawler import Picrawler
from time import sleep
import sys
import tty
import termios
import copy

crawler = Picrawler()
speed = 80

def readchar():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch


manual = '''
Press keys on keyboard to control!
    w: Y++
    a: X--
    s: Y--
    d: X++
    r: Z++
    f: Z--
    1: Select right front leg
    2: Select left front leg
    3: Select left rear leg
    4: Select right rear leg
    Space: Print all leg coodinate & Save this step
    p: Play all saved step
    esc: Quit
'''


new_step=[]

def save_new_step():
    new_step.append(copy.deepcopy(crawler.current_step_all_leg_value()))
    print(new_step)

def play_all_new_step():
    for step in new_step:
        crawler.do_step(step,speed)
        sleep(0.6)

def main():

    speed = 80
    print(manual)
    crawler.do_step('sit',speed)
    leg = 0
    coodinate=crawler.current_step_leg_value(leg)
    while True:
        key = readchar()
        key = key.lower()
        # print(key)
        if 'w' == key:
            coodinate[1]=coodinate[1]+2
        elif 's' == key:
            coodinate[1]=coodinate[1]-2
        elif 'a' == key:
            coodinate[0]=coodinate[0]-2
        elif 'd' == key:
            coodinate[0]=coodinate[0]+2
        elif 'r' == key:
            coodinate[2]=coodinate[2]+2
        elif 'f' == key:
            coodinate[2]=coodinate[2]-2
        elif '1' == key:
            leg=0
            coodinate=crawler.current_step_leg_value(leg)
        elif '2' == key:
            leg=1
            coodinate=crawler.current_step_leg_value(leg)
        elif '3' == key:
            leg=2
            coodinate=crawler.current_step_leg_value(leg)
        elif '4' == key:
            leg=3
            coodinate=crawler.current_step_leg_value(leg)
        elif chr(32) == key:
            print("[[right front],[left front],[left rear],[right rear]]")
            print("saved new step")
            print(crawler.current_step_all_leg_value())
            save_new_step()
        elif 'p' == key:
            play_all_new_step()
        elif chr(27) == key:# 27 for ESC
            break

        sleep(0.05)
        crawler.do_single_leg(leg,coodinate,speed)
    print("\n q Quit")


if __name__ == "__main__":
    main()

How it works?

This project was born out of 10. Adjust Posture. Added recording and replay functions.

The recording function is implemented by the following code.

new_step=[]

def save_new_step():
    new_step.append(copy.deepcopy(crawler.current_step_all_leg_value()))
    print(new_step)

Note

The assignment here needs to use the Deep Copy function, otherwise the new_step will not get a new array object when appending.

The replay function is implemented by the following code.

def play_all_new_step():
    for step in new_step:
        crawler.do_step(step,speed)
        sleep(0.6)