kitt_platform
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros
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 
40 namespace ros {
41 
42 /* Base Message Type */
43 class Msg
44 {
45 public:
46  virtual int serialize(unsigned char *outbuffer) const = 0;
47  virtual int deserialize(unsigned char *data) = 0;
48  virtual const char * getType() = 0;
49  virtual const char * getMD5() = 0;
50 
62  static int serializeAvrFloat64(unsigned char* outbuffer, const float f)
63  {
64  const int32_t* val = (int32_t*) &f;
65  int32_t exp = ((*val >> 23) & 255);
66  if (exp != 0)
67  {
68  exp += 1023 - 127;
69  }
70 
71  int32_t sig = *val;
72  *(outbuffer++) = 0;
73  *(outbuffer++) = 0;
74  *(outbuffer++) = 0;
75  *(outbuffer++) = (sig << 5) & 0xff;
76  *(outbuffer++) = (sig >> 3) & 0xff;
77  *(outbuffer++) = (sig >> 11) & 0xff;
78  *(outbuffer++) = ((exp << 4) & 0xF0) | ((sig >> 19) & 0x0F);
79  *(outbuffer++) = (exp >> 4) & 0x7F;
80 
81  // Mark negative bit as necessary.
82  if (f < 0)
83  {
84  *(outbuffer - 1) |= 0x80;
85  }
86 
87  return 8;
88  }
89 
100  static int deserializeAvrFloat64(const unsigned char* inbuffer, float* f)
101  {
102  uint32_t* val = (uint32_t*)f;
103  inbuffer += 3;
104 
105  // Copy truncated mantissa.
106  *val = ((uint32_t)(*(inbuffer++)) >> 5 & 0x07);
107  *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 3;
108  *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 11;
109  *val |= ((uint32_t)(*inbuffer) & 0x0f) << 19;
110 
111  // Copy truncated exponent.
112  uint32_t exp = ((uint32_t)(*(inbuffer++)) & 0xf0)>>4;
113  exp |= ((uint32_t)(*inbuffer) & 0x7f) << 4;
114  if (exp != 0)
115  {
116  *val |= ((exp) - 1023 + 127) << 23;
117  }
118 
119  // Copy negative sign.
120  *val |= ((uint32_t)(*(inbuffer++)) & 0x80) << 24;
121 
122  return 8;
123  }
124 
125 };
126 
127 } // namespace ros
128 
129 #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:62
virtual int deserialize(unsigned char *data)=0
virtual const char * getType()=0
virtual const char * getMD5()=0
virtual int serialize(unsigned char *outbuffer) const =0
Definition: msg.h:43
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:100