-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmp.py
133 lines (101 loc) · 2.98 KB
/
dmp.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
import yarp;
import time
import pickle
import numpy as np
import matplotlib.pyplot as plt
import pydmps
import pydmps.dmp_discrete
yarp.Network.init();
# prepare a property object
props = yarp.Property()
props.put("device","remote_controlboard")
props.put("local","/client/right_arm")
props.put("remote","/icubSim/right_arm")
# create remote driver
armDriver = yarp.PolyDriver(props)
time.sleep(1)
#query motor control interfaces
iPos = armDriver.viewIPositionControl()
iVel = armDriver.viewIVelocityControl()
iEnc = armDriver.viewIEncoders()
time.sleep(1)
#retrieve number of joints
jnts=iPos.getAxes()
print 'Controlling', jnts, 'joints'
# read encoders
encs=yarp.Vector(jnts)
iEnc.getEncoders(encs.data())
## store as home position
home=yarp.Vector(jnts, encs.data())
time.sleep(1)
#initialize a new tmp vector identical to encs
tmp=yarp.Vector(jnts)
for i in range(jnts):
tmp.set(i, encs.get(i))
motions={}
with open('recorded.pickle', 'rb') as handle:
motions = pickle.load(handle)
print(motions)
N=1000
demo = np.zeros((N, jnts))
ystart = [encs.get(i) for i in range(jnts)]
ymiddle = motions["neutraldown"]
yend = motions["stableup"]
for i in range(jnts):
demo[0:int(N/2), i] = np.linspace(ystart[i], ymiddle[i], int(N/2))
demo[int(N/2):N, i] = np.linspace(ymiddle[i], yend[i], int(N/2))
#y_des = np.load('2.npz')['arr_0'].T
#y_des -= y_des[:, 0][:, None]
y_des = demo.T
# test normal run
dmp = pydmps.dmp_discrete.DMPs_discrete(n_dmps=jnts, n_bfs=500, ay=np.ones(jnts)*10.0, dt=0.01)
dmp.imitate_path(y_des=y_des)
#y_track, dy_track, ddy_track = dmp.rollout()
# run while moving the target up and to the right
y_track = []
dmp.reset_state()
_error=0
for t in range(5*dmp.timesteps):
y, _, _ = dmp.step(error=_error)
y_track.append(np.copy(y))
# move the target slightly every time step
#dmp.goal += np.array([1e-2, 1e-2])
for i in range(y.shape[0]):
tmp.set(i, y[i])
iPos.positionMove(tmp.data())
time.sleep(0.1)
iEnc.getEncoders(encs.data())
time.sleep(0.1)
encs_now=yarp.Vector(jnts, encs.data())
qs = np.array([encs.get(i) for i in range(jnts)])
_error = np.linalg.norm(y-qs)/16
if np.isnan(_error):
_error=0.001
print(t)
print(y)
print(qs)
print(_error)
y_track = np.array(y_track)
end=False
while end==False:
cmd=raw_input("?")
if cmd=="q":
with open('filename.pickle', 'wb') as handle:
pickle.dump(motions, handle, protocol=pickle.HIGHEST_PROTOCOL)
end=True
if cmd=="r":
iEnc.getEncoders(encs.data())
encs_now=yarp.Vector(jnts, encs.data())
time.sleep(0.1)
name=raw_input("name?")
qs = [encs.get(i) for i in range(jnts)]
motions[name]=qs
if cmd=='p':
name=raw_input("name?")
tmp=yarp.Vector(jnts)
try:
for i in range(jnts):
tmp.set(i, motions[name][i])
iPos.positionMove(tmp.data())
except KeyError:
print("does not exist")