Subscriber fails to reach published data
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
Asked by Nongtuy on 2023-08-07 01:41:06 UTC
Answers
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.
Asked by gvdhoorn on 2023-08-07 03:42:55 UTC
Comments
I’ve just found the solution recently, Thank youuu.
Asked by Nongtuy on 2023-08-07 04:12:40 UTC
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.
Asked by gvdhoorn on 2023-08-07 04:24:08 UTC
Comments