rosserial library for mbed Inspired by nucho's rosserial library This library is still under development

Dependencies:   MODSERIAL mbed

Dependents:   mbed_roshydro_test

Library still under development!

Committer:
akashvibhute
Date:
Sun Feb 15 10:53:43 2015 +0000
Revision:
0:30537dec6e0b
First commit; Library still need to be debugged, compilation issues with new mbed and modserial updates.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akashvibhute 0:30537dec6e0b 1 /*
akashvibhute 0:30537dec6e0b 2 * Software License Agreement (BSD License)
akashvibhute 0:30537dec6e0b 3 *
akashvibhute 0:30537dec6e0b 4 * Copyright (c) 2011, Willow Garage, Inc.
akashvibhute 0:30537dec6e0b 5 * All rights reserved.
akashvibhute 0:30537dec6e0b 6 *
akashvibhute 0:30537dec6e0b 7 * Redistribution and use in source and binary forms, with or without
akashvibhute 0:30537dec6e0b 8 * modification, are permitted provided that the following conditions
akashvibhute 0:30537dec6e0b 9 * are met:
akashvibhute 0:30537dec6e0b 10 *
akashvibhute 0:30537dec6e0b 11 * * Redistributions of source code must retain the above copyright
akashvibhute 0:30537dec6e0b 12 * notice, this list of conditions and the following disclaimer.
akashvibhute 0:30537dec6e0b 13 * * Redistributions in binary form must reproduce the above
akashvibhute 0:30537dec6e0b 14 * copyright notice, this list of conditions and the following
akashvibhute 0:30537dec6e0b 15 * disclaimer in the documentation and/or other materials provided
akashvibhute 0:30537dec6e0b 16 * with the distribution.
akashvibhute 0:30537dec6e0b 17 * * Neither the name of Willow Garage, Inc. nor the names of its
akashvibhute 0:30537dec6e0b 18 * contributors may be used to endorse or promote prducts derived
akashvibhute 0:30537dec6e0b 19 * from this software without specific prior written permission.
akashvibhute 0:30537dec6e0b 20 *
akashvibhute 0:30537dec6e0b 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
akashvibhute 0:30537dec6e0b 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
akashvibhute 0:30537dec6e0b 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
akashvibhute 0:30537dec6e0b 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
akashvibhute 0:30537dec6e0b 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
akashvibhute 0:30537dec6e0b 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
akashvibhute 0:30537dec6e0b 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
akashvibhute 0:30537dec6e0b 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
akashvibhute 0:30537dec6e0b 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
akashvibhute 0:30537dec6e0b 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
akashvibhute 0:30537dec6e0b 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
akashvibhute 0:30537dec6e0b 32 * POSSIBILITY OF SUCH DAMAGE.
akashvibhute 0:30537dec6e0b 33 */
akashvibhute 0:30537dec6e0b 34
akashvibhute 0:30537dec6e0b 35 #ifndef _ROS_MSG_H_
akashvibhute 0:30537dec6e0b 36 #define _ROS_MSG_H_
akashvibhute 0:30537dec6e0b 37
akashvibhute 0:30537dec6e0b 38 #include <stdint.h>
akashvibhute 0:30537dec6e0b 39
akashvibhute 0:30537dec6e0b 40 namespace ros {
akashvibhute 0:30537dec6e0b 41
akashvibhute 0:30537dec6e0b 42 /* Base Message Type */
akashvibhute 0:30537dec6e0b 43 class Msg
akashvibhute 0:30537dec6e0b 44 {
akashvibhute 0:30537dec6e0b 45 public:
akashvibhute 0:30537dec6e0b 46 virtual int serialize(unsigned char *outbuffer) const = 0;
akashvibhute 0:30537dec6e0b 47 virtual int deserialize(unsigned char *data) = 0;
akashvibhute 0:30537dec6e0b 48 virtual const char * getType() = 0;
akashvibhute 0:30537dec6e0b 49 virtual const char * getMD5() = 0;
akashvibhute 0:30537dec6e0b 50
akashvibhute 0:30537dec6e0b 51 /**
akashvibhute 0:30537dec6e0b 52 * @brief This tricky function handles promoting a 32bit float to a 64bit
akashvibhute 0:30537dec6e0b 53 * double, so that AVR can publish messages containing float64
akashvibhute 0:30537dec6e0b 54 * fields, despite AVV having no native support for double.
akashvibhute 0:30537dec6e0b 55 *
akashvibhute 0:30537dec6e0b 56 * @param[out] outbuffer pointer for buffer to serialize to.
akashvibhute 0:30537dec6e0b 57 * @param[in] f value to serialize.
akashvibhute 0:30537dec6e0b 58 *
akashvibhute 0:30537dec6e0b 59 * @return number of bytes to advance the buffer pointer.
akashvibhute 0:30537dec6e0b 60 *
akashvibhute 0:30537dec6e0b 61 */
akashvibhute 0:30537dec6e0b 62 static int serializeAvrFloat64(unsigned char* outbuffer, const float f)
akashvibhute 0:30537dec6e0b 63 {
akashvibhute 0:30537dec6e0b 64 const int32_t* val = (int32_t*) &f;
akashvibhute 0:30537dec6e0b 65 int32_t exp = ((*val >> 23) & 255);
akashvibhute 0:30537dec6e0b 66 if (exp != 0)
akashvibhute 0:30537dec6e0b 67 {
akashvibhute 0:30537dec6e0b 68 exp += 1023 - 127;
akashvibhute 0:30537dec6e0b 69 }
akashvibhute 0:30537dec6e0b 70
akashvibhute 0:30537dec6e0b 71 int32_t sig = *val;
akashvibhute 0:30537dec6e0b 72 *(outbuffer++) = 0;
akashvibhute 0:30537dec6e0b 73 *(outbuffer++) = 0;
akashvibhute 0:30537dec6e0b 74 *(outbuffer++) = 0;
akashvibhute 0:30537dec6e0b 75 *(outbuffer++) = (sig << 5) & 0xff;
akashvibhute 0:30537dec6e0b 76 *(outbuffer++) = (sig >> 3) & 0xff;
akashvibhute 0:30537dec6e0b 77 *(outbuffer++) = (sig >> 11) & 0xff;
akashvibhute 0:30537dec6e0b 78 *(outbuffer++) = ((exp << 4) & 0xF0) | ((sig >> 19) & 0x0F);
akashvibhute 0:30537dec6e0b 79 *(outbuffer++) = (exp >> 4) & 0x7F;
akashvibhute 0:30537dec6e0b 80
akashvibhute 0:30537dec6e0b 81 // Mark negative bit as necessary.
akashvibhute 0:30537dec6e0b 82 if (f < 0)
akashvibhute 0:30537dec6e0b 83 {
akashvibhute 0:30537dec6e0b 84 *(outbuffer - 1) |= 0x80;
akashvibhute 0:30537dec6e0b 85 }
akashvibhute 0:30537dec6e0b 86
akashvibhute 0:30537dec6e0b 87 return 8;
akashvibhute 0:30537dec6e0b 88 }
akashvibhute 0:30537dec6e0b 89
akashvibhute 0:30537dec6e0b 90 /**
akashvibhute 0:30537dec6e0b 91 * @brief This tricky function handles demoting a 64bit double to a
akashvibhute 0:30537dec6e0b 92 * 32bit float, so that AVR can understand messages containing
akashvibhute 0:30537dec6e0b 93 * float64 fields, despite AVR having no native support for double.
akashvibhute 0:30537dec6e0b 94 *
akashvibhute 0:30537dec6e0b 95 * @param[in] inbuffer pointer for buffer to deserialize from.
akashvibhute 0:30537dec6e0b 96 * @param[out] f pointer to place the deserialized value in.
akashvibhute 0:30537dec6e0b 97 *
akashvibhute 0:30537dec6e0b 98 * @return number of bytes to advance the buffer pointer.
akashvibhute 0:30537dec6e0b 99 */
akashvibhute 0:30537dec6e0b 100 static int deserializeAvrFloat64(const unsigned char* inbuffer, float* f)
akashvibhute 0:30537dec6e0b 101 {
akashvibhute 0:30537dec6e0b 102 uint32_t* val = (uint32_t*)f;
akashvibhute 0:30537dec6e0b 103 inbuffer += 3;
akashvibhute 0:30537dec6e0b 104
akashvibhute 0:30537dec6e0b 105 // Copy truncated mantissa.
akashvibhute 0:30537dec6e0b 106 *val = ((uint32_t)(*(inbuffer++)) >> 5 & 0x07);
akashvibhute 0:30537dec6e0b 107 *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 3;
akashvibhute 0:30537dec6e0b 108 *val |= ((uint32_t)(*(inbuffer++)) & 0xff) << 11;
akashvibhute 0:30537dec6e0b 109 *val |= ((uint32_t)(*inbuffer) & 0x0f) << 19;
akashvibhute 0:30537dec6e0b 110
akashvibhute 0:30537dec6e0b 111 // Copy truncated exponent.
akashvibhute 0:30537dec6e0b 112 uint32_t exp = ((uint32_t)(*(inbuffer++)) & 0xf0)>>4;
akashvibhute 0:30537dec6e0b 113 exp |= ((uint32_t)(*inbuffer) & 0x7f) << 4;
akashvibhute 0:30537dec6e0b 114 if (exp != 0)
akashvibhute 0:30537dec6e0b 115 {
akashvibhute 0:30537dec6e0b 116 *val |= ((exp) - 1023 + 127) << 23;
akashvibhute 0:30537dec6e0b 117 }
akashvibhute 0:30537dec6e0b 118
akashvibhute 0:30537dec6e0b 119 // Copy negative sign.
akashvibhute 0:30537dec6e0b 120 *val |= ((uint32_t)(*(inbuffer++)) & 0x80) << 24;
akashvibhute 0:30537dec6e0b 121
akashvibhute 0:30537dec6e0b 122 return 8;
akashvibhute 0:30537dec6e0b 123 }
akashvibhute 0:30537dec6e0b 124
akashvibhute 0:30537dec6e0b 125 };
akashvibhute 0:30537dec6e0b 126
akashvibhute 0:30537dec6e0b 127 } // namespace ros
akashvibhute 0:30537dec6e0b 128
akashvibhute 0:30537dec6e0b 129 #endif
akashvibhute 0:30537dec6e0b 130