capstone_finish

Dependencies:   BufferedSerial motor_sn7544

Committer:
Jeonghoon
Date:
Tue Nov 26 15:30:27 2019 +0000
Revision:
10:ca4e4062701a
Parent:
3:2a3664dc6634
capstone finish

Who changed what in which revision?

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