Skip to content
This repository was archived by the owner on Jun 28, 2021. It is now read-only.

Commit 7aa3855

Browse files
committed
Do not convert objects to dict and log more data
- With pickle it is not necessary to convert the action/observation objects to dictionaries. In fact, this conversion led to some inaccuracy. - Log robot and object observation of each step for easier debugging in case something goes wrong in the replay.
1 parent b541dc9 commit 7aa3855

File tree

2 files changed

+69
-23
lines changed

2 files changed

+69
-23
lines changed

python/rrc_simulation/trifinger_platform.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import pickle
32
import numpy as np
43

@@ -205,11 +204,8 @@ def __init__(
205204
# Initialize log
206205
# ==============
207206
self._action_log = {
208-
"initial_robot_position": initial_robot_position.tolist(),
209-
"initial_object_pose": {
210-
"position": initial_object_pose.position.tolist(),
211-
"orientation": initial_object_pose.orientation.tolist(),
212-
},
207+
"initial_robot_position": initial_robot_position,
208+
"initial_object_pose": initial_object_pose,
213209
"actions": [],
214210
}
215211

@@ -262,13 +258,14 @@ def append_desired_action(self, action):
262258
].timestamp = camera_timestamp_s
263259

264260
# write the desired action to the log
261+
object_pose = self.get_object_pose(t)
262+
robot_obs = self.get_robot_observation(t)
265263
self._action_log["actions"].append(
266264
{
267265
"t": t,
268-
"torque": action.torque.tolist(),
269-
"position": action.position.tolist(),
270-
"position_kp": action.position_kp.tolist(),
271-
"position_kd": action.position_kd.tolist(),
266+
"action": action,
267+
"object_pose": object_pose,
268+
"robot_observation": robot_obs,
272269
}
273270
)
274271

@@ -387,10 +384,9 @@ def store_action_log(self, filename):
387384
t = self.simfinger.get_current_timeindex()
388385
object_pose = self.get_object_pose(t)
389386
self._action_log["final_object_pose"] = {
390-
"position": object_pose.position.tolist(),
391-
"orientation": object_pose.orientation.tolist(),
387+
"t": t,
388+
"pose": object_pose,
392389
}
393390

394391
with open(filename, "wb") as fh:
395-
# json.dump(self._action_log, fh)
396392
pickle.dump(self._action_log, fh)

scripts/replay_action_log.py

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
2020
"""
2121
import argparse
22-
import json
2322
import pickle
2423
import sys
2524
import numpy as np
@@ -66,16 +65,43 @@ def main():
6665
args = parser.parse_args()
6766

6867
with open(args.logfile, "rb") as fh:
69-
# log = json.load(fh)
7068
log = pickle.load(fh)
7169

7270
initial_object_pose = move_cube.Pose.from_json(args.initial_pose)
7371
goal_pose = move_cube.Pose.from_json(args.goal_pose)
7472

73+
# verify that the initial object pose matches with the one in the log file
74+
try:
75+
np.testing.assert_array_almost_equal(
76+
initial_object_pose.position, log["initial_object_pose"].position,
77+
err_msg=("Given initial object position does not match with log file.")
78+
)
79+
np.testing.assert_array_almost_equal(
80+
initial_object_pose.orientation, log["initial_object_pose"].orientation,
81+
err_msg=("Given initial object orientation does not match with log file.")
82+
)
83+
except AssertionError as e:
84+
print("Failed.", file=sys.stderr)
85+
print(e, file=sys.stderr)
86+
sys.exit(1)
87+
7588
platform = trifinger_platform.TriFingerPlatform(
7689
visualization=False, initial_object_pose=initial_object_pose
7790
)
7891

92+
# verify that the robot is initialized to the same position as in the log
93+
# file
94+
initial_robot_position = platform.get_robot_observation(0).position
95+
try:
96+
np.testing.assert_array_almost_equal(
97+
initial_robot_position, log["initial_robot_position"],
98+
err_msg=("Initial robot position does not match with log file.")
99+
)
100+
except AssertionError as e:
101+
print("Failed.", file=sys.stderr)
102+
print(e, file=sys.stderr)
103+
sys.exit(1)
104+
79105
# verify that the number of logged actions matches with the episode length
80106
n_actions = len(log["actions"])
81107
assert (
@@ -84,14 +110,11 @@ def main():
84110

85111
accumulated_reward = 0
86112
for logged_action in log["actions"]:
87-
action = platform.Action()
88-
action.torque = np.array(logged_action["torque"])
89-
action.position = np.array(logged_action["position"])
90-
action.position_kp = np.array(logged_action["position_kp"])
91-
action.position_kd = np.array(logged_action["position_kd"])
113+
action = logged_action["action"]
92114

93115
t = platform.append_desired_action(action)
94116

117+
robot_obs = platform.get_robot_observation(t)
95118
cube_pose = platform.get_object_pose(t)
96119
reward = -move_cube.evaluate_state(
97120
goal_pose, cube_pose, args.difficulty
@@ -100,20 +123,47 @@ def main():
100123

101124
assert logged_action["t"] == t
102125

126+
np.testing.assert_array_almost_equal(
127+
robot_obs.position, logged_action["robot_observation"].position,
128+
err_msg=("Step %d: Recorded robot position does not match with"
129+
" the one achieved by the replay" % t)
130+
)
131+
np.testing.assert_array_almost_equal(
132+
robot_obs.torque, logged_action["robot_observation"].torque,
133+
err_msg=("Step %d: Recorded robot torque does not match with"
134+
" the one achieved by the replay" % t)
135+
)
136+
np.testing.assert_array_almost_equal(
137+
robot_obs.velocity, logged_action["robot_observation"].velocity,
138+
err_msg=("Step %d: Recorded robot velocity does not match with"
139+
" the one achieved by the replay" % t)
140+
)
141+
142+
np.testing.assert_array_almost_equal(
143+
cube_pose.position, logged_action["object_pose"].position,
144+
err_msg=("Step %d: Recorded object position does not match with"
145+
" the one achieved by the replay" % t)
146+
)
147+
np.testing.assert_array_almost_equal(
148+
cube_pose.orientation, logged_action["object_pose"].orientation,
149+
err_msg=("Step %d: Recorded object orientation does not match with"
150+
" the one achieved by the replay" % t)
151+
)
152+
103153
cube_pose = platform.get_object_pose(t)
104-
final_pose = log["final_object_pose"]
154+
final_pose = log["final_object_pose"]["pose"]
105155

106156
print("Accumulated Reward:", accumulated_reward)
107157

108158
# verify that actual and logged final object pose match
109159
try:
110160
np.testing.assert_array_almost_equal(
111-
cube_pose.position, final_pose["position"], decimal=3,
161+
cube_pose.position, final_pose.position, decimal=3,
112162
err_msg=("Recorded object position does not match with the one"
113163
" achieved by the replay")
114164
)
115165
np.testing.assert_array_almost_equal(
116-
cube_pose.orientation, final_pose["orientation"], decimal=3,
166+
cube_pose.orientation, final_pose.orientation, decimal=3,
117167
err_msg=("Recorded object orientation does not match with the one"
118168
" achieved by the replay")
119169
)

0 commit comments

Comments
 (0)