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

Rate of function [Python]

asked 2019-01-17 10:52:26 -0500

Dylan gravatar image

updated 2019-01-17 11:57:42 -0500

I need to change the frequency at which I enter to the function def detect_marker(self, msg)

Imagine that my code is like this (I deleted a lot of lines because this way would be easier to read):

#!/usr/bin/env python
from __future__ import division
import rospy
import sys
from std_msgs.msg import String
from std_msgs.msg import Empty
from geometry_msgs.msg import Twist
from ardrone_autonomy.msg import  Navdata


import threading
import tf
import time
from std_msgs.msg import Empty
from nav_msgs.msg import Odometry
from sensor_msgs.msg import Image, CameraInfo, CompressedImage
from cv_bridge import CvBridge, CvBridgeError
import cv2
from geometry_msgs.msg import Twist
from geometry_msgs.msg import PoseStamped
from geometry_msgs.msg import Pose

import numpy as np
import math

import time
from time import sleep


from ar_track_alvar_msgs.msg import AlvarMarkers




class gonnaGetDown():


    def __init__(self):


        self.velocity_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
        self.orientation_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=10)

        print "b"

        global u_x_k, u_x_k_pre
        u_x_k, u_x_k_pre = 0, 0

        rospy.Subscriber('/ar_pose_marker', AlvarMarkers, self.detect_marker)

    def detect_marker(self, msg):
        print "c"

        u_x_k_pre = u_x_k
        u_x_k = u_x_k +1

        if not msg.markers:
            print "Tag not found"
            vel_msg = Twist()

            vel_msg.linear.x = 0.0
            vel_msg.linear.y = 0.0
            vel_msg.linear.z = 0.0
            vel_msg.angular.x = 0.0
            vel_msg.angular.y = 0.0
            vel_msg.angular.z = 0.0

            self.velocity_publisher.publish(vel_msg)
            return
        else:
            marker = msg.markers[0]
            print "d"


            self.velocity_publisher.publish(vel_msg)




if __name__ == '__main__':

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



    try:
        rate = rospy.Rate(0.0001) 
        gonna_get_down = gonnaGetDown()
        while not rospy.is_shutdown():

            print "a"

            rate.sleep()


    except rospy.ROSInterruptException:
        rospy.loginfo("Node terminated.")

The most basic rqt graph is this https://imgur.com/LlWrhUW (ar_track_alvar node posts on /ar_pose_marker topic)

Leaving the code like that, it prints: a --> b --> c --> d --> c --> d --> c --> d --> c --> ... and so on

But the time between d's is 1 second. What if I want to make that much faster, like 10 times per second? Where do I change the frequency?

Thanks :D

edit retag flag offensive close merge delete

Comments

Have you seen this question, it gives a good explanation on rospy.Rate.

Isha Dijcks gravatar image Isha Dijcks  ( 2019-01-17 11:11:59 -0500 )edit

But changing the rospy.Rate would change the rate of the function gonnaGetDown, and I would like to stay in detect_marker but at a much higher frequency. If I enter to gonnaGetDown, how can I define the global variables and to maintain there values between gonnaGetDown and detect_marker?

Dylan gravatar image Dylan  ( 2019-01-17 11:15:28 -0500 )edit

I mean, I need them to stay in their past values. If they enter to gonnaGetDown, they're always going to be =0

Dylan gravatar image Dylan  ( 2019-01-17 11:17:30 -0500 )edit

detect_marker() is a callback of the subscriber to the topic /ar_pose_marker. If you want to have the callback triggered more often, you'd have to publish more on that topic or increase the rate with which you check for messages on that topic if you are missing some.

Isha Dijcks gravatar image Isha Dijcks  ( 2019-01-17 11:22:53 -0500 )edit

And how can I do that @IshaDijcks?

Dylan gravatar image Dylan  ( 2019-01-17 11:32:34 -0500 )edit

You could increase the rate of the node that publishes to that topic. However I am not completely sure what it is you are trying to accomplish. What exactly is your goal and why?

Isha Dijcks gravatar image Isha Dijcks  ( 2019-01-17 11:37:28 -0500 )edit

If I just change the rospy.Rate to 10, the program prints "a" 10 times, then "c" and then "d"

Dylan gravatar image Dylan  ( 2019-01-17 11:46:24 -0500 )edit

I want to enter more times to that function so I get much more samples per second. I'm trying to control a quadcopter. I don't know how to change the rate of the ar_track_alvar node

Dylan gravatar image Dylan  ( 2019-01-17 11:47:58 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2019-01-17 13:30:54 -0500

As has been mentioned by @Isha Dijcks the detect_marker function in your code is a message callback. It gets executed every time your node receives a message on it's topic, in this case it's own execution takes an insignificant amount of time so the rate it is executed at is entirely depended on the rate of messages being published on that topic.

If you want to speed this up you don't need to do anything to this code, you need to change the settings for ar_track_alvar that controls the rate at which it publishes marker pose detection messages.

There is a non-documented (as far as I can tell) configuration parameter (see source code) for this node called max_frequency which defaults to 8 (Hz). By adjusting this you should be able to increase the rate of your callback function.

Hope this helps.

edit flag offensive delete link more

Comments

Solved, thanks !!!

Dylan gravatar image Dylan  ( 2019-01-17 14:21:56 -0500 )edit

@Dylan if this answered your question, then could you please click on the check mark (✓) to the left of the answer

jayess gravatar image jayess  ( 2019-01-17 16:33:23 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-01-17 10:52:26 -0500

Seen: 580 times

Last updated: Jan 17 '19