forked from ros-visualization/visualization_tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_controls.py
More file actions
executable file
·484 lines (398 loc) · 17.3 KB
/
Copy pathbasic_controls.py
File metadata and controls
executable file
·484 lines (398 loc) · 17.3 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env python
"""
Copyright (c) 2011, Willow Garage, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Willow Garage, Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""
import rospy
import copy
from interactive_markers.interactive_marker_server import *
from interactive_markers.menu_handler import *
from visualization_msgs.msg import *
from geometry_msgs.msg import Point
from tf.broadcaster import TransformBroadcaster
from random import random
from math import sin
server = None
menu_handler = MenuHandler()
br = None
counter = 0
def frameCallback( msg ):
global counter, br
time = rospy.Time.now()
br.sendTransform( (0, 0, sin(counter/140.0)*2.0), (0, 0, 0, 1.0), time, "base_link", "moving_frame" )
counter += 1
def processFeedback( feedback ):
s = "Feedback from marker '" + feedback.marker_name
s += "' / control '" + feedback.control_name + "'"
mp = ""
if feedback.mouse_point_valid:
mp = " at " + str(feedback.mouse_point.x)
mp += ", " + str(feedback.mouse_point.y)
mp += ", " + str(feedback.mouse_point.z)
mp += " in frame " + feedback.header.frame_id
if feedback.event_type == InteractiveMarkerFeedback.BUTTON_CLICK:
rospy.loginfo( s + ": button click" + mp + "." )
elif feedback.event_type == InteractiveMarkerFeedback.MENU_SELECT:
rospy.loginfo( s + ": menu item " + str(feedback.menu_entry_id) + " clicked" + mp + "." )
elif feedback.event_type == InteractiveMarkerFeedback.POSE_UPDATE:
rospy.loginfo( s + ": pose changed")
# TODO
# << "\nposition = "
# << feedback.pose.position.x
# << ", " << feedback.pose.position.y
# << ", " << feedback.pose.position.z
# << "\norientation = "
# << feedback.pose.orientation.w
# << ", " << feedback.pose.orientation.x
# << ", " << feedback.pose.orientation.y
# << ", " << feedback.pose.orientation.z
# << "\nframe: " << feedback.header.frame_id
# << " time: " << feedback.header.stamp.sec << "sec, "
# << feedback.header.stamp.nsec << " nsec" )
elif feedback.event_type == InteractiveMarkerFeedback.MOUSE_DOWN:
rospy.loginfo( s + ": mouse down" + mp + "." )
elif feedback.event_type == InteractiveMarkerFeedback.MOUSE_UP:
rospy.loginfo( s + ": mouse up" + mp + "." )
server.applyChanges()
def alignMarker( feedback ):
pose = feedback.pose
pose.position.x = round(pose.position.x-0.5)+0.5
pose.position.y = round(pose.position.y-0.5)+0.5
rospy.loginfo( feedback.marker_name + ": aligning position = " + str(feedback.pose.position.x) + "," + str(feedback.pose.position.y) + "," + str(feedback.pose.position.z) + " to " +
str(pose.position.x) + "," + str(pose.position.y) + "," + str(pose.position.z) )
server.setPose( feedback.marker_name, pose )
server.applyChanges()
def rand( min_, max_ ):
return min_ + random()*(max_-min_)
def makeBox( msg ):
marker = Marker()
marker.type = Marker.CUBE
marker.scale.x = msg.scale * 0.45
marker.scale.y = msg.scale * 0.45
marker.scale.z = msg.scale * 0.45
marker.color.r = 0.5
marker.color.g = 0.5
marker.color.b = 0.5
marker.color.a = 1.0
return marker
def makeBoxControl( msg ):
control = InteractiveMarkerControl()
control.always_visible = True
control.markers.append( makeBox(msg) )
msg.controls.append( control )
return control
def saveMarker( int_marker ):
server.insert(int_marker, processFeedback)
#####################################################################
# Marker Creation
def normalizeQuaternion( quaternion_msg ):
norm = quaternion_msg.x**2 + quaternion_msg.y**2 + quaternion_msg.z**2 + quaternion_msg.w**2
s = norm**(-0.5)
quaternion_msg.x *= s
quaternion_msg.y *= s
quaternion_msg.z *= s
quaternion_msg.w *= s
def make6DofMarker( fixed, interaction_mode, position, show_6dof = False):
int_marker = InteractiveMarker()
int_marker.header.frame_id = "base_link"
int_marker.pose.position = position
int_marker.scale = 1
int_marker.name = "simple_6dof"
int_marker.description = "Simple 6-DOF Control"
# insert a box
makeBoxControl(int_marker)
int_marker.controls[0].interaction_mode = interaction_mode
if fixed:
int_marker.name += "_fixed"
int_marker.description += "\n(fixed orientation)"
if interaction_mode != InteractiveMarkerControl.NONE:
control_modes_dict = {
InteractiveMarkerControl.MOVE_3D : "MOVE_3D",
InteractiveMarkerControl.ROTATE_3D : "ROTATE_3D",
InteractiveMarkerControl.MOVE_ROTATE_3D : "MOVE_ROTATE_3D" }
int_marker.name += "_" + control_modes_dict[interaction_mode]
int_marker.description = "3D Control"
if show_6dof:
int_marker.description += " + 6-DOF controls"
int_marker.description += "\n" + control_modes_dict[interaction_mode]
if show_6dof:
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 1
control.orientation.y = 0
control.orientation.z = 0
normalizeQuaternion(control.orientation)
control.name = "rotate_x"
control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
if fixed:
control.orientation_mode = InteractiveMarkerControl.FIXED
int_marker.controls.append(control)
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 1
control.orientation.y = 0
control.orientation.z = 0
normalizeQuaternion(control.orientation)
control.name = "move_x"
control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
if fixed:
control.orientation_mode = InteractiveMarkerControl.FIXED
int_marker.controls.append(control)
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 0
control.orientation.y = 1
control.orientation.z = 0
normalizeQuaternion(control.orientation)
control.name = "rotate_z"
control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
if fixed:
control.orientation_mode = InteractiveMarkerControl.FIXED
int_marker.controls.append(control)
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 0
control.orientation.y = 1
control.orientation.z = 0
normalizeQuaternion(control.orientation)
control.name = "move_z"
control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
if fixed:
control.orientation_mode = InteractiveMarkerControl.FIXED
int_marker.controls.append(control)
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 0
control.orientation.y = 0
control.orientation.z = 1
normalizeQuaternion(control.orientation)
control.name = "rotate_y"
control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
if fixed:
control.orientation_mode = InteractiveMarkerControl.FIXED
int_marker.controls.append(control)
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 0
control.orientation.y = 0
control.orientation.z = 1
normalizeQuaternion(control.orientation)
control.name = "move_y"
control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
if fixed:
control.orientation_mode = InteractiveMarkerControl.FIXED
int_marker.controls.append(control)
server.insert(int_marker, processFeedback)
menu_handler.apply( server, int_marker.name )
def makeRandomDofMarker( position ):
int_marker = InteractiveMarker()
int_marker.header.frame_id = "base_link"
int_marker.pose.position = position
int_marker.scale = 1
int_marker.name = "6dof_random_axes"
int_marker.description = "6-DOF\n(Arbitrary Axes)"
makeBoxControl(int_marker)
control = InteractiveMarkerControl()
for i in range(3):
control.orientation.w = rand(-1,1)
control.orientation.x = rand(-1,1)
control.orientation.y = rand(-1,1)
control.orientation.z = rand(-1,1)
normalizeQuaternion(control.orientation)
control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
int_marker.controls.append(copy.deepcopy(control))
control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
int_marker.controls.append(copy.deepcopy(control))
server.insert(int_marker, processFeedback)
def makeViewFacingMarker(position):
int_marker = InteractiveMarker()
int_marker.header.frame_id = "base_link"
int_marker.pose.position = position
int_marker.scale = 1
int_marker.name = "view_facing"
int_marker.description = "View Facing 6-DOF"
# make a control that rotates around the view axis
control = InteractiveMarkerControl()
control.orientation_mode = InteractiveMarkerControl.VIEW_FACING
control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
control.orientation.w = 1
control.name = "rotate"
int_marker.controls.append(control)
# create a box in the center which should not be view facing,
# but move in the camera plane.
control = InteractiveMarkerControl()
control.orientation_mode = InteractiveMarkerControl.VIEW_FACING
control.interaction_mode = InteractiveMarkerControl.MOVE_PLANE
control.independent_marker_orientation = True
control.name = "move"
control.markers.append( makeBox(int_marker) )
control.always_visible = True
int_marker.controls.append(control)
server.insert(int_marker, processFeedback)
def makeQuadrocopterMarker(position):
int_marker = InteractiveMarker()
int_marker.header.frame_id = "base_link"
int_marker.pose.position = position
int_marker.scale = 1
int_marker.name = "quadrocopter"
int_marker.description = "Quadrocopter"
makeBoxControl(int_marker)
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 0
control.orientation.y = 1
control.orientation.z = 0
normalizeQuaternion(control.orientation)
control.interaction_mode = InteractiveMarkerControl.MOVE_ROTATE
int_marker.controls.append(copy.deepcopy(control))
control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
int_marker.controls.append(control)
server.insert(int_marker, processFeedback)
def makeChessPieceMarker(position):
int_marker = InteractiveMarker()
int_marker.header.frame_id = "base_link"
int_marker.pose.position = position
int_marker.scale = 1
int_marker.name = "chess_piece"
int_marker.description = "Chess Piece\n(2D Move + Alignment)"
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 0
control.orientation.y = 1
control.orientation.z = 0
normalizeQuaternion(control.orientation)
control.interaction_mode = InteractiveMarkerControl.MOVE_PLANE
int_marker.controls.append(copy.deepcopy(control))
# make a box which also moves in the plane
control.markers.append( makeBox(int_marker) )
control.always_visible = True
int_marker.controls.append(control)
# we want to use our special callback function
server.insert(int_marker, processFeedback)
# set different callback for POSE_UPDATE feedback
server.setCallback(int_marker.name, alignMarker, InteractiveMarkerFeedback.POSE_UPDATE )
def makePanTiltMarker(position):
int_marker = InteractiveMarker()
int_marker.header.frame_id = "base_link"
int_marker.pose.position = position
int_marker.scale = 1
int_marker.name = "pan_tilt"
int_marker.description = "Pan / Tilt"
makeBoxControl(int_marker)
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 0
control.orientation.y = 1
control.orientation.z = 0
normalizeQuaternion(control.orientation)
control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
control.orientation_mode = InteractiveMarkerControl.FIXED
int_marker.controls.append(control)
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 0
control.orientation.y = 0
control.orientation.z = 1
normalizeQuaternion(control.orientation)
control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
control.orientation_mode = InteractiveMarkerControl.INHERIT
int_marker.controls.append(control)
server.insert(int_marker, processFeedback)
def makeMenuMarker(position):
int_marker = InteractiveMarker()
int_marker.header.frame_id = "base_link"
int_marker.pose.position = position
int_marker.scale = 1
int_marker.name = "context_menu"
int_marker.description = "Context Menu\n(Right Click)"
# make one control using default visuals
control = InteractiveMarkerControl()
control.interaction_mode = InteractiveMarkerControl.MENU
control.description="Options"
control.name = "menu_only_control"
int_marker.controls.append(copy.deepcopy(control))
# make one control showing a box
marker = makeBox( int_marker )
control.markers.append( marker )
control.always_visible = True
int_marker.controls.append(control)
server.insert(int_marker, processFeedback)
menu_handler.apply( server, int_marker.name )
def makeMovingMarker(position):
int_marker = InteractiveMarker()
int_marker.header.frame_id = "moving_frame"
int_marker.pose.position = position
int_marker.scale = 1
int_marker.name = "moving"
int_marker.description = "Marker Attached to a\nMoving Frame"
control = InteractiveMarkerControl()
control.orientation.w = 1
control.orientation.x = 1
control.orientation.y = 0
control.orientation.z = 0
normalizeQuaternion(control.orientation)
control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
int_marker.controls.append(copy.deepcopy(control))
control.interaction_mode = InteractiveMarkerControl.MOVE_PLANE
control.always_visible = True
control.markers.append( makeBox(int_marker) )
int_marker.controls.append(control)
server.insert(int_marker, processFeedback)
if __name__=="__main__":
rospy.init_node("basic_controls")
br = TransformBroadcaster()
# create a timer to update the published transforms
rospy.Timer(rospy.Duration(0.01), frameCallback)
server = InteractiveMarkerServer("basic_controls")
menu_handler.insert( "First Entry", callback=processFeedback )
menu_handler.insert( "Second Entry", callback=processFeedback )
sub_menu_handle = menu_handler.insert( "Submenu" )
menu_handler.insert( "First Entry", parent=sub_menu_handle, callback=processFeedback )
menu_handler.insert( "Second Entry", parent=sub_menu_handle, callback=processFeedback )
position = Point(-3, 3, 0)
make6DofMarker( False, InteractiveMarkerControl.NONE, position, True)
position = Point( 0, 3, 0)
make6DofMarker( True, InteractiveMarkerControl.NONE, position, True)
position = Point( 3, 3, 0)
makeRandomDofMarker( position )
position = Point(-3, 0, 0)
make6DofMarker( False, InteractiveMarkerControl.ROTATE_3D, position, False)
position = Point( 0, 0, 0)
make6DofMarker( False, InteractiveMarkerControl.MOVE_ROTATE_3D, position, True )
position = Point( 3, 0, 0)
make6DofMarker( False, InteractiveMarkerControl.MOVE_3D, position, False)
position = Point(-3, -3, 0)
makeViewFacingMarker( position )
position = Point( 0, -3, 0)
makeQuadrocopterMarker( position )
position = Point( 3, -3, 0)
makeChessPieceMarker( position )
position = Point(-3, -6, 0)
makePanTiltMarker( position )
position = Point( 0, -6, 0)
makeMovingMarker( position )
position = Point( 3, -6, 0)
makeMenuMarker( position )
server.applyChanges()
rospy.spin()