ROS---发送自定义消息,接收消息

来源:互联网 发布:软件企业投标资质 编辑:程序博客网 时间:2024/05/20 03:38

上一篇已经建好了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.0sxmlrpc reply from http://yjp-VirtualBox:39751/  time=1.528025msxmlrpc reply from http://yjp-VirtualBox:39751/  time=0.714064msxmlrpc reply from http://yjp-VirtualBox:39751/  time=0.741005msxmlrpc reply from http://yjp-VirtualBox:39751/  time=1.779079msping average: 1.190543ms

列出topic

rostopic list

输出

/chatter/rosout/rosout_agg

查看topic信息

rostopic info /chatter

输出

Type: std_msgs/StringPublishers:  * /rostopic_313_1503658278237 (http://yjp-VirtualBox:39059/)Subscribers:  * /hello_node (http://yjp-VirtualBox:39751/)

查看消息日志

cd ~/.ros/logcat 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 latestcat rosout.log

输出

1503658161.685882161  Node Startup1503658279.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]... ...
原创粉丝点击