-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspeed_freq_no_delta_env.py
423 lines (317 loc) · 15.3 KB
/
speed_freq_no_delta_env.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
421
422
423
from .cassiemujoco import pd_in_t, state_out_t, CassieSim, CassieVis
from .trajectory import CassieTrajectory
from math import floor
import numpy as np
import os
import random
import pickle
class CassieIKTrajectory:
def __init__(self, filepath):
with open(filepath, "rb") as f:
trajectory = pickle.load(f)
self.qpos = np.copy(trajectory["qpos"])
self.qvel = np.copy(trajectory["qvel"])
#self.foot =
def __len__(self):
return len(self.qpos)
class CassieEnv_speed_freq_no_delta:
def __init__(self, traj, simrate=60, clock_based=False, state_est=False):
self.sim = CassieSim("./cassie/cassiemujoco/cassie.xml")
self.vis = None
self.clock_based = clock_based
self.state_est = state_est
if clock_based:
self.observation_space = np.zeros(42 + 2)
if self.state_est:
self.observation_space = np.zeros(48 + 2) # Size for use with state est
else:
self.observation_space = np.zeros(80)
if self.state_est:
self.observation_space = np.zeros(86) # Size for use with state est
self.action_space = np.zeros(10)
dirname = os.path.dirname(__file__)
if traj == "walking":
traj_path = os.path.join(dirname, "trajectory", "stepdata.bin")
elif traj == "stepping":
# traj_path = os.path.join(dirname, "trajectory", "spline_stepping_traj.pkl")
traj_path = os.path.join(dirname, "trajectory", "more-poses-trial.bin")
# self.trajectory = CassieIKTrajectory(traj_path)
self.trajectory = CassieTrajectory(traj_path)
self.P = np.array([100, 100, 88, 96, 50])
self.D = np.array([10.0, 10.0, 8.0, 9.6, 5.0])
self.u = pd_in_t()
# TODO: should probably initialize this to current state
self.cassie_state = state_out_t()
self.simrate = simrate # simulate X mujoco steps with same pd target
# 60 brings simulation from 2000Hz to roughly 30Hz
self.time = 0 # number of time steps in current episode
self.phase = 0 # portion of the phase the robot is in
self.counter = 0 # number of phase cycles completed in episode
# NOTE: a reference trajectory represents ONE phase cycle
# should be floor(len(traj) / simrate) - 1
# should be VERY cautious here because wrapping around trajectory
# badly can cause assymetrical/bad gaits
self.phaselen = floor(len(self.trajectory) / self.simrate) - 1
# see include/cassiemujoco.h for meaning of these indices
self.pos_idx = [7, 8, 9, 14, 20, 21, 22, 23, 28, 34]
self.vel_idx = [6, 7, 8, 12, 18, 19, 20, 21, 25, 31]
self.speed = 0
# maybe make ref traj only send relevant idxs?
ref_pos, ref_vel = self.get_ref_state(self.phase)
self.phase_add = 1
def step_simulation(self, action):
self.u = pd_in_t()
for i in range(5):
# TODO: move setting gains out of the loop?
# maybe write a wrapper for pd_in_t ?
self.u.leftLeg.motorPd.pGain[i] = self.P[i]
self.u.rightLeg.motorPd.pGain[i] = self.P[i]
self.u.leftLeg.motorPd.dGain[i] = self.D[i]
self.u.rightLeg.motorPd.dGain[i] = self.D[i]
self.u.leftLeg.motorPd.torque[i] = 0 # Feedforward torque
self.u.rightLeg.motorPd.torque[i] = 0
self.u.leftLeg.motorPd.pTarget[i] = action[i]
self.u.rightLeg.motorPd.pTarget[i] = action[i + 5]
self.u.leftLeg.motorPd.dTarget[i] = 0
self.u.rightLeg.motorPd.dTarget[i] = 0
self.cassie_state = self.sim.step_pd(self.u)
def step(self, action):
for _ in range(self.simrate):
# h = 0.005
# Tf = 1.0 / 300.0
# alpha = h / (Tf + h)
# real_action = (1-alpha)*self.prev_action + alpha*action
# self.prev_action = real_action
# self.step_simulation(real_action)
self.step_simulation(action)
height = self.sim.qpos()[2]
self.time += 1
self.phase += self.phase_add
if self.phase > self.phaselen:
self.phase = 0
self.counter += 1
# Early termination
done = not(height > 0.4 and height < 3.0)
reward = self.compute_reward()
# TODO: make 0.3 a variable/more transparent
if reward < 0.3:
done = True
return self.get_full_state(), reward, done, {}
def reset(self):
self.phase = random.randint(0, self.phaselen)
self.time = 0
self.counter = 0
qpos, qvel = self.get_ref_state(self.phase)
# qpos[2] -= .1
self.sim.set_qpos(qpos)
self.sim.set_qvel(qvel)
# Need to reset u? Or better way to reset cassie_state than taking step
self.cassie_state = self.sim.step_pd(self.u)
self.speed = (random.randint(0, 10)) / 10
# self.phase_add = random.randint(1, 4)
# maybe make ref traj only send relevant idxs?
ref_pos, ref_vel = self.get_ref_state(self.phase)
self.prev_action = ref_pos[self.pos_idx]
return self.get_full_state()
# used for plotting against the reference trajectory
def reset_for_test(self):
self.phase = 0
self.phase_add = 1
self.time = 0
self.counter = 0
self.speed = 1
qpos, qvel = self.get_ref_state(self.phase)
self.sim.set_qpos(qpos)
self.sim.set_qvel(qvel)
# maybe make ref traj only send relevant idxs?
ref_pos, ref_vel = self.get_ref_state(self.phase)
self.prev_action = ref_pos[self.pos_idx]
# Need to reset u? Or better way to reset cassie_state than taking step
self.cassie_state = self.sim.step_pd(self.u)
return self.get_full_state()
def set_joint_pos(self, jpos, fbpos=None, iters=5000):
"""
Kind of hackish.
This takes a floating base position and some joint positions
and abuses the MuJoCo solver to get the constrained forward
kinematics.
There might be a better way to do this, e.g. using mj_kinematics
"""
# actuated joint indices
joint_idx = [7, 8, 9, 14, 20,
21, 22, 23, 28, 34]
# floating base indices
fb_idx = [0, 1, 2, 3, 4, 5, 6]
for _ in range(iters):
qpos = np.copy(self.sim.qpos())
qvel = np.copy(self.sim.qvel())
qpos[joint_idx] = jpos
if fbpos is not None:
qpos[fb_idx] = fbpos
self.sim.set_qpos(qpos)
self.sim.set_qvel(0 * qvel)
self.sim.step_pd(pd_in_t())
# NOTE: this reward is slightly different from the one in Xie et al
# see notes for details
def compute_reward(self):
qpos = np.copy(self.sim.qpos())
qvel = np.copy(self.sim.qvel())
ref_pos, ref_vel = self.get_ref_state(self.phase)
# TODO: should be variable; where do these come from?
# TODO: see magnitude of state variables to gauge contribution to reward
weight = [0.15, 0.15, 0.1, 0.05, 0.05, 0.15, 0.15, 0.1, 0.05, 0.05]
joint_error = 0
com_error = 0
orientation_error = 0
spring_error = 0
# each joint pos
for i, j in enumerate(self.pos_idx):
target = ref_pos[j]
actual = qpos[j]
joint_error += 30 * weight[i] * (target - actual) ** 2
# center of mass: x, y, z
for j in [0, 1, 2]:
target = ref_pos[j]
actual = qpos[j]
# NOTE: in Xie et al y target is 0
com_error += (target - actual) ** 2
# COM orientation: qx, qy, qz
for j in [4, 5, 6]:
target = ref_pos[j] # NOTE: in Xie et al orientation target is 0
actual = qpos[j]
orientation_error += (target - actual) ** 2
# left and right shin springs
for i in [15, 29]:
target = ref_pos[i] # NOTE: in Xie et al spring target is 0
actual = qpos[i]
spring_error += 1000 * (target - actual) ** 2
reward = 0.5 * np.exp(-joint_error) + \
0.3 * np.exp(-com_error) + \
0.1 * np.exp(-orientation_error) + \
0.1 * np.exp(-spring_error)
# orientation error does not look informative
# maybe because it's comparing euclidean distance on quaternions
# print("reward: {8}\njoint:\t{0:.2f}, % = {1:.2f}\ncom:\t{2:.2f}, % = {3:.2f}\norient:\t{4:.2f}, % = {5:.2f}\nspring:\t{6:.2f}, % = {7:.2f}\n\n".format(
# 0.5 * np.exp(-joint_error), 0.5 * np.exp(-joint_error) / reward * 100,
# 0.3 * np.exp(-com_error), 0.3 * np.exp(-com_error) / reward * 100,
# 0.1 * np.exp(-orientation_error), 0.1 * np.exp(-orientation_error) / reward * 100,
# 0.1 * np.exp(-spring_error), 0.1 * np.exp(-spring_error) / reward * 100,
# reward
# )
# )
# reward = np.sign(qvel[0])*qvel[0]**2
# desired_speed = 3.0
# speed_diff = np.abs(qvel[0] - desired_speed)
# if speed_diff > 1:
# speed_diff = speed_diff**2
# reward = 20 - speed_diff
return reward
# get the corresponding state from the reference trajectory for the current phase
def get_ref_state(self, phase=None):
if phase is None:
phase = self.phase
if phase > self.phaselen:
phase = 0
pos = np.copy(self.trajectory.qpos[phase * self.simrate])
# this is just setting the x to where it "should" be given the number
# of cycles
# pos[0] += (self.trajectory.qpos[-1, 0] - self.trajectory.qpos[0, 0]) * self.counter
# ^ should only matter for COM error calculation,
# gets dropped out of state variable for input reasons
###### Setting variable speed #########
pos[0] *= self.speed
pos[0] += (self.trajectory.qpos[-1, 0]- self.trajectory.qpos[0, 0])* self.counter * self.speed
###### ########
# setting lateral distance target to 0?
# regardless of reference trajectory?
pos[1] = 0
vel = np.copy(self.trajectory.qvel[phase * self.simrate])
vel[0] *= self.speed
return pos, vel
def get_full_state(self):
qpos = np.copy(self.sim.qpos())
qvel = np.copy(self.sim.qvel())
ref_pos, ref_vel = self.get_ref_state(self.phase + self.phase_add)
# TODO: maybe convert to set subtraction for clarity
# {i for i in range(35)} -
# {0, 10, 11, 12, 13, 17, 18, 19, 24, 25, 26, 27, 31, 32, 33}
# this is everything except pelvis x and qw, achilles rod quaternions,
# and heel spring/foot crank/plantar rod angles
# note: x is forward dist, y is lateral dist, z is height
# makes sense to always exclude x because it is in global coordinates and
# irrelevant to phase-based control. Z is inherently invariant to
# trajectory despite being global coord. Y is only invariant to straight
# line trajectories.
# [ 0] Pelvis y
# [ 1] Pelvis z
# [ 2] Pelvis orientation qw
# [ 3] Pelvis orientation qx
# [ 4] Pelvis orientation qy
# [ 5] Pelvis orientation qz
# [ 6] Left hip roll (Motor [0])
# [ 7] Left hip yaw (Motor [1])
# [ 8] Left hip pitch (Motor [2])
# [ 9] Left knee (Motor [3])
# [10] Left shin (Joint [0])
# [11] Left tarsus (Joint [1])
# [12] Left foot (Motor [4], Joint [2])
# [13] Right hip roll (Motor [5])
# [14] Right hip yaw (Motor [6])
# [15] Right hip pitch (Motor [7])
# [16] Right knee (Motor [8])
# [17] Right shin (Joint [3])
# [18] Right tarsus (Joint [4])
# [19] Right foot (Motor [9], Joint [5])
pos_index = np.array([1,2,3,4,5,6,7,8,9,14,15,16,20,21,22,23,28,29,30,34])
# [ 0] Pelvis x
# [ 1] Pelvis y
# [ 2] Pelvis z
# [ 3] Pelvis orientation wx
# [ 4] Pelvis orientation wy
# [ 5] Pelvis orientation wz
# [ 6] Left hip roll (Motor [0])
# [ 7] Left hip yaw (Motor [1])
# [ 8] Left hip pitch (Motor [2])
# [ 9] Left knee (Motor [3])
# [10] Left shin (Joint [0])
# [11] Left tarsus (Joint [1])
# [12] Left foot (Motor [4], Joint [2])
# [13] Right hip roll (Motor [5])
# [14] Right hip yaw (Motor [6])
# [15] Right hip pitch (Motor [7])
# [16] Right knee (Motor [8])
# [17] Right shin (Joint [3])
# [18] Right tarsus (Joint [4])
# [19] Right foot (Motor [9], Joint [5])
vel_index = np.array([0,1,2,3,4,5,6,7,8,12,13,14,18,19,20,21,25,26,27,31])
if self.clock_based:
#qpos[self.pos_idx] -= ref_pos[self.pos_idx]
#qvel[self.vel_idx] -= ref_vel[self.vel_idx]
clock = [np.sin(2 * np.pi * self.phase / self.phaselen),
np.cos(2 * np.pi * self.phase / self.phaselen)]
ext_state = np.concatenate((clock, [self.speed, self.phase_add]))
else:
ext_state = np.concatenate([ref_pos[pos_index], ref_vel[vel_index]])
# Use state estimator
robot_state = np.concatenate([
[self.cassie_state.pelvis.position[2] - self.cassie_state.terrain.height], # pelvis height
self.cassie_state.pelvis.orientation[:], # pelvis orientation
self.cassie_state.motor.position[:], # actuated joint positions
self.cassie_state.pelvis.translationalVelocity[:], # pelvis translational velocity
self.cassie_state.pelvis.rotationalVelocity[:], # pelvis rotational velocity
self.cassie_state.motor.velocity[:], # actuated joint velocities
self.cassie_state.pelvis.translationalAcceleration[:], # pelvis translational acceleration
self.cassie_state.joint.position[:], # unactuated joint positions
self.cassie_state.joint.velocity[:] # unactuated joint velocities
])
if self.state_est:
return np.concatenate([robot_state,
ext_state])
else:
return np.concatenate([qpos[pos_index],
qvel[vel_index],
ext_state])
def render(self):
if self.vis is None:
self.vis = CassieVis(self.sim, "./cassie/cassiemujoco/cassie.xml")
return self.vis.draw(self.sim)