-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimtorealcomparison.py
250 lines (224 loc) · 11.2 KB
/
simtorealcomparison.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 5 11:28:50 2023
@author: orochi
"""
import json
import pickle as pkl
import matplotlib.pyplot as plt
import numpy as np
def build_state_sim(state_container, PREV_VALS, state_list):
"""
Method takes in a State object
Extracts state information from state_container and returns it as a list based on
current used states contained in self.state_list
:param state: :func:`~mojograsp.simcore.phase.State` object.
:type state: :func:`~mojograsp.simcore.phase.State`
"""
state = []
if PREV_VALS > 0:
for i in range(PREV_VALS):
for key in state_list:
if key == 'op':
state.extend(state_container['previous_state'][i]['obj_2']['pose'][0][0:2])
elif key == 'ftp':
state.extend(state_container['previous_state'][i]['f1_pos'][0:2])
state.extend(state_container['previous_state'][i]['f2_pos'][0:2])
elif key == 'fbp':
state.extend(state_container['previous_state'][i]['f1_base'][0:2])
state.extend(state_container['previous_state'][i]['f2_base'][0:2])
elif key == 'fcp':
state.extend(state_container['previous_state'][i]['f1_contact_pos'][0:2])
state.extend(state_container['previous_state'][i]['f2_contact_pos'][0:2])
elif key == 'ja':
state.extend([item for item in state_container['previous_state'][i]['two_finger_gripper']['joint_angles'].values()])
elif key == 'fta':
state.extend([state_container['previous_state'][i]['f1_ang'],state_container['previous_state'][i]['f2_ang']])
elif key == 'gp':
state.extend(state_container['previous_state'][i]['goal_pose']['goal_pose'])
else:
raise Exception('key does not match list of known keys')
for key in state_list:
if key == 'op':
state.extend(state_container['obj_2']['pose'][0][0:2])
elif key == 'ftp':
state.extend(state_container['f1_pos'][0:2])
state.extend(state_container['f2_pos'][0:2])
elif key == 'fbp':
state.extend(state_container['f1_base'][0:2])
state.extend(state_container['f2_base'][0:2])
elif key == 'fcp':
state.extend(state_container['f1_contact_pos'][0:2])
state.extend(state_container['f2_contact_pos'][0:2])
elif key == 'ja':
state.extend([item for item in state_container['two_finger_gripper']['joint_angles'].values()])
elif key == 'fta':
state.extend([state_container['f1_ang'],state_container['f2_ang']])
elif key == 'gp':
state.extend(state_container['goal_pose']['goal_pose'])
else:
raise Exception('key does not match list of known keys')
return state
def build_state_real(state_container, PREV_VALS, state_list):
"""
Method takes in a State object
Extracts state information from state_container and returns it as a list based on
current used states contained in self.state_list
:param state: :func:`~mojograsp.simcore.phase.State` object.
:type state: :func:`~mojograsp.simcore.phase.State`
"""
state = []
if PREV_VALS > 0:
for i in range(PREV_VALS):
for key in state_list:
if key == 'op':
state.extend(state_container['previous_state'][i]['obj_2']['pose'][0][0:2])
elif key == 'ftp':
state.extend(state_container['previous_state'][i]['f1_pos'][0:2])
state.extend(state_container['previous_state'][i]['f2_pos'][0:2])
elif key == 'fbp':
state.extend(state_container['previous_state'][i]['f1_base'][0:2])
state.extend(state_container['previous_state'][i]['f2_base'][0:2])
elif key == 'fcp':
state.extend(state_container['previous_state'][i]['f1_contact_pos'][0:2])
state.extend(state_container['previous_state'][i]['f2_contact_pos'][0:2])
elif key == 'ja':
state.extend([item for item in state_container['previous_state'][i]['two_finger_gripper']['joint_angles'].values()])
elif key == 'fta':
state.extend([state_container['previous_state'][i]['f1_ang'],state_container['previous_state'][i]['f2_ang']])
elif key == 'gp':
state.extend(state_container['previous_state'][i]['goal_pose']['goal_pose'])
else:
raise Exception('key does not match list of known keys')
for key in state_list:
if key == 'op':
state.extend(state_container['current_state']['obj_2']['pose'][0][0:2])
elif key == 'ftp':
state.extend(state_container['current_state']['f1_pos'][0:2])
state.extend(state_container['current_state']['f2_pos'][0:2])
elif key == 'fbp':
state.extend(state_container['current_state']['f1_base'][0:2])
state.extend(state_container['current_state']['f2_base'][0:2])
elif key == 'fcp':
state.extend(state_container['current_state']['f1_contact_pos'][0:2])
state.extend(state_container['current_state']['f2_contact_pos'][0:2])
elif key == 'ja':
state.extend([item for item in state_container['current_state']['two_finger_gripper']['joint_angles'].values()])
elif key == 'fta':
state.extend([state_container['current_state']['f1_ang'],state_container['current_state']['f2_ang']])
elif key == 'gp':
state.extend(state_container['current_state']['goal_pose']['goal_pose'])
else:
raise Exception('key does not match list of known keys')
return state
def build_state(state_container, state_list):
"""
Method takes in a State object
Extracts state information from state_container and returns it as a list based on
current used states contained in self.state_list
:param state: :func:`~mojograsp.simcore.phase.State` object.
:type state: :func:`~mojograsp.simcore.phase.State`
"""
state = []
for key in state_list:
if key == 'op':
state.extend(state_container['obj_2']['pose'][0][0:2])
elif key == 'ftp':
state.extend(state_container['f1_pos'][0:2])
state.extend(state_container['f2_pos'][0:2])
elif key == 'fbp':
state.extend(state_container['f1_base'][0:2])
state.extend(state_container['f2_base'][0:2])
elif key == 'fcp':
state.extend(state_container['f1_contact_pos'][0:2])
state.extend(state_container['f2_contact_pos'][0:2])
elif key == 'ja':
state.extend([item for item in state_container['two_finger_gripper']['joint_angles'].values()])
elif key == 'fta':
state.extend([state_container['f1_ang'],state_container['f2_ang']])
elif key == 'gp':
state.extend(state_container['goal_pose']['goal_pose'])
else:
raise Exception('key does not match list of known keys')
return state
def draw_parameter_comparison_multi(sim_dictionary,real_list,state_key):
data = sim_dictionary['timestep_list']
sim_state = []
real_state = []
temp_sim_state = [f['state'][state_key][0] for f in data]
sim_state.append(temp_sim_state)
temp_sim_state = [f['state'][state_key][1] for f in data]
sim_state.append(temp_sim_state)
temp_real_state = [f['current_state'][state_key][0] for f in real_list]
real_state.append(temp_real_state)
temp_real_state = [f['current_state'][state_key][1] for f in real_list]
real_state.append(temp_real_state)
#sim_state = np.array(sim_state)
#real_state = np.array(real_state)
legend = []
plt.plot(range(len(sim_state[0])),sim_state[0])
plt.plot(range(len(real_state[0])),real_state[0])
plt.plot(range(len(sim_state[1])),sim_state[1])
plt.plot(range(len(real_state[1])),real_state[1])
legend.extend(['Sim ' + state_key + ' x', 'Real ' + state_key+ ' x','Sim ' + state_key + ' y', 'Real ' + state_key+ ' y'])
plt.legend(legend)
plt.grid(True)
plt.ylabel('State Comparison')
plt.xlabel('Timestep (1/30 s)')
def draw_parameter_comparison(sim_dictionary,real_list,state_key_list):
data = sim_dictionary['timestep_list']
sim_state = []
real_state = []
for key in state_key_list:
temp_sim_state = [f['state'][key] for f in data]
sim_state.append(temp_sim_state)
temp_real_state = [f['state']['current_state'][key] for f in real_list['timestep_list']]
real_state.append(temp_real_state)
#sim_state = np.array(sim_state)
#real_state = np.array(real_state)
legend = []
for i,key in enumerate(state_key_list):
plt.plot(range(len(sim_state[i])),sim_state[i])
plt.plot(range(len(real_state[i])),real_state[i])
legend.extend(['Sim ' + key, 'Real ' + key])
plt.legend(legend)
plt.grid(True)
plt.ylabel('State Comparison')
plt.xlabel('Timestep (1/30 s)')
def draw_path(sim_dict, real_dict):
simdata = sim_dict['timestep_list']
realdata = real_dict
simtrajectory_points = [f['state']['obj_2']['pose'][0] for f in simdata]
realtrajectory_points = [f['current_state']['obj_2']['pose'][0] for f in realdata]
goal_pose = simdata[1]['reward']['goal_position']
simtrajectory_points = np.array(simtrajectory_points)
realtrajectory_points = np.array(realtrajectory_points)
plt.plot(simtrajectory_points[:,0], simtrajectory_points[:,1])
plt.plot([simtrajectory_points[0,0], goal_pose[0]],[simtrajectory_points[0,1],goal_pose[1]])
plt.plot(realtrajectory_points[:,0],realtrajectory_points[:,1])
plt.xlim([-0.07,0.07])
plt.ylim([0.04,0.16])
plt.xlabel('X pos (m)')
plt.ylabel('Y pos (m)')
plt.legend(['Sim RL Trajectory', 'Ideal Path to Goal', 'Real RL Trajectory'])
plt.title('Object Path')
simpath = '/home/orochi/mojo/mojo-grasp/demos/rl_demo/data/PPO_JA_long/'
realpath = '/home/orochi/mojo/mojo-grasp/demos/rl_demo/data/real_world/'
with open(simpath+'experiment_config.json','r') as File:
args = json.load(File)
with open(simpath+'Train/episode_5000.pkl','rb') as File:
simdata = pkl.load(File)
with open(realpath+'E_episode.pkl','rb') as File:
realdata = pkl.load(File)
sim_built_state = build_state(simdata['timestep_list'][0]['state'], args['state_list'])
real_built_state = build_state(realdata['timestep_list'][0]['state']['current_state'], args['state_list'])
sim_built_state[-2:] = real_built_state[-2:]
errors = []
for i in range(len(sim_built_state)):
value_range = args['state_maxes'][i] - args['state_mins'][i]
percent_error = abs(sim_built_state[i] - real_built_state[i])/value_range
errors.append(percent_error)
if percent_error >0.03:
print('ya fucked up')
print(sim_built_state[i], real_built_state[i])