Just changed OUTPUT_SIZE and INPUT_SIZE in ros/node_handle.h

Dependencies:   BufferedSerial

Dependents:   WRS2020_mecanum_node

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 #include <stddef.h>
00040 
00041 namespace ros
00042 {
00043 
00044 /* Base Message Type */
00045 class Msg
00046 {
00047 public:
00048   virtual int serialize(unsigned char *outbuffer) const = 0;
00049   virtual int deserialize(unsigned char *data) = 0;
00050   virtual const char * getType() = 0;
00051   virtual const char * getMD5() = 0;
00052 
00053   /**
00054    * @brief This tricky function handles promoting a 32bit float to a 64bit
00055    *        double, so that AVR can publish messages containing float64
00056    *        fields, despite AVV having no native support for double.
00057    *
00058    * @param[out] outbuffer pointer for buffer to serialize to.
00059    * @param[in] f value to serialize.
00060    *
00061    * @return number of bytes to advance the buffer pointer.
00062    *
00063    */
00064   static int serializeAvrFloat64(unsigned char* outbuffer, const float f)
00065   {
00066     const int32_t* val = (int32_t*) &f;
00067     int32_t exp = ((*val >> 23) & 255);
00068     if (exp != 0)
00069     {
00070       exp += 1023 - 127;
00071     }
00072 
00073     int32_t sig = *val;
00074     *(outbuffer++) = 0;
00075     *(outbuffer++) = 0;
00076     *(outbuffer++) = 0;
00077     *(outbuffer++) = (sig << 5) & 0xff;
00078     *(outbuffer++) = (sig >> 3) & 0xff;
00079     *(outbuffer++) = (sig >> 11) & 0xff;
00080     *(outbuffer++) = ((exp << 4) & 0xF0) | ((sig >> 19) & 0x0F);
00081     *(outbuffer++) = (exp >> 4) & 0x7F;
00082 
00083     // Mark negative bit as necessary.
00084     if (f < 0)
00085     {
00086       *(outbuffer - 1) |= 0x80;
00087     }
00088 
00089     return 8;
00090   }
00091 
00092   /**
00093    * @brief This tricky function handles demoting a 64bit double to a
00094    *        32bit float, so that AVR can understand messages containing
00095    *        float64 fields, despite AVR having no native support for double.
00096    *
00097    * @param[in] inbuffer pointer for buffer to deserialize from.
00098    * @param[out] f pointer to place the deserialized value in.
00099    *
00100    * @return number of bytes to advance the buffer pointer.
00101    */
00102   static int deserializeAvrFloat64(const unsigned char* inbuffer, float* f)
00103   {
00104     uint32_t* val = (uint32_t*)f;
00105     inbuffer += 3;
00106 
00107     // Copy truncated mantissa.
00108     *val = ((uint32_t)(*(inbuffer++)) >> 5 & 0x07);
00109     *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 3;
00110     *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 11;
00111     *val |= ((uint32_t)(*inbuffer) & 0x0f) << 19;
00112 
00113     // Copy truncated exponent.
00114     uint32_t exp = ((uint32_t)(*(inbuffer++)) & 0xf0) >> 4;
00115     exp |= ((uint32_t)(*inbuffer) & 0x7f) << 4;
00116     if (exp != 0)
00117     {
00118       *val |= ((exp) - 1023 + 127) << 23;
00119     }
00120 
00121     // Copy negative sign.
00122     *val |= ((uint32_t)(*(inbuffer++)) & 0x80) << 24;
00123 
00124     return 8;
00125   }
00126 
00127   // Copy data from variable into a byte array
00128   template<typename A, typename V>
00129   static void varToArr(A arr, const V var)
00130   {
00131     for (size_t i = 0; i < sizeof(V); i++)
00132       arr[i] = (var >> (8 * i));
00133   }
00134 
00135   // Copy data from a byte array into variable
00136   template<typename V, typename A>
00137   static void arrToVar(V& var, const A arr)
00138   {
00139     var = 0;
00140     for (size_t i = 0; i < sizeof(V); i++)
00141       var |= (arr[i] << (8 * i));
00142   }
00143 
00144 };
00145 
00146 }  // namespace ros
00147 
00148 #endif