无法向ROS发送消息

时间:2017-02-24 12:39:38

标签: linux windows visual-studio ros

我是ROS的新手。我只是尝试使用以下代码将消息发布到Linux服务器中的节点:

#include "stdafx.h"
#include "ros.h"
#include <string>
#include <stdio.h>
#include <Windows.h>

using std::string;

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

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

    printf("Advertising cmd_vel message\n");
    string sent = "Hello robot";
    ros::Publisher cmd_vel_pub("try", sent);
    nh.advertise(cmd_vel_pub);

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

编译器给了我这些错误:

 Error  C2664 'ros::Publisher::Publisher(ros::Publisher &&)': cannot convert argument 2 from 'std::string' to 'ros::Msg *'  LeapMotion     c:\users\vive-vr-pc\documents\visual studio    2015\projects\leapmotion\leapmotion\leapmotion.cpp    22  
Error (active)  no instance of constructor "ros::Publisher::Publisher" matches the argument list    LeapMotion  c:\Users\Vive-VR-PC\Documents\Visual Studio 2015\Projects\LeapMotion\LeapMotion\LeapMotion.cpp  22  

我在Visual Studio上并没有很多从windows到linux的教程,所以我很担心该怎么做。非常感谢您的帮助! :d

1 个答案:

答案 0 :(得分:2)

看看Hello World示例。您不能发送未定义为消息的类型,即std :: string不是ros消息类型。你需要的是

#include <std_msgs/String.h>

定义并填写字符串消息

std_msgs::String sent;
ros::Publisher cmd_vel_pub("try", &sent);
nh.advertise(cmd_vel_pub);

ros::Rate r(1); // once a second
sent.data = "Hello robot";
while (n.ok()){
  cmd_vel_pun.publish(sent);
  ros::spinOnce();
  r.sleep();
}

查看此blabbler示例和这些tutorials