Ask Your Question
1

Trying to write a service to publish data from serial

asked 2013-03-09 03:34:40 -0500

ianphil397 gravatar image

updated 2014-01-28 17:15:36 -0500

ngrennan gravatar image

Hi, I've copied the code from the tutorial "publisher and subscriber", and tried to modify it to read serial data from an an arduino and publish it. Here is the code for my publisher:

#!/usr/bin/env python
import roslib; roslib.load_manifest('beginner_tutorials')
import rospy
from std_msgs.msg import String
import serial

def talker():
    ser = serial.Serial('/dev/ttyACM0', 9600)

    pub = rospy.Publisher('chatter', String)
    rospy.init_node('talker')
    while not rospy.is_shutdown():
        ser.readline()
        rospy.loginfo(ser)
        pub.publish(String(ser))
        rospy.sleep(1.0)


if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

The code for my listener is unchanged from the tutorial. when I rosrun them both the publisher stop with the following error message:

File "/home/pi/fuerte_workspace/sandbox/serial_message/scripts/talker.py", line 22, in <module>
    talker()
  File "/home/pi/fuerte_workspace/sandbox/serial_message/scripts/talker.py", line 16, in talker
    pub.publish(String(ser))
  File "/opt/ros/fuerte/lib/python2.7/dist-packages/ros_comm-1.8.12-py2.7.egg/rospy/topics.py", line 796, in publish
    raise ROSSerializationException(str(e))
rospy.exceptions.ROSSerializationException: field data must be of type str

What is it I'm doing wrong and how and I resolve this issue? (I'm aware I can ser up the Arduino to publish directly though I'd like to avoid it as that would require a large modification to my Arduino code.)

Amend: After adding the following code:

sensor = ser.readline()
rospy.loginfo(sensor)
pub.publish(String(sensor))

When I run them both I get the following error from the listener.

[ERROR] [WallTime: 1362846465.102142] bad callback: <function callback="" at="" 0x922430="">
Traceback (most recent call last):
  File "/opt/ros/fuerte/lib/python2.7/dist-packages/ros_comm-1.8.12-py2.7.egg/rospy/topics.py", line 678, in _invoke_callback
    cb(msg)
  File "/home/pi/fuerte_workspace/sandbox/serial_message/scripts/listener.py", line 8, in callback
    rospy.loginfo(rospy.get_name() % data.data)
TypeError: not all arguments converted during string formatting

How do I fix this?

edit retag flag offensive close merge delete

Comments

Okay so I need something like

data = ser.readline()

then

pub.publish(String(data))

?

ianphil397 gravatar imageianphil397 ( 2013-03-09 04:03:28 -0500 )edit

Your listener printout in loginfo is wrong. This is unrelated to this question. Please open a new one.

dornhege gravatar imagedornhege ( 2013-03-09 04:55:08 -0500 )edit

DId your code work?

uzair gravatar imageuzair ( 2014-04-30 09:43:17 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2013-03-09 04:01:40 -0500

dornhege gravatar image

You are publishing the Serial object instead of the line that you read from it (which you store nowhere).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-03-09 03:34:40 -0500

Seen: 1,429 times

Last updated: Mar 09 '13