向 ROS 发送消息时遇到问题

Having trouble sending a message to 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

看看 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 example and these tutorials