ROS学习手记 - 8 编写ROS的Publisher and Subscriber

原创 2015年07月10日 14:52:05


上一节我们完成了 message & srv 文件的创建和加入编译,这次我们要玩简单的Publisher 和 Subscriber

要玩 Publisher 和 Subscriber, 需要具备的条件有哪些呢?先总结一下:

  1.  创建并生成自己的Package,本次是 beginner_tutorials
  2.  创建并生成ROS message & srv

详细版的手记要看我上传到百度文库的文件了:http://wenku.baidu.com/view/f872755a26fff705cc170ade
这里会写个总结~

  1. Writing a Simple Publisher and Subscriber (C++)

    This tutorial covers how to write a publisher and subscriber node in C++.


ROS学习笔记10 - 编写编译和检验Service Node

百度文库:http://wenku.baidu.com/view/e14d5e18dd88d0d232d46a37

Writing a Service Node

Here we'll create the service ("add_two_ints_server")node which will receive two ints and return the sum.

Change directories to your beginner_tutorialspackage you created in your catkin workspace previous tutorials:

cd ~/catkin_ws/src/beginner_tutorials

Please make sure you have followed the directions in the previous tutorialfor creating the service needed in this tutorial,creating the AddTwoInts.srv (be sure to choose the rightversion of build tool you're using at the top of wiki page in the link).

The Code

Create the src/add_two_ints_server.cpp file within the beginner_tutorialspackage and paste the following inside it:

切换行号显示

#include "ros/ros.h"

#include "beginner_tutorials/AddTwoInts.h"

 

bool add(beginner_tutorials::AddTwoInts::Request  &req,

        beginner_tutorials::AddTwoInts::Response &res)

{

  res.sum = req.a + req.b;

  ROS_INFO("request:x=%ld, y=%ld", (long int)req.a, (long int)req.b);

  ROS_INFO("sendingback response: [%ld]", (long int)res.sum);

  return true;

}

 

int main(int argc, char **argv)

{

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

  ros::NodeHandle n;

 

  ros::ServiceServer service = n.advertiseService("add_two_ints",add);

  ROS_INFO("Readyto add two ints.");

  ros::spin();

 

  return 0;

}

The Code Explained

Now, let's break the code down.

切换行号显示

#include "ros/ros.h"

#include "beginner_tutorials/AddTwoInts.h"

 

beginner_tutorials/AddTwoInts.h is the header file generated fromthe srv file that we created earlier. 位置:~/catkin_ws/include/beginner_tutorials

切换行号显示

bool add(beginner_tutorials::AddTwoInts::Request &req,

        beginner_tutorials::AddTwoInts::Response &res)

This function provides the service for adding two ints,it takes in the request and response type defined in the srvfile(AddTwoInts.srv) and returns a boolean.

切换行号显示

{

  res.sum = req.a + req.b;

  ROS_INFO("request:x=%ld, y=%ld", (long int)req.a, (long int)req.b);

  ROS_INFO("sendingback response: [%ld]", (long int)res.sum);

  return true;

}

Here the two ints are added and stored in theresponse. Then some information about the request and response are logged.Finally the service returns true when it is complete.

切换行号显示

ros::ServiceServerservice = n.advertiseService("add_two_ints", add);

Here the service is created and advertised over ROS.
对比一下前一节publisher nodes的创建:

 ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);  //topic name “chatter”, bufferSize:1000

对比一下后面client相关声明语句:

ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");

This creates a client for the add_two_ints service. The ros::ServiceClient object is used to call the service later on.

 

Writing the Client Node

The Code

Create the src/add_two_ints_client.cpp file within the beginner_tutorialspackage and paste the following inside it:

切换行号显示

#include "ros/ros.h"

#include "beginner_tutorials/AddTwoInts.h"

#include <cstdlib>

 

int main(int argc, char **argv)

{

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

  if (argc != 3)

  {

    ROS_INFO("usage:add_two_ints_client X Y");

    return1;

  }

 

  ros::NodeHandle n;

  ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");

  beginner_tutorials::AddTwoInts srv;

  srv.request.a= atoll(argv[1]);

  srv.request.b= atoll(argv[2]);

  if (client.call(srv))

  {

    ROS_INFO("Sum:%ld", (long int)srv.response.sum);

  }

  else

  {

    ROS_ERROR("Failedto call service add_two_ints");

    return1;

  }

 

  return 0;

}

The Code Explained

Now, let's break the code down.

切换行号显示

  ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");

This creates a client for the add_two_intsservice. The ros::ServiceClient object is used to call the service later on.

切换行号显示

  beginner_tutorials::AddTwoInts srv;

  srv.request.a= atoll(argv[1]);

  srv.request.b= atoll(argv[2]);

Here we instantiate列举 an autogenerated service class, and assignvalues into its request member.Aservice class contains two members, requestand response.It alsocontains two class definitions, Requestand Response.

切换行号显示

  if (client.call(srv))

This actually calls the service. Since service calls are blocking, it willreturn once the call is done. If the service call succeeded, call()will return true and the value in srv.response willbe valid. If the call did not succeed, call() willreturn false and the value in srv.response will beinvalid.

