msg.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef _ROS_MSG_H_
19 #define _ROS_MSG_H_
20 
21 #include <stdint.h>
22 
23 namespace ros {
24 
25 /* Base Message Type */
26 class Msg
27 {
28 public:
29  virtual int serialize(unsigned char *outbuffer) const = 0;
30  virtual int deserialize(unsigned char *data) = 0;
31  virtual const char * getType() = 0;
32  virtual const char * getMD5() = 0;
33 
45  static int serializeAvrFloat64(unsigned char* outbuffer, const float f)
46  {
47  const int32_t* val = (int32_t*) &f;
48  int32_t exp = ((*val >> 23) & 255);
49  if (exp != 0)
50  {
51  exp += 1023 - 127;
52  }
53 
54  int32_t sig = *val;
55  *(outbuffer++) = 0;
56  *(outbuffer++) = 0;
57  *(outbuffer++) = 0;
58  *(outbuffer++) = (sig << 5) & 0xff;
59  *(outbuffer++) = (sig >> 3) & 0xff;
60  *(outbuffer++) = (sig >> 11) & 0xff;
61  *(outbuffer++) = ((exp << 4) & 0xF0) | ((sig >> 19) & 0x0F);
62  *(outbuffer++) = (exp >> 4) & 0x7F;
63 
64  // Mark negative bit as necessary.
65  if (f < 0)
66  {
67  *(outbuffer - 1) |= 0x80;
68  }
69 
70  return 8;
71  }
72 
83  static int deserializeAvrFloat64(const unsigned char* inbuffer, float* f)
84  {
85  uint32_t* val = (uint32_t*)f;
86  inbuffer += 3;
87 
88  // Copy truncated mantissa.
89  *val = ((uint32_t)(*(inbuffer++)) >> 5 & 0x07);
90  *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 3;
91  *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 11;
92  *val |= ((uint32_t)(*inbuffer) & 0x0f) << 19;
93 
94  // Copy truncated exponent.
95  uint32_t exp = ((uint32_t)(*(inbuffer++)) & 0xf0)>>4;
96  exp |= ((uint32_t)(*inbuffer) & 0x7f) << 4;
97  if (exp != 0)
98  {
99  *val |= ((exp) - 1023 + 127) << 23;
100  }
101 
102  // Copy negative sign.
103  *val |= ((uint32_t)(*(inbuffer++)) & 0x80) << 24;
104 
105  return 8;
106  }
107 
108 };
109 
110 } // namespace ros
111 
112 #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:45
data
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:26
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:83


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57