Skip to content

Instantly share code, notes, and snippets.

@Giromi
Last active March 15, 2024 12:04
Show Gist options
  • Save Giromi/49134235f0e6dfe4266af22aba49a008 to your computer and use it in GitHub Desktop.
Save Giromi/49134235f0e6dfe4266af22aba49a008 to your computer and use it in GitHub Desktop.
로보틱스 교수님 코드 리뷰 및 개선
print('\n\n< Prof. code >')
print('-----------------------------------------')
num_links = 0
num_total_joints = 0
num_fixed_joints = 0
num_revolute_joints = 0
num_prismatic_joints = 0
processed_links = set()
processed_joints = set()
dof = 0
# Check each link and joint in the URDF file
for link in root.findall('.//link'): # tag
link_name = link.get('name') # [!] None 체크 안함 => 안하면 런타임 시간 낭비
if 'link' in link_name.lower():
num_links += 1 # [!] set을 사용하기 때문에 굳이 안에서 증감할 필요 없음 (my.py 참고)
processed_links.add(link_name)
for joint in root.findall('.//joint'):
parent_link = joint.find('parent').get('link') # [!] None 체크 안함 => 프로그램 터질 수 있음
child_link = joint.find('child').get('link') # [!] None 체크 안함 => 프로그램 터질 수 있음
joint_type = joint.get('type') # [!] None 체크 안함 (이건 안해도 괜춘할듯)
if parent_link in processed_links and child_link in processed_links:
joint_name = joint.get('name')
if joint_name not in processed_joints: # [!] set이기 때문에 중복 체크할 필요없음
num_total_joints += 1 # [!] set을 사용하기 때문에 굳이 증감할 필요 없음 (my.py 참고)
processed_joints.add(joint_name)
if joint_type == 'fixed': # [!] if문 남발은 좋지 않음 타입이 여러개면 유지보수 힘듦 (my.py 참고)
num_fixed_joints += 1
elif joint_type == 'revolute':
num_revolute_joints += 1
elif joint_type == 'prismatic':
num_prismatic_joints += 1
# Calculate DOF using Grubler's formula
m = 6
N = num_links
J = num_total_joints
sum_f_i = 0 * num_fixed_joints + 1 * num_revolute_joints + 1 * num_prismatic_joints
dof = m * (N - 1 - J) + sum_f_i
# Print the results
print("DOF:", dof)
print("Number of links with names containing 'link':", num_links)
print("Total number of joints connecting these links:", num_total_joints)
print("Number of fixed joints:", num_fixed_joints)
print("Number of revolute joints:", num_revolute_joints)
print("Number of prismatic joints:", num_prismatic_joints)
print('\n\n< My code >')
print('-----------------------------------------')
num_links = 0
num_total_joints = 0
num_fixed_joints = 0
num_revolute_joints = 0
num_prismatic_joints = 0
processed_links = set()
processed_joints = set()
dof = 0
joint_types_count = { 'fixed': 0, 'revolute': 0, 'prismatic': 0 }
# Check each link and joint in the URDF file
for link in root.findall('.//link'): # tag
link_name = link.get('name')
if link_name == None:
continue
if 'link' in link_name.lower():
processed_links.add(link_name)
for joint in root.findall('.//joint'):
parent = joint.find('parent')
child = joint.find('child')
if parent == None or child == None:
continue
parent_link = parent.get('link')
child_link = child.get('link')
joint_name = joint.get('name')
joint_type = joint.get('type')
if joint_name == None or joint_type == None:
continue
if parent_link in processed_links and child_link in processed_links:
processed_joints.add(joint_name)
joint_types_count[joint_type] = joint_types_count.get(joint_type, 0) + 1
num_links = len(processed_links) # [V] set 길이만 쟤면 됨
num_total_joints = len(processed_joints)
num_fixed_joints = joint_types_count['fixed'] # [V] if문 남발 안하고 그냥 대입하면 끝
num_revolute_joints = joint_types_count['revolute']
num_prismatic_joints = joint_types_count['prismatic']
# Calculate DOF using Grubler's formula
m = 6
N = num_links
J = num_total_joints
sum_f_i = 0 * num_fixed_joints + 1 * num_revolute_joints + 1 * num_prismatic_joints
dof = m * (N - 1 - J) + sum_f_i
# Print the results
print("DOF:", dof)
print("Number of links with names containing 'link':", num_links)
print("Total number of joints connecting these links:", num_total_joints)
print("Number of fixed joints:", num_fixed_joints)
print("Number of revolute joints:", num_revolute_joints)
print("Number of prismatic joints:", num_prismatic_joints)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment