1. 程式人生 > >ROS---傳送自定義訊息,接收訊息

ROS---傳送自定義訊息,接收訊息

上一篇已經建好了hello包及其內部的檔案,本篇講訊息

接收訊息

hello_node.cpp 作一些修改

#include "iostream"
#include "../include/hello/add.h"
#include "ros/ros.h"
#include "std_msgs/String.h"
using namespace std;

// 接收到訊息後進行的處理
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
    ROS_INFO("I heard: [%s]", msg->data
.c_str()); cout << Add(1, 2) << endl; } int main(int argc, char **argv) { // 初始化,listener是當前節點的名稱 ros::init(argc, argv, "listener"); // 與ROS系統的互動點 ros::NodeHandle n; // topic名為chatter,1000為訊息佇列長度 ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback); // 進入迴圈,無訊息時,不太浪費CPU
ros::spin(); return 0; }

CMakeLists.txt 作一些修改
將一些註釋去掉,然後進行相應修改

cmake_minimum_required(VERSION 2.8.3)
project(hello)

find_package(catkin REQUIRED COMPONENTS roscpp std_msgs genmsg)

generate_messages(
   DEPENDENCIES
   std_msgs
 )
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES hello
# CATKIN_DEPENDS roscpp std_msgs # DEPENDS system_lib ) include_directories( include ${catkin_INCLUDE_DIRS} ) add_executable(${PROJECT_NAME}_node src/hello_node.cpp src/add.cpp) target_link_libraries(${PROJECT_NAME}_node ${catkin_LIBRARIES} )

上一篇是通過命令的方式啟動了node,本篇採用launch檔案,所以要在hello下的src下一個launch檔案
hello.launch

<launch>
    <node pkg="hello" type="hello_node" name="hello_node">
    </node>
</launch>

catkin_make成功後,輸入

# hello 為包名,hello.launchlaunch檔名
roslaunch hello hello.launch

傳送訊息

啟動新的終端,已經知道了topic名是chatter,如果不知道topic的型別,可以先檢視一下型別

rostopic type chatter

會看到顯示

std_msgs/String

正式傳送訊息

# chatter 為topic名
# std_msgs/String 為topic型別
# 'hello' 為訊息內容
rostopic pub /chatter std_msgs/String 'hello'

檢視接收訊息者的反應

新開啟一個終端

cd ~/.ros/log/latest
cat hello*

然後會看到如下輸出:

[ INFO] [1503658482.462387895]: I heard: [hello]
3
[ INFO] [1503658483.461851516]: I heard: [hello]
3
[ INFO] [1503658484.461503076]: I heard: [hello]
3
[ INFO] [1503658485.461699246]: I heard: [hello]
3
[ INFO] [1503658486.461231311]: I heard: [hello]
3
... ...

檢查訊息

列出節點

rosnode list

輸出

/hello_node
/rosout
/rostopic_313_1503658278237

檢查節點是否正常

rosnode ping hello_node

輸出

rosnode: node is [/hello_node]
pinging /hello_node with a timeout of 3.0s
xmlrpc reply from http://yjp-VirtualBox:39751/  time=1.528025ms
xmlrpc reply from http://yjp-VirtualBox:39751/  time=0.714064ms
xmlrpc reply from http://yjp-VirtualBox:39751/  time=0.741005ms
xmlrpc reply from http://yjp-VirtualBox:39751/  time=1.779079ms
ping average: 1.190543ms

列出topic

rostopic list

輸出

/chatter
/rosout
/rosout_agg

檢視topic資訊

rostopic info /chatter

輸出

Type: std_msgs/String

Publishers: 
 * /rostopic_313_1503658278237 (http://yjp-VirtualBox:39059/)

Subscribers: 
 * /hello_node (http://yjp-VirtualBox:39751/)

檢視訊息日誌

cd ~/.ros/log
cat rostopic_313_150*

輸出

[rospy.client][INFO] 2017-08-25 18:51:18,331: init_node, name[/rostopic_313_1503658278237], pid[313]
[xmlrpc][INFO] 2017-08-25 18:51:18,333: XML-RPC server binding to 0.0.0.0:0
[xmlrpc][INFO] 2017-08-25 18:51:18,333: Started XML-RPC server [http://yjp-VirtualBox:39059/]
[rospy.impl.masterslave][INFO] 2017-08-25 18:51:18,333: _ready: http://yjp-VirtualBox:39059/
[rospy.init][INFO] 2017-08-25 18:51:18,334: ROS Slave URI: [http://yjp-VirtualBox:39059/]
[xmlrpc][INFO] 2017-08-25 18:51:18,338: xml rpc node: starting XML-RPC server
[rospy.registration][INFO] 2017-08-25 18:51:18,339: Registering with master node http://localhost:11311
[rospy.init][INFO] 2017-08-25 18:51:18,438: registered with master
[rospy.internal][INFO] 2017-08-25 18:51:18,678: topic[/chatter] adding connection to [/hello_node], count 0

檢視訊息輸出

cd latest
cat rosout.log

輸出

1503658161.685882161  Node Startup
1503658279.462730574 INFO [/home/yjp/catkin_ws/src/hello/src/hello_node.cpp:9(chatterCallback) [topics: /rosout] I heard: [hello]
1503658280.461326972 INFO [/home/yjp/catkin_ws/src/hello/src/hello_node.cpp:9(chatterCallback) [topics: /rosout] I heard: [hello]
1503658281.460512245 INFO [/home/yjp/catkin_ws/src/hello/src/hello_node.cpp:9(chatterCallback) [topics: /rosout] I heard: [hello]
1503658282.461100244 INFO [/home/yjp/catkin_ws/src/hello/src/hello_node.cpp:9(chatterCallback) [topics: /rosout] I heard: [hello]
1503658283.460651781 INFO [/home/yjp/catkin_ws/src/hello/src/hello_node.cpp:9(chatterCallback) [topics: /rosout] I heard: [hello]
... ...

檢視所有輸出,包括標準輸出

cat hello*

輸出

[ INFO] [1503658279.462572018]: I heard: [hello]
3
[ INFO] [1503658280.461288952]: I heard: [hello]
3
[ INFO] [1503658281.460471073]: I heard: [hello]
3
[ INFO] [1503658282.461083733]: I heard: [hello]
3
[ INFO] [1503658283.460599458]: I heard: [hello]
3
[ INFO] [1503658284.460500660]: I heard: [hello]
3
[ INFO] [1503658285.461569927]: I heard: [hello]
... ...