rosserial客户端库之rosserial_windows

    rosserial_windows对rosserial_client在wondows系统运行提供了特定的扩展支持,它会生成一个含有头文件和若干cpp文件的包,使用中你需要将这些文件添加到你的visual studio工程来实现Windows和ROS 运行系统之间的通信(通常是基于TCP socket)。

     以下是该包常用使用场景介绍:


1 借助rosserial_windows从Windows发布消息给ROS节点管理器

1.1 简介

    由于Windows系统能够支持很多其它系统(如Ubuntu)不能支持的软件或者应用 ,如果我们期望运行在Ubuntu系统的ROS能够使用这些Windows的特有功能,我们可以借助rosserial_windows来架起一座通信桥梁。将rosserial_windows生成的文件添加到windows的visual studio中可以开发应用实现在windows系统收发ROS消息。一般流程如下:

     1)在ROS运行系统生成生成ros_lib

    2)将ros_lib加入到windows的Visual Studios

    3)在windows端调用ros_lib创建可以连接到ROS master并收发消息的应用

    4)在ROS master端运行rosserial_server

    5)启动3)中生成的Windows 应用


下面提供以上步骤的一个实例验证。


1.2 创建ros_lib

1.2.1 安装rosserial_windows和rosserial server

sudo apt-get install ros-hydro-rosserial-windows
sudo apt-get install ros-hydro-rosserial-server

1.2.2 生成ros_lib

    这一步将生成用于和ROS master通信的代码,这些代码需要添加到windows的visual studio中。

rosrun rosserial_windows make_libraries.py my_library

1.3 添加ros_lib到visual studio工程

 Note:以下说明基于Visual Studio 2013 Express Desktop

1.3.1 按照如下步骤创建win32 console应用

1)打开Visual Studio

2)文件 -> 新建Project 

3)在Installed -> Templates -> Visual C++ -> Win32下找到Win32 Console应用

4)给项目取名,如rosserial_hello_world

5)保留预编译头文件


1.3.2 拷贝ros_lib文件夹到rosserial_hello_world文件夹下,拷贝后文件夹结构如下:

rosserial_hello_world/

  • ipch/
  • ros_lib/
    • ros.h
    • WindowsSocket.cpp

    • ... all of the rosserial generated code, including folders for all of the message packages
  • rosserial_hello_world/
    • ReadMe.txt

    • rosserial_hello_world.cpp
    • rosserial_hello_world.vcxproj
    • rosserial_hello_world.vcxproj.filters
    • stdafx.cpp
    • stdafx.h
    • targetver.h
  • rosserial_hello_world.opensdf
  • rosserial_hello_world.sdf
  • rosserial_hello_world.sln
  • rosserial_hello_world.v12.suo
    将ros_lib文件夹下(不包括子文件夹)所有文件添加到project,如:

  • os.h
  • duration.cpp
  • time.cpp
  • WindowsSocket.h

  • WindowsSocket.cpp

然后将ros_lib文件夹添加到include目录中。

1)右键点击rosserial_hello_world项目,选择最下角的“属性”

2)在C/C++的选项中,点击“添加包含目录”,ros_lib的路径添加到目录中


