1.關於無rospy.spin()調用多次callback 2. subscrib后面語句和callback函數運行順序


#!/usr/bin/env python
import rospy
from bzrobot_msgs.msg import bzr_WheelLinearVels

#from threading import Thread
from time import sleep

class RS232MotorComm():

    def callback(self, data):
        print '****************************************start callback'
        self.flg=0 sleep(0.05)
        rospy.loginfo("speed %f %f",data.leftWheelLinearVel, data.rightWheelLinearVel)
        self.flg=1
    
    def motor_listener(self):
        self.flg=1
        rospy.init_node('rs232_motor_comm', anonymous=True)

        rospy.Subscriber("control_wheel_linear_vel", bzr_WheelLinearVels, self.callback)
        #sleep(1)
    
 r = rospy.Rate(10) # 10hz while self.flg==1:#not rospy.is_shutdown():#True: print'where' r.sleep()  #time must enough for callback,or it will out while loop print'after sleep 1s'

if __name__ == '__main__':
    
    motor_controller = RS232MotorComm()
    print'the1'

    motor_controller.motor_listener()
    #rospy.spin()
    print'end'

1.關於rospy.spin()調用多次callback 

程序若無rospy.spin(),也無上面黃色部分,則訂閱一次發布消息會調用多次callback。

 

2.當接收到訂閱消息時,ballback在r.sleep()時間空隙的時候調用,所以callback的執行時間不能超過r.sleep()提供的間隙時間,例如在callback里設置sleep(1),則會消耗完r.sleep(),然后運行print 'after sleep 1s',此時flg=0,因此跳出while循環,結束程序。

  直接用sleep()代替rospy.Rate和r.sleep(),也是可以的。

 

3.最好使用:

   while not rospy.is_shutdown(): #True:
         if self.flg==1: #encoder_list[0]='e':


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM