⇤ ← Revision 1 as of 2009-07-20 20:21:55
Size: 347
Comment:
|
Size: 1870
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
TODO | This tutorial is meant for people are already comfortable using numpy and rospy. = Step 1: Create a Package = Lets create a package to house the new code that we are developing. {{{ roscreate-pkg numpy_tutorial rospy rospy_tutorials }}} As we are using numpy, we also need to make sure that our package declares this, so lets edit the [:Manifest:manifest.xml] file to add the following ["rosdep"] line: {{{ <rosdep name="python-numpy" /> }}} = Step 2: Create a Listener = By now, you're probably familiar with creating listener nodes. Lets start off simple and create a standard listener node that listens to the `floats` [:Topics:Topic] using the `rospy_tutorials.msg.Floats` ["msg"] type. {{{#!python PKG = 'rospy_tutorials' # this package name import roslib; roslib.load_manifest(PKG) import rospy from rospy_tutorials.msg import Floats from rospy.numpy_msg import numpy_msg def callback(data): print rospy.get_caller_id(), "I heard %s"%data.data def listener(): rospy.init_node('listener') rospy.Subscriber("floats", Floats, callback) # spin() simply keeps python from exiting until this node is stopped rospy.spin() if __name__ == '__main__': listener() }}} Just for reference, here is the `rospy_tutorials/Floats` definition: {{{ $ rosmsg show rospy_tutorials/Floats float32[] data }}} = Step 3: Numpy-ize the Listener = from rospy.numpy_msg import numpy_msg rospy.Subscriber("floats", numpy_msg(Floats), callback) |
This tutorial covers using [http://numpy.scipy.org/ numpy] with ["rospy"], the ROS Python client library. Numpy is a popular scientific computing package for Python. You will often want to consider using Numpy with rospy if you are working with sensor data as it has better performance and many libraries for manipulating arrays of data.
This tutorial is meant for people are already comfortable using numpy and rospy.
Step 1: Create a Package
Lets create a package to house the new code that we are developing.
roscreate-pkg numpy_tutorial rospy rospy_tutorials
As we are using numpy, we also need to make sure that our package declares this, so lets edit the [:Manifest:manifest.xml] file to add the following ["rosdep"] line:
<rosdep name="python-numpy" />
Step 2: Create a Listener
By now, you're probably familiar with creating listener nodes. Lets start off simple and create a standard listener node that listens to the floats [:Topics:Topic] using the rospy_tutorials.msg.Floats ["msg"] type.
1 PKG = 'rospy_tutorials' # this package name
2 import roslib; roslib.load_manifest(PKG)
3
4 import rospy
5 from rospy_tutorials.msg import Floats
6 from rospy.numpy_msg import numpy_msg
7
8 def callback(data):
9 print rospy.get_caller_id(), "I heard %s"%data.data
10
11 def listener():
12 rospy.init_node('listener')
13 rospy.Subscriber("floats", Floats, callback)
14 # spin() simply keeps python from exiting until this node is stopped
15 rospy.spin()
16
17 if __name__ == '__main__':
18 listener()
Just for reference, here is the rospy_tutorials/Floats definition:
$ rosmsg show rospy_tutorials/Floats float32[] data
Step 3: Numpy-ize the Listener
from rospy.numpy_msg import numpy_msg
- rospy.Subscriber("floats", numpy_msg(Floats), callback)