forked from ZhaomingXie/cassie_rl_env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcassiemujoco.py
216 lines (175 loc) · 6.7 KB
/
cassiemujoco.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
# Copyright (c) 2018 Dynamic Robotics Laboratory
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from cassiemujoco_ctypes import *
import os
import ctypes
# Get base directory
_dir_path = os.path.dirname(os.path.realpath(__file__))
# Initialize libcassiesim
cassie_mujoco_init(str.encode(_dir_path))
# Interface classes
class CassieSim:
def __init__(self):
self.c = cassie_sim_init()
def step(self, u):
y = cassie_out_t()
cassie_sim_step(self.c, y, u)
return y
def step_pd(self, u):
y = state_out_t()
cassie_sim_step_pd(self.c, y, u)
return y
def get_state(self):
s = CassieState()
cassie_get_state(self.c, s.s)
return s
def set_state(self, s):
cassie_set_state(self.c, s.s)
def time(self):
timep = cassie_sim_time(self.c)
return timep[0]
def qpos(self):
qposp = cassie_sim_qpos(self.c)
return qposp[:35]
def qvel(self):
qvelp = cassie_sim_qvel(self.c)
return qvelp[:32]
def set_time(self, time):
timep = cassie_sim_time(self.c)
timep[0] = time
def set_qpos(self, qpos):
qposp = cassie_sim_qpos(self.c)
for i in range(min(len(qpos), 35)):
qposp[i] = qpos[i]
def set_qvel(self, qvel):
qvelp = cassie_sim_qvel(self.c)
for i in range(min(len(qvel), 32)):
qvelp[i] = qvel[i]
def hold(self):
cassie_sim_hold(self.c)
def release(self):
cassie_sim_release(self.c)
def apply_force(self, xfrc, body=1):
xfrc_array = (ctypes.c_double * 6)()
for i in range(len(xfrc)):
xfrc_array[i] = xfrc[i]
cassie_sim_apply_force(self.c, xfrc_array, body)
def clear_forces(self):
cassie_sim_clear_forces(self.c)
def __del__(self):
cassie_sim_free(self.c)
class CassieVis:
def __init__(self):
self.v = cassie_vis_init()
def draw(self, c):
return cassie_vis_draw(self.v, c.c)
def valid(self):
return cassie_vis_valid(self.v)
def __del__(self):
cassie_vis_free(self.v)
class CassieState:
def __init__(self):
self.s = cassie_state_alloc()
def time(self):
timep = cassie_state_time(self.s)
return timep[0]
def qpos(self):
qposp = cassie_state_qpos(self.s)
return qposp[:35]
def qvel(self):
qvelp = cassie_state_qvel(self.s)
return qvelp[:32]
def set_time(self, time):
timep = cassie_state_time(self.s)
timep[0] = time
def set_qpos(self, qpos):
qposp = cassie_state_qpos(self.s)
for i in range(min(len(qpos), 35)):
qposp[i] = qpos[i]
def set_qvel(self, qvel):
qvelp = cassie_state_qvel(self.s)
for i in range(min(len(qvel), 32)):
qvelp[i] = qvel[i]
def __del__(self):
cassie_state_free(self.s)
class CassieUdp:
def __init__(self, remote_addr='127.0.0.1', remote_port='25000',
local_addr='0.0.0.0', local_port='25001'):
self.sock = udp_init_client(str.encode(remote_addr),
str.encode(remote_port),
str.encode(local_addr),
str.encode(local_port))
self.packet_header_info = packet_header_info_t()
self.recvlen = 2 + 697
self.sendlen = 2 + 58
self.recvlen_pd = 2 + 493
self.sendlen_pd = 2 + 476
self.recvbuf = (ctypes.c_ubyte * max(self.recvlen, self.recvlen_pd))()
self.sendbuf = (ctypes.c_ubyte * max(self.sendlen, self.sendlen_pd))()
self.inbuf = ctypes.cast(ctypes.byref(self.recvbuf, 2),
ctypes.POINTER(ctypes.c_ubyte))
self.outbuf = ctypes.cast(ctypes.byref(self.sendbuf, 2),
ctypes.POINTER(ctypes.c_ubyte))
def send(self, u):
pack_cassie_user_in_t(u, self.outbuf)
send_packet(self.sock, self.sendbuf, self.sendlen, None, 0)
def send_pd(self, u):
pack_pd_in_t(u, self.outbuf)
send_packet(self.sock, self.sendbuf, self.sendlen_pd, None, 0)
def recv_wait(self):
nbytes = -1
while nbytes != self.recvlen:
nbytes = get_newest_packet(self.sock, self.recvbuf, self.recvlen,
None, None)
process_packet_header(self.packet_header_info,
self.recvbuf, self.sendbuf)
cassie_out = cassie_out_t()
unpack_cassie_out_t(self.inbuf, cassie_out)
return cassie_out
def recv_wait_pd(self):
nbytes = -1
while nbytes != self.recvlen_pd:
nbytes = get_newest_packet(self.sock, self.recvbuf, self.recvlen_pd,
None, None)
process_packet_header(self.packet_header_info,
self.recvbuf, self.sendbuf)
state_out = state_out_t()
unpack_state_out_t(self.inbuf, state_out)
return state_out
def recv_newest(self):
nbytes = get_newest_packet(self.sock, self.recvbuf, self.recvlen,
None, None)
if nbytes != self.recvlen:
return None
process_packet_header(self.packet_header_info,
self.recvbuf, self.sendbuf)
cassie_out = cassie_out_t()
unpack_cassie_out_t(self.inbuf, cassie_out)
return cassie_out
def recv_newest_pd(self):
nbytes = get_newest_packet(self.sock, self.recvbuf, self.recvlen_pd,
None, None)
if nbytes != self.recvlen_pd:
return None
process_packet_header(self.packet_header_info,
self.recvbuf, self.sendbuf)
state_out = state_out_t()
unpack_state_out_t(self.inbuf, state_out)
return state_out
def delay(self):
return ord(self.packet_header_info.delay)
def seq_num_in_diff(self):
return ord(self.packet_header_info.seq_num_in_diff)
def __del__(self):
udp_close(self.sock)