ROS_Kinetic_26 使用rosserial_windows实现windows与ROS master发送与接收消息

使用rosserial_windows实现windows与ROS master发送与接收消息(适用版本hydro,indigo,jade,kinetic

官方wiki地址汇总请参考:http://blog.csdn.net/zhangrelay/article/details/52705019

调试视频链接:http://v.youku.com/v_show/id_XMTc0MzUxMjE3Mg

1 简介

在windows系统下有大量的软硬件支持,有些难以移植到Ubuntu系统供ROS使用,如果使得ROS master和windows pc之间进行高效通信,这就可能需要使用rosserial_windows功能包,它可以实现从windows接收和发送ROS消息。在windows端,需要使用Visual Studios Solution,基本流程如下:

(1)使用ROS功能包生成ros_lib

(2)在Visual Studios Solution中使用ros_lib

(3)编写代码使用ros_lib实现和ROS master连接,并收发消息

(4)在Ubuntu端启动rosserial_server socket

(5)编译并运行windows app

2 生成ros_lib

在ubuntu安装相应功能包,如下:

~$ sudo apt-get install ros-kinetic-rosserial-windows ros-kinetic-rosserial-server

生成ros_lib,这是windows必须文件:

~$ rosrun rosserial_windows make_libraries.py ros_lib

3 在Visual Studio Project中添加并使用ros_lib发送消息

新建一个win32工程如下,具体如下,细节请参考官方教程:

Create a new Win32 Console Application

  1. Open Visual Studio
  2. File -> New Project

  3. Find the Win32 Console Application under Installed -> Templates -> Visual C++ -> Win32

  4. Give your project a name. We'll use rosserial_hello_world
  5. You will probably want to turn off precompile headers. The project should work with them enabled, but precompiled headers have caused problems in the past.

Copy ros_lib into the project

Copy the ros_lib folder into the rosserial_hello_world project folder. The folder structure should look like:

  • 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

Add ros_lib to project

Add all of the files in the ros_lib folder that aren't in subfolders to your project. As of writing this, those are:

Then add the ros_lib folder to your includes path by:

  1. Right-click on the rosserial_hello_world project in the Solution Explorer and go to Properties
  2. Under C/C++, add "../ros_lib" to the Additional Include Directories property

 

 

 

主要代码:

#include "stdafx.h"
#include <string>
#include <stdio.h>
#include "ros.h"
#include <geometry_msgs/Twist.h>
#include <windows.h>

using std::string;

int _tmain(int argc, _TCHAR* argv[])
{
  ros::NodeHandle nh;
  char *ros_master = "192.168.1.102";

  printf ("Connecting to server at %s\n", ros_master);
  nh.initNode (ros_master);

  printf ("Advertising cmd_vel message\n");
  geometry_msgs::Twist twist_msg;
  ros::Publisher cmd_vel_pub ("cmd_vel", &twist_msg);
  nh.advertise (cmd_vel_pub);

  printf ("Go robot go!\n");
  while (1)
  {
    twist_msg.linear.x = 0.4;
    twist_msg.linear.y = 0;
    twist_msg.linear.z = 0;
    twist_msg.angular.x = 0;
    twist_msg.angular.y = 0;
    twist_msg.angular.z = 0.2;
    cmd_vel_pub.publish (&twist_msg);

    nh.spinOnce ();
    Sleep (100);
  }

  printf ("All done!\n");
  return 0;
}

如果报错,请认真核查是否严格按步骤进行,编译成功后,如下:



rospc端,启动一个小海龟接收消息:

~$ roscore
~$ rosrun turtlesim turtlesim_node
~$ rosrun rosserial_server socket_node /cmd_vel:=/turtle1/cmd_vel


4 在Visual Studio Project中添加并使用ros_lib接收消息

过程和发送消息类似,具体如下:








这个例子和发送类似不详细叙述。

5 在Visual Studio Project中添加并使用ros_lib收发消息

这里例子具体说明一下,rospc接收手机发送的速度消息后发送给winpc,winpc再转发给rospc控制小海龟或turblebot运动。

#include "stdafx.h"
#include <string>
#include <stdio.h>
#include "ros.h"
#include <geometry_msgs/Twist.h>
#include <windows.h>

using std::string;
geometry_msgs::Twist twist_msg;

void cmd_vel_angular_callback (const geometry_msgs::Twist & cmd_vel)
{
	  printf ("接收手机cmd_vel %f, %f, %f, %f, %f, %f\n",
	  cmd_vel.linear.x, cmd_vel.linear.y, cmd_vel.linear.z,
	  cmd_vel.angular.x, cmd_vel.angular.y, cmd_vel.angular.z);
	  twist_msg=cmd_vel;
}

int _tmain(int argc, _TCHAR* argv[])
{
  ros::NodeHandle nh;
  char *ros_master = "192.168.1.102";

  printf ("正在连接 %s\n", ros_master);
  nh.initNode (ros_master);

  ros::Subscriber < geometry_msgs::Twist > 
    poseSub ("cmd_vel", &cmd_vel_angular_callback);
  nh.subscribe (poseSub);
  printf ("等待接受消息\n");

  printf ("转发cmd_vel_winpc消息 \n");
  ros::Publisher cmd_vel_pub ("cmd_vel_winpc", &twist_msg);
  nh.advertise (cmd_vel_pub);

  while (1)
  {
	cmd_vel_pub.publish (&twist_msg);
    nh.spinOnce ();
    Sleep (100);
  }

  printf ("All done!\n");
  return 0;
}

ros:

~$ roslaunch turtlebot_gazebo turtlebot_world.launch
~$ rostopic echo /cmd_vel_winpc







-End-


posted on 2016-09-30 14:49  王亮1  阅读(906)  评论(0编辑  收藏  举报

导航