|
1 | 1 | #!/bin/python3 |
2 | 2 | import argparse |
3 | | -import numpy |
4 | | -import os |
5 | 3 | import sys |
6 | 4 |
|
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 |
34 | 6 |
|
| 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 |
35 | 16 |
|
| 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 | + |
36 | 27 |
|
37 | 28 | if __name__=='__main__': |
38 | 29 | parser = argparse.ArgumentParser( |
|
44 | 35 |
|
45 | 36 | args = parser.parse_args() |
46 | 37 | 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 |
60 | 40 |
|
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) |
0 commit comments