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