1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

URDFを定数倍スケールするPythnonコード

Last updated at Posted at 2024-03-13

URDFを定数倍拡大・縮小するコードを書きました。box,radisu,sphereの寸法や各種jointのxyzを線形倍、質量を3乗倍、慣性モーメントを5乗倍します。Pythonの標準ライブラリであるElementTreeを使っています。

import xml.etree.ElementTree as ET
##ここにサイズ比を書く
mag_rate = 10
##ここに入力ファイル名を書く
input_file = "input.urdf"
##ここに出力ファイル名を書く
output_file = "output.urdf"


#サイズ比の1乗だけ大きくなる要素
elems_and_keys_linear = {"origin":["xyz"],
                         "cylinder":["length","radius"],
                         "box":["size"],
                         "sphere":["radius"]
                         "mesh":["scale"]
                         }
#サイズ比の3乗大きくなる要素
elems_and_keys_cubic = {"mass":["value"]}
#サイズ比の5乗大きくなる要素
elems_and_keys_5pow ={"inertia":["ixx","ixy","ixz","iyy","iyz","izz"]}

tree = ET.parse(input_file)
root = tree.getroot()


# まずは線形要素
for k in elems_and_keys_linear.keys():
    for el in root.findall(".//{}".format(k)):
        for op in elems_and_keys_linear[k]:
            vals = map(float,el.get(op).split())
            el.set(op," ".join([str(val*mag_rate) for val in vals]))

#3乗要素
for k in elems_and_keys_cubic.keys():
    for el in root.findall(".//{}".format(k)):
        for op in elems_and_keys_cubic[k]:
            vals = map(float,el.get(op).split())
            el.set(op," ".join([str(val*(mag_rate**3)) for val in vals]))

#5乗要素
for k in elems_and_keys_5pow.keys():
    for el in root.findall(".//{}".format(k)):
        for op in elems_and_keys_5pow[k]:
            vals = map(float,el.get(op).split())
            el.set(op," ".join([str(val*(mag_rate**5)) for val in vals]))

newtree = ET.ElementTree(root)
newtree.write(output_file)

1
0
3

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?