msg.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote prducts derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _ROS_MSG_H_
36 #define _ROS_MSG_H_
37 
38 #include <stdint.h>
39 #include <stddef.h>
40 
41 namespace ros
42 {
43 
44 /* Base Message Type */
45 class Msg
46 {
47 public:
48  virtual int serialize(unsigned char *outbuffer) const = 0;
49  virtual int deserialize(unsigned char *data) = 0;
50  virtual const char * getType() = 0;
51  virtual const char * getMD5() = 0;
52 
64  static int serializeAvrFloat64(unsigned char* outbuffer, const float f)
65  {
66  const int32_t* val = (int32_t*) &f;
67  int32_t exp = ((*val >> 23) & 255);
68  if (exp != 0)
69  {
70  exp += 1023 - 127;
71  }
72 
73  int32_t sig = *val;
74  *(outbuffer++) = 0;
75  *(outbuffer++) = 0;
76  *(outbuffer++) = 0;
77  *(outbuffer++) = (sig << 5) & 0xff;
78  *(outbuffer++) = (sig >> 3) & 0xff;
79  *(outbuffer++) = (sig >> 11) & 0xff;
80  *(outbuffer++) = ((exp << 4) & 0xF0) | ((sig >> 19) & 0x0F);
81  *(outbuffer++) = (exp >> 4) & 0x7F;
82 
83  // Mark negative bit as necessary.
84  if (f < 0)
85  {
86  *(outbuffer - 1) |= 0x80;
87  }
88 
89  return 8;
90  }
91 
102  static int deserializeAvrFloat64(const unsigned char* inbuffer, float* f)
103  {
104  uint32_t* val = (uint32_t*)f;
105  inbuffer += 3;
106 
107  // Copy truncated mantissa.
108  *val = ((uint32_t)(*(inbuffer++)) >> 5 & 0x07);
109  *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 3;
110  *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 11;
111  *val |= ((uint32_t)(*inbuffer) & 0x0f) << 19;
112 
113  // Copy truncated exponent.
114  uint32_t exp = ((uint32_t)(*(inbuffer++)) & 0xf0) >> 4;
115  exp |= ((uint32_t)(*inbuffer) & 0x7f) << 4;
116  if (exp != 0)
117  {
118  *val |= ((exp) - 1023 + 127) << 23;
119  }
120 
121  // Copy negative sign.
122  *val |= ((uint32_t)(*(inbuffer++)) & 0x80) << 24;
123 
124  return 8;
125  }
126 
127  // Copy data from variable into a byte array
128  template<typename A, typename V>
129  static void varToArr(A arr, const V var)
130  {
131  for (size_t i = 0; i < sizeof(V); i++)
132  arr[i] = (var >> (8 * i));
133  }
134 
135  // Copy data from a byte array into variable
136  template<typename V, typename A>
137  static void arrToVar(V& var, const A arr)
138  {
139  var = 0;
140  for (size_t i = 0; i < sizeof(V); i++)
141  var |= (arr[i] << (8 * i));
142  }
143 
144 };
145 
146 } // namespace ros
147 
148 #endif
static int serializeAvrFloat64(unsigned char *outbuffer, const float f)
This tricky function handles promoting a 32bit float to a 64bit double, so that AVR can publish messa...
Definition: msg.h:64
static void arrToVar(V &var, const A arr)
Definition: msg.h:137
virtual int deserialize(unsigned char *data)=0
virtual const char * getType()=0
static void varToArr(A arr, const V var)
Definition: msg.h:129
virtual const char * getMD5()=0
virtual int serialize(unsigned char *outbuffer) const =0
Definition: msg.h:45
static int deserializeAvrFloat64(const unsigned char *inbuffer, float *f)
This tricky function handles demoting a 64bit double to a 32bit float, so that AVR can understand mes...
Definition: msg.h:102


rosserial_client
Author(s): Michael Ferguson, Adam Stambler
autogenerated on Mon Jun 10 2019 14:53:19