Main Page
Namespaces
Classes
Files
File List
003_listener_with_user_data
listener_with_user_data.py
Go to the documentation of this file.
1
#!/usr/bin/env python
2
# Software License Agreement (BSD License)
3
#
4
# Copyright (c) 2008, Willow Garage, Inc.
5
# All rights reserved.
6
#
7
# Redistribution and use in source and binary forms, with or without
8
# modification, are permitted provided that the following conditions
9
# are met:
10
#
11
# * Redistributions of source code must retain the above copyright
12
# notice, this list of conditions and the following disclaimer.
13
# * Redistributions in binary form must reproduce the above
14
# copyright notice, this list of conditions and the following
15
# disclaimer in the documentation and/or other materials provided
16
# with the distribution.
17
# * Neither the name of Willow Garage, Inc. nor the names of its
18
# contributors may be used to endorse or promote products derived
19
# from this software without specific prior written permission.
20
#
21
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
# POSSIBILITY OF SUCH DAMAGE.
33
#
34
# Revision $Id$
35
36
## Simple listener demo that demonstrates how to register additional
37
## arguments to be passed to a subscription callback
38
39
NAME =
'listener_with_user_data'
40
41
import
sys
42
43
import
rospy
44
from
std_msgs.msg
import
*
45
46
def
callback
(data, args):
47
if
args == 1:
48
print
"#1: I heard [%s]"
%data.data
49
elif
args == 2:
50
print
"#2: I heard [%s]"
%data.data
51
else
:
52
print
"I heard [%s] with userdata [%s]"
%(data.data, str(args))
53
54
def
listener_with_user_data
():
55
# Callback arguments (aka user data) allow you to reuse the same
56
# callback for different topics, or they can even allow you to use
57
# the same callback for the same topic, but have it do something
58
# different based on the arguments.
59
rospy.Subscriber(
"chatter"
, String, callback, 1)
60
rospy.Subscriber(
"chatter"
, String, callback, 2)
61
rospy.Subscriber(
"chatter"
, String, callback, (
"chatter1"
, 4))
62
rospy.Subscriber(
"chatter2"
, String, callback,
"This is from chatter2"
)
63
rospy.init_node(NAME, anonymous=
True
)
64
rospy.spin()
65
66
if
__name__ ==
'__main__'
:
67
try
:
68
listener_with_user_data
()
69
except
KeyboardInterrupt, e:
70
pass
71
print
"exiting"
listener_with_user_data.listener_with_user_data
def listener_with_user_data()
Definition:
listener_with_user_data.py:54
listener_with_user_data.callback
def callback(data, args)
Definition:
listener_with_user_data.py:46
msg
listener_with_user_data
Definition:
listener_with_user_data.py:1
rospy_tutorials
Author(s): Ken Conley
autogenerated on Fri Jun 7 2019 22:01:45