Skip to content

Commit e53e41c

Browse files
Merge pull request #82 from Hiroaki-Masuzawa/pr_fix_gen_settings
設定ファイル生成スクリプトの整理
2 parents 0360baf + 14616be commit e53e41c

14 files changed

Lines changed: 143 additions & 945 deletions

python/generate_cnoid.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

python/generate_controller_config.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

python/generate_dxl_shm_config.py

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
11
#!/bin/python3
22
import argparse
3-
import numpy
4-
import os
53
import sys
64

7-
from distutils.util import strtobool
8-
9-
try:
10-
import cnoid.Body
11-
import cnoid.Util
12-
except ImportError:
13-
import sys
14-
import shutil
15-
choreonoid_bin_path = shutil.which('choreonoid')
16-
if choreonoid_bin_path is None:
17-
print('Error: choreonoid is not found.', file=sys.stderr)
18-
sys.exit(1)
19-
choreonoid_bin_dir_path = os.path.dirname(choreonoid_bin_path)
20-
choreonoid_share_path = os.path.join(choreonoid_bin_dir_path, '../share')
21-
chorenoid_ver = [dirname[dirname.find('choreonoid-')+len('choreonoid-'):] for dirname in os.listdir(choreonoid_share_path) if dirname.find('choreonoid-') != -1]
22-
if len(chorenoid_ver) > 0:
23-
chorenoid_ver = chorenoid_ver[0]
24-
else :
25-
chorenoid_ver = None
26-
choreonoid_python_path = os.path.join(choreonoid_bin_dir_path, '../lib/choreonoid-{}/python'.format(chorenoid_ver))
27-
print(choreonoid_python_path)
28-
if choreonoid_python_path is None or not os.path.exists(choreonoid_python_path):
29-
print('Error: choreonoid_python_path not found.', file=sys.stderr)
30-
sys.exit(1)
31-
sys.path.append(choreonoid_python_path)
32-
import cnoid.Body
33-
import cnoid.Util
5+
from irsl_choreonoid.robot_util import RobotModelWrapped as RobotModel
346

7+
def print_config(joint_names, output=None):
8+
"""
9+
Args:
10+
output (optional) : output file-stream. If None, sys.stdout is used
11+
"""
12+
if output is None:
13+
output = sys.stdout
14+
text = f"""\
15+
# This file is configuration for irsl_dynamixel_hardware_shm
3516
17+
dynamixel_hardware_shm:
18+
port_name: /dev/ttyUSB0
19+
baud_rate: 1000000
20+
joint:
21+
"""
22+
print(text, file=output)
23+
for jointname in joint_names:
24+
print(" #{}:".format(jointname), file=output)
25+
print(" - { ID: XX, DynamixelSettings: { Return_Delay_Time: 0, Operating_Mode: 3 } }", file=output)
26+
3627

