Ask Your Question
0

Issue with rospy.spin and rospy.Rate

asked 2021-02-05 11:05:50 -0600

German gravatar image

updated 2021-02-06 05:19:31 -0600

gvdhoorn gravatar image

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+")

fv1 = open("velocidad1.txt","w+")
fv2 = open("velocidad2.txt","w+")
fv3 = open("velocidad3.txt","w+")
fv4 = open("velocidad4.txt","w+")
fv5 = open("velocidad5.txt","w+")
fv6 = open("velocidad6.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):
    #fv1 = open("velocidad1.txt","a+")
    velocidad[0]=data.data

def Escribe_vel2(data):
    #fv2 = open("velocidad2.txt","a+")
    velocidad[1]=data.data

def Escribe_vel3(data):
    #fv3 = open("velocidad3.txt","a+")
    velocidad[2]=data.data

def Escribe_vel4(data):
    #fv4 = open("velocidad4.txt","a+")
    velocidad[3]=data.data

def Escribe_vel5(data):
    #fv5 = open("velocidad5.txt","a+")
    velocidad[4]=data.data

def Escribe_vel6(data):
    #fv6 = open("velocidad6.txt","a+")
    velocidad[5]=data.data

def Escribe_pos(data):
    start_p=1
    rospy.loginfo("k dise surmano")

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)

    sub7 = rospy.Subscriber("/control_effort/joint1_position_controller/velocidad_actual1", Float64, Escribe_vel1)
    sub8 = rospy.Subscriber("/control_effort/joint2_position_controller/velocidad_actual2", Float64, Escribe_vel2)
    sub9 = rospy.Subscriber("/control_effort/joint3_position_controller/velocidad_actual3", Float64, Escribe_vel3)
    sub10 = rospy.Subscriber("/control_effort/joint4_position_controller/velocidad_actual4", Float64, Escribe_vel4)
    sub11 = rospy.Subscriber("/control_effort/joint5_position_controller/velocidad_actual5", Float64, Escribe_vel5)
    sub11 = rospy.Subscriber("/control_effort/joint6_position_controller/velocidad_actual6", Float64, Escribe_vel6)

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

    rospy.spin()

    r = rospy.Rate(23) #23Hz
    while not rospy.is_shutdown():
        rospy.loginfo("%d" % start_p)
        if (start_p>0):
            rospy.loginfo("ara ba flama abe")
            f1.write("%f\n" % posicion[0])
            f2 ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-02-05 13:04:34 -0600

rdelgadov gravatar image

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.

edit flag offensive delete link more

Comments

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-02-05 11:05:50 -0600

Seen: 122 times

Last updated: Feb 06 '21