ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ros msg serialize and deserialize for type double

asked 2019-02-08 05:07:36 -0600

gab27 gravatar image

updated 2019-02-08 05:49:41 -0600

Hi

I have done a custom message for a type uint64_t but how I do this for type double?

Here my code:

class EkfParameters : public ros::Msg
  {
    public:
      typedef uint64_t _residual_type;
      _residual_type residual;

    EkfParameters():
      residual(0)
    {
    }

    virtual int serialize(unsigned char *outbuffer) const
    {
      int offset = 0;
      *(outbuffer + offset + 0) = (this->residual >> (8 * 0)) & 0xFF;
      *(outbuffer + offset + 1) = (this->residual >> (8 * 1)) & 0xFF;
      *(outbuffer + offset + 2) = (this->residual >> (8 * 2)) & 0xFF;
      *(outbuffer + offset + 3) = (this->residual >> (8 * 3)) & 0xFF;
      *(outbuffer + offset + 4) = (this->residual >> (8 * 4)) & 0xFF;
      *(outbuffer + offset + 5) = (this->residual >> (8 * 5)) & 0xFF;
      *(outbuffer + offset + 6) = (this->residual >> (8 * 6)) & 0xFF;
      *(outbuffer + offset + 7) = (this->residual >> (8 * 7)) & 0xFF;
      offset += sizeof(this->residual);
      return offset;
    }

    virtual int deserialize(unsigned char *inbuffer)
    {
      int offset = 0;
      this->residual =  ((uint64_t) (*(inbuffer + offset)));
      this->residual |= ((uint64_t) (*(inbuffer + offset + 1))) << (8 * 1);
      this->residual |= ((uint64_t) (*(inbuffer + offset + 2))) << (8 * 2);
      this->residual |= ((uint64_t) (*(inbuffer + offset + 3))) << (8 * 3);
      this->residual |= ((uint64_t) (*(inbuffer + offset + 4))) << (8 * 4);
      this->residual |= ((uint64_t) (*(inbuffer + offset + 5))) << (8 * 5);
      this->residual |= ((uint64_t) (*(inbuffer + offset + 6))) << (8 * 6);
      this->residual |= ((uint64_t) (*(inbuffer + offset + 7))) << (8 * 7);
      offset += sizeof(this->residual);
     return offset;
    }

    const char * getType(){ return "arduino_vi_sync/EkfParameters"; };
    const char * getMD5(){ return "429a0337b7125479e9bf5f4056e69192"; };

  };

But how I do this for a type double. If I just change the type then I get a error message because << is not for type double. What can I do?

Thanks

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2019-02-08 05:53:00 -0600

Have you read the tutorials about creating and using custom messages? You don't have to write any of these objects yourself, they are automatically generated from .msg files for you.

edit flag offensive delete link more

Comments

if I want a double and a int in one message, then I have to write one by myself?

gab27 gravatar image gab27  ( 2019-02-08 11:02:32 -0600 )edit
1

I think we're not sure what your actualy goal is here.

To avoid an xy-problem, can you describe what you're trying to do for which you think this is necessary?

Also: is this for rosserial or roscpp?

gvdhoorn gravatar image gvdhoorn  ( 2019-02-08 11:04:15 -0600 )edit

for rosserial:

I want to build a message which has different type. For int it works, but not for a double.

gab27 gravatar image gab27  ( 2019-02-08 11:11:40 -0600 )edit
1

Please work through the tutorials regarding custom messages. There is a whole system setup to make it easy for your define custom messages, and you don't do it anything like what you're doing here.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-02-08 11:19:45 -0600 )edit
1

As @PeteBlackerThe3rd writes: please check this tutorial.

There is infrastructure for you that generates all the code.

There should be no need to implement (de)serialisation yourself.

gvdhoorn gravatar image gvdhoorn  ( 2019-02-08 11:28:15 -0600 )edit
0

answered 2019-02-11 03:39:08 -0600

Davide Faconti gravatar image

updated 2019-02-11 04:08:48 -0600

@PeteBlackerThe3rd is 100% right, you should probably create your custom message as explained in the tutorials.

Nevertheless, just for your information, ROS use the Boost Serialization library. May be you want to learn how that work to do your own serializations...

If you are not worry about endianess (and usually you should), serializing a double is as easy as using reinterpret_cast

double val = 3.14; 
uint8_t buffer[sizeof(double)]; 
//serialize 
memcopy( buffer, reinterpret_cast<uint8_t*>(&val), sizeof(double) );

//deserialize 
double val2 =  *( reinterpret_cast<double*>( buffer ));
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-02-08 05:07:36 -0600

Seen: 959 times

Last updated: Feb 11 '19