How to deserialize objects in ROS CPP?

asked 2017-11-15 06:14:32 -0600

Ravi Joshi gravatar image

updated 2017-11-15 06:18:25 -0600

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, data_buffer, data_size_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?

edit retag flag offensive close merge delete

Comments

1

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++.

gvdhoorn gravatar imagegvdhoorn ( 2017-11-15 09:18:29 -0600 )edit
1

You might have better luck asking this at stack overflow since it's more of a C++ question than a ROS question.

Airuno2L gravatar imageAiruno2L ( 2017-11-15 12:20:57 -0600 )edit