-
Notifications
You must be signed in to change notification settings - Fork 8
/
kinematics_bittle.py
251 lines (224 loc) · 8.38 KB
/
kinematics_bittle.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
# coding: utf-8
import ikpy
from ikpy.chain import Chain
from ikpy.link import OriginLink, URDFLink
import numpy as np
from numpy import sin, cos, pi
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
deg2rad = pi/180
rad2deg = 180/pi
# Dimensions Bittle (should not be changed - all values in cm)
upperArmLength = 4.5
lowerArmLength = 5.0
bodyLength = 10.5
bodyWidth = 9.7
# Variable parameters
distanceFloor = 6.0 # Distance torso to floor
stepLength = 3.5 # Longitudinal movement of each paw within one period
swingHeight = 0.5 # Amplitude for swing
swingFactorLegs = 5 # Increase swing amplitude for legs
leanForward = 0.0 # Shifting torso in x-direction
stanceFront = 0.0 # Distance of front paws from shoulder in x-direction
stanceBack = 3 # Distance of back paws from hip in negative x-direction
# Number of Frames
frames = 57
# Kinematic chain of Bittle
left_arm = Chain(name='left_arm', links=[
URDFLink(
name="center",
origin_translation=[leanForward, 0, distanceFloor],
origin_orientation=[0, 0, 0],
rotation=[0, 0, 0],
),
URDFLink(
name="shoulder",
origin_translation=[0, bodyWidth/2, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 1, 0],
bounds=(0.1*deg2rad, 179.9*deg2rad),
),
URDFLink(
name="upperArm",
origin_translation=[upperArmLength, 0, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 1, 0],
bounds=(-179.9*deg2rad, -0.1*deg2rad),
),
URDFLink(
name="lowerArm",
origin_translation=[lowerArmLength, 0, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 0, 0],
)
])
right_arm = Chain(name='right_arm', links=[
URDFLink(
name="center",
origin_translation=[leanForward, 0, distanceFloor],
origin_orientation=[0, 0, 0],
rotation=[0, 0, 0],
),
URDFLink(
name="shoulder",
origin_translation=[0, -bodyWidth/2, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 1, 0],
bounds=(0.1*deg2rad, 179.9*deg2rad),
),
URDFLink(
name="upperArm",
origin_translation=[upperArmLength, 0, 0],
origin_orientation=[0,0,0],
rotation=[0,1,0],
bounds=(-179.9*deg2rad, -0.1*deg2rad),
),
URDFLink(
name="lowerArm",
origin_translation=[lowerArmLength, 0, 0],
origin_orientation=[0,0,0],
rotation=[0, 1, 0],
)
])
left_leg = Chain(name='left_leg', links=[
URDFLink(
name="center",
origin_translation=[leanForward, 0, distanceFloor],
origin_orientation=[0, 0, 0],
rotation=[0, 0, 0],
),
URDFLink(
name="butt",
origin_translation=[-bodyLength, 0, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 0, 0],
),
URDFLink(
name="hip",
origin_translation=[0, bodyWidth/2, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 1, 0],
bounds=(0.1*deg2rad, 200*deg2rad),
),
URDFLink(
name="upperLeg",
origin_translation=[upperArmLength, 0, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 1, 0],
bounds=(-200*deg2rad, -0.1*deg2rad),
),
URDFLink(
name="lowerLeg",
origin_translation=[lowerArmLength, 0, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 1, 0],
)
])
right_leg = Chain(name='right_leg', links=[
URDFLink(
name="center",
origin_translation=[leanForward, 0, distanceFloor],
origin_orientation=[0, 0, 0],
rotation=[0, 0, 0],
),
URDFLink(
name="butt",
origin_translation=[-bodyLength, 0, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 0, 0],
),
URDFLink(
name="hip",
origin_translation=[ 0, -bodyWidth/2, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 1, 0],
bounds=(0.1*deg2rad, 200*deg2rad),
),
URDFLink(
name="upperLeg",
origin_translation=[upperArmLength, 0, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 1, 0],
bounds=(-200*deg2rad, -0.1*deg2rad),
),
URDFLink(
name="lowerLeg",
origin_translation=[lowerArmLength, 0, 0],
origin_orientation=[0, 0, 0],
rotation=[0, 1, 0],
)
])
def buildGait(frames):
frame = np.arange(0,frames)
swingEnd = pi/2
# longitudinal Movement
swing = -cos(2*(frame*2*pi/frames))
stance = +cos(2/3*(frame*2*pi/frames-swingEnd))
swingSlice = np.less_equal(frame,swingEnd/(2*pi/frames))
stanceSlice = np.invert(swingSlice)
longitudinalMovement = np.concatenate((swing[swingSlice], stance[stanceSlice]))
longitudinalMovement = np.concatenate((longitudinalMovement,longitudinalMovement, longitudinalMovement, longitudinalMovement))
# vertical Movement
lift = sin(2*(frame*2*pi/frames)) #np.ones(frames)
liftSlice = swingSlice
verticalMovement = np.concatenate((lift[liftSlice], np.zeros(np.count_nonzero(stanceSlice))))
verticalMovement = np.concatenate((verticalMovement, verticalMovement, verticalMovement, verticalMovement))
return longitudinalMovement, verticalMovement
longitudinalMovement, verticalMovement = buildGait(frames)
plt.plot(longitudinalMovement)
plt.plot(verticalMovement)
plt.show()
plt.close()
shiftFrame = np.round(np.array([0, pi/2, pi, 3*pi/2])/2/pi*frames)
#shiftFrame = np.round(np.array([0, pi, pi, 0])/2/pi*frames)
#shiftFrame = np.round(np.array([0, 0, pi, pi])/2/pi*frames)
shiftFrame = shiftFrame.astype(int)
for frame in range(0, frames):
right_arm_angles_raw = right_arm.inverse_kinematics(target_position = np.array([stanceFront+stepLength*longitudinalMovement[frame], -bodyWidth/2 ,swingHeight*verticalMovement[frame]]), optimization_method='SLSQP')
right_arm_correction = np.array([0, -pi/2, pi/2, 0])
right_arm_angles = np.round(rad2deg*(right_arm_angles_raw+right_arm_correction))
right_arm_angles = np.delete(right_arm_angles, np.s_[0,3], axis=0)
right_arm_angles = right_arm_angles.astype(int)
right_leg_angles_raw = right_leg.inverse_kinematics(target_position = np.array([-stanceBack-bodyLength+stepLength*longitudinalMovement[frame+shiftFrame[1]], -bodyWidth/2 , swingFactorLegs*swingHeight*verticalMovement[frame+shiftFrame[1]]]), optimization_method='SLSQP')
right_leg_correction = np.array([0, 0, -pi/2, pi/2, 0])
right_leg_angles = np.round(rad2deg*(right_leg_angles_raw+right_leg_correction))
right_leg_angles = np.delete(right_leg_angles, np.s_[0,1,4], axis=0)
right_leg_angles = right_leg_angles.astype(int)
left_arm_angles_raw = left_arm.inverse_kinematics(target_position = np.array([stanceFront+stepLength*longitudinalMovement[frame+shiftFrame[2]], +bodyWidth/2 , swingHeight*verticalMovement[frame+shiftFrame[2]]]), optimization_method='SLSQP')
left_arm_correction = np.array([0, -pi/2, pi/2, 0])
left_arm_angles = np.round(rad2deg*(left_arm_angles_raw+left_arm_correction))
left_arm_angles = np.delete(left_arm_angles, np.s_[0,3], axis=0)
left_arm_angles = left_arm_angles.astype(int)
left_leg_angles_raw = left_leg.inverse_kinematics(target_position = np.array([-stanceBack-bodyLength+stepLength*longitudinalMovement[frame+shiftFrame[3]], +bodyWidth/2 , swingFactorLegs*swingHeight*verticalMovement[frame+shiftFrame[3]]]), optimization_method='SLSQP')
left_leg_correction = np.array([0, 0, -pi/2, pi/2, 0])
left_leg_angles = np.round(rad2deg*(left_leg_angles_raw+left_leg_correction))
left_leg_angles = np.delete(left_leg_angles, np.s_[0,1,4],axis=0)
left_leg_angles = left_leg_angles.astype(int)
# Writing sequence to file
gait_sequence = np.concatenate((left_arm_angles, right_arm_angles, right_leg_angles, left_leg_angles))
print(frame," ",gait_sequence)
f = open("gait_sequence.csv", "a")
#f.write("[")
np.savetxt(f, gait_sequence[[0,2,4,6,1,3,5,7]], fmt='%3.1i', delimiter=',', newline=', ')
#f.write("],")
f.write("\n")
f.close()
# Create plot and image for each frame
fig = plt.figure()
ax = p3.Axes3D(fig)
ax.set_box_aspect([3, 3/2,1])
ax.set_xlim3d([-20, 10])
ax.set_xlabel('X')
ax.set_ylim3d([-10, 10])
ax.set_ylabel('Y')
ax.set_zlim3d([0.0, 10])
ax.set_zlabel('Z')
right_arm.plot(right_arm_angles_raw, ax)
right_leg.plot(right_leg_angles_raw, ax)
left_arm.plot(left_arm_angles_raw, ax)
left_leg.plot(left_leg_angles_raw, ax)
figureName = "Bittle_" + str(frame)
plt.savefig(figureName)
plt.close()
#plt.show()