3728
if __name__=='__main__':
3829
parser = argparse.ArgumentParser(
@@ -44,36 +35,7 @@
4435

4536
args = parser.parse_args()
4637
fname = args.bodyfile
47-
if not os.path.isfile(str(fname)):
48-
print("File is not exist.", file=sys.stderr)
49-
print("Please check file : {}".format(fname), file=sys.stderr)
50-
exit(1)
51-
rbody = cnoid.Body.BodyLoader().load(str(fname))
52-
if rbody is None:
53-
print("File is broken.", file=sys.stderr)
54-
print("Please check file : {}".format(fname), file=sys.stderr)
55-
exit(1)
56-
57-
rbody.updateLinkTree()
58-
rbody.initializePosition()
59-
rbody.calcForwardKinematics()
38+
robot = RobotModel.loadModel(fname)
39+
joint_names = robot.jointNames
6040

61-
num_joint = rbody.getNumJoints()
62-
joint_list = []
63-
for idx in range(num_joint):
64-
joint = rbody.getJoint(idx)
65-
joint_list.append(joint)
66-
print("# This file is configlation for irsl_dynamixel_hardware_shm")
67-
print("")
68-
print("_default_joint: &default_joint")
69-
print(" DynamixelSettings:")
70-
print(" { Return_Delay_Time: 0, Operating_Mode: 3 }")
71-
print("dynamixel_hardware_shm:")
72-
print(" port_name: /dev/ttyUSB0")
73-
print(" baud_rate: 1000000")
74-
print(" joint:")
75-
76-
for jointname in [j.jointName for j in joint_list]:
77-
print(" #{}:".format(jointname))
78-
print(" - { ID: XX, <<: *default_joint}")
79-
41+
print_config(joint_names)

python/generate_dynamixel_config.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

python/generate_jointlist.py

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
11
#!/bin/python3
22
import argparse
3-
import numpy
4-
import os
53
import sys
64

7-
from distutils.util import strtobool
8-
9-
try:
10-
import cnoid.Body
11-
import cnoid.Util
12-
except ImportError:
13-
import sys
14-
import shutil
15-
choreonoid_bin_path = shutil.which('choreonoid')
16-
if choreonoid_bin_path is None:
17-
print('Error: choreonoid is not found.', file=sys.stderr)
18-
sys.exit(1)
19-
choreonoid_bin_dir_path = os.path.dirname(choreonoid_bin_path)
20-
choreonoid_share_path = os.path.join(choreonoid_bin_dir_path, '../share')
21-
chorenoid_ver = [dirname[dirname.find('choreonoid-')+len('choreonoid-'):] for dirname in os.listdir(choreonoid_share_path) if dirname.find('choreonoid-') != -1]
22-
if len(chorenoid_ver) > 0:
23-
chorenoid_ver = chorenoid_ver[0]
24-
else :
25-
chorenoid_ver = None
26-
choreonoid_python_path = os.path.join(choreonoid_bin_dir_path, '../lib/choreonoid-{}/python'.format(chorenoid_ver))
27-
print(choreonoid_python_path)
28-
if choreonoid_python_path is None or not os.path.exists(choreonoid_python_path):
29-
print('Error: choreonoid_python_path not found.', file=sys.stderr)
30-
sys.exit(1)
31-
sys.path.append(choreonoid_python_path)
32-
import cnoid.Body
33-
import cnoid.Util
34-
5+
from irsl_choreonoid.robot_util import RobotModelWrapped as RobotModel
356

7+
def print_config(joint_names, output=None):
8+
"""
9+
Args:
10+
output (optional) : output file-stream. If None, sys.stdout is used
11+
"""
12+
if output is None:
13+
output = sys.stdout
14+
for jointname in joint_names:
15+
print(f'- "{jointname}"', file=output)
3616

3717
if __name__=='__main__':
3818
parser = argparse.ArgumentParser(
@@ -44,25 +24,7 @@
4424

4525
args = parser.parse_args()
4626
fname = args.bodyfile
47-
if not os.path.isfile(str(fname)):
48-
print("File is not exist.", file=sys.stderr)
49-
print("Please check file : {}".format(fname), file=sys.stderr)
50-
exit(1)
51-
rbody = cnoid.Body.BodyLoader().load(str(fname))
52-
if rbody is None:
53-
print("File is broken.", file=sys.stderr)
54-
print("Please check file : {}".format(fname), file=sys.stderr)
55-
exit(1)
56-
57-
rbody.updateLinkTree()
58-
rbody.initializePosition()
59-
rbody.calcForwardKinematics()
6027

61-
num_joint = rbody.getNumJoints()
62-
joint_list = []
63-
for idx in range(num_joint):
64-
joint = rbody.getJoint(idx)
65-
joint_list.append(joint)
66-
jointnames = [j.jointName for j in joint_list]
67-
for jointname in jointnames:
68-
print('- "{}"'.format(jointname))
28+
robot = RobotModel.loadModel(fname)
29+
joint_names = robot.jointNames
30+
print_config(joint_names)

0 commit comments

Comments
 (0)