我每1/30秒(即频率为30 Hz)接收到我的相机和目标点之间的距离信息(在三个轴上:x,y,z),但是我的脚本正在运行更快(每个决定的时间为1E-5秒)。我需要放慢代码的速度,因此每次我进行1次距离测量时,它只会移动一次。
我的代码如下(我只粘贴了有趣的部分):
class gonnaGetDown():
def __init__(self):
self.velocity_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=10)
print "b"
rospy.Subscriber('/ar_pose_marker', AlvarMarkers, self.detect_marker)
def detect_marker(self, msg):
print "c"
saturacion = 0.2
vel_msg = Twist()
if not msg.markers:
return
else:
marker = msg.markers[0]
print "d"
now = datetime.datetime.now()
print "time [us]: ", now.microsecond
if __name__ == '__main__':
rospy.init_node('go_to', anonymous=True)
try:
rate = rospy.Rate(0.1) #I don't know why this is here, I don't use it
gonna_get_down = gonnaGetDown()
while not rospy.is_shutdown():
print "a"
rate.sleep()
except rospy.ROSInterruptException:
rospy.loginfo("Node terminated.")
例如,打印我的代码:
b
b
a
c
d
time [us]: 607766
c
d
time [us]: 642230
c
d
time [us]: 710855
c
d
time [us]: 777346
c
d
time [us]: 807427
c
d
time [us]: 883408
c
d
time [us]: 916693
c
d
time [us]: 973941
c
d
time [us]: 7659
c
d
time [us]: 80693
不是30 Hz。我希望时差为33333.3333纳秒。怎么了?