payload_base.hpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10  ** Ifdefs
11  *****************************************************************************/
12 
13 #ifndef ROBOT_DATA_HPP_
14 #define ROBOT_DATA_HPP_
15 
16 /*****************************************************************************
17  ** Includes
18  *****************************************************************************/
19 
20 #include <ecl/containers.hpp>
21 #include <stdint.h>
22 
23 /*****************************************************************************
24  ** Namespaces
25  *****************************************************************************/
26 
27 namespace packet_handler
28 {
29 
30 /*****************************************************************************
31  ** Interface
32  *****************************************************************************/
39 {
40 public:
41 
46  bool yes;
47 
52  const bool is_dynamic;
53 
59  const unsigned char length;
60 
61  /*
62  * construct and destruct
63  */
64  payloadBase(const bool is_dynamic_ = false, const unsigned char length_ = 0 )
65  : yes(false)
66  , is_dynamic(is_dynamic_)
67  , length(length_)
68  {};
69  virtual ~payloadBase() {};
70 
71  /*
72  * serialisation
73  */
74  virtual bool serialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
75  virtual bool deserialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
76 
77  // utilities
78  // todo; let's put more useful converters here. Or we may use generic converters
79 protected:
80  // below funciton should be replaced wiht converter
81  //###########################################################
82  //#数据在计算机内存中存储是高位在前,低位在后,int型10存储为:00000000000000000000000000001010
83  //#数据在串口传输过程中是低位在前,高位在后,因而10存储为00001010000000000000000000000000
84  //###########################################################
85  template<typename T>
87  {
88  if (buffer.size() < sizeof(T))
89  return;
90  V = static_cast<unsigned char>(buffer.pop_front());
91 
92  unsigned int size_value(sizeof(T));
93  for (unsigned int i = 1; i < size_value; i++)
94  {
95  V |= ((static_cast<unsigned char>(buffer.pop_front())) << (8 * i));
96  }
97  }
98 
99  template<typename T>
100  void buildBytes(const T & V, ecl::PushAndPop<unsigned char> & buffer)
101  {
102  unsigned int size_value(sizeof(T));
103  for (unsigned int i = 0; i < size_value; i++)
104  {
105  buffer.push_back(static_cast<unsigned char>((V >> (i * 8)) & 0xff));
106  }
107  }
108 };
109 
116 template<>
117 inline void payloadBase::buildVariable<float>(float & V, ecl::PushAndPop<unsigned char> & buffer)
118  {
119  if (buffer.size() < 4)
120  return;
121  unsigned int ui;
122  ui = static_cast<unsigned char>(buffer.pop_front());
123 
124  unsigned int size_value(4);
125  for (unsigned int i = 1; i < size_value; i++)
126  {
127  ui |= ((static_cast<unsigned char>(buffer.pop_front())) << (8 * i));
128  }
129 
130  V = reinterpret_cast<float&>(ui);
131  }
132 
133 template<>
134 inline void payloadBase::buildBytes<float>(const float & V, ecl::PushAndPop<unsigned char> & buffer)
135  {
136  if (buffer.size() < 4)
137  return;
138  unsigned int size_value(4);
139  unsigned int ui(reinterpret_cast<const unsigned int&>(V));
140  for (unsigned int i = 0; i < size_value; i++)
141  {
142  buffer.push_back(static_cast<unsigned char>((ui >> (i * 8)) & 0xff));
143  }
144  }
145 
146 //#define FRAC_MAX 9223372036854775807LL /* 2**63 - 1 */
147 //
148 //struct dbl_packed
149 //{
150 // int exp;
151 // long long frac;
152 //};
153 //
154 //void pack(double x, struct dbl_packed *r)
155 //{
156 // double xf = fabs(frexp(x, &r->exp)) - 0.5;
157 //
158 // if (xf < 0.0)
159 // {
160 // r->frac = 0;
161 // return;
162 // }
163 //
164 // r->frac = 1 + (long long)(xf * 2.0 * (FRAC_MAX - 1));
165 //
166 // if (x < 0.0)
167 // r->frac = -r->frac;
168 //}
169 //
170 //double unpack(const struct dbl_packed *p)
171 //{
172 // double xf, x;
173 //
174 // if (p->frac == 0)
175 // return 0.0;
176 //
177 // xf = ((double)(llabs(p->frac) - 1) / (FRAC_MAX - 1)) / 2.0;
178 //
179 // x = ldexp(xf + 0.5, p->exp);
180 //
181 // if (p->frac < 0)
182 // x = -x;
183 //
184 // return x;
185 //}
186 
187 }
188 ;
189 // namespace packet_handler
190 
191 #endif /* ROBOT_DATA_HPP_ */
unsigned int size() const
const unsigned char length
virtual bool deserialise(ecl::PushAndPop< unsigned char > &byteStream)=0
Provides base class for payloads.
void push_back(const Type &datum)
virtual bool serialise(ecl::PushAndPop< unsigned char > &byteStream)=0
payloadBase(const bool is_dynamic_=false, const unsigned char length_=0)
void buildVariable(T &V, ecl::PushAndPop< unsigned char > &buffer)
void buildBytes(const T &V, ecl::PushAndPop< unsigned char > &buffer)


xbot_driver
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:27:37