Having trouble sending a message to ROS
I am pretty new in ROS. I am just trying to publish a message to a node in a linux server with this code:
#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;
}
The compiler gives me these errors:
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
I am on Visual Studio 2015 and there aren't a lot of tutorial from windows to linux, so I am confused on what to do. Many thanks for the help! :D
Edit: Thanks for the clarity.
So I have modified my code in this way:
#include "stdafx.h"
#include "ros.h"
#include <string>
#include <stdio.h>
#include <Windows.h>
#include <std_msgs\String.h>
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");
std_msgs::String sent;
ros::Publisher cmd_vel_pub("try", &sent);
nh.advertise(cmd_vel_pub);
while (1) {
sent.data = "Hello robot";
cmd_vel_pub.publish(&sent);
nh.spinOnce();
Sleep(10);
}
printf("All done!\n");
return 0;
}
But it has not builded yet. Now I have errors related to Windows socket or stuff like that.
1>------ Build started: Project: LeapMotion, Configuration: Debug Win32 ------
1> LeapMotion.cpp
1>LeapMotion.obj : error LNK2019: unresolved external symbol "public: __thiscall WindowsSocket::WindowsSocket(void)" (??0WindowsSocket@@QAE@XZ) referenced in function "public: __thiscall ros::NodeHandle_<class WindowsSocket,25,25,512,512>::NodeHandle_<class WindowsSocket,25,25,512,512>(void)" (??0?$NodeHandle_@VWindowsSocket@@$0BJ@$0BJ@$0CAA@$0CAA@@ros@@QAE@XZ)
1>LeapMotion.obj : error LNK2019: unresolved external symbol "public: void __thiscall WindowsSocket::init(char *)" (?init@WindowsSocket@@QAEXPAD@Z) referenced in function "public: void __thiscall ros::NodeHandle_<class WindowsSocket,25,25,512,512>::initNode(char *)" (?initNode@?$NodeHandle_@VWindowsSocket@@$0BJ@$0BJ@$0CAA@$0CAA@@ros@@QAEXPAD@Z)
1>LeapMotion.obj : error LNK2019: unresolved external symbol "public: int __thiscall WindowsSocket::read(void)" (?read@WindowsSocket@@QAEHXZ) referenced in function "public: virtual int __thiscall ros::NodeHandle_<class WindowsSocket,25,25,512,512>::spinOnce(void)" (?spinOnce@?$NodeHandle_@VWindowsSocket@@$0BJ@$0BJ@$0CAA@$0CAA@@ros@@UAEHXZ)
1>LeapMotion.obj : error LNK2019: unresolved external symbol "public: void __thiscall WindowsSocket::write(unsigned char const *,int)" (?write@WindowsSocket@@QAEXPBEH@Z) referenced in function "public: virtual int __thiscall ros::NodeHandle_<class WindowsSocket,25,25,512,512>::publish(int,class ros::Msg const *)" (?publish@?$NodeHandle_@VWindowsSocket@@$0BJ@$0BJ@$0CAA@$0CAA@@ros@@UAEHHPBVMsg@2@@Z)
1>LeapMotion.obj : error LNK2019: unresolved external symbol "public: unsigned long __thiscall WindowsSocket ...
I admire your courage, but if you're really just starting out, I would not start with using the "deprecated" (not really, but it's just not maintained very well / at all anymore) Windows VC++ port. Do you have a hard requirement to use it, or would other approaches be acceptable?
I need to send the coordinates of hands taken with LeapMotion and head taken with HTC Vive to the linux machine. Actually I don't need to use Visual Studio, it is just to start to understand ROS. I would use Unity and C# instead of C++.
Not an answer as we don't solve your problem (so keeping it as a comment): see if uml-robotics/ROS.NET supports enough of ROS to get you where you want. Alternatively, look into
rosbridge_suite
and use that.Ok I will try... thanks for the help. But I would like to know why the code above doesn't work...
I was mistaken actually: I thought you were using
win_ros
, but it's likely you're actually usingrosserial_windows
. So ignore my earlier comments.For future questions: please always include your OS + version, ROS version, the package you are using and links to any tutorials or API docs. That will avoid a lot of potential confusion and possibly even annoyance.
I am sorry, didn't know exactly how works ROS, that's why.
No problem. We're here to help.