-
Notifications
You must be signed in to change notification settings - Fork 1
/
trotPoints.py
350 lines (301 loc) · 10.5 KB
/
trotPoints.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# -*- coding: utf-8 -*-
"""
@author: Deep
leg no.s 1,2,3,4 form a Z from ForwardLeft leg
"""
import time
import math
import numpy as np
import plotView3d as view3d
import fourLegSimulator as fls
# from dxl_control.Ax12 import Ax12
#----------variables---------------------------------------------
# # create motor object
# femur = Ax12(2)
# tibia = Ax12(1)
# # connecting
# Ax12.open_port()
# Ax12.set_baudrate()
t_elapse_ref = 0 #variable initialised
TD = False
clr_height=4 # clearance height in cm
L_span = 8 #centimetres
v_d = 0.8 #10/11 #m/s
T_st = 2*L_span/(100*v_d) #0.18
T_sw = 0.15 #sec
T_stride = T_st + T_sw #0.33
precision=0.01
legNum=1
S_st_i=0
S_sw_i=0
phase=[0,0,0,0]
phase=np.array(phase)
loopStart=0
loopEnd=0
loopTime=0
dS_gallop=[0,0.2,0.55,0.75]#gallop
dS_trot=[0,0.5,0.5,0]#trot
dS_walk=[0,0.75,0.5,0.25]#walk
dS_pace=[0,0,0.5,0.5]# pace
dS_bound=[0,0.5,0,0.5] # bound
dS = []
dS = dS_trot
print(dS_trot)
t1=0
t2=0
t3=0
t4=0
t_elapse_ref=0.0 #change this value between 0 and 0.33(stride time)
t_i=[t1,t2,t3,t4]
t_i=np.array(t_i)
x=0 #coordinates initialized
y=0
z=0
lateral_fraction=0
bodyHeight=18
delta=-0.15
t_gt = 0.02 #transition starting time
dT_gt = 0.10 #duration of gait transition
bezierControlPoints=[[-L_span,0.0-bodyHeight],
[-L_span*1.4,0.0-bodyHeight],
[-L_span*1.5,clr_height*0.9-bodyHeight],
[-L_span*1.5,clr_height*0.9-bodyHeight],
[-L_span*1.5,clr_height*0.9-bodyHeight],
[0.0,clr_height*0.9-bodyHeight],
[0.0,clr_height*0.9-bodyHeight],
[0.0,clr_height*1.157-bodyHeight],
[L_span*1.5,clr_height*1.157-bodyHeight],
[L_span*1.5,clr_height*1.157-bodyHeight],
[L_span*1.4,0.0-bodyHeight],
[L_span,0.0-bodyHeight]]
bezierControlPoints=np.array(bezierControlPoints)
# k belongs to 0,1,2...11 for 12 bezier contol points
femur=12
tibia=11.5
hip=0
angles=[]
alpha_list=[]
beta_list=[]
gamma_list=[]
alpha_list2=[]
beta_list2=[]
gamma_list2=[]
alpha_list3=[]
beta_list3=[]
gamma_list3=[]
alpha_list4=[]
beta_list4=[]
gamma_list4=[]
x_list1 = []
z_list1 = []
x_list2 = []
z_list2 = []
x_list3 = []
z_list3 = []
x_list4 = []
z_list4 = []
# helix=0.1
def nCr(n,r):
f = math.factorial
return f(n) // f(r) // f(n-r)
def bernstein(S,N,K,v):
return nCr(N,K) * (1-S)**(N-K) * S**K * v
def legIK(x,y,z):
R = math.sqrt(x**2+y**2+z**2)
R1 = math.sqrt(z**2 + y**2)
theta1=math.atan2(-z,y)
theta2=math.acos(hip/R1)
alpha = (theta2-theta1)*180/math.pi
R2=math.sqrt(R**2-hip**2)
phi1=math.asin(x/R2)
# print(phi1*180/math.pi)
temp=(femur**2+R2**2-tibia**2)/(2*femur*R2)
# print(temp)
if temp>1:
temp=1
print('_______breach on positive side')
if temp<-1:
temp=-1
print('_______breach on negative side')
phi2=math.acos(temp)
# print(phi2*180/math.pi)
beta = (phi1-phi2)*180/math.pi
temp2=(femur**2+tibia**2-R2**2)/(2*femur*tibia)
if temp2>1:
temp2=1
print('_______breach on positive side')
if temp2<-1:
temp2=-1
print('_______breach on negative side')
psi=math.acos(temp2)
# print(psi*180/math.pi)
gamma = (math.pi-psi)*180/math.pi
return np.array([alpha,beta,gamma])
def lateralMotion(lateral_fraction, x):
X_POLAR = np.cos(lateral_fraction)
Y_POLAR = np.sin(lateral_fraction)
stepX = x * X_POLAR
stepY = x * Y_POLAR
return stepX, stepY
def gaitTransition(t, t_gt, dT_gt, current_gait, target_gait):
current_gait = np.array(current_gait)
target_gait = np.array(target_gait)
branch1 = (t - t_gt)/dT_gt
branch2 = 1
# pro tip- make a lambda func for SAT(x,y)
if branch1 <= branch2 and t >= t_gt:
dS = current_gait + (target_gait - current_gait)*(branch1)
elif branch1 <= branch2 and t < t_gt:
dS = current_gait
else:
dS = current_gait + (target_gait - current_gait)*(branch2)
print('PHASE MATRIX RIGHT NOW : ',end='')
print(dS)
return dS
#------------------------------------------------------------------
view3d.drawPoints3d([0,0,0])
view3d.drawCurve3d([[22,15,0],[-22,15,0],[-22,-15,0],[22,-15,0],[22,15,0]])
t = 0.0 #clock started
t_TD_ref = t #initialized from TouchDown
start=time.perf_counter()
for i in range(0,1): #no. of cycles
S_st_i=0
S_sw_i=0
print('Cycle no: ' + str(i+1))
TD=False
while(not TD):
# if precision<loopTime:
# precision=loopTime
# time.sleep(precision-(0.75*loopTime))
loopStart=time.perf_counter()
t_elapse_ref = t - t_TD_ref
print(t_elapse_ref)
TD=False
# -------------------------------------------------
# dS = gaitTransition(t, t_gt, dT_gt, dS_trot, dS_pace)
# ---------------------------------------------------
if t_elapse_ref >= T_stride:
t_elapse_ref = 0
TD = True
if TD:
t_TD_ref = t
print('\nTouchDown\n')
# print(t_elapse_ref)
t_i = t_elapse_ref - T_stride * np.array(dS)
# print(t_i)
for legTime in t_i:
print('Leg no: ' + str(legNum))
"""NOTE: PAPER WAS MISSING THIS LOGIC!!
This is to avoid a phase discontinuity if the user selects a Step Length and Velocity combination that causes Tstance > Tswing.
"""
if legTime < -T_sw: #old stance phase active
legTime += T_stride
print('slow stance case')
#time updated to current stance phase, triggers next 'if'
if legTime>0 and legTime<T_st: #current stance phase active
x=0
y=0
z=0
S_st_i = legTime/T_st
phase[legNum-1]=S_st_i
x=L_span*(1-2*S_st_i)+0
z=delta*(math.cos(math.pi*x/(2*L_span))+0 )- bodyHeight #this 0 is Pox and 18 Poy
x,y=lateralMotion(lateral_fraction,x)
# y+=helix
print('in current stance: '+ str(S_st_i))
print('Coords :'+'('+str(x)+', '+str(y)+', '+str(z)+')')
print('Angles :', end="")
angles=legIK(x, y, z)
print(angles)
if legTime>= -T_sw and legTime<=0: #old swing phase active
x=0
y=0
z=0
S_sw_i = (legTime+T_sw)/T_sw
phase[legNum-1]=S_sw_i
for index in range(0,12):
x+=bernstein(S_sw_i, 11, index, bezierControlPoints[index][0])
z+=bernstein(S_sw_i, 11, index, bezierControlPoints[index][1])
x,y=lateralMotion(lateral_fraction,x)
# y+=helix
print('in old swing: '+ str(S_sw_i))
print('Coords :'+'('+str(x)+', '+str(y)+', '+str(z)+')')
print('Angles :', end="")
angles=legIK(x, y, z)
print(angles)
elif legTime>= T_st and legTime<=T_stride: #current swing phase active
x=0
y=0
z=0
S_sw_i = (legTime-T_st)/T_sw
phase[legNum-1]=S_sw_i
for index in range(0,12):
x+=bernstein(S_sw_i, 11, index, bezierControlPoints[index][0])
z+=bernstein(S_sw_i, 11, index, bezierControlPoints[index][1])
x,y=lateralMotion(lateral_fraction,x)
# y+=helix
print('in current swing: '+ str(S_sw_i))
print('Coords :'+'('+str(x)+', '+str(y)+', '+str(z)+')')
print('Angles :', end="")
angles=legIK(x, y, z)
print(angles)
if legNum==1: #change this no. to change leg
view3d.drawPoints3d([x+20,y+15,z])
val0=angles[0]*math.pi/180
val1=angles[1]*math.pi/180
val2=angles[2]*math.pi/180
alpha_list.append(val0)
beta_list.append(val1)
gamma_list.append(val2)
x_list1.append(x)
z_list1.append(z)
# M1.set_position(val1)
# M2.set_position(val2)
if legNum==2: #change this no. to change leg
view3d.drawPoints3d([x+20,y-15,z])
val0=angles[0]*math.pi/180
val1=angles[1]*math.pi/180
val2=angles[2]*math.pi/180
alpha_list2.append(val0)
beta_list2.append(val1)
gamma_list2.append(val2)
x_list2.append(x)
z_list2.append(z)
if legNum==3: #change this no. to change leg
view3d.drawPoints3d([x-20,y+15,z])
val0=angles[0]*math.pi/180
val1=angles[1]*math.pi/180
val2=angles[2]*math.pi/180
alpha_list3.append(val0)
beta_list3.append(val1)
gamma_list3.append(val2)
x_list3.append((float(x) + 40))
z_list3.append(z)
if legNum==4: #change this no. to change leg
view3d.drawPoints3d([x-20,y-15,z])
val0=angles[0]*math.pi/180
val1=angles[1]*math.pi/180
val2=angles[2]*math.pi/180
alpha_list4.append(val0)
beta_list4.append(val1)
gamma_list4.append(val2)
x_list4.append((float(x)+40))
z_list4.append(z)
legNum+=1
print('----this leg done----\n')
legNum=1
print('********this time instant done for all legs**********')
t_elapse_ref +=precision
t +=precision
loopEnd=time.perf_counter()
loopTime=loopEnd-loopStart
# helix+=0.1
print('\n========================this cycle done============================\n')
print('\n\nTotal time taken: '+str(time.perf_counter()-start)+' sec')
fls.fourLegSimulator(beta_list, gamma_list, beta_list2, gamma_list2, beta_list3, gamma_list3, beta_list4, gamma_list4, bodyHeight, femur, tibia)
# sf.One_Leg_Simulation(beta_list, gamma_list)
# sf.One_Leg_Simulation(beta_list2, gamma_list2)
# # disconnect
# femur.disable_torque()
# tibia.disable_torque()
# Ax12.close_port()