注釈

こんにちは!SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Communityへようこそ!Raspberry Pi、Arduino、ESP32に関心のある仲間とともに、さらに深く学びましょう。

参加する理由

  • 専門家のサポート: 購入後の問題や技術的な課題を、コミュニティやチームのサポートで解決できます。

  • 学びと共有: ヒントやチュートリアルを交換して、スキルを向上させましょう。

  • 特別なプレビュー: 新製品の発表や先行公開に早期アクセスできます。

  • 特別割引: 新製品に対して限定割引を楽しめます。

  • 祝祭プロモーションとプレゼント: プレゼントや祝祭プロモーションに参加できます。

👉 私たちと一緒に探求し、創造する準備はできましたか?[ここ]をクリックして、今すぐ参加しましょう!

11. 新しいステップの記録

このプロジェクトでは、キーボードを使ってPiCrawlerを操作し、いくつかのポーズを順番に実行し、それらのポーズを記録します。その後、記録したポーズを再生できます。

コードの実行

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

コードを実行した後、ターミナルに表示されるプロンプトに従って操作してください。

  • 1234 キーを押して足を個別に選択します。 1 : 右前足、 2 : 左前足、 3 : 左後足、 4 : 右後足

  • wasdrf を押して、PiCrawlerの座標値をゆっくりと制御します。

  • space キーを押すと、すべての座標値が表示され、現在のステップが保存されます。

  • p キーを押すと、記録されたアクションを再生します。

  • esc キーを押すと、終了します。

コード

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はESCキー
            break

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


if __name__ == "__main__":
    main()

仕組みは?

このプロジェクトは、10. 姿勢調整 から生まれました。録音と再生機能が追加されています。

録音機能は以下のコードで実現されています。

new_step=[]

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

注釈

ここでの代入には、 Deep Copy 関数を使用する必要があります。さもなければ、 new_step に新しい配列オブジェクトが追加されません。

再生機能は以下のコードで実現されています。

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