-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfr3_direct_control.py
More file actions
198 lines (161 loc) · 7.62 KB
/
fr3_direct_control.py
File metadata and controls
198 lines (161 loc) · 7.62 KB
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
import logging
import numpy as np
from rcs._core.common import RobotPlatform
from rcs._core.sim import CameraType
from rcs.camera.sim import SimCameraConfig, SimCameraSet
from rcs_fr3._core import hw
from rcs_fr3.desk import FCI, ContextManager, Desk, load_creds_fr3_desk
import rcs
from rcs import sim
ROBOT_IP = "192.168.101.1"
ROBOT_INSTANCE = RobotPlatform.SIMULATION
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
"""
This examples demonstrates the direct robot control api without gym env.
First the robot is moved in x, y and z direction 5cm. Then the arm is rotated to the right
and trying to grasp an object placed 25cm to the right of it. Afterwards it moves back
to the home position.
Create a .env file in the same directory as this file with the following content:
FR3_USERNAME=<username on franka desk>
FR3_PASSWORD=<password on franka desk>
When you use a real FR3 you first need to unlock its joints using the following cli script:
python -m rcs_fr3 unlock <ip>
or put it into guiding mode using:
python -m rcs_fr3 guiding-mode <ip>
When you are done you lock it again using:
python -m rcs_fr3 lock <ip>
or even shut it down using:
python -m rcs_fr3 shutdown <ip>
"""
def main():
context_manger: FCI | ContextManager
if ROBOT_INSTANCE == RobotPlatform.HARDWARE:
user, pw = load_creds_fr3_desk()
context_manger = FCI(Desk(ROBOT_IP, user, pw), unlock=False, lock_when_done=False)
else:
context_manger = ContextManager()
with context_manger:
robot: rcs.common.Robot
gripper: rcs.common.Gripper
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation = sim.Sim(rcs.scenes["fr3_empty_world"].mjb)
urdf_path = rcs.scenes["fr3_empty_world"].urdf
ik = rcs.common.RL(str(urdf_path))
cfg = sim.SimRobotConfig()
cfg.add_id("0")
cfg.tcp_offset = rcs.common.Pose(rcs.common.FrankaHandTCPOffset())
robot = rcs.sim.SimRobot(simulation, ik, cfg)
gripper_cfg_sim = sim.SimGripperConfig()
gripper_cfg_sim.add_id("0")
gripper = sim.SimGripper(simulation, gripper_cfg_sim)
# add camera to have a rendering gui
cameras = {
"default_free": sim.SimCameraConfig(
identifier="",
type=CameraType.default_free,
resolution_width=1280,
resolution_height=720,
frame_rate=20,
),
"wrist": SimCameraConfig(
identifier="wrist_0",
type=CameraType.fixed,
resolution_width=640,
resolution_height=480,
frame_rate=30,
),
}
camera_set = SimCameraSet(simulation, cameras) # noqa: F841
simulation.open_gui()
else:
urdf_path = rcs.scenes["fr3_empty_world"].urdf
ik = rcs.common.RL(str(urdf_path))
robot = hw.FR3(ROBOT_IP, ik)
robot_cfg = hw.FR3Config()
robot_cfg.tcp_offset = rcs.common.Pose(rcs.common.FrankaHandTCPOffset())
robot_cfg.ik_solver = hw.IKSolver.rcs_ik
robot.set_config(robot_cfg) # type: ignore
gripper_cfg_hw = hw.FHConfig()
gripper_cfg_hw.epsilon_inner = gripper_cfg_hw.epsilon_outer = 0.1
gripper_cfg_hw.speed = 0.1
gripper_cfg_hw.force = 30
gripper = hw.FrankaHand(ROBOT_IP, gripper_cfg_hw)
input("the robot is going to move, press enter whenever you are ready")
# move to home position and open gripper
robot.move_home()
gripper.open()
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation.step_until_convergence()
logger.info("Robot is in home position, gripper is open")
# 5cm in x direction
robot.set_cartesian_position(
robot.get_cartesian_position() * rcs.common.Pose(translation=np.array([0.05, 0, 0])) # type: ignore
)
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation.step_until_convergence()
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
logger.debug(f"sim converged: {simulation.is_converged()}")
# 5cm in y direction
robot.set_cartesian_position(
robot.get_cartesian_position() * rcs.common.Pose(translation=np.array([0, 0.05, 0])) # type: ignore
)
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation.step_until_convergence()
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
logger.debug(f"sim converged: {simulation.is_converged()}")
# 5cm in z direction
robot.set_cartesian_position(
robot.get_cartesian_position() * rcs.common.Pose(translation=np.array([0, 0, 0.05])) # type: ignore
)
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation.step_until_convergence()
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
logger.debug(f"sim converged: {simulation.is_converged()}")
# rotate the arm 90 degrees around the inverted y and z axis
new_pose = robot.get_cartesian_position() * rcs.common.Pose(
translation=np.array([0, 0, 0]), rpy=rcs.common.RPY(roll=0, pitch=-np.deg2rad(90), yaw=-np.deg2rad(90)) # type: ignore
)
robot.set_cartesian_position(new_pose)
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation.step_until_convergence()
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
logger.debug(f"sim converged: {simulation.is_converged()}")
if ROBOT_INSTANCE == RobotPlatform.HARDWARE:
input(
"hold an object 25cm in front of the gripper, the robot is going to grasp it, press enter when you are ready"
)
# move 25cm towards the gripper direction
robot.set_cartesian_position(
robot.get_cartesian_position() * rcs.common.Pose(translation=np.array([0, 0, 0.25])) # type: ignore
)
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation.step_until_convergence()
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
logger.debug(f"sim converged: {simulation.is_converged()}")
# grasp the object
gripper.grasp()
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation.step_until_convergence()
logger.debug(f"sim converged: {simulation.is_converged()}")
# move 25cm backward
robot.set_cartesian_position(
robot.get_cartesian_position() * rcs.common.Pose(translation=np.array([0, 0, -0.25])) # type: ignore
)
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation.step_until_convergence()
logger.debug(f"IK success: {robot.get_state().ik_success}") # type: ignore
logger.debug(f"sim converged: {simulation.is_converged()}")
if ROBOT_INSTANCE == RobotPlatform.HARDWARE:
input("gripper is going to be open, press enter when you are ready")
# open gripper
gripper.open()
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation.step_until_convergence()
# move back to home position
robot.move_home()
if ROBOT_INSTANCE == RobotPlatform.SIMULATION:
simulation.step_until_convergence()
if __name__ == "__main__":
main()