/
broken_ant.py
146 lines (138 loc) · 3.68 KB
/
broken_ant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""component_lib: BrokenAnt. Broken legs are pre-defined."""
import numpy as np
def generate_ant_config_with_n_legs(n: int, broken_id: int):
"""Generate info for n-legged ant."""
def template_leg(theta, ind):
tmp = f"""
bodies {{
name: "Aux 1_{str(ind)}"
colliders {{
rotation {{ x: 90 y: -90 }}
capsule {{
radius: 0.08
length: 0.4428427219390869
}}
}}
inertia {{ x: 1.0 y: 1.0 z: 1.0 }}
mass: 1
}}
bodies {{
name: "$ Body 4_{str(ind)}"
colliders {{
rotation {{ x: 90 y: -90 }}
capsule {{
radius: 0.08
length: 0.7256854176521301
end: -1
}}
}}
inertia {{ x: 1.0 y: 1.0 z: 1.0 }}
mass: 1
}}
joints {{
name: "torso_Aux 1_{str(ind)}"
parent_offset {{ x: {((0.4428427219390869/2.)+.08)*np.cos(theta)} y: {((0.4428427219390869/2.)+.08)*np.sin(theta)} }}
child_offset {{ }}
parent: "torso"
child: "Aux 1_{str(ind)}"
stiffness: 5000.0
angular_damping: 35
angle_limit {{ min: -30.0 max: 30.0 }}
rotation {{ y: -90 }}
reference_rotation {{ z: {theta*180/np.pi} }}
}}
joints {{
name: "Aux 1_$ Body 4_{str(ind)}"
parent_offset {{ x: {0.4428427219390869/2. - .08} }}
child_offset {{ x:{-0.7256854176521301/2. + .08} }}
parent: "Aux 1_{str(ind)}"
child: "$ Body 4_{str(ind)}"
stiffness: 5000.0
angular_damping: 35
rotation: {{ z: 90 }}
angle_limit {{
min: 30.0
max: 70.0
}}
}}
actuators {{
name: "torso_Aux 1_{str(ind)}"
joint: "torso_Aux 1_{str(ind)}"
strength: 350.0
torque {{}}
}}
actuators {{
name: "Aux 1_$ Body 4_{str(ind)}"
joint: "Aux 1_$ Body 4_{str(ind)}"
strength: 350.0
torque {{}}
}}
"""
collides = (f'Aux 1_{str(ind)}', f'$ Body 4_{str(ind)}')
return tmp, collides
def broken_leg(theta, ind):
tmp = f"""
bodies {{
name: "Aux 1_{str(ind)}"
colliders {{
rotation {{ x: 90 y: -90 }}
capsule {{
radius: 0.08
length: 0.4428427219390869
}}
}}
inertia {{ x: 1.0 y: 1.0 z: 1.0 }}
mass: 1
}}
joints {{
name: "torso_Aux 1_{str(ind)}"
parent_offset {{ x: {((0.4428427219390869/2.)+.08)*np.cos(theta)} y: {((0.4428427219390869/2.)+.08)*np.sin(theta)} }}
child_offset {{ }}
parent: "torso"
child: "Aux 1_{str(ind)}"
stiffness: 5000.0
angular_damping: 35
angle_limit {{ min: -30.0 max: 30.0 }}
rotation {{ y: -90 }}
reference_rotation {{ z: {theta*180/np.pi} }}
}}
actuators {{
name: "torso_Aux 1_{str(ind)}"
joint: "torso_Aux 1_{str(ind)}"
strength: 350.0
torque {{}}
}}
"""
collides = (f'Aux 1_{str(ind)}', )
return tmp, collides
base_config = f"""
bodies {{
name: "torso"
colliders {{
capsule {{
radius: 0.25
length: 0.5
end: 1
}}
}}
inertia {{ x: 1.0 y: 1.0 z: 1.0 }}
mass: 10
}}
"""
collides = ('torso',)
for i in range(n):
if i == broken_id:
config_i, collides_i = broken_leg((1. * i / n) * 2 * np.pi, i)
else:
config_i, collides_i = template_leg((1. * i / n) * 2 * np.pi, i)
base_config += config_i
collides += collides_i
return base_config, collides
def get_specs(num_legs: int = 4, broken_id: int = 0):
assert broken_id <= num_legs - 1
message_str, collides = generate_ant_config_with_n_legs(num_legs, broken_id)
return dict(
message_str=message_str,
collides=collides,
root='torso',
term_fn=None)