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 #include <string.h>
41 
42 namespace ros
43 {
44 
45 /* Base Message Type */
46 class Msg
47 {
48 public:
49  virtual int serialize(unsigned char *outbuffer) const = 0;
50  virtual int deserialize(unsigned char *data) = 0;
51  virtual const char * getType() = 0;
52  virtual const char * getMD5() = 0;
53 
65  static int serializeAvrFloat64(unsigned char* outbuffer, const float f)
66  {
67  int32_t val;
68  memcpy(&val, &f, sizeof(val));
69 
70  int16_t exp = ((val >> 23) & 255);
71  uint32_t mantissa = val & 0x7FFFFF;
72 
73  if (exp == 255)
74  {
75  exp = 2047; // Special value for NaN, infinity etc.
76  }
77  else if (exp != 0)
78  {
79  exp += 1023 - 127; // Normal case
80  }
81  else if (!mantissa)
82  {
83  exp = 0; // Zero
84  }
85  else
86  {
87  // Denormalized value in float, will fit as normalized value in double
88  exp += 1023 - 127;
89  mantissa <<= 1;
90  while (!(mantissa & 0x800000))
91  {
92  mantissa <<= 1;
93  exp--;
94  }
95  mantissa &= 0x7FFFFF;
96  }
97 
98  *(outbuffer++) = 0;
99  *(outbuffer++) = 0;
100  *(outbuffer++) = 0;
101  *(outbuffer++) = (mantissa << 5) & 0xff;
102  *(outbuffer++) = (mantissa >> 3) & 0xff;
103  *(outbuffer++) = (mantissa >> 11) & 0xff;
104  *(outbuffer++) = ((exp << 4) & 0xF0) | ((mantissa >> 19) & 0x0F);
105  *(outbuffer++) = (exp >> 4) & 0x7F;
106 
107  // Mark negative bit as necessary.
108  if (f < 0)
109  {
110  *(outbuffer - 1) |= 0x80;
111  }
112 
113  return 8;
114  }
115 
126  static int deserializeAvrFloat64(const unsigned char* inbuffer, float* f)
127  {
128  int16_t exp;
129  uint32_t mantissa;
130 
131  // Skip lowest 24 bits
132  inbuffer += 3;
133 
134  // Copy truncated mantissa.
135  mantissa = ((uint32_t)(*(inbuffer++)) >> 4 & 0x0F);
136  mantissa |= ((uint32_t)(*(inbuffer++)) & 0xff) << 4;
137  mantissa |= ((uint32_t)(*(inbuffer++)) & 0xff) << 12;
138  mantissa |= ((uint32_t)(*inbuffer) & 0x0f) << 20;
139 
140  // Copy exponent.
141  exp = ((uint32_t)(*(inbuffer++)) & 0xf0) >> 4;
142  exp |= ((uint32_t)(*inbuffer) & 0x7f) << 4;
143 
144  if (exp == 2047)
145  {
146  exp = 255; // NaN, infinity etc.
147  }
148  else if (exp - 1023 > 127)
149  {
150  exp = 255;
151  mantissa = 0; // Too large for float, convert to infinity
152  }
153  else if (exp - 1023 >= -126)
154  {
155  exp -= 1023 - 127; // Normal case
156  }
157  else if (exp - 1023 < -150)
158  {
159  exp = 0; // Too small or zero
160  }
161  else
162  {
163  // Have to convert to denormalized representation for float
164  mantissa |= 0x1000000;
165  mantissa >>= ((-126 + 1023) - exp);
166  exp = 0;
167  }
168 
169  // Round off mantissa
170  if (mantissa != 0xFFFFFF)
171  mantissa += 1;
172 
173  mantissa >>= 1;
174 
175  // Put mantissa and exponent into place
176  uint32_t val = mantissa;
177  val |= static_cast<uint32_t>(exp) << 23;
178 
179  // Copy negative sign.
180  val |= (static_cast<uint32_t>(*(inbuffer++)) & 0x80) << 24;
181 
182  memcpy(f, &val, sizeof(val));
183  return 8;
184  }
185 
186  // Copy data from variable into a byte array
187  template<typename A, typename V>
188  static void varToArr(A arr, const V var)
189  {
190  for (size_t i = 0; i < sizeof(V); i++)
191  arr[i] = (var >> (8 * i));
192  }
193 
194  // Copy data from a byte array into variable
195  template<typename V, typename A>
196  static void arrToVar(V& var, const A arr)
197  {
198  var = 0;
199  for (size_t i = 0; i < sizeof(V); i++)
200  var |= (arr[i] << (8 * i));
201  }
202 
203 };
204 
205 } // namespace ros
206 
207 #endif
ros::Msg::deserializeAvrFloat64
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:126
ros
ros::Msg
Definition: msg.h:46
ros::Msg::varToArr
static void varToArr(A arr, const V var)
Definition: msg.h:188
f
f
ros::Msg::getType
virtual const char * getType()=0
ros::Msg::deserialize
virtual int deserialize(unsigned char *data)=0
ros::Msg::serialize
virtual int serialize(unsigned char *outbuffer) const =0
ros::Msg::arrToVar
static void arrToVar(V &var, const A arr)
Definition: msg.h:196
ros::Msg::serializeAvrFloat64
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:65
ros::Msg::getMD5
virtual const char * getMD5()=0


rosserial_client
Author(s): Michael Ferguson, Adam Stambler
autogenerated on Wed Mar 2 2022 00:58:01