-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathCalc.py
420 lines (339 loc) · 14.3 KB
/
pathCalc.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 28 16:47:28 2024
@author: mateo-drr
"""
import matplotlib.pyplot as plt
import numpy as np
import spatialmath as sm
from roboticstoolbox import quintic
from itertools import chain
#Metal phantom
# size: 5x17 [cm]
# it has 9 rough, 8 smooth each 1 cm
# middle point lands in stripe #5 and its position is 2.5x8.5 [cm]
#table = [64.5,100,89] #[w,l,h][cm]
def curvedScene(config,flip,swift=False):
if not config['angleDiv']: #ie alpha needs to be calculated
config['alphaL'] = calcAlpha(config['stopsL'],config['maxRotL'])
config['alphaW'] = calcAlpha(config['stopsW'],config['maxRotW'])
#Calculate the positions of the stops along lenght
config['pitsL'],config['stopsL'] = pitStopsAng(config['alphaL'],
config['maxRotL'],
config['rad'])
#Project the 2d coordinates into 3d
tcoordL,trotL = projPath3dAng(config['pitsL'],
config,
path='length',flip=flip,swift=swift)
#Calculate the position of the stops along width
config['pitsW'],config['stopsW'] = pitStopsAng(config['alphaW'],
config['maxRotW'],
config['rad'])
#Project the 2d coordinates into 3d
tcoordW,trotW = projPath3dAng(config['pitsW'],
config,
path='width',flip=flip,swift=swift)
return tcoordL+tcoordW,trotL+trotW
def linearScene(config,flip):
#Calculate the positions of the stops along lenght
config['pitsL'] = pitStopsLin(config['shape'],
config['stopsL'],
config['rad'],
path='length')
#Project the 2d coordinates into 3d
tcoordL,trotL = projPath3dLin(config['pitsL'],
config['point-base'],
config['rad'],
config['flange'],
path='length',flip=flip)
#Calculate the positions of the stops along width
config['pitsW'] = pitStopsLin(config['shape'],
config['stopsW'],
config['rad'],
path='width')
#Project the 2d coordinates into 3d
tcoordW,trotW = projPath3dLin(config['pitsW'],
config['point-base'],
config['rad'],
config['flange'],
path='width',flip=flip)
return tcoordL+tcoordW,trotL+trotW
def rotationScene(config,flip,swift=False):
#Same as curved scene but simply with a very small radius
#to avoid being in the water we need to use an offset
#config['radOffset'] = copy.deepcopy(config['rad'])
config['rad'] = 1 #this value is used to calculate the rotation and wont
#affect position
if not config['angleDiv']: #ie alpha needs to be calculated
config['alphaL'] = calcAlpha(config['stopsL'],config['maxRotL'])
config['alphaW'] = calcAlpha(config['stopsW'],config['maxRotW'])
#Calculate the positions of the stops along lenght
config['pitsL'],config['stopsL'] = pitStopsAng(config['alphaL'],
config['maxRotL'],
config['rad'],
)
#Project the 2d coordinates into 3d
tcoordL,trotL = projPath3dRot(config['pitsL'],
config,
path='length',flip=flip,swift=swift)
#Calculate the position of the stops along width
config['pitsW'],config['stopsW'] = pitStopsAng(config['alphaW'],
config['maxRotW'],
config['rad'],
)
#Project the 2d coordinates into 3d
tcoordW,trotW = projPath3dRot(config['pitsW'],
config,
path='width',flip=flip,swift=swift)
return tcoordL+tcoordW,trotL+trotW
def pitStopsAng(alpha_t,maxRot,rad,offset=0):
stops_t = maxRot/alpha_t
stops = round(stops_t)
alpha = maxRot/stops
#This would be for half of the path
#Allowing us to always sample extremes and center point
stops = stops*2+1
print(f'Given the angle {alpha_t}, there would be {stops_t*2 +1} stops total',
f'\nRounding to {alpha} deg., with {stops} stops total')
theta = 90-maxRot
pitsA = []
for i in range(stops):
xdist = np.cos(np.radians(alpha*i+theta))*rad
ydist = np.sin(np.radians(alpha*i+theta))*rad + offset
pitsA.append((xdist,ydist))
return pitsA,stops
def calcAlpha(stops_t,maxRot):
if stops_t % 2 == 0:
stops_t +=1
print('Adding an extra stop, total of',stops_t,'stops')
alpha = maxRot/(int(stops_t/2))
return alpha
def plotPathAng(pitsA,rad):
plt.figure(dpi=200)
for stop in pitsA:
plt.plot(stop[0],stop[1],marker='x', markersize=6,linewidth=5)
plt.xlim(-rad - 0.5, rad + 0.5) # Set x-axis limits
plt.ylim(-rad - 0.5, rad + 0.5) # Set y-axis limits
plt.show()
def pathOffset(offset):
xf,zf,yf = offset
#Offset the targets to match the probe position
coordinates = coord2SE3(-xf, -yf, -zf, scaling=0.01)#sm.SE3.Tx(-xf*0.01) * sm.SE3.Ty(-yf*0.01) * sm.SE3.Tz(-zf*0.01)
rotation = rot2SE3(0, 0, 0)#sm.SE3.Rx(0) * sm.SE3.Ry(0) * sm.SE3.Rz(0)
return coordinates * rotation
def projPath3dAng(pitsA,config,path,flip,swift=False):
aa,bb=[],[]
for point in pitsA:
xs,zs,ys = config['point-base']
print(point)
if path == 'length':
aa.append([(point[0] + xs) * 0.01,
ys * 0.01,
(zs - point[1]) * 0.01])
elif path == 'width':
aa.append([(xs) * 0.01,
(point[0] + ys) * 0.01,
(zs - point[1]) * 0.01])
#calculate rotation angle to keep the probe facing the point
distX = pitsA[len(pitsA)//2][0] - point[0] #center point - stop point
ang = np.degrees(np.arcsin(distX/config['rad'])) #angle to rotate the end-effector
if swift:
bAng=90
else:
bAng=0
if path == 'length':
bb.append([0,-bAng+ang,config['flange']])
elif path =='width':
bb.append([-ang,-bAng,config['flange']])
#bb.append([0,270,0])
return aa,bb
def projPath3dRot(pitsA,config,path,flip,swift=False):
aa,bb=[],[]
for point in pitsA:
xs,zs,ys = config['point-base']
print(point)
aa.append([(xs) * 0.01,
ys * 0.01,
(zs - config['radOffset']) * 0.01])
#calculate rotation angle to keep the probe facing the point
distX = pitsA[len(pitsA)//2][0] - point[0] #center point - stop point
ang = np.degrees(np.arcsin(distX/config['rad'])) #angle to rotate the end-effector
if swift:
bAng=90
else:
bAng=0
if path == 'length':
bb.append([0,-bAng+ang,config['flange']])
elif path =='width':
bb.append([-ang,-bAng,config['flange']])
#bb.append([0,270,0])
return aa,bb
def encodeStops(tcoord,trot,flangeOffset,rotation=True):
#Calculate offset transformation matrix
offset = pathOffset(flangeOffset)
targets = []
probeTargets=[]
for i in range(len(tcoord)):
coordinates = coord2SE3(*tcoord[i]) #* unpacks the values of the list
if rotation:
rotation = rot2SE3(*trot[i])
targetEndPose = coordinates * rotation
else:
targetEndPose = coordinates
targets.append(targetEndPose * offset)
probeTargets.append(targetEndPose)
return targets,probeTargets
def encodeStop(coord,rot):
#TODO this is messy, switching z and y
coordinates = coord2SE3(coord[0],coord[2],coord[1],scaling=0.01) #sm.SE3.Tx(coord[0]*0.01) * sm.SE3.Ty(coord[2]*0.01) * sm.SE3.Tz(coord[1]*0.01)
rotation = rot2SE3(*rot)#sm.SE3.Rx(rot[0], unit='deg') * sm.SE3.Ry(rot[1], unit='deg') * sm.SE3.Rz(rot[2], unit='deg')
return coordinates * rotation
def getQuat(target,numpy=True):
temp = sm.UnitQuaternion(target)
if numpy:
return temp.A
else:
return temp
def pitStopsLin(shape,stops,rad,path):
if path == 'length':
idx=0
elif path == 'width':
idx=1
#get the size of the path
size = shape[idx]
#get the midpoint
mid = size/2
#calculate the stops along the path
spacing = size/(stops+1)
pits=[]
for i in range(stops):
pits.append((spacing*(i+1) - mid,rad))
return pits
def projPath3dLin(pits,middlepoint,rad,flange,path,flip,swift=False):
aa,bb=[],[]
for point in pits:
xs,zs,ys = middlepoint
if path == 'length':
aa.append([(point[0] + xs) * 0.01,
ys * 0.01,
(zs - point[1]) * 0.01])
elif path == 'width':
aa.append([(xs) * 0.01,
(point[0] + ys) * 0.01,
(zs - point[1]) * 0.01])
#no rotation needed in this case
if swift:
bAng=90
else:
bAng=0
if path == 'length':
bb.append([0,-bAng,flange])
elif path =='width':
bb.append([0,-bAng,flange])
return aa,bb
def slerp(q0, q1, t):
"""
Spherical linear interpolation between two quaternions w<xyz>
:type q0: numpy.array
:param q0: 4 x 1 vector representation of a quaternion q = [q0;qv]
:type q1: numpy.array
:param q1: 4 x 1 vector representation of a quaternion q = [q0;qv]
:type t: number
:param t: interpolation parameter in the range [0,1]
:rtype: numpy.array
:returns: the 4 x 1 interpolated quaternion
https://github.com/rpiRobotics/rpi_general_robotics_toolbox_py/blob/master/src/general_robotics_toolbox/general_robotics_toolbox.py
"""
assert (t >= 0 and t <= 1), "t must be in the range [0,1]"
q0 = q0/np.linalg.norm(q0)
q1 = q1/np.linalg.norm(q1)
if (np.dot(q0,q1) < 0):
q0 = -q0
theta = np.arccos(np.dot(q0,q1))
if (np.abs(theta) < 1e-6):
return q0
q = (np.sin((1-t)*theta)*q0 + np.sin(t*theta)*q1)/np.sin(theta)
return q/np.linalg.norm(q)
def coord2SE3(xc,yc,zc,scaling=1):
return sm.SE3.Tx(xc*scaling) * sm.SE3.Ty(yc*scaling) * sm.SE3.Tz(zc*scaling)
def rot2SE3(xr,yr,zr,unit='deg'):
return sm.SE3.Rx(xr,unit=unit) * sm.SE3.Ry(yr,unit=unit) * sm.SE3.Rz(zr,unit=unit)
def splitCalc(num_splits):
# Calculate the step size
step_size = 1 / (num_splits + 1)
# Generate the split points using a list comprehension
splits = [step_size * (i + 1) for i in range(num_splits)]
return splits
def slerpCalc(coord,quatern,split):
tlist = splitCalc(split)
interpol = []
for t in tlist:
interpol.append(slerp(coord, quatern, t))
return interpol
def interpolateCoord(tcoord,speed,period):
checkpoints = []
for i in range(1,len(tcoord)):
#get the distance
dist = distCalc(tcoord[i-1], tcoord[i])
#calculate time needed
ttot = dist/speed
#create the time array
tarr = np.arange(0,ttot+period,period)
#interpolate coordinates
checkpointsX = quintic(tcoord[i-1][0],tcoord[i][0],tarr)
checkpointsY = quintic(tcoord[i-1][1],tcoord[i][1],tarr)
checkpointsZ = quintic(tcoord[i-1][2],tcoord[i][2],tarr)
checkpoints.append([])
#loop the checkpoints between each target
for j in range(len(checkpointsX.q)):
#get just the coordinate/rot
ckX,ckY,ckZ = checkpointsX.q[j],checkpointsY.q[j],checkpointsZ.q[j]
#fix starting point
if j == 0:
ckX,ckY,ckZ = tcoord[i-1]
#fix end point and save it only if it's the last target
if i == len(tcoord)-1 and j == len(checkpointsX.q)-1:
ckX,ckY,ckZ = tcoord[i]
checkpoints[i-1].append([ckX,ckY,ckZ])
#save all points except last one
if j != len(checkpointsX.q)-1:
checkpoints[i-1].append([ckX,ckY,ckZ])
#get the number of checkpoints between each target
numint = [len(c) for c in checkpoints]
return checkpoints,numint
def interpolateRot(quaternions,numint):
print(numint)
print(quaternions)
#calculate slerp
checkrot = []
for i in range(1,len(quaternions)):
#fix num stops because last stop includes the first and last position
nint = numint[i-1]-1 if i != len(quaternions)-1 else numint[i-1]-2
#append initial quaternion
checkrot.append(quaternions[i-1])
#append interpolated quaternions
#if nint > 0
checkrot += [q for q in slerpCalc(quaternions[i - 1], quaternions[i], nint)]
#Add the last target only
if i == len(quaternions)-1:
checkrot.append(quaternions[i])
return sm.UnitQuaternion(checkrot).A
def distCalc(start, end):
#input has to be [x,y,z]
xdist = np.abs(start[0]-end[0])
ydist = np.abs(start[1]-end[1])
zdist = np.abs(start[2]-end[2])
dist = np.sqrt(xdist**2 + ydist**2 + zdist**2)
return dist
def interpolateTargets(tcoord,speed,timer_period,targets,offset):
#Calculate interpolation of position
tcoordInt,numint = interpolateCoord(tcoord, speed, timer_period)
#Calculate quaternions
quat = getQuat(targets)
#Calculate slerp
quatInt = interpolateRot(quat, numint)
#flatten the list
tcoordInt = list(chain.from_iterable(tcoordInt))
#transform coordinates to se3 without rotation (not necessary)
_,targetsInt = encodeStops(tcoordInt, None, offset, rotation=False)
return targetsInt, quatInt, numint