国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

解決ROSPY沒(méi)有spinOnce

2019-11-08 18:37:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

之前ROS編程一直用C++,C++訂閱消息的時(shí)候可以用spin(一直進(jìn)入回調(diào)函數(shù)),大循環(huán)中用spinOnce時(shí)才進(jìn)入回調(diào)函數(shù)。最近想顯示一個(gè)圖像界面,python中有matplot模塊可以直接figure圖像。找到一個(gè)最簡(jiǎn)單的話題訂閱程序:

#!/usr/bin/env pythonimport rospyfrom std_msgs.msg import Stringdef callback(data): rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)def listener(): # In ROS, nodes are uniquely named. If two nodes with the same # node are launched, the PRevious one is kicked off. The # anonymous=True flag means that rospy will choose a unique # name for our 'listener' node so that multiple listeners can # run simultaneously. rospy.init_node('listener', anonymous=True) rospy.Subscriber("chatter", String, callback) # spin() simply keeps python from exiting until this node is stopped rospy.spin()if __name__ == '__main__': listener()

改為兩線程程序:

#!/usr/bin/env pythonimport rospyfrom std_msgs.msg import Stringdef callback(data): rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)def listener(): # In ROS, nodes are uniquely named. If two nodes with the same # node are launched, the previous one is kicked off. The # anonymous=True flag means that rospy will choose a unique # name for our 'listener' node so that multiple listeners can # run simultaneously. rospy.init_node('listener', anonymous=True) rospy.Subscriber("chatter", String, callback) #這里加一個(gè)while循環(huán)就行 while(1): #此處添加另外一個(gè)線程的代碼 # spin() simply keeps python from exiting until this node is stopped rospy.spin()if __name__ == '__main__': listener()

rospy.spin()作用是當(dāng)節(jié)點(diǎn)停止時(shí)讓python程序退出,顯然和C++ spin的作用不同。

官方的解釋:The final addition, rospy.spin() simply keeps your node from exiting until the node has been shutdown. Unlike roscpp, rospy.spin() does not affect the subscriber callback functions, as those have their own threads.


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 射阳县| 新野县| 涟源市| 贡觉县| 墨竹工卡县| 都昌县| 子长县| 浪卡子县| 泉州市| 台南县| 资溪县| 高唐县| 香格里拉县| 西贡区| 张家港市| 岑溪市| 德保县| 东兴市| 东至县| 龙海市| 离岛区| 莒南县| 盐池县| 邹城市| 马龙县| 抚顺县| 凤阳县| 当雄县| 教育| 千阳县| 江油市| 九寨沟县| 平山县| 盈江县| 井冈山市| 元江| 长岛县| 顺昌县| 威远县| 田阳县| 化州市|