1.3.3 向ros master发送命令示例代码

    向ros master发布cmd_vel消息,让机器人前进并旋转。

   1 // rosserial_hello_world.cpp : Example of sending command velocities from Windows using rosserial
   2 //
   3 #include "stdafx.h"
   4 #include <string>
   5 #include <stdio.h>
   6 #include "ros.h"
   7 #include <geometry_msgs/Twist.h>
   8 #include <windows.h>
   9 
  10 using std::string;
  11 
  12 int _tmain (int argc, _TCHAR * argv[])
  13 {
  14   ros::NodeHandle nh;
  15   char *ros_master = "1.2.3.4"; /<span style="font-family:Microsoft YaHei;">*changed to ros master IP*/</span>
  16 
  17   printf ("Connecting to server at %s\n", ros_master);
  18   nh.initNode (ros_master);
  19 
  20   printf ("Advertising cmd_vel message\n");
  21   geometry_msgs::Twist twist_msg;
  22   ros::Publisher cmd_vel_pub ("cmd_vel", &twist_msg);
  23   nh.advertise (cmd_vel_pub);
  24 
  25   printf ("Go robot go!\n");
  26   while (1)
  27   {
  28     twist_msg.linear.x = 5.1;
  29     twist_msg.linear.y = 0;
  30     twist_msg.linear.z = 0;
  31     twist_msg.angular.x = 0;
  32     twist_msg.angular.y = 0;
  33     twist_msg.angular.z = -1.8;
  34     cmd_vel_pub.publish (&twist_msg);
  35 
  36     nh.spinOnce ();
  37     Sleep (100);
  38   }
  39 
  40   printf ("All done!\n");
  41   return 0;
  42 }

1.3.4 启动ROS和rosserial_server

    为了让windows端的rosserial_windows能够和ROS master通信,我们需要在ROS master一侧启动一个server来建立连接。示例中我们采用TCP建立连接,理论上也可以用串口建立连接。运行命令如下:

roscore
rosrun rosserial_server socket_node
rostopic echo /cmd_vel


1.3.5 windows端运行应用


如果你在gazebo中有一个机器人模拟器,你可以看到机器人按照你从windows端发来的命令在运动。


2 借助rosserial_windows在windows系统接收消息

2.1 使用回调函数  

    接收消息与发送消息相比稍微复杂一点。需要告知节点句柄收到消息时候应该做什么,为此我们需要使用一个回调函数来完成做什么。

    比如说我们希望接收机器人位姿消息,当收到后将其打印出来。
<span style="font-family:Microsoft YaHei;font-size:14px;"> void estimated_pose_callback (const geometry_msgs::PoseWithCovarianceStamped & pose)
   {
      printf ("Received pose %f, %f, %f\n", pose.pose.pose.position.x,
             pose.pose.pose.position.y, pose.pose.pose.position.z);
   }</span>

   回调函数注册方法如下:

<span style="font-family:Microsoft YaHei;font-size:14px;">ros::Subscriber < geometry_msgs::PoseWithCovarianceStamped > 
     poseSub ("estimated_pose", &estimated_pose_callback);
   nh.subscribe (poseSub);</span>


2.2 接收消息

   

 while (1)
   {
     nh.spinOnce ();
     Sleep (100);
   }

2.3 示例代码

   

#include "stdafx.h"
   2 #include <string>
   3 #include <stdio.h>
   4 #include "ros.h"
   5 #include <geometry_msgs/PoseWithCovarianceStamped.h>
   6 #include <windows.h>
   7 
   8 using std::string;
   9 
  10 void estimated_pose_callback (const geometry_msgs::PoseWithCovarianceStamped & pose)
  11 {
  12   printf ("Received pose %f, %f, %f\n", pose.pose.pose.position.x,
  13           pose.pose.pose.position.y, pose.pose.pose.position.z);
  14 }
  15 
  16 int _tmain (int argc, _TCHAR * argv[])
  17 {
  18   ros::NodeHandle nh;
  19   char *ros_master = "1.2.3.4";
  20 
  21   printf ("Connecting to server at %s\n", ros_master);
  22   nh.initNode (ros_master);
  23 
  24   ros::Subscriber < geometry_msgs::PoseWithCovarianceStamped > 
  25     poseSub ("estimated_pose", &estimated_pose_callback);
  26   nh.subscribe (poseSub);
  27 
  28   printf ("Waiting to receive messages\n");
  29   while (1)
  30   {
  31     nh.spinOnce ();
  32     Sleep (100);
  33   }
  34 
  35   printf ("All done!\n");
  36   return 0;
  37 }


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值