見出し画像

Petoi Bittle 入門 (19) - PythonによるBittleの制御

「Petoi Bittle」を「Python」で操作する方法をまとめました。

前回

1. pyBittle

pyBittle」は、「Petoi Bittle」を「Python」で操作するためのPythonライブラリです。「シリアル接続」「Bluetooth」「Wi-Fi」を介して「Bittle」と通信するための、一連のメソッドを提供します。

2. インストール

Pythonの仮想環境(Python 3.7で試しました)に以下のコマンドでインストールします。

$ pip install pyBittle

3. ポーズ命令の実行

「ポーズ命令」を実行するには、bittle.send_command_serial()で命令定数を指定します。応答を受信するには、bittle.receive_msg_serial()を使います。ポートはbittle.serialManager.portをで、自分の環境にあわせて指定します。

import pyBittle
import time

# Bittleインスタンスの準備
bittle = pyBittle.Bittle() 

# ポートの指定
bittle.serialManager.port = '/dev/cu.usbserial-14320'

# シリアル接続の開始
if bittle.connect_serial():
    # ポーズ命令の送信 (お座り)
    bittle.send_command_serial(pyBittle.Command.SIT)

    # 応答の受信
    received = bittle.receive_msg_serial()
    decoded_msg = received.decode('utf-8')
    decoded_msg = decoded_msg.replace('\r\n', '')
    print(decoded_msg)

    # 5秒スリープ
    time.sleep(5)

    # シリアル接続の終了
    bittle.disconnect_serial()
else:
   print("Bittle not found!")​

命令定数は、次のとおりです。

・pyBittle.Command.REST : d
・pyBittle.Command.FORWARD : F
・pyBittle.Command.GYRO : g
・pyBittle.Command.LEFT : L
・pyBittle.Command.BALANCE : kbalance
・pyBittle.Command.RIGHT : R
・pyBittle.Command.SHUTDOWN : z
・pyBittle.Command.BACKWARD : B
・pyBittle.Command.CALIBRATION : c
・pyBittle.Command.STEP : kvt
・pyBittle.Command.CRAWL : kcr
・pyBittle.Command.WALK : kwk
・pyBittle.Command.TROT : ktr
・pyBittle.Command.LOOKUP : klu
・pyBittle.Command.BUTTUP : kbuttUp
・pyBittle.Command.RUN : krn
・pyBittle.Command.BOUND : kbd
・pyBittle.Command.GREETING : khi
・pyBittle.Command.PUSHUP : kpu
・pyBittle.Command.PEE : kpee
・pyBittle.Command.STRETCH : kstr
・pyBittle.Command.SIT : ksit
・pyBittle.Command.ZERO : kzero
・pyBittle.Command.BUNNY : kbdF
・pyBittle.Command.BACKFLIP : kbf
・pyBittle.Command.SLEEP : kstp
・pyBittle.Command.CHECKAROUND : kck

4. 移動命令の実行

「移動命令」を実行するには、bittle.gaitで歩行種別を指定した後、bittle.send_movement_serial()で移動種別を指定します。

import pyBittle
import time

# Bittleインスタンスの準備
bittle = pyBittle.Bittle() 

# ポートの指定
bittle.serialManager.port = '/dev/cu.usbserial-14320'

# シリアル接続の開始
if bittle.connect_serial():
    # 這う
    bittle.gait = pyBittle.Gait.CRAWL  # 這う
    bittle.send_movement_serial(pyBittle.Direction.FORWARD) # 前進
    time.sleep(5)
    bittle.send_movement_serial(pyBittle.Direction.FORWARDRIGHT) # 右進
    time.sleep(3)
  
    # 駆け足
    bittle.gait = pyBittle.Gait.TROT # 駆け足
    bittle.send_movement_serial(pyBittle.Direction.FORWARD) # 前進
    time.sleep(5)
  
    # 停止
    bittle.send_movement_serial(pyBittle.Command.BALANCE)  # 停止
    time.sleep(2)

   # シリアル接続の終了
   bittle.disconnect_serial()
else:
   print("Bittle not found!")

歩行定数は、次のとおりです。

・pyBittle.Gait.WALK : kwk
・pyBittle.Gait.CRAWL : kcr
・pyBittle.Gait.TROT : ktr
・pyBittle.Gait.RUN : krn

 方向定数は、次のとおりです。

・pyBittle.Direction.FORWARD : F
・pyBittle.Direction.FORWARDLEFT : L
・pyBittle.Direction.FORWARDRIGHT : R
・pyBittle.Direction.BACKWARD : B
・pyBittle.Direction.BACKWARDLEFT : BL
・pyBittle.Direction.BACKWARDRIGHT : BR

次回


いいなと思ったら応援しよう!