Skip to content

Commit ac39405

Browse files
committed
Separate examples for sim and real
Have separate "move_up_and_down" demos, one for the real robot (the one that already existed) and one for the simulation. To make them more self-contained, do not define the move function in a separate file. Further add separate "sim_trajectory_example_with_gym" for simulation and prepend the existing one with "real_".
1 parent adcaee0 commit ac39405

9 files changed

+149
-82
lines changed

README.md

+25-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Example Package for the Real Robot Challenge
22
============================================
33

44
This is a basic example for a package that can be submitted to the robots of
5-
the [Real Robot Challenge](https://real-robot-challenge.com).
5+
the [Real Robot Challenge 2021](https://real-robot-challenge.com).
66

77
It is a normal ROS2 Python package that can be build with colcon. However,
88
there are a few special files in the root directory that are needed for
@@ -18,10 +18,27 @@ documentation](https://docs.ros.org/en/foxy/Tutorials/Creating-Your-First-ROS2-P
1818
Challenge Simulation Phase
1919
--------------------------
2020

21-
For evaluation of the simulation phase (phase 1) of the challenge, the critical
22-
file is the `evaluate_policy.py` at the root directory of the package. This is
23-
what is going to be executed by `rrc_evaluate_prestage.py` (found in
24-
`scripts/`).
21+
There are two example scripts using the simulation:
22+
23+
- `sim_move_up_and_down`: Directly uses the `TriFingerPlatform` class to simply
24+
move the robot between two fixed positions. This is implemented in
25+
`rrc_example_package/scripts/sim_move_up_and_down.py`.
26+
27+
- `sim_trajectory_example_with_gym`: Wraps the robot class in a Gym environment
28+
and uses that to run a dummy policy which simply points with one finger on the
29+
goal positions of the trajectory. This is implemented in
30+
`rrc_example_package/scripts/sim_trajectory_example_with_gym.py`.
31+
32+
To execute the examples, [build the
33+
package](https://people.tuebingen.mpg.de/felixwidmaier/rrc2021/singularity.html#singularity-build-ws)
34+
and execute
35+
36+
ros2 run rrc_example_package <example_name>
37+
38+
39+
For evaluation of the pre-stage of the challenge, the critical file is the
40+
`evaluate_policy.py` at the root directory of the package. This is what is
41+
going to be executed by `rrc_evaluate_prestage.py` (found in `scripts/`).
2542

2643
For more information, see https://people.tuebingen.mpg.de/felixwidmaier/rrc2021/
2744

@@ -36,15 +53,15 @@ For the challenge phases on the real robots, you need to provide the following
3653
files at the root directory of the package such that your jobs can executed on
3754
the robots:
3855

39-
- `goal.json`: May contain some configuration for sampling random goals or a
40-
fixed goal (might be useful for testing/training). See the documentation of
41-
the challenge tasks for more details.
4256
- `run`: Script that is executed when submitting the package to the robot.
4357
This can, for example, be a Python script or a symlink to a script somewhere
4458
else inside the repository. In the given example, it is a shell script
4559
running a Python script via `ros2 run`. This approach would also work for C++
4660
executables. When executed, a JSON string encoding the goal is passed as
4761
argument (the exact structure of the goal depends on the current task).
62+
- `goal.json`: Optional. May contain a fixed goal (might be useful for
63+
testing/training). See the documentation of the challenge tasks for more
64+
details.
4865

4966
It is important that the `run` script is executable. For this, you need to do
5067
two things:

rrc_example_package/example.py

+1-41
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
"""Simple example on how to move the robot."""
1+
"""Example policies, demonstrating how to control the robot."""
22
import os
33

44
import numpy as np
55
from ament_index_python.packages import get_package_share_directory
66

77
import trifinger_simulation.finger_types_data
88
import trifinger_simulation.pinocchio_utils
9-
import robot_interfaces
10-
import robot_fingers
119

1210

1311
class PointAtTrajectoryPolicy:
@@ -145,41 +143,3 @@ def predict(self, observation, t):
145143

146144
# make sure to return a copy, not a reference to self.joint_positions
147145
return np.array(self.joint_positions)
148-
149-
150-
def move_up_and_down(
151-
frontend: robot_fingers.TriFingerPlatformWithObjectFrontend, episode_length: int
152-
):
153-
"""Move up and down multiple times using fixed goal positions.
154-
155-
Args:
156-
frontend: Frontend of the TriFingerPro platform. Used to control the
157-
robot.
158-
episode_length: Number of time steps in the episode. Used to ensure
159-
that the limit is not exceeded.
160-
"""
161-
position_down = [-0.08, 0.84, -1.2] * 3
162-
position_up = [0.5, 1.2, -2.4] * 3
163-
target_positions = [position_down, position_up]
164-
165-
i = 0
166-
while True:
167-
print("Iteration {}".format(i))
168-
action = robot_interfaces.trifinger.Action(
169-
position=target_positions[i % 2]
170-
)
171-
i += 1
172-
173-
for _ in range(500):
174-
t = frontend.append_desired_action(action)
175-
frontend.wait_until_timeindex(t)
176-
177-
# make sure to not exceed the number of allowed actions
178-
if t >= episode_length - 1:
179-
return
180-
181-
robot_observation = frontend.get_robot_observation(t)
182-
print("Finger positions:", robot_observation.position)
183-
184-
camera_observation = frontend.get_camera_observation(t)
185-
print("Object position:", camera_observation.object_pose.position)

rrc_example_package/scripts/move_up_and_down.py

-29
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
"""Simple example on how to move the robot."""
3+
import json
4+
import sys
5+
import robot_interfaces
6+
import robot_fingers
7+
from trifinger_simulation.tasks import move_cube_on_trajectory
8+
9+
10+
def main():
11+
# the goal is passed as JSON string
12+
goal_json = sys.argv[1]
13+
goal = json.loads(goal_json)
14+
print("Goal: %s" % goal)
15+
16+
# It is possible to create custom files in "/output"
17+
with open("/output/hello.txt", "w") as fh:
18+
fh.write("Hello there!\n")
19+
20+
# create the robot frontend
21+
frontend = robot_fingers.TriFingerPlatformWithObjectFrontend()
22+
23+
# move the robot between two fixed positions ("up" and "down")
24+
position_down = [-0.08, 0.84, -1.2] * 3
25+
position_up = [0.5, 1.2, -2.4] * 3
26+
target_positions = [position_down, position_up]
27+
28+
i = 0
29+
while True:
30+
print("Iteration {}".format(i))
31+
action = robot_interfaces.trifinger.Action(
32+
position=target_positions[i % 2]
33+
)
34+
i += 1
35+
36+
for _ in range(500):
37+
t = frontend.append_desired_action(action)
38+
frontend.wait_until_timeindex(t)
39+
40+
# make sure to not exceed the number of allowed actions
41+
if t >= move_cube_on_trajectory.EPISODE_LENGTH - 1:
42+
return
43+
44+
robot_observation = frontend.get_robot_observation(t)
45+
print("Finger positions:", robot_observation.position)
46+
47+
camera_observation = frontend.get_camera_observation(t)
48+
print("Object position:", camera_observation.object_pose.position)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
"""Simple example on how to move the robot."""
3+
import trifinger_simulation
4+
from trifinger_simulation.tasks import move_cube_on_trajectory
5+
from trifinger_simulation.trifinger_platform import ObjectType
6+
7+
8+
def main():
9+
"""Example on how to move the robot in simulation."""
10+
# create the robot platform simulation
11+
robot = trifinger_simulation.TriFingerPlatform(
12+
visualization=True,
13+
object_type=ObjectType.COLORED_CUBE,
14+
)
15+
16+
# move the robot
17+
position_down = [-0.08, 0.84, -1.2] * 3
18+
position_up = [0.5, 1.2, -2.4] * 3
19+
target_positions = [position_down, position_up]
20+
21+
i = 0
22+
while True:
23+
action = robot.Action(
24+
position=target_positions[i % 2]
25+
)
26+
i += 1
27+
28+
for _ in range(500):
29+
t = robot.append_desired_action(action)
30+
31+
# make sure to not exceed the number of allowed actions
32+
if t >= move_cube_on_trajectory.EPISODE_LENGTH - 1:
33+
return
34+
35+
robot_observation = robot.get_robot_observation(t)
36+
print("Finger positions:", robot_observation.position)
37+
38+
camera_observation = robot.get_camera_observation(t)
39+
print("Object position:", camera_observation.object_pose.position)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
"""Demo on how to run the simulation using the Gym environment
3+
4+
This demo creates a SimCubeTrajectoryEnv environment and runs one episode using
5+
a dummy policy.
6+
"""
7+
from rrc_example_package import cube_trajectory_env
8+
from rrc_example_package.example import PointAtTrajectoryPolicy
9+
10+
11+
def main():
12+
env = cube_trajectory_env.SimCubeTrajectoryEnv(
13+
goal_trajectory=None, # passing None to sample a random trajectory
14+
action_type=cube_trajectory_env.ActionType.POSITION,
15+
visualization=True,
16+
)
17+
18+
is_done = False
19+
observation = env.reset()
20+
t = 0
21+
22+
policy = PointAtTrajectoryPolicy(env.action_space, env.info["trajectory"])
23+
while not is_done:
24+
action = policy.predict(observation, t)
25+
observation, reward, is_done, info = env.step(action)
26+
t = info["time_index"]
27+
28+
29+
if __name__ == "__main__":
30+
main()

run

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
# run the script via `ros2 run` (for python scripts a simple symlink would also
44
# work but using `ros2 run` works for C++ executables as well)
55

6-
# ros2 run rrc_example_package trajectory_example_with_gym "$@"
6+
# ros2 run rrc_example_package real_trajectory_example_with_gym "$@"
77
ros2 run rrc_example_package dice_example_with_gym "$@"
8-
# ros2 run rrc_example_package move_up_and_down "$@"
8+
# ros2 run rrc_example_package real_move_up_and_down "$@"

setup.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
# scripts here.
3131
entry_points={
3232
"console_scripts": [
33-
"move_up_and_down = rrc_example_package.scripts.move_up_and_down:main",
34-
"trajectory_example_with_gym = rrc_example_package.scripts.trajectory_example_with_gym:main",
33+
"real_move_up_and_down = rrc_example_package.scripts.real_move_up_and_down:main",
34+
"sim_move_up_and_down = rrc_example_package.scripts.sim_move_up_and_down:main",
35+
"real_trajectory_example_with_gym = rrc_example_package.scripts.real_trajectory_example_with_gym:main",
36+
"sim_trajectory_example_with_gym = rrc_example_package.scripts.sim_trajectory_example_with_gym:main",
3537
"dice_example_with_gym = rrc_example_package.scripts.dice_example_with_gym:main",
3638
],
3739
},

0 commit comments

Comments
 (0)