Rate of function [Python]
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
Have you seen this question, it gives a good explanation on rospy.Rate.
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?
I mean, I need them to stay in their past values. If they enter to gonnaGetDown, they're always going to be =0
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.And how can I do that @IshaDijcks?
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?
If I just change the rospy.Rate to 10, the program prints "a" 10 times, then "c" and then "d"
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