ROS实践(3)-收发节点


一 代码下载

https://www.packtpub.com,注册登陆后,点击support,搜索“Learning ROS for Robotics Programming”,下载源代码。

 

二 代码拷贝

root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src# pwd
/root/dev/rosbook/chapter2_tutorials/src

root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src# cp /home/yangkai04/Documents/Learning\ ROS\ for\ Robotics\ Programming\ 1448OS_Code/1448OS_02_Code/chapter2_tutorials/src/example1_a.cpp .

root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src# cp /home/yangkai04/Documents/Learning\ ROS\ for\ Robotics\ Programming\ 1448OS_Code/1448OS_02_Code/chapter2_tutorials/src/example1_b.cpp .

 

三 编译节点

root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src# rosed chapter2_tutorials CMakeLists.txt

添加下面两行到结尾

rosbuild_add_executable(example1_a src/example1_a.cpp)

rosbuild_add_executable(example1_b src/example1_b.cpp)

 

root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src#rosmake chapter2_tutorials



 

四 启动节点

主节点

root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials# roscore
... logging to /root/.ros/log/b85ec466-a62f-11e6-bcd2-f48e38af57c0/roslaunch-yangkai04-Inspiron-3650-25477.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://yangkai04-Inspiron-3650:44387/
ros_comm version 1.11.20


SUMMARY
========

PARAMETERS
 * /rosdistro: indigo
 * /rosversion: 1.11.20

NODES

auto-starting new master
process[master]: started with pid [25489]
ROS_MASTER_URI=http://yangkai04-Inspiron-3650:11311/

setting /run_id to b85ec466-a62f-11e6-bcd2-f48e38af57c0
process[rosout-1]: started with pid [25502]
started core service [/rosout]

检查ros是否运行
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src# rosnode list
/rosout


 

其他terminal运行下面命令

root@yangkai04-Inspiron-3650:/home/yangkai04#rosrun chapter2_tutorials example1_a


root@yangkai04-Inspiron-3650:/home/yangkai04#rosrun chapter2_tutorials example1_b
[ INFO] [1478663608.828902693]: I heard: [ I am the example1_a node ]
[ INFO] [1478663608.928542361]: I heard: [ I am the example1_a node ]
[ INFO] [1478663609.028693039]: I heard: [ I am the example1_a node ]
[ INFO] [1478663609.128679581]: I heard: [ I am the example1_a node ]
[ INFO] [1478663609.228640477]: I heard: [ I am the example1_a node ]
[ INFO] [1478663609.328685535]: I heard: [ I am the example1_a node ]
[ INFO] [1478663609.428603744]: I heard: [ I am the example1_a node ]



可以发现b一直在拉取a发的消息

 

五 查看执行情况

root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src#rosnode list
/example1_a
/example1_b
/rosout
 
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src# rostopic list
/message
/rosout
/rosout_agg
 
 
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src# rostopic echo /message
data:  I am the example1_a node
---
data:  I am the example1_a node
---
data:  I am the example1_a node
---
data:  I am the example1_a node
---
 
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src# rostopic info /message 
Type: std_msgs/String
Publishers: 
 * /example1_a (http://yangkai04-Inspiron-3650:46673/)
Subscribers: 
 * /example1_b (http://yangkai04-Inspiron-3650:33482/)
root@yangkai04-Inspiron-3650:~/dev/rosbook/chapter2_tutorials/src#


 


 六 源代码

example1_a.cpp

#include "ros/ros.h"

#include "std_msgs/String.h"

#include <sstream>

 

int main(int argc, char **argv)

{

  ros::init(argc, argv, "example1_a");

  ros::NodeHandle n;

  ros::Publisher pub = n.advertise<std_msgs::String>("message", 1000);

  ros::Rate loop_rate(10);

  while (ros::ok())

  {

    std_msgs::String msg;

    std::stringstream ss;

    ss << " I am the example1_a node ";

    msg.data = ss.str();

    //ROS_INFO("%s", msg.data.c_str());

    pub.publish(msg);

    ros::spinOnce();

    loop_rate.sleep();

  }

  return 0;

}


 

example1_b.cpp

#include "ros/ros.h"

#include "std_msgs/String.h"

 

void messageCallback(const std_msgs::String::ConstPtr& msg)

{

  ROS_INFO("I heard: [%s]", msg->data.c_str());

}

 

int main(int argc, char **argv)

{

  ros::init(argc, argv, "example1_b");

  ros::NodeHandle n;

  ros::Subscriber sub = n.subscribe("message", 1000, messageCallback);

  ros::spin();

  return 0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值