-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandupRL.lua
More file actions
executable file
·407 lines (320 loc) · 11.2 KB
/
standupRL.lua
File metadata and controls
executable file
·407 lines (320 loc) · 11.2 KB
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
-- Author : Heejin Jeong (University of Pennsylvania)
-- Start date : June 24th, 2015
-- Last modified : July 30th, 2016
-- Description: This file is for executing stand up motions learned by the Reinforcement Learning methods. This will follow the optimal policy written below.
module(..., package.seeall);
name = ...;
webots = false;
darwin = true;
require('Motion')
require('Body')
require('vector')
require('unix')
require('Kinematics')
require('Getup')
require('mcm')
require('Config')
local Getup = require'Getup'
local sqrt = math.sqrt
local sin = math.sin
local cos = math.cos
local pi = math.pi
local abs = math.abs
local D2R = pi/180
local init_local = 1
local duration = 1.5
local Rmax = 1
local Rsup = 1
-- [ Robot Specs ]-------------------------------------------------------------------------------
local joint_MAX ={ vector.new({175,175, 1})*D2R, --larm
vector.new({175, -5, 1})*D2R, --rarm
vector.new({ 45, 80, 20, 135, 72, 72})*D2R, --lleg
vector.new({145, 80, 20, 135, 72, 72})*D2R --rleg
}
local joint_MIN = { vector.new({-175, 5, -140})*D2R, --larm
vector.new({-175, -175, -140})*D2R, --rarm
vector.new({-145, -80, -110, -9, -85, -72})*D2R,
vector.new({-45, -80, -110, -9, -85, -72})*D2R
}
---[ Variables ]---------------------------------------------------------------------------------
local qLArm0, qRArm0,qLLeg0,qRLeg0
local qLArm1, qRArm1,qLLeg1,qRLeg1
local prop = 1
local LS
local stateO, state_pre, sym
local cwd = unix.getcwd();
if string.find(cwd, "WebotsController") then
cwd = cwd.."/../Player";
end
local optState = dofile(cwd.."/Motion/".."stateSym.lua");--dofile(cwd.."/Motion/".."options.lua");
-- RL learned results
local opt28 = dofile(cwd.."/Motion/".."opt_policy28.lua")
local optLS = dofile(cwd.."/Motion/".."opt_policyLS.lua")
-- 07.30.2016
--local optLS2 = dofile(cwd.."/Motion/".."opt_policyLS2.lua")
--local opt282 = dofile(cwd.."/Motion/".."opt_policy282.lua")
local stateNum = #optState
function entry()
--fid = io.open("opt2801.txt","w")
--act = opt2
fid = io.open("LSnew01.txt","w")
act = optLS2
LS = 1
print("Motion SM:".._NAME.." entry")
-- TIME
t_entry = Body.get_time()
t_start = t_entry
gyros = 0
gyros_pre = gyros
angAcc = 0
t_pre = t_start
execution = 0
stepO = 1
stepO_pre = 0
-- Get Data from the robot
qLArm0 = vector.new(Body.get_larm_position())
qRArm0 = vector.new(Body.get_rarm_position())
qLLeg0 = vector.new(Body.get_lleg_position())
qRLeg0 = vector.new(Body.get_rleg_position())
if webots then
qLArm1 = vector.new({1.2, 0.6, -1.0})--vector.slice(optState[o],1,3)
qRArm1 = vector.new({1.3, -0.9, -1.2}) --vector.slice(optState[o],4,6)
qLLeg1 = vector.new({0, 0, -1, 1.0, -0.6, 0}) --vector.slice(optState[o],7,12)
qRLeg1 = vector.new({0, -0.8, -1, 1.0, -0.8, 0})--vector.slice(optState[o],13,18)
else
init_local = 0
--quat = Body.get_sensor_quaternion()
rpy = {Body.get_sensor_imuAngle(1),Body.get_sensor_imuAngle(2),Body.get_sensor_imuAngle(3)}
pitch_pre = rpy[2]
rawStateVec = util.concat(qLArm0, qRArm0)
rawStateVec = util.concat(rawStateVec, qLLeg0)
rawStateVec = util.concat(rawStateVec, qRLeg0)
rawStateVec = util.concat(rawStateVec,vector.new({rpy[1],rpy[2]}))
if LS == 1 then
stateO, prop, sym = Getup.make_stateSYM(rawStateVec)
else
stateO = Getup.make_state(rawStateVec)
end
--minH, supStateVec = Getup.getSup(quat, qLArm0, qRArm0, qLLeg0, qRLeg0) -- ex) [1,0,0,1]^T
--minH_pre = minH
t_pre = t_start
angAcc = 0
gyro_pre = vector.new(Body.get_sensor_imuGyrRPY())*D2R
rpy_pre = rpy
end
end
function update()
local t = Body.get_time()
local t_diff = t - t_start
--if tbreak == 0 then
if init_local == 0 then
if execution == 0 then
print("======step: ",stepO,"==========")
-- [[Middle Level Option]]--
if stepO_pre ~= stepO then
o = act[stateO]
--o = optsymNo[stateO]
stepO_pre = stepO
end
print("state ", stateO, "action ", o)
local o_real
if LS == 1 then
if sym==0 and o > 28 then -- s, a sym
if o < 39 then
o_real = o-28
else
o_real = o-25
end
qRArm1 = vector.slice(optState[o_real],1,3)
qLArm1 = vector.slice(optState[o_real],4,6)
qRLeg1 = vector.slice(optState[o_real],7,12)
qLLeg1 = vector.slice(optState[o_real],13,18)
qLArm1[2] = -qLArm1[2]
qRArm1[2] = -qRArm1[2]
qLLeg1[1] = -qLLeg1[1]
qRLeg1[1] = -qRLeg1[1]
qLLeg1[2] = -qLLeg1[2]
qRLeg1[2] = -qRLeg1[2]
qLLeg1[6] = -qLLeg1[6]
qRLeg1[6] = -qRLeg1[6]
elseif sym == 1 then
if o < 22 then -- s sym, and (s,a) pair, a should be converted to a sym
qRArm1 = vector.slice(optState[o],1,3)
qLArm1 = vector.slice(optState[o],4,6)
qRLeg1 = vector.slice(optState[o],7,12)
qLLeg1 = vector.slice(optState[o],13,18)
qLArm1[2] = -qLArm1[2]
qRArm1[2] = -qRArm1[2]
qLLeg1[1] = -qLLeg1[1]
qRLeg1[1] = -qRLeg1[1]
qLLeg1[2] = -qLLeg1[2]
qRLeg1[2] = -qRLeg1[2]
qLLeg1[6] = -qLLeg1[6]
qRLeg1[6] = -qRLeg1[6]
elseif o > 28 then -- s sym, and (s, a sym) pair, a sym should be converted to a
if o < 39 then
o_real = o-28
else
o_real = o-25
end
qLArm1 = vector.slice(optState[o_real],1,3)
qRArm1 = vector.slice(optState[o_real],4,6)
qLLeg1 = vector.slice(optState[o_real],7,12)
qRLeg1 = vector.slice(optState[o_real],13,18)
else
qLArm1 = vector.slice(optState[o],1,3)
qRArm1 = vector.slice(optState[o],4,6)
qLLeg1 = vector.slice(optState[o],7,12)
qRLeg1 = vector.slice(optState[o],13,18)
end
else
qLArm1 = vector.slice(optState[o],1,3)
qRArm1 = vector.slice(optState[o],4,6)
qLLeg1 = vector.slice(optState[o],7,12)
qRLeg1 = vector.slice(optState[o],13,18)
end
else
qLArm1 = vector.slice(optState[o],1,3)
qRArm1 = vector.slice(optState[o],4,6)
qLLeg1 = vector.slice(optState[o],7,12)
qRLeg1 = vector.slice(optState[o],13,18)
end
execution = 1
else --- Action is Excuted!
--print("execute?",t_diff/duration)
if t_diff <= duration then -- execution
local ph = math.max(0,math.min(1, t_diff/duration))
Body.set_larm_command((1-ph)*qLArm0 + ph * qLArm1)
Body.set_rarm_command((1-ph)*qRArm0 + ph * qRArm1)
Body.set_lleg_command((1-ph)*qLLeg0 + ph * qLLeg1)
Body.set_rleg_command((1-ph)*qRLeg0 + ph * qRLeg1)
gyro = vector.new(Body.get_sensor_imuGyrRPY())*D2R
angAcc = angAcc + vector.norm((gyro - gyro_pre))/(t - t_pre)
gyro_pre = gyro
t_pre = t
elseif t_diff > 1.5*duration then -- update
--unix.usleep(1000000)
qLArm0 = vector.new(Body.get_larm_position())
qRArm0 = vector.new(Body.get_rarm_position())
qLLeg0 = vector.new(Body.get_lleg_position())
qRLeg0 = vector.new(Body.get_rleg_position())
q = qLArm0
q = util.concat(q,qRArm0)
q = util.concat(q,qLLeg0)
q = util.concat(q,qRLeg0)
--quat = Body.get_sensor_quaternion()
rpy = Body.get_sensor_imuAngle()
--minH, supStateVec = Getup.getSup(quat, qLArm0, qRArm0, qLLeg0, qRLeg0, rpy[2]) -- ex) [1,0,0,1]^T
rawStateVec = util.concat(qLArm0, qRArm0)
rawStateVec = util.concat(rawStateVec, qLLeg0)
rawStateVec = util.concat(rawStateVec, qRLeg0)
rawStateVec = util.concat(rawStateVec,vector.new({rpy[1],rpy[2]}))
state_pre = stateO
o_pre = o
if LS == 1 then
stateO, prop, sym = Getup.make_stateSYM(rawStateVec)
else
stateO = Getup.make_state(rawStateVec)
end
print("next State: ",stateO, "executed action: ", o_pre)
--print("H ", abs(minH)," ANG ACC ", angAcc)
print("RPY: ", unpack(rpy))--rpy[1]/D2R,rpy[2]/D2R,rpy[3]/D2R)
print("angAcc ", angAcc)
----- WRITING -----------------------------------------------------------------------------------
fid:write(stepO,"\t", angAcc,"\t")
for it=1,#rpy do
fid:write(rpy[it],"\t")
end
for it=1,#q do
fid:write(q[it],"\t")
end
fid:write(state_pre,"\t",o,"\n")
fid:write(state_pre,"\t", o_pre, "\t", stateO,"\n")
----- RESET -----------------------------------------------------------------------------------
--minH_pre = minH
stepO = stepO + 1
t_start = t
rpy_pre = rpy
gyro_pre = vector.new(Body.get_sensor_imuGyrRPY())*D2R
angAcc = 0
t_pre = t
--gyros = 0
execution = 0
--tbreak = 1
----- TERMINATION -----------------------------------------------------------------------------------
if (stateO == 29) and (abs(rpy[1])<0.3 and abs(rpy[2])<0.5) then
print("DONE")
io.close(fid)
return'done'
end
else -- waiting
gyro = vector.new(Body.get_sensor_imuGyrRPY())*D2R
angAcc = angAcc + vector.norm((gyro - gyro_pre))/(t - t_pre)
gyro_pre = gyro
t_pre = t
end -- time duration ends
end -- Action execution end
else -- init == 1
if t_diff <= duration then
local ph = math.max(0,math.min(1, t_diff/duration))
Body.set_larm_command((1-ph)*qLArm0 + ph * qLArm1)
Body.set_rarm_command((1-ph)*qRArm0 + ph * qRArm1)
Body.set_lleg_command((1-ph)*qLLeg0 + ph * qLLeg1)
Body.set_rleg_command((1-ph)*qRLeg0 + ph * qRLeg1)
elseif t_diff > 1.5*duration then --and hcm.get_state_proceed()==1 then
qLArm0 = vector.new(Body.get_larm_position())
qRArm0 = vector.new(Body.get_rarm_position())
qLLeg0 = vector.new(Body.get_lleg_position())
qRLeg0 = vector.new(Body.get_rleg_position())
-- State Update
--quat = Body.get_sensor_quaternion()
rpy = {Body.get_sensor_imuAngle(1),Body.get_sensor_imuAngle(2),Body.get_sensor_imuAngle(3)}
--print("quat", unpack(quat))
rawStateVec = util.concat(qLArm0, qRArm0)
rawStateVec = util.concat(rawStateVec, qLLeg0)
rawStateVec = util.concat(rawStateVec, qRLeg0)
rawStateVec = util.concat(rawStateVec,vector.new({rpy[1],rpy[2]}))
stateO, prop = Getup.make_state(rawStateVec)
state_pre = stateNum
o_pre = o
--minH, supStateVec = Getup.getSup(quat, qLArm0, qRArm0, qLLeg0, qRLeg0,rpy[2]) -- ex) [1,0,0,1]^T
--minH_pre = minH
--print("INITIAL HEIGHT ", abs(minH))
--tbreak = 1
--unix.usleep(1000000)
t_start = t
t_pre = t
angAcc = 0
gyro_pre = vector.new(Body.get_sensor_imuGyrRPY())*D2R
rpy_pre = rpy
init_local = 0
execution = 0
end
end
end
function exit()
end
function joint_lim(q, mv)
local lim = 0
local i = 1
while i < (#q+1) do
-- print(mv,i,q[i],joint_MAX[mv][i],joint_MIN[mv][i])
if q[i] > math.pi then
q[i] = q[i] - 2*math.pi
elseif q[i] < -math.pi then
q[i] = q[i] + 2*math.pi
end
if (q[i] > joint_MAX[mv][i]) or (q[i] < joint_MIN[mv][i]) then
lim = 1
print("joint limit")
print("why?", q[i], joint_MAX[mv][i], joint_MIN[mv][i], i)
break
else
i = i+1
end
end
if lim == 1 then
print("joint limit?",i)
end
return lim
end