msg.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 #ifndef _ROS_MSG_H_
00019 #define _ROS_MSG_H_
00020 
00021 #include <stdint.h>
00022 
00023 namespace ros {
00024 
00025 /* Base Message Type */
00026 class Msg
00027 {
00028 public:
00029   virtual int serialize(unsigned char *outbuffer) const = 0;
00030   virtual int deserialize(unsigned char *data) = 0;
00031   virtual const char * getType() = 0;
00032   virtual const char * getMD5() = 0;
00033 
00045   static int serializeAvrFloat64(unsigned char* outbuffer, const float f)
00046   {
00047     const int32_t* val = (int32_t*) &f;
00048     int32_t exp = ((*val >> 23) & 255);
00049     if (exp != 0)
00050     {
00051       exp += 1023 - 127;
00052     }
00053 
00054     int32_t sig = *val;
00055     *(outbuffer++) = 0;
00056     *(outbuffer++) = 0;
00057     *(outbuffer++) = 0;
00058     *(outbuffer++) = (sig << 5) & 0xff;
00059     *(outbuffer++) = (sig >> 3) & 0xff;
00060     *(outbuffer++) = (sig >> 11) & 0xff;
00061     *(outbuffer++) = ((exp << 4) & 0xF0) | ((sig >> 19) & 0x0F);
00062     *(outbuffer++) = (exp >> 4) & 0x7F;
00063 
00064     // Mark negative bit as necessary.
00065     if (f < 0)
00066     {
00067       *(outbuffer - 1) |= 0x80;
00068     }
00069 
00070     return 8;
00071   }
00072 
00083   static int deserializeAvrFloat64(const unsigned char* inbuffer, float* f)
00084   {
00085     uint32_t* val = (uint32_t*)f;
00086     inbuffer += 3;
00087 
00088     // Copy truncated mantissa.
00089     *val = ((uint32_t)(*(inbuffer++)) >> 5 & 0x07);
00090     *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 3;
00091     *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 11;
00092     *val |= ((uint32_t)(*inbuffer) & 0x0f) << 19;
00093 
00094     // Copy truncated exponent.
00095     uint32_t exp = ((uint32_t)(*(inbuffer++)) & 0xf0)>>4;
00096     exp |= ((uint32_t)(*inbuffer) & 0x7f) << 4;
00097     if (exp != 0)
00098     {
00099       *val |= ((exp) - 1023 + 127) << 23;
00100     }  
00101 
00102     // Copy negative sign.
00103     *val |= ((uint32_t)(*(inbuffer++)) & 0x80) << 24;
00104 
00105     return 8;
00106   }
00107 
00108 };
00109 
00110 }  // namespace ros
00111 
00112 #endif


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57