How to deserialize objects in ROS CPP?
I have an array of CameraSpacePoint in C# programming language on Windows 10. The 'CameraSpacePoint' have three float variables, and defined inside Microsoft SDK.
I have serialized the array of CameraSpacePoint in C# by following this suggestion.
private int data_width = 512;
private int data_height = 424;
private int data_size = data_width * data_height;
var cameraSpacePoints = new CameraSpacePoint[data_size];
// fill the values in cameraSpacePoints here
var bytes = ObjectToByteArray(cameraSpacePoints);
The converted byte array is then transferred to ROS Indigo on Ubuntu 14.04 LTS. In order to deserialize the byte array, first I designed a similar structure in C++ (Ubuntu) as follow:
struct CameraSpacePoint
{
float X;
float Y;
float Z;
};
Now I am using 'memcpy(cameraSpacePoints, databuffer, datasize_bytes)' to deserialize but it gives segmentation fault. I highly doubt that it is because of mismatching of array size in C++. Below is the code snippet:
#include <ros/ros.h>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
constexpr size_t data_width = 512;
constexpr size_t data_height = 424;
constexpr size_t data_size = data_width * data_height;
constexpr size_t data_size_bytes = data_size * 3 * sizeof(float); // each point have three float variables i.e., X, Y and Z
unsigned char data_buffer[data_size_bytes];
CameraSpacePoint cameraSpacePoints[data_size];
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::iterator endpoint_iterator = resolver.resolve({server_host, server_port});
tcp::socket socket(io_service);
try
{
boost::asio::connect(socket, endpoint_iterator);
}
catch (boost::system::system_error const& e)
{
std::cout << "Failed to connect to server "<< server_host.c_str() <<":"<< server_port.c_str() << e.what() << std::endl;
return -1;
}
while(ros::ok())
{
boost::asio::read(socket, boost::asio::buffer(data_buffer, data_size_bytes));
memcpy(cameraSpacePoints, data_buffer, data_size_bytes);
//CameraSpacePoint midPoint = cameraSpacePoints[data_size/2];
//std::cout << ":{" << midPoint.X << "," << midPoint.Y << "," << midPoint.Z << "}" << std::endl;
std::cout << "Data Received" << std::endl;
boost::asio::write(socket, boost::asio::buffer("OK\n"));
ros::spinOnce();
}
Please note that, if I comment memcpy command, Data Received is being printed on the terminal. But with memcpy, the program stops by throwing segmentation fault error.
I want to know how to deserialize object in ROS CPP?
Asked by ravijoshi on 2017-11-15 07:14:32 UTC
Comments
Can you clarify how this is related to ROS? Everything you show is essentially 'bare' C++,
roscpp
is not involved.Note that
roscpp
is the name of the client library that makes it possible to write ROS programs in C++, it's not some dialect of C++.Asked by gvdhoorn on 2017-11-15 10:18:29 UTC
You might have better luck asking this at stack overflow since it's more of a C++ question than a ROS question.
Asked by Airuno2L on 2017-11-15 13:20:57 UTC