Building your nodes

Again edit the beginner_tutorials CMakeLists.txt located at ~/catkin_ws/src/beginner_tutorials/CMakeLists.txt and add the following at the end:

https://raw.github.com/ros/catkin_tutorials/master/create_package_srvclient/catkin_ws/src/beginner_tutorials/CMakeLists.txt

切换行号显示

add_executable(add_two_ints_server src/add_two_ints_server.cpp)

target_link_libraries(add_two_ints_server ${catkin_LIBRARIES})

add_dependencies(add_two_ints_server beginner_tutorials_gencpp)

 

add_executable(add_two_ints_client src/add_two_ints_client.cpp)

target_link_libraries(add_two_ints_client ${catkin_LIBRARIES})

add_dependencies(add_two_ints_client beginner_tutorials_gencpp)

This will create two executables, add_two_ints_server and add_two_ints_client, which by default will go intopackage directory of yourdevel space, located by default at ~/catkin_ws/devel/lib/share/<package name>. You can invoke executablesdirectly or you can use rosrun to invoke调用 them. They are not placed in '<prefix>/bin' because that would pollute thePATH when installing your package to the system. If you wish for your executableto be on the PATH at installation time, you can setup an install target, see:catkin/CMakeLists.txt

For more detailed discription of the CMakeLists.txt file see: catkin/CMakeLists.txt

Now run catkin_make:

# In your catkin workspace

cd ~/catkin_ws

catkin_make

If your build fails for some reason:

Now that you have written a simple service and client, let's examine the simple service and client.

Examining the Simple Service and Client

Running the Service

Let's start by running the service:

$ rosrunbeginner_tutorials add_two_ints_server     (C++)

$ rosrunbeginner_tutorials add_two_ints_server.py  (Python)

You should see something similar to:

Ready to add two ints.

Running the Client

Now let's run the client with the necessaryarguments:

$ rosrunbeginner_tutorials add_two_ints_client1 3     (C++)

$ rosrunbeginner_tutorials add_two_ints_client.py 1 3  (Python)

You should see something similar to:

Requesting 1+3

1 + 3 = 4

Now that you've successfully run your first serverand client, let's learn how torecord and play back data.

Further examples on Service and Client nodes

If you want to investigate further and get ahands-on example, you can get one here. A simple Client and Servicecombination shows the use of custom message types. The Service node is writtenin C++ while the Client is available in C++, Python and LISP.




版权声明:sonictl原创文章,转载注明,否则读者享用不了后续服务,感谢!

相关文章推荐

ROS的学习(十五)验证publisher和subscriber

在前面的两篇博客中我们用C++在ROS中创建了一个发布者和接收者,并使用catkin_make构建了新的节点,下面就需要验证一下,我们写的是否正确。      首先运行roscore roscor...

ROS的学习(九)理解ROS的参数

rosparam命令允许你在ROS的参数服务器上操作和存储数据,参数服务器可以存储整数,浮点数,布尔类型,字典,列表。ROS使用YAML标记语言作为语法,在简单的情况下,YAML看起来是非常自然的:1...

ROS的学习(十二)用C++写一个简单的发布者

节点是一个可执行程序,它连接到了ROS的网络系统中。我们将会创建一个发布者,也就是说话者节点,它将会持续的广播一个信息。      改变目录到之前所建立的那个包下: cd ~/catkin_ws/...

ROS的学习(十)rqt_console和roslaunch

打开一个新的终端在里面输入: sudo apt-get install ros-hydro-rqt ros-hydro-rqt-common-plugins ros-hydro-turtlesim ...

ROS的学习(七)ROS的话题

首先需要打开一个终端在里面运行roscore: roscore      再打开一个终端,在里面运行一个turtlesim_node节点: rosrun turtlesim turtlesi...

ROS的学习(二十)rosserial中的Publisher和Subscriber中的编程步骤

1.在一个话题上发布消息。      可以使用ros::Publisher类创建一个句柄来在一个话题上发布消息,应该首先创建一个publisher将要使用的消息,然后在setup函数中调用adver...

ROS的学习(十四)用C++写一个简单的接收者

打开一个终端,进入到beginner_tutorials包下面: cd ~/catkin_ws/src/beginner_tutorials      编辑文件src/listener.cpp...

让ubuntu在一个窗口下打开多个终端的软件——terminator

Terminator是CrunchBang的默认终端,该终端基于GNOME terminal。Terminator最大的特点就是可以在一个屏幕下同时显示多个终端。Terminator可以在Ubuntu...

ROS的学习(八)ROS的服务

服务是节点之间通信的另一种方式,服务允许节点发起一个请求和接收一个响应。      打开终端在里面输入: roscore      另外打开一个终端在里面输入: rosrun t...

ROS的学习(六)理解ROS的节点(NODE)

经过前面的学习,我们已经知道了如何构建一个ROS的包,这篇博客将介绍ROS中的节点的概念。      在继续之前,请按ctrl+alt+t打开一个终端,在里面输入: sudo apt-get in...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)