前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ROS_Kinetic_26 使用rosserial_windows实现windows与ROS master发送与接收消息

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

作者头像
zhangrelay
发布2019-01-23 11:18:25
1.5K0
发布2019-01-23 11:18:25
举报
文章被收录于专栏:机器人课程与技术

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

目前已经正式支持ROS1和ROS2

在windows系统使用ROS:https://cloud.tencent.com/developer/article/1387067

官方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安装相应功能包,如下:

代码语言:javascript
复制
~$ sudo apt-get install ros-kinetic-rosserial-windows ros-kinetic-rosserial-server

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

代码语言:javascript
复制
~$ 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:

代码语言:txt
复制
    - ... all of the rosserial generated code, including folders for all of the message packages
- rosserial\_hello\_world/ 		
    -  [ReadMe](http://wiki.ros.org/ReadMe).txt
代码语言: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

主要代码:

代码语言:javascript
复制
#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端,启动一个小海龟接收消息:

代码语言:javascript
复制
~$ roscore
代码语言:javascript
复制
~$ rosrun turtlesim turtlesim_node
代码语言:javascript
复制
~$ 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运动。

代码语言:javascript
复制
#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:

代码语言:javascript
复制
~$ roslaunch turtlebot_gazebo turtlebot_world.launch
代码语言:javascript
复制
~$ rostopic echo /cmd_vel_winpc

-End-

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年09月30日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用rosserial_windows实现windows与ROS master发送与接收消息(适用版本hydro,indigo,jade,kinetic)
    • 1 简介
      • 2 生成ros_lib
        • 3 在Visual Studio Project中添加并使用ros_lib发送消息
          • Create a new Win32 Console Application
          • Copy ros_lib into the project
          • Add ros_lib to project
        • 4 在Visual Studio Project中添加并使用ros_lib接收消息
          • 5 在Visual Studio Project中添加并使用ros_lib收发消息
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档