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

Trying to publish information from subscriber node.

asked 2023-08-08 03:09:57 -0600

Nongtuy gravatar image

updated 2023-08-08 09:15:32 -0600

I'm trying to make system consist of several sections, it must surely have at least 1 node doing 2 roles, both subscriber and publisher. and here it my codes.


First section

#!/usr/bin/env python3

import rospy as R
from std_msgs.msg import Float64

def sender_3():
    x = 0 
    y = float(input("Give us the float num: "))
    pub = R.Publisher("line3", Float64, queue_size=10)
    R.init_node("sender_3", anonymous=True)
    rate = R.Rate(10)
    while not R.is_shutdown() and x < y:
        x += 0.1
        t = x
        print(x)
        pub.publish(t)
        rate.sleep()

if __name__ == "__main__":
    try:
        sender_3()
    except R.ROSInterruptException:
        pass

Second section

#!/usr/bin/env python3
import rospy as R
from std_msgs.msg import Float64, String

def callback(data):
    global r_data
    r_data = data.data
    R.loginfo("sawadee krub i've received as info: %f", data.data)
    global t_o
    t_o = str(r_data)
    R.loginfo("Applied num to chatGPT: %s", t_o)
def receiver_2():
    R.init_node("receiver_2", anonymous=10)
    R.Subscriber("line3", Float64, callback, queue_size=True)
    pub = R.Publisher("translne", String, queue_size=10)
    rate = R.Rate(10)
    while not R.is_shutdown:
        print(t_o)
        pub.publish(t_o)
        rate.sleep()
    R.spin()
#def transporter():
 #   R.init_node("transporter send you", anonymous=10)
  #  pub = R.Publisher("translne", String, queue_size=10)
   # while not R.is_shutdown:
    #    print(receiver_2)
     #   pub.publish(receiver_2)
      #  R.Rate(10)
if __name__ == "__main__":
    receiver_2()

Last section

#!/usr/bin/env python3

import rospy as R
from std_msgs.msg import String

def callback(data):
    R.loginfo("I've received an information: %s", data.data)
def sub_receiver():
    R.init_node("sub_receiver", anonymous=True)
    R.Subscriber("translne", String , callback, queue_size=10)
    R.spin() 

if __name__ == "__main__":
    sub_receiver()

My problem is during the transition between second and last section, it seems like last section didn't receive any information which is translated into String and published to last section, cause there's nothing appears even error. P.S I commented on third section for several previous tries. Edited: Mistakes have been fixed.

Thank you in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-08-08 07:36:15 -0600

Mike Scheutzow gravatar image

updated 2023-08-08 07:44:25 -0600

Your code is not doing this correctly. Please look at section 2 of http://wiki.ros.org/rospy/Overview/Time for the proper way to construct a while/Rate loop in python.

while not R.is_shutdown:     # <- wrong
    print(t_o)
    pub.publish(t_o)
    R.Rate(10)        # <- wrong
R.spin()    #  <- wrong
edit flag offensive delete link more

Comments

Oh silly me, I always forget to add () in definition. After I added that it works properly now Thank you Best regard, Tuy

Nongtuy gravatar image Nongtuy  ( 2023-08-08 23:28:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2023-08-08 03:09:57 -0600

Seen: 713 times

Last updated: Aug 08 '23