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

Why did the pub before rospy.spin() not work?

asked 2021-03-31 02:24:17 -0500

Win gravatar image

updated 2021-04-01 22:04:54 -0500

jayess gravatar image

I want to use Python to publish a topic before the spin. There is no problem with data printing, but there is no data in the echo outside, It can work in the callbacks.

self.pub_distance = rospy.Publisher('/path_distance',Int32,queue_size=10)
def run(self):
        distance_ = Int32()
        distance_.data = self.generate_path(0, self.path.keys()[-1])
        print distance_ #Here's a value printed on the screen
        self.pub_distance.publish(distance_) #But this doesn't work!!!
        rospy.spin()
edit retag flag offensive close merge delete

Comments

Too little info! Could you include your code?

abhishek47 gravatar image abhishek47  ( 2021-03-31 11:19:04 -0500 )edit

already added.

Win gravatar image Win  ( 2021-04-01 23:07:29 -0500 )edit

rospy.spin() is blocking and sets off the "event loop" (an "infinite while" if you will). How/where do you call run()?

abhishek47 gravatar image abhishek47  ( 2021-04-02 00:54:02 -0500 )edit

this code is my simple version,run() is running in main function.

Win gravatar image Win  ( 2021-04-02 01:42:13 -0500 )edit

I can understand spin(), but why did the pub is not working?

Win gravatar image Win  ( 2021-04-02 01:46:49 -0500 )edit

Did you solve this problem because i am facing with the same problem right now?

anonymous userAnonymous ( 2022-11-22 02:35:34 -0500 )edit
1

Two solutions, delay 1s before pub or make publish global (move out of class)

Win gravatar image Win  ( 2022-12-02 00:05:47 -0500 )edit

2 Answers

Sort by » oldest newest most voted
-2

answered 2021-04-19 09:12:38 -0500

Win gravatar image

updated 2022-12-02 04:54:14 -0500

两种解决方法,在pub前延迟1s或者将publish定义为全局(移出class)

Two solutions, delay 1s before pub or make publish global (move out of class)

edit flag offensive delete link more

Comments

I'm glad you solved it. If you like, you can leave it in English. I think it will help others who follow.

By the way, is the code of the solution like this?

1

self.pub_distance = rospy.Publisher('/path_distance',Int32,queue_size=10)
def run(self):
        distance_ = Int32()
        distance_.data = self.generate_path(0, self.path.keys()[-1])
        print distance_ #Here's a value printed on the screen
        rospy.sleep(1.0) # added
        self.pub_distance.publish(distance_)
        rospy.spin()

2

pub_distance = rospy.Publisher('/path_distance',Int32,queue_size=10) # changed
def run(self):
        distance_ = Int32()
        distance_.data = self.generate_path(0, self.path.keys()[-1])
        print distance_ #Here's a value printed on the screen
        pub_distance.publish(distance_) # changed
        rospy.spin()
miura gravatar image miura  ( 2021-04-19 09:31:58 -0500 )edit

Yes, it is right.

Win gravatar image Win  ( 2022-12-02 04:54:52 -0500 )edit
0

answered 2021-04-02 08:04:06 -0500

miura gravatar image

Probably, if you are not running spin(), the node is terminated before the topic is sent.

The topic will not be sent when you publish it. As a way to wait for it to be sent, spin() is an option.

It is important to note that callbacks are not always necessary here.

The topic will probably still be sent below.

self.pub_distance = rospy.Publisher('/path_distance',Int32,queue_size=10)
def run(self):
        distance_ = Int32()
        distance_.data = self.generate_path(0, self.path.keys()[-1])
        print distance_ #Here's a value printed on the screen
        self.pub_distance.publish(distance_) #But this doesn't work!
        # rospy.spin()
        Rate = rospy.Rate(10)
        while not rospy.is_shutdown():
                rate.sleep()
edit flag offensive delete link more

Comments

I tried, this is invalid.

Win gravatar image Win  ( 2021-04-06 03:08:32 -0500 )edit

Thank you for trying it. There is no other cause I can think of. I'm looking forward to other people's answers.

miura gravatar image miura  ( 2021-04-06 08:19:55 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-03-31 02:24:17 -0500

Seen: 335 times

Last updated: Dec 02 '22