ROS Serial library for Mbed platforms for ROS Indigo Igloo. Check http://wiki.ros.org/rosserial_mbed/ for more information

Dependencies:   BufferedSerial

Dependents:   rosserial_mbed_hello_world_publisher rtos_base_control rosserial_mbed_F64MA ROS-RTOS ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers msg.h Source File

msg.h

00001 /* 
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage, Inc.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  *  * Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *  * Redistributions in binary form must reproduce the above
00014  *    copyright notice, this list of conditions and the following
00015  *    disclaimer in the documentation and/or other materials provided
00016  *    with the distribution.
00017  *  * Neither the name of Willow Garage, Inc. nor the names of its
00018  *    contributors may be used to endorse or promote prducts derived
00019  *    from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 #ifndef _ROS_MSG_H_
00036 #define _ROS_MSG_H_
00037 
00038 #include <stdint.h>
00039 
00040 namespace ros {
00041 
00042 /* Base Message Type */
00043 class Msg
00044 {
00045 public:
00046   virtual int serialize(unsigned char *outbuffer) const = 0;
00047   virtual int deserialize(unsigned char *data) = 0;
00048   virtual const char * getType() = 0;
00049   virtual const char * getMD5() = 0;
00050 
00051   /**
00052    * @brief This tricky function handles promoting a 32bit float to a 64bit
00053    *        double, so that AVR can publish messages containing float64
00054    *        fields, despite AVV having no native support for double.
00055    *
00056    * @param[out] outbuffer pointer for buffer to serialize to.
00057    * @param[in] f value to serialize.
00058    *
00059    * @return number of bytes to advance the buffer pointer.
00060    *
00061    */
00062   static int serializeAvrFloat64(unsigned char* outbuffer, const float f)
00063   {
00064     const int32_t* val = (int32_t*) &f;
00065     int32_t exp = ((*val >> 23) & 255);
00066     if (exp != 0)
00067     {
00068       exp += 1023 - 127;
00069     }
00070 
00071     int32_t sig = *val;
00072     *(outbuffer++) = 0;
00073     *(outbuffer++) = 0;
00074     *(outbuffer++) = 0;
00075     *(outbuffer++) = (sig << 5) & 0xff;
00076     *(outbuffer++) = (sig >> 3) & 0xff;
00077     *(outbuffer++) = (sig >> 11) & 0xff;
00078     *(outbuffer++) = ((exp << 4) & 0xF0) | ((sig >> 19) & 0x0F);
00079     *(outbuffer++) = (exp >> 4) & 0x7F;
00080 
00081     // Mark negative bit as necessary.
00082     if (f < 0)
00083     {
00084       *(outbuffer - 1) |= 0x80;
00085     }
00086 
00087     return 8;
00088   }
00089 
00090   /**
00091    * @brief This tricky function handles demoting a 64bit double to a
00092    *        32bit float, so that AVR can understand messages containing
00093    *        float64 fields, despite AVR having no native support for double.
00094    *
00095    * @param[in] inbuffer pointer for buffer to deserialize from.
00096    * @param[out] f pointer to place the deserialized value in.
00097    *
00098    * @return number of bytes to advance the buffer pointer.
00099    */
00100   static int deserializeAvrFloat64(const unsigned char* inbuffer, float* f)
00101   {
00102     uint32_t* val = (uint32_t*)f;
00103     inbuffer += 3;
00104 
00105     // Copy truncated mantissa.
00106     *val = ((uint32_t)(*(inbuffer++)) >> 5 & 0x07);
00107     *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 3;
00108     *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 11;
00109     *val |= ((uint32_t)(*inbuffer) & 0x0f) << 19;
00110 
00111     // Copy truncated exponent.
00112     uint32_t exp = ((uint32_t)(*(inbuffer++)) & 0xf0)>>4;
00113     exp |= ((uint32_t)(*inbuffer) & 0x7f) << 4;
00114     if (exp != 0)
00115     {
00116       *val |= ((exp) - 1023 + 127) << 23;
00117     }  
00118 
00119     // Copy negative sign.
00120     *val |= ((uint32_t)(*(inbuffer++)) & 0x80) << 24;
00121 
00122     return 8;
00123   }
00124 
00125 };
00126 
00127 }  // namespace ros
00128 
00129 #endif