dual_arm_interactive_marker.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """ This simple script creates an interactive marker for changing desired centering pose of
3  two the dual_panda_cartesian_impedance_example_controller. It features also resetting the
4  marker to current centering pose between the left and the right endeffector.
5 """
6 
7 import rospy
8 import argparse
9 
11  InteractiveMarkerServer, InteractiveMarkerFeedback
12 from visualization_msgs.msg import InteractiveMarker, \
13  InteractiveMarkerControl, Marker
14 from franka_msgs.msg import FrankaState
15 from geometry_msgs.msg import PoseStamped
16 
17 marker_pose = PoseStamped()
18 
19 centering_frame_ready = False
20 
21 has_error = False
22 left_has_error = False
23 right_has_error = False
24 
25 pose_pub = None
26 
27 
28 def make_sphere(scale=0.3):
29  """
30  This function returns sphere marker for 3D translational movements.
31  :param scale: scales the size of the sphere
32  :return: sphere marker
33  """
34  marker = Marker()
35  marker.type = Marker.SPHERE
36  marker.scale.x = scale * 0.45
37  marker.scale.y = scale * 0.45
38  marker.scale.z = scale * 0.45
39  marker.color.r = 0.5
40  marker.color.g = 0.5
41  marker.color.b = 0.5
42  marker.color.a = 1.0
43  return marker
44 
45 
47  """
48  This function publishes desired centering pose which the controller will subscribe to.
49  :return: None
50  """
51  marker_pose.header.stamp = rospy.Time(0)
52  pose_pub.publish(marker_pose)
53 
54 
56  """
57  This callback function set `has_error` variable to True if the left arm is having an error.
58  :param msg: FrankaState msg data
59  :return: None
60  """
61  global has_error, left_has_error
62  if msg.robot_mode == FrankaState.ROBOT_MODE_MOVE:
63  left_has_error = False
64  else:
65  left_has_error = True
66  has_error = left_has_error or right_has_error
67 
68 
70  """
71  This callback function set `has_error` variable to True if the right arm is having an error.
72  :param msg: FrankaState msg data
73  :return: None
74  """
75  global has_error, right_has_error
76  if msg.robot_mode == FrankaState.ROBOT_MODE_MOVE:
77  right_has_error = False
78  else:
79  right_has_error = True
80  has_error = left_has_error or right_has_error
81 
82 
84  """
85  This callback function sets the marker pose to the current centering pose from a subscribed topic.
86  :return: None
87  """
88  global centering_frame_ready
89  global marker_pose
90 
91  marker_pose = msg
92  centering_frame_ready = True
93 
94 
96  """
97  This function resets the marker pose to current "middle pose" of left and right arm EEs.
98  :return: None
99  """
100 
101  global centering_frame_ready
102  global marker_pose
103 
104  centering_frame_ready = False
105 
106  centering_frame_pose_sub = rospy.Subscriber(
107  "dual_arm_cartesian_impedance_example_controller/centering_frame",
108  PoseStamped, centering_pose_callback)
109 
110  # Get initial pose for the interactive marker
111  while not centering_frame_ready:
112  rospy.sleep(0.1)
113 
114  centering_frame_pose_sub.unregister()
115 
116 
117 def process_feedback(feedback):
118  """
119  This callback function clips the marker_pose inside a predefined box to prevent misuse of the marker.
120  :param feedback: feedback data of interactive marker
121  :return: None
122  """
123  global marker_pose
124  if feedback.event_type == InteractiveMarkerFeedback.POSE_UPDATE:
125  marker_pose.pose.position.x = feedback.pose.position.x
126  marker_pose.pose.position.y = feedback.pose.position.y
127  marker_pose.pose.position.z = feedback.pose.position.z
128  marker_pose.pose.orientation = feedback.pose.orientation
129  server.applyChanges()
130 
131 
132 if __name__ == "__main__":
133  rospy.init_node("target_pose_node")
134 
135  parser = argparse.ArgumentParser("dual_panda_interactive_marker.py")
136  parser.add_argument("--left_arm_id",
137  help="The id of the left arm.",
138  required=True)
139  parser.add_argument("--right_arm_id",
140  help="The id of the right arm.",
141  required=True)
142  parser.add_argument('args', nargs=argparse.REMAINDER)
143  args = parser.parse_args()
144 
145  # Arm IDs of left and right arms
146  left_arm_id = args.left_arm_id
147  right_arm_id = args.right_arm_id
148 
149  # Initialize subscribers for error states of the arms
150  left_state_sub = rospy.Subscriber(left_arm_id + "_state_controller/franka_states",
151  FrankaState, left_franka_state_callback)
152 
153  right_state_sub = rospy.Subscriber(right_arm_id + "_state_controller/franka_states",
154  FrankaState, right_franka_state_callback)
155 
156  # Set marker pose to be the current "middle pose" of both EEs
158 
159  # Initialize publisher for publishing desired centering pose
160  pose_pub = rospy.Publisher(
161  "dual_arm_cartesian_impedance_example_controller/centering_frame_target_pose",
162  PoseStamped,
163  queue_size=1)
164 
165  # Interactive marker settings
166  server = InteractiveMarkerServer("target_pose_marker")
167  int_marker = InteractiveMarker()
168  int_marker.header.frame_id = marker_pose.header.frame_id
169  int_marker.scale = 0.3
170  int_marker.name = "centering_frame_pose"
171  int_marker.description = ("Target centering pose\n"
172  "BE CAREFUL! \n"
173  "If you move the target marker\n"
174  "both robots will follow it \n"
175  "as the center between the two\n"
176  "endeffectors. Be aware of\n"
177  "potential collisions!")
178  int_marker.pose = marker_pose.pose
179 
180  # insert a box
181  control = InteractiveMarkerControl()
182  control.orientation.w = 1
183  control.orientation.x = 1
184  control.orientation.y = 0
185  control.orientation.z = 0
186  control.name = "rotate_x"
187  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
188  int_marker.controls.append(control)
189 
190  control = InteractiveMarkerControl()
191  control.orientation.w = 1
192  control.orientation.x = 1
193  control.orientation.y = 0
194  control.orientation.z = 0
195  control.name = "move_x"
196  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
197  int_marker.controls.append(control)
198  control = InteractiveMarkerControl()
199  control.orientation.w = 1
200  control.orientation.x = 0
201  control.orientation.y = 1
202  control.orientation.z = 0
203  control.name = "rotate_y"
204  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
205  int_marker.controls.append(control)
206  control = InteractiveMarkerControl()
207  control.orientation.w = 1
208  control.orientation.x = 0
209  control.orientation.y = 1
210  control.orientation.z = 0
211  control.name = "move_y"
212  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
213  int_marker.controls.append(control)
214  control = InteractiveMarkerControl()
215  control.orientation.w = 1
216  control.orientation.x = 0
217  control.orientation.y = 0
218  control.orientation.z = 1
219  control.name = "rotate_z"
220  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
221  int_marker.controls.append(control)
222  control = InteractiveMarkerControl()
223  control.orientation.w = 1
224  control.orientation.x = 0
225  control.orientation.y = 0
226  control.orientation.z = 1
227  control.name = "move_z"
228  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
229  int_marker.controls.append(control)
230  control = InteractiveMarkerControl()
231  control.orientation.w = 1
232  control.orientation.x = 1
233  control.orientation.y = 1
234  control.orientation.z = 1
235  control.name = "move_3D"
236  control.always_visible = True
237  control.markers.append(make_sphere())
238  control.interaction_mode = InteractiveMarkerControl.MOVE_3D
239  int_marker.controls.append(control)
240 
241  server.insert(int_marker, process_feedback)
242  server.applyChanges()
243 
244  # main loop
245  while not rospy.is_shutdown():
247  if has_error:
250  server.setPose("centering_frame_pose", marker_pose.pose)
251  server.applyChanges()
252  rospy.sleep(0.5)
253  rospy.sleep(0.1)


franka_example_controllers
Author(s): Franka Emika GmbH
autogenerated on Fri Oct 23 2020 03:47:17