# Issue with rospy.spin and rospy.Rate

I got a problem with this code, after doing some tests, i think that my callbacks are not being called, should i make another program without mixing rospy.spin and rospy.Rate? i want the rospy.spin in order to the loop for remain my subscribers and the rospy rate is required because i need that frequency to write some data at that specific frequency. Thanks.

#!/usr/bin/env python

import rospy
from std_msgs.msg import *
from vector_msgs.msg import *
import numpy as np
#Abrimos los archivos de texto en los que escribimos el estado actual del robot (q y q')
f1 = open("posicion1.txt","w+")
f2 = open("posicion2.txt","w+")
f3 = open("posicion3.txt","w+")
f4 = open("posicion4.txt","w+")
f5 = open("posicion5.txt","w+")
f6 = open("posicion6.txt","w+")

posicion = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
velocidad = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0])

start_p=0

start_v=0

def Escribe_pos1(data):
#f1 = open("posicion1.txt","a+")
posicion[0]=data.data

def Escribe_pos2(data):
#f2 = open("posicion2.txt","a+")
posicion[1]=data.data

def Escribe_pos3(data):
#f3 = open("posicion3.txt","a+")
posicion[2]=data.data

def Escribe_pos4(data):
#f4 = open("posicion4.txt","a+")
posicion[3]=data.data

def Escribe_pos5(data):
#f5 = open("posicion5.txt","a+")
posicion[4]=data.data

def Escribe_pos6(data):
#f6 = open("posicion6.txt","a+")
posicion[5]=data.data

def Escribe_vel1(data):

def Escribe_vel2(data):

def Escribe_vel3(data):

def Escribe_vel4(data):

def Escribe_vel5(data):

def Escribe_vel6(data):

def Escribe_pos(data):
start_p=1

def Escribe_vel(data):
start_v=1

if _name_ == '_main_':

rospy.init_node('Escritor_de_posiciones', anonymous=True)

sub1 = rospy.Subscriber("/control_effort/joint1_position_controller/posicion_actual1", Float64, Escribe_pos1)
sub2 = rospy.Subscriber("/control_effort/joint2_position_controller/posicion_actual2", Float64, Escribe_pos2)
sub3 = rospy.Subscriber("/control_effort/joint3_position_controller/posicion_actual3", Float64, Escribe_pos3)
sub4 = rospy.Subscriber("/control_effort/joint4_position_controller/posicion_actual4", Float64, Escribe_pos4)
sub5 = rospy.Subscriber("/control_effort/joint5_position_controller/posicion_actual5", Float64, Escribe_pos5)
sub6 = rospy.Subscriber("/control_effort/joint6_position_controller/posicion_actual6", Float64, Escribe_pos6)

sub13 = rospy.Subscriber("/referencias_posicion", vector, Escribe_pos)

rospy.spin()

r = rospy.Rate(23) #23Hz
while not rospy.is_shutdown():
if (start_p>0):
f1.write("%f\n" % posicion[0])
f2 ...
edit retag close merge delete

Sort by ยป oldest newest most voted

Hi there!

The problem is where you put the rospy.spin().

rospy.spin() keeps the current thread alive but does not execute any other instructions.

In your example, removing the rospy.spin() command before and after while not rospy.is_shutdown () will run the code I think you want to run.

more

Also, while not rospy.is_shutdown() should have r.sleep() as explained in rospy Sleeping and Rates.

( 2021-02-07 08:36:59 -0600 )edit