messages were subscribed, but callback function does not work

asked 2021-05-07 06:21:16 -0500

IgararA gravatar image

In rqt_graph image, i can see the extent_back topic was subscribed correctly. But the member function back_callback does not work even once. I want to know what makes this and what should i do? I refer to this solution, but it doesn't work for me. answers.ros.org/question/304569/callback-fuction-is-not-getting-called

This node published /extent_back topic

#!/usr/bin/python
import rospy
import time
import threading
import numpy as np
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
from car_msgs.msg import Decode, Extent
class Back(object):

    def __init__(self):
        self.left_rpt = 0
        self.right_rpt = 0
        self.cv_mat = np.zeros([480, 640])
        self.mat_df= 0
        rospy.init_node("is_back")
        rospy.Subscriber("/decode", Decode, self.encoder_callback, queue_size=1)
        rospy.Subscriber("/camera/depth_registered/image_raw", Image, self.depth_callback, queue_size=1)
        self.back_pub = rospy.Publisher("/extent_back", Extent, queue_size=1)


    def encoder_callback(self, msg):
        self.left_rpt = msg.left_rpt
        self.right_rpt = msg.right_rpt


    def depth_callback(self, msg):
        br = CvBridge()
        tmp_mat = self.cv_mat
        self.cv_mat = br.imgmsg_to_cv2(msg)
        if self.cv_mat[240:400, 150:490].any() != tmp_mat[240:400, 150:490].any():
            self.mat_df += 1



    def threading_main(self):
        left_rpt = self.left_rpt
        right_rpt = self.right_rpt
        time.sleep(0.1)
        mat_df = self.mat_df
       # if self.left_rpt - left_rpt < XXX or self.right_rpt - right_rpt < XXX or self.mat_df == mat_df:
        if self.mat_df == mat_df: 
            self.back_pub.publish(Extent(wheel="back", extent=0))
 #          rospy.loginfo("can't move")
if __name__ == "__main__":
    back = Back()
    threading._start_new_thread(rospy.spin, ())
    rate = rospy.Rate(1)
        while not rospy.is_shutdown():
            back.threading_main()
        rate.sleep()

and this node subscribes /extent_back

#!/usr/bin/python
from sensor_msgs.msg import Image
from car_msgs.msg import Extent
from cv_bridge import CvBridge
import cv2
import ros_numpy as rnp
import rospy
import time
import threading
import numpy as np
depth_topic = "/camera/depth_registered/image_raw"

def ex_cal(distance):
    return 4 * distance - 2.2


class Avoid(object):

    def __init__(self, depth_topic,que=5):
        self.br = CvBridge()
       # self.extent = 1  #Flag bit, this ensures that we can't decrease the speed of wheel in once direction changing
        self.image = np.zeros((640, 480))
        self.image_precision = 340 * 160 / 8
        self.back = True
        rospy.init_node('depth_graph', anonymous=True)
        rospy.Subscriber("/extent_back", Extent, self.back_callback, queue_size=que)
        time.sleep(1)
        self.extent_pub = rospy.Publisher("/extent", Extent, queue_size=que)
        rospy.Subscriber(depth_topic, Image, callback=self.depth_callback, queue_size=que)


    def back_callback(self, msg):
        self.back = True
        rospy.loginfo("It is time to {}".format(msg.wheel))

    def depth_callback(self, image):
        self.image = self.br.imgmsg_to_cv2(image)

    def pub_foo(self):
        if self.image.any() :
            count = 0
            nan_count = 0
            left_or_right = 0
            for i in range(240, 400, 2):
                for j in range(150, 490, 2):
                    min_distance = self.image[i, j]
                    if self.image[i, j] == self.image[i, j]:
                        if self.image[i , j] < 0.65:
                            count += 1
                            if 150 < j < 320:
                                left_or_right += 1
                            if min_distance > self.image[i, j]:
                                min_distance = self.image[i, j]
                    else:
                        nan_count += 1
                    if count > self.image_precision:
                        break
                if count > self.image_precision:
                    break
            if nan_count > self.image_precision * 1.2 and not self.back ...
(more)
edit retag flag offensive close merge delete

Comments

1

Could you explain what's threading._start_new_thread(rospy.spin, ()) for? Since you've got both rospy.spin() and while not rospy.is_shutdown(), please make sure to understand how are they different (see point 4 of this answer)

abhishek47 gravatar image abhishek47  ( 2021-05-07 09:02:19 -0500 )edit

thanks, i know every subscribers callback function will start a new thread by now. i use threading._start_new_thread(rospy.spin, ()) to get a more real-time image message. your answer makes me know it is meaningless.but i think this may not cause this situation. in fact, when i removed threading._start_new_threadthis situation does not change,the callback function can not be called as before.thanks again.

IgararA gravatar image IgararA  ( 2021-05-07 10:06:23 -0500 )edit