Skip to content

Commit 985969e

Browse files
committed
experiments
1 parent 733f395 commit 985969e

File tree

12 files changed

+72442
-2
lines changed

12 files changed

+72442
-2
lines changed

examples/expNeo1.py

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
#!/usr/bin/env python
2+
"""
3+
@author Jesse Haviland
4+
"""
5+
6+
import roboticstoolbox as rp
7+
import spatialmath as sm
8+
import numpy as np
9+
import time
10+
import qpsolvers as qp
11+
import pybullet as p
12+
import matplotlib
13+
import matplotlib.pyplot as plt
14+
import pickle
15+
16+
matplotlib.rcParams['pdf.fonttype'] = 42
17+
matplotlib.rcParams['ps.fonttype'] = 42
18+
plt.style.use('ggplot')
19+
matplotlib.rcParams['font.size'] = 4.5
20+
matplotlib.rcParams['lines.linewidth'] = 0.5
21+
matplotlib.rcParams['xtick.major.size'] = 1.5
22+
matplotlib.rcParams['ytick.major.size'] = 1.5
23+
matplotlib.rcParams['axes.labelpad'] = 1
24+
plt.rc('grid', linestyle="-", color='#dbdbdb')
25+
26+
fig, ax = plt.subplots()
27+
fig.set_size_inches(2.5, 1.5)
28+
29+
ax.set(xlabel='Time (s)', ylabel='Distance (m)')
30+
ax.grid()
31+
plt.grid(True)
32+
ax.set_xlim(xmin=0, xmax=12.1)
33+
ax.set_ylim(ymin=0, ymax=0.7)
34+
plt.subplots_adjust(left=0.13, bottom=0.18, top=0.95, right=1)
35+
36+
37+
pld0 = ax.plot(
38+
[0], [0], label='Distance to Obstacle 1')
39+
40+
pld1 = ax.plot(
41+
[0], [0], label='Distance to Obstacle 2')
42+
43+
pld2 = ax.plot(
44+
[0], [0], label='Distance to Goal')
45+
46+
plm = ax.plot(
47+
[0], [0], label='Manipuability')
48+
49+
ax.legend()
50+
ax.legend(loc="lower right")
51+
52+
plt.ion()
53+
plt.show()
54+
55+
qdmax = np.array([
56+
2.1750, 2.1750, 2.1750, 2.1750, 2.6100, 2.6100, 2.6100,
57+
5000000, 5000000, 5000000, 5000000, 5000000, 5000000])
58+
lb = -qdmax
59+
ub = qdmax
60+
61+
q0 = [-0.5653, -0.1941, -1.2602, -0.7896, -2.3227, -0.3919, -2.5173]
62+
63+
s0 = rp.Shape.Sphere(
64+
radius=0.05,
65+
base=sm.SE3(0.45, 0.4, 0.3)
66+
)
67+
68+
s1 = rp.Shape.Sphere(
69+
radius=0.05,
70+
base=sm.SE3(0.1, 0.35, 0.65)
71+
)
72+
73+
s2 = rp.Shape.Sphere(
74+
radius=0.02,
75+
base=sm.SE3(0.3, 0, 0)
76+
)
77+
78+
s0.v = [0.01, -0.2, 0, 0, 0, 0]
79+
# s1.v = [0, -0.2, 0, 0, 0, 0]
80+
# s2.v = [0, 0.1, 0, 0, 0, 0]
81+
82+
env = rp.backend.Swift()
83+
env.launch()
84+
85+
r = rp.models.Panda()
86+
87+
n = 7
88+
env.add(r)
89+
env.add(s0)
90+
# env.add(s1)
91+
env.add(s2)
92+
time.sleep(1)
93+
94+
95+
s0.x = []
96+
s1.x = []
97+
s2.x = []
98+
s0.y = []
99+
s1.y = []
100+
s2.y = []
101+
m = []
102+
103+
def update_graph2(ob, graph):
104+
# Update the robot links
105+
106+
graph[0].set_xdata(ob.x)
107+
graph[0].set_ydata(ob.y)
108+
109+
110+
def link_calc(link, col, ob, q):
111+
dii = 5
112+
di = 0.3
113+
ds = 0.05
114+
115+
ret = p.getClosestPoints(col.co, ob.co, dii)
116+
117+
if len(ret) > 0:
118+
ret = ret[0]
119+
wTlp = sm.SE3(ret[5])
120+
wTcp = sm.SE3(ret[6])
121+
lpTcp = wTlp.inv() * wTcp
122+
123+
d = ret[8]
124+
125+
if d < di:
126+
n = lpTcp.t / d
127+
nh = np.expand_dims(np.r_[n, 0, 0, 0], axis=0)
128+
129+
Je = r.jacobe(q, from_link=r.base_link, to_link=link, offset=col.base)
130+
n = Je.shape[1]
131+
dp = nh @ ob.v
132+
l_Ain = np.zeros((1, 13))
133+
l_Ain[0, :n] = nh @ Je
134+
l_bin = (0.8 * (d - ds) / (di - ds)) + dp
135+
else:
136+
l_Ain = None
137+
l_bin = None
138+
139+
return l_Ain, l_bin, d, wTcp
140+
141+
142+
def servo(q0, Tep, it):
143+
144+
r.q = q0
145+
r.fkine_all()
146+
r.qd = np.zeros(r.n)
147+
env.step(1)
148+
links = r._fkpath[1:]
149+
150+
arrived = False
151+
i = 0
152+
Q = 0.1 * np.eye(n + 6)
153+
154+
while not arrived and i < it:
155+
156+
if i > (4 / 0.05):
157+
s2.v = [0, 0, 0, 0, 0, 0]
158+
159+
Tep.A[:3, 3] = s2.base.t
160+
Tep.A[2, 3] += 0.1
161+
q = r.q
162+
v, arrived = rp.p_servo(r.fkine(), Tep, 0.5, 0.05)
163+
164+
eTep = r.fkine().inv() * Tep
165+
e = np.sum(np.abs(np.r_[eTep.t, eTep.rpy() * np.pi/180]))
166+
167+
Q[n:, n:] = 1 * (1 / e) * np.eye(6)
168+
Aeq = np.c_[r.jacobe(), np.eye(6)]
169+
beq = v.reshape((6,))
170+
Jm = r.jacobm().reshape(7,)
171+
c = np.r_[-Jm, np.zeros(6)]
172+
173+
Ain = None
174+
bin = None
175+
176+
closest = 1000000
177+
closest_obj = None
178+
closest_p = None
179+
j = 0
180+
for link in links:
181+
if link.jtype == link.VARIABLE:
182+
j += 1
183+
for col in link.collision:
184+
obj = s0
185+
l_Ain, l_bin, ret, wTcp = link_calc(link, col, obj, q[:j])
186+
if ret < closest:
187+
closest = ret
188+
closest_obj = obj
189+
closest_p = wTcp
190+
191+
if l_Ain is not None and l_bin is not None:
192+
if Ain is None:
193+
Ain = l_Ain
194+
else:
195+
Ain = np.r_[Ain, l_Ain]
196+
197+
if bin is None:
198+
bin = np.array(l_bin)
199+
else:
200+
bin = np.r_[bin, l_bin]
201+
202+
s0.y.append(closest)
203+
s0.x.append(i * 0.05)
204+
205+
s2.y.append(np.linalg.norm(eTep.t))
206+
s2.x.append(i * 0.05)
207+
m.append(r.manipulability())
208+
209+
210+
try:
211+
qd = qp.solve_qp(Q, c, Ain, bin, Aeq, beq, lb=lb, ub=ub)
212+
except (ValueError, TypeError):
213+
print("Value Error")
214+
break
215+
216+
r.qd = qd[:7]
217+
218+
i += 1
219+
env.step(50)
220+
221+
# r.loc = np.c_[r.loc, r.fkine().t]
222+
# s0.loc = np.c_[s0.loc, s0.base.t]
223+
# s1.loc = np.c_[s1.loc, s1.base.t]
224+
# s2.loc = np.c_[s2.loc, s2.base.t]
225+
226+
# update_graph2(r, plr)
227+
update_graph2(s0, pld0)
228+
update_graph2(s2, pld2)
229+
plm[0].set_xdata(s2.x)
230+
plm[0].set_ydata(m)
231+
plt.pause(0.001)
232+
233+
return arrived
234+
235+
236+
q0 = r.qr
237+
r.q = q0
238+
239+
s2.base = sm.SE3.Tx(0.6) * sm.SE3.Tz(0.1) * sm.SE3.Ty(-0.2) * sm.SE3.Tz(-0.1)
240+
241+
Tep = r.fkine()
242+
Tep.A[:3, 3] = s2.base.t
243+
244+
245+
servo(q0, Tep, 5000)
246+
247+
obj = {
248+
's0': [s0.x, s0.y],
249+
's1': [s1.x, s1.y],
250+
's2': [s2.x, s2.y],
251+
'm': [s0.x, m]
252+
}
253+
254+
pickle.dump(obj, open('neo1.p', 'wb'))
255+
256+
plt.ioff()
257+
plt.show()

0 commit comments

Comments
 (0)