2017-02-24 201 views
0

我在ROS很新。我只是想发布消息到一个节点在Linux服务器与此代码:无法发送消息到ROS

#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

+0

贵的问题得到解答? – cassinaj

回答

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