0
0
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

【Pybulletサンプル解説】最大速度を設定し、関節を位置制御【motorMaxVelocity.py】

Posted at

Pybullet公式gitリポジトリのサンプルコードを解説するシリーズです(一覧はこちら)。


今回は、motorMaxVelocity.pyを解説します。(コードのリンクはこちら

本コードを実行すると、

motorMaxVelocity.gif

使用している機能

本コードは、以下の機能を使用して「関節のモータ制御」を実現しています。

関節のモータ制御

setJointMotorControl2 関数を使用することで、オブジェクトのジョイントを制御できます。

pybullet.setJointMotorControl2(objectId,
                        jointIndex,
                        mode,
                        targetPosition,
                        targetVelocity,
                        force,
                        positionGain,
                        velocityGain,
                        maxVelocity)
  • objectId:オブジェクトのID
  • jointIdx:関節のインデックス
  • mode:関節の制御モード
    • 今回の場合 POSITION_CONTROL(位置制御モード)で設定
  • targetPosition:目標位置
  • targetVelocity:目標速度
  • force:力
  • positionGain:位置ゲイン
  • velocityGain:速度ゲイン
  • maxVelocity:最大速度

コメントをつけたサンプルコード

サンプルコードにコメントをつけたものが以下になります(もともとあった不要と思われるコメント等については削除しています)

import pybullet as p
import time
import pybullet_data

# PybulletにGUIモードで接続
p.connect(p.GUI)

# Pybulletに関するデータパスを取得
p.setAdditionalSearchPath(pybullet_data.getDataPath())

# カートポールのオブジェクトを生成
cartpole = p.loadURDF("cartpole.urdf")

# リアルタイムシミュレーションモードに設定
p.setRealTimeSimulation(1)

# カートポールを1番目のジョイントをモータ制御
p.setJointMotorControl2(cartpole,
                        1,
                        p.POSITION_CONTROL,
                        targetPosition=1000,
                        targetVelocity=0,
                        force=1000,
                        positionGain=1,
                        velocityGain=0,
                        maxVelocity=0.5)
                        
while (1):

  # 重力を設定
  p.setGravity(0, 0, -10)

  # ジョイントの位置と速度を取得、表示
  js = p.getJointState(cartpole, 1)
  print("position=", js[0], "velocity=", js[1])
  
  time.sleep(0.01)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0