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

subscriber infinite loop in python

asked 2022-05-31 11:49:01 -0500

reg gravatar image

updated 2022-05-31 11:50:00 -0500

Hi

I am starting with ROS and i have done this code who can read a image provided by topic to do something more when this run properly.

The problem is that never run "print("salida del bucle"). it enters at def (obtener)..... and never exit from this function

why can be the reason?

#!/usr/bin/env python
#%s string, %f  float, %d integer

import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import numpy as np
import cv2
import datetime



#.....................lectura topic odometria.....................
def obtener(datos):
    #rospy.loginfo(rospy.get_caller_id() + " datos actualizados")
    #rospy.loginfo(rospy.get_caller_id() + " anchura de pixeles %i", datos.width)
    #rospy.loginfo(rospy.get_caller_id() + " datos leidos de camara %i", datos.data[100])  
    #convertimos la imagen a codigo python
    global cv_image
    cv_image = bridge.imgmsg_to_cv2(datos)      
    cv_image_p = cv2.normalize(cv_image, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
    cv2.imshow('image_desde_ROS', cv_image_p)
    cv2.waitKey(30)
    #rospy.sleep(0.1)
    #rospy.Rate(1)

#creamos un subscriptor para obtener la odometria
def leer_odometria():
    rospy.Subscriber("/d435i_camera/depth/image_rect_raw", Image, obtener)
    rospy.spin()
#----------------------------publicacion topic velocidad----------------------
if __name__ == '__main__':
    # Inicializamos el nodo de ROS
    rospy.init_node("test_drive")

    # Creamos un publicista para que publique comandos de velocidad
    cmd_pub = rospy.Publisher("cmd_vel", Twist, queue_size=1)
    bridge = CvBridge() 

    col = [0, 0, 0, 0, 0]
    col_nueva = [0, 0, 0, 0, 0]
    try:
        while not rospy.is_shutdown():
            leer_odometria()
            print("salida del bucle")            
    except rospy.ROSInterruptException:
        pass
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2022-05-31 16:18:12 -0500

rospy.spin() is starting a loop waiting for a callback or an interrupt signal. See post for more details. It is basically doing what your while section is doing.

edit flag offensive delete link more
0

answered 2023-04-12 07:50:27 -0500

i0nly gravatar image

To add onto what bi3ri said, if your goal is to gather 1 image then see your message, you could try rospy.wait_for_message() instead. There's a good explanation of it here.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-05-31 11:49:01 -0500

Seen: 327 times

Last updated: May 31 '22