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 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
Take a look at the Hello World example. You cannot send types which are not defined as messages, i.e. std::string is not a ros message type. What you need is
#include <std_msgs/String.h>
Define and fill the string messages
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();
}
Check out this blabbler example and these tutorials.
Related
Consider the following C program:
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>
int main() {
const char* name = "/memmap_ipc_shm";
int shmFd = shm_open(name, O_RDWR | O_CREAT, 0777);
if (shmFd < 0) {
fprintf(stderr,"bad shmfd opening %s: %s\n", name, strerror(errno));
return -1;
}
return 0;
}
When I run it on my GNU/Linux system (Devuan Beowulf, Linux 5.10.0-9, amd64 CPU), I get:
bad shmfd opening /memmap_ipc_shm: Permission denied
Why am I denied permission? I'm pretty sure I followed all the guidelines in the man shm_open page, my requested permissions seem ok - so what's wrong?
This may be happening when you have already created this shared memory object, but perhaps without the appropriate . Try calling shm_unlink(name) to ensure that's the case (or even do so as root if you encounter a permission failure again).
Note that this behavior seems to clash with the man page:
O_EXCL If O_CREAT was also specified, and a shared memory object with the given
name already exists, return an error. The check for the existence of the
object, and its creation if it does not exist, are performed atomically.
I found a particular 100MB bin file (CarveObj_k5_rgbThreshold10_triangleCameraMatches.bin in minimal example), where cereal fails to load throwing exception "Failed to read 368 bytes from input stream! Read 288"
The respective 900MB XML file (CarveObj_k5_rgbThreshold10_triangleCameraMatches.xml in minimal example), built from the same data, loads normally.
The XML file was produced by
// {
// std::ofstream outFile(base + "_triangleCameraMatches.xml");
// cereal::XMLOutputArchive oarchive(outFile);
// oarchive(m_triangleCameraMatches);
// }
and the binary version was produced by
// {
// std::ofstream outFile(base + "_triangleCameraMatches.bin");
// cereal::BinaryOutputArchive oarchive(outFile);
// oarchive(m_triangleCameraMatches);
// }
Minimal example: https://www.dropbox.com/sh/fu9e8km0mwbhxvu/AAAfrbqn_9Tnokj4BVXB8miea?dl=0
Version of Cereal used: 1.3.0
MSVS 2017
Windows 10
Is this a bug? Am I missing something obvious?
Created a bug report in the meanwhile: https://github.com/USCiLab/cereal/issues/607
In this particular instance, the "failed to read from input stream exception" thrown from line 105 of binary.hpp arises because the ios::binary flag is missing from the ifstream constructor call. (This is needed, otherwise ifstream will attempt to interpret some of the file contents as carriage return and linefeed characters. See this question for more information.)
So the few lines of code in your minimal example that read from the .bin file should look like this:
vector<vector<float>> testInBinary;
{
std::ifstream is("CarveObj_k5_rgbThreshold10_triangleCameraMatches.bin", ios::binary);
cereal::BinaryInputArchive iarchive(is);
iarchive(testInBinary);
}
However, even after this is fixed there does also seem to be another problem with the data in that particular .bin file, as when I try to read it I get a different exception thrown, seemingly arising from an incorrectly encoded size value. I don't know if this is an artefact of copying to/from Dropbox though.
There doesn't seem to be a fundamental 100MB limit on Cereal binary files. The following minimal example creates a binary file of around 256MB and reads it back fine:
#include <iostream>
#include <fstream>
#include <vector>
#include <cereal/types/vector.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/archives/xml.hpp>
#include <cereal/archives/binary.hpp>
using namespace std;
int main(int argc, char* argv[])
{
vector<vector<double>> test;
test.resize(32768, vector<double>(1024, -1.2345));
{
std::ofstream outFile("test.bin");
cereal::BinaryOutputArchive oarchive(outFile, ios::binary);
oarchive(test);
}
vector<vector<double>> testInBinary;
{
std::ifstream is("test.bin", ios::binary);
cereal::BinaryInputArchive iarchive(is);
iarchive(testInBinary);
}
return 0;
}
It might be worth noting that in your example code on Dropbox, you're also missing the ios::binary flag on the ofstream constructor when you're writing the .bin file:
/// Produced by:
// {
// std::ofstream outFile(base + "_triangleCameraMatches.bin");
// cereal::BinaryOutputArchive oarchive(outFile);
// oarchive(m_triangleCameraMatches);
// }
It might be worth trying with the flag set. Hope some of this helps.
I am learning how to build C/C++ addons for nodejs.
I could successfully node-gyp configure, node-gyp build and run node index.js on a simple hellow world program. So, my basic setup is working. The below code (copy-paste from official nodejs documentation) is the working version of my C++ code.
//hello.cc
//#include <node.h>
//#include <nan.h>
#include "./node_modules/nan/nan.h"
#include <iostream>
using namespace v8;
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void Method (const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
std::cout << "Executing some stupid func..." << std::endl;
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(addon, init)
} // namespace demo
However, when I use Nan module, and the version of code documented in the nan site, I get compilation errors indicating NanScope is not declared in this scope.
//hello.cc
#include "./node_modules/nan/nan.h"
#include <iostream>
using namespace v8;
NAN_METHOD(Method) {
Nan::NanScope();
NanReturnValue(String::New("world"));
}
void init(Handle<Object> exports) {
exports->Set(NanSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
Part of the error output...
make: Entering directory '/home/rvnath/projects/comviva/node-addons/hello-world/build'
CXX(target) Release/obj.target/addon/hello1.o
../hello1.cc: In function ‘Nan::NAN_METHOD_RETURN_TYPE Method(Nan::NAN_METHOD_ARGS_TYPE)’:
../hello1.cc:7:14: error: ‘NanScope’ was not declared in this scope
NanScope();
^
After a bit of googling around, a few sites pointed out that we should be using Nan::Scope, and that the Nan documentation was out-of-date. I tried the change but it still didn't work. It gives an error saying "Scope is not a member of Nan".
I am unable to find, how to use the Nan version correctly. Any help here will be highly appreciated.
Using visual studio 2015 to compile a test code using catch.hpp unit test. I need to write code to interface with serial port and will need to interface with Widnows API and need to include windows.h
But compiler generates complains with error messages below.
Severity Code Description Project File Line Suppression State
Error C2888 'Catch::Colour::Colour(Catch::Colour::Code)': symbol cannot be defined within namespace 'Catch' NMCR_Testing c:\users\ahajmousa\google drive\cto projects\new mc receiver\software\testing\nmcr_testing\nmcr_testing\catch.hpp 7796
Error C2888 'Catch::Colour::Colour(const Catch::Colour &)': symbol cannot be defined within namespace 'Catch' NMCR_Testing c:\users\ahajmousa\google drive\cto projects\new mc ....
......
code:
#include "stdafx.h"
#include <windows.h>
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("first Test")
{
REQUIRE(1 == 1);
}
TEST_CASE("2nd Test")
{
REQUIRE(1 == 0);
}
Errors will go away if I do not include widnows.h
Is there a way to get catch.hpp to compile without these errors.
Is it possible to side-step _NSGetExecutablePath on Ubuntu Linux in place of a non-Apple specific approach?
I am trying to compile the following code on Ubuntu: https://github.com/Bohdan-Khomtchouk/HeatmapGenerator/blob/master/HeatmapGenerator2_Macintosh_OSX.cxx
As per this prior question that I asked: fatal error: mach-o/dyld.h: No such file or directory, I decided to comment out line 52 and am wondering if there is a general cross-platform (non-Apple specific) way that I can rewrite the code block of line 567 (the _NSGetExecutablePath block) in a manner that is non-Apple specific.
Alen Stojanov's answer to Programmatically retrieving the absolute path of an OS X command-line app and also How do you determine the full path of the currently running executable in go? gave me some ideas on where to start but I want to make certain that I am on the right track here before I go about doing this.
Is there a way to modify _NSGetExecutablePath to be compatible with Ubuntu Linux?
Currently, I am experiencing the following compiler error:
HeatmapGenerator_Macintosh_OSX.cxx:568:13: error: use of undeclared identifier
'_NSGetExecutablePath'
if (_NSGetExecutablePath(path, &size) == 0)
Basic idea how to do it in a way that should be portable across POSIX systems:
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
static char *path;
const char *appPath(void)
{
return path;
}
static void cleanup()
{
free(path);
}
int main(int argc, char **argv)
{
path = realpath(argv[0], 0);
if (!path)
{
perror("realpath");
return 1;
}
atexit(&cleanup);
printf("App path: %s\n", appPath());
return 0;
}
You can define an own module for it, just pass it argv[0] and export the appPath() function from a header.
edit: replaced exported variable by accessor method