ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 2013-04-16 05:06:45 -0500

joq gravatar image

The ROS Groovy release on Ubuntu Precise 12.04 uses Python 2.7, which is the default interpreter for that system.

Precise also provides Python 3.2, which you can install, but the ROS distribution was not built to use that. It is probably possible to get it working by building everything from source, but I do not recommend that approach.

If you are willing to use Python 2, things will be much easier for now.

First, you need to get comfortable with basic ROS programming in Python. This tutorial will explain how to publish ROS messages.

It's a little hard to untangle the code you posted. If you are new to Python, beware that indentation is significant because it defines the block structure of your program. So, be careful with that.

You should not try to create two ROS nodes in that single script. You don't need that, anyway. One node will suffice. Your main function will probably look more like this:

rospy.init_node('ticks')
lpub = rospy.Publisher('lwheel', Int16)
rpub = rospy.Publisher('rwheel', Int16)
hz_rate = rospy.Rate(80)
while not rospy.is_shutdown():
    # read left encoder
    lpub.publish(Int16(data=left))
    # read right encoder
    rpub.publish(Int16(data=right))
    hz_rate.sleep()

That is just a simplified overview. Your actual logic will be more complex.

Note that handling of bytes and strings differs between Python 2 and Python 3. It is possible to write code that works with either, but that gets tricky.

The ROS Groovy release on Ubuntu Precise 12.04 uses Python 2.7, which is the default interpreter for that system.

Precise also provides Python 3.2, which you can install, but the ROS distribution was not built to use that. It is probably possible to get it working by building everything from source, but I do not recommend that approach.

If you are willing to use Python 2, things will be much easier for now.now. Note that handling of bytes and strings differs between Python 2 and Python 3. It is possible to write code that works with either, but that gets tricky. Keep it simple until you have something that works.

First, you need to get comfortable with basic ROS programming in Python. This tutorial will explain how to publish ROS messages.

It's a little hard to untangle the code you posted. If you are new to Python, Python: beware that indentation is significant because it defines the block structure of your program. So, be careful with that.careful.

You should not try to create two ROS nodes in that single script. You don't need that, anyway. One to, anyway, one node will suffice. Your main function will probably look more like this:

rospy.init_node('ticks')
lpub = rospy.Publisher('lwheel', Int16)
rpub = rospy.Publisher('rwheel', Int16)
hz_rate = rospy.Rate(80)
while not rospy.is_shutdown():
    # read left encoder
    lpub.publish(Int16(data=left))
    # read right encoder
    rpub.publish(Int16(data=right))
    hz_rate.sleep()

That is just a simplified overview. Your actual logic will be more complex.

Note that handling of bytes and strings differs between Python 2 and Python 3. It is possible to write code that works with either, but that gets tricky.

The ROS Groovy release on Ubuntu Precise 12.04 uses Python 2.7, which is the default interpreter for that system.

Precise also provides Python 3.2, which you can install, but the ROS distribution was not built to use that. It is probably possible to get it working by building everything from source, but I do not recommend that approach.

If you are willing to use Python 2, things will be much easier for now. Note that handling of bytes and strings differs between Python 2 and Python 3. It is possible to write code that works with either, but that gets tricky. Keep it simple until you have something that works.

First, you need to get comfortable with basic ROS programming in Python. This tutorial will explain how to publish ROS messages.

It's a little hard to untangle the code you posted. If you are new to Python: beware that indentation is significant because it defines the block structure of your program. So, be careful.

You should not try to create two ROS nodes in that single script. You don't need to, anyway, one node will suffice. Your main function will probably look more like this:

rospy.init_node('ticks')
lpub = rospy.Publisher('lwheel', Int16)
rpub = rospy.Publisher('rwheel', Int16)
hz_rate = rospy.Rate(80)
while not rospy.is_shutdown():
    # read left encoder
    lpub.publish(Int16(data=left))
lpub.publish(Int16(data=int(left)))
    # read right encoder
    rpub.publish(Int16(data=right))
rpub.publish(Int16(data=int(right)))
    hz_rate.sleep()

That is just a simplified overview. Your actual logic will be more complex.