国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 横山县| 嘉义市| 西充县| 昌图县| 自治县| 神木县| 军事| 寿宁县| 安西县| 繁昌县| 义马市| 长寿区| 福泉市| 安顺市| 全南县| 东平县| 上虞市| 革吉县| 德州市| 武山县| 沂水县| 平南县| 繁昌县| 潍坊市| 聂拉木县| 巴里| 铜鼓县| 阿拉善右旗| 铜川市| 西林县| 灵璧县| 上饶县| 南陵县| 凭祥市| 景泰县| 潍坊市| 电白县| 南宫市| 辽宁省| 从化市| 汉阴县|