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

Subscriber fails to reach published data

asked 2023-08-07 01:41:06 -0500

Nongtuy gravatar image

Publisher seems to be workable but when I do a rosrun on receiver script the error exists. as very new on this field I have no idea how to fix. thank you in advance.


1. Error Details

Traceback (most recent call last):
File "/home/------/proto_one/src/sensors_unit/scripts/first_sensor.py", line 28, in <module>
first_sensor()
File "/home/------/proto_one/src/sensors_unit/scripts/first_sensor.py", line 23, in first_sensor
pub.publish(testnum)
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 886, in publish
  raise ROSSerializationException(str(e))
rospy.exceptions.ROSSerializationException: field data must be an integer type

2. Receiver code(Subscriber)

 #!/usr/bin/env python3
import rospy as R
from std_msgs.msg import Int16
import random

def callback(data):
    R.loginfo(data.data)
def reciever():
    R.init_node("reciever", anonymous=True)
    R.Subscriber("commuline1", Int16, callback)
    R.spin()

if __name__ =="__main__":
    reciever()

3. Sender code (Publisher)

#!/usr/bin/env python3

import rospy as R
from std_msgs.msg import Int16
import random
global x
x = 0

def first_sensor():
    x=0
    pub = R.Publisher("commuline1", Int16, queue_size=10)
    R.init_node("first_sensor", anonymous=True)
    rate =R.Rate(10)

    while not R.is_shutdown() and x <1000000 :
        x += 1
        t = x
        testnum = "helloooo %s" %R.get_time()
        ##testnum2 = print(testnum)
        ##print(testnum2)
        ##pub.publish(testnum2)
        print(t)
        pub.publish(testnum)
        rate = R.Rate(10)

if __name__ == "__main__":
    try:
        first_sensor()
    except R.ROSInterruptException:
        pass
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2023-08-07 03:42:55 -0500

gvdhoorn gravatar image

updated 2023-08-07 03:43:39 -0500

def first_sensor():
    x=0
    pub = R.Publisher("commuline1", Int16, queue_size=10)

    ...

    while not R.is_shutdown() and x <1000000 :
        testnum = "helloooo %s" %R.get_time()
        ...
        pub.publish(testnum)

Your publisher (ie: pub) is initialised to publish Int16 messages.

testnum is a str.

You can't publish str with an Int16 publisher.

You should be able to publish x or t -- both integers, or change testnum so it's an int again.

edit flag offensive delete link more

Comments

I’ve just found the solution recently, Thank youuu.

Nongtuy gravatar image Nongtuy  ( 2023-08-07 04:12:40 -0500 )edit

Could you please mark the question as answered by ticking the checkmark (✓) to the left of the answer if you feel it has been answered? Thanks.

gvdhoorn gravatar image gvdhoorn  ( 2023-08-07 04:24:08 -0500 )edit

Question Tools

Stats

Asked: 2023-08-07 01:41:06 -0500

Seen: 44 times

Last updated: Aug 07 '23