registers.h
Go to the documentation of this file.
1 
36 #ifndef UM6_REGISTERS_H
37 #define UM6_REGISTERS_H
38 
39 #if __APPLE__
40 #include <machine/endian.h>
41 #else
42 #include <endian.h>
43 #endif
44 
45 #include <math.h>
46 #include <stdint.h>
47 #include <string.h>
48 
49 #include <stdexcept>
50 #include <string>
51 
52 #include "um6/firmware_registers.h"
53 
54 #define TO_RADIANS (M_PI / 180.0)
55 #define TO_DEGREES (180.0 / M_PI)
56 
57 // This excludes the command registers, which are always sent
58 // and received with no data.
59 #define NUM_REGISTERS (DATA_REG_START_ADDRESS + DATA_ARRAY_SIZE)
60 
61 
62 namespace um6
63 {
64 
65 inline void memcpy_network(void* dest, void* src, size_t count)
66 {
67 #if __BYTE_ORDER == __LITTLE_ENDIAN
68  uint8_t* d = reinterpret_cast<uint8_t*>(dest);
69  uint8_t* s = reinterpret_cast<uint8_t*>(src);
70  for (uint8_t i = 0; i < count; i++)
71  {
72  d[i] = s[count - (i+1)];
73  }
74 #else
75  // Copy bytes without reversing.
76 #warning Big-endian implementation is untested.
77  memcpy(dest, src, count);
78 #endif
79 }
80 
81 class Registers;
82 
93 class Accessor_
94 {
95 public:
96  Accessor_(Registers* registers, uint8_t register_index,
97  uint8_t register_width, uint8_t array_length)
98  : index(register_index), width(register_width),
99  length(array_length), registers_(registers)
100  {}
101 
102  void* raw() const;
103 
107  const uint8_t index;
108 
111  const uint8_t width;
112 
116  const uint16_t length;
117 
118 private:
120 };
121 
122 template<typename RegT>
123 class Accessor : public Accessor_
124 {
125 public:
126  Accessor(Registers* registers, uint8_t register_index, uint8_t array_length = 0, double scale_factor = 1.0)
127  : Accessor_(registers, register_index, sizeof(RegT), array_length), scale_(scale_factor)
128  {}
129 
130  RegT get(uint8_t field) const
131  {
132  RegT* raw_ptr = reinterpret_cast<RegT*>(raw());
133  RegT value;
134  memcpy_network(&value, raw_ptr + field, sizeof(value));
135  return value;
136  }
137 
138  double get_scaled(uint16_t field) const
139  {
140  return get(field) * scale_;
141  }
142 
143  void set(uint8_t field, RegT value) const
144  {
145  RegT* raw_ptr = reinterpret_cast<RegT*>(raw());
146  memcpy_network(raw_ptr + field, &value, sizeof(value));
147  }
148 
149  void set_scaled(uint16_t field, double value) const
150  {
151  set(field, value / scale_);
152  }
153 
154 private:
155  const double scale_;
156 };
157 
159 {
160 public:
162  gyro_raw(this, UM6_GYRO_RAW_XY, 3),
163  accel_raw(this, UM6_ACCEL_RAW_XY, 3),
164  mag_raw(this, UM6_MAG_RAW_XY, 3),
165  gyro(this, UM6_GYRO_PROC_XY, 3, 0.0610352 * TO_RADIANS),
166  accel(this, UM6_ACCEL_PROC_XY, 3, 0.00179639),
167  mag(this, UM6_MAG_PROC_XY, 3, 0.000305176),
168  euler(this, UM6_EULER_PHI_THETA, 3, 0.0109863 * TO_RADIANS),
169  quat(this, UM6_QUAT_AB, 4, 0.0000335693),
170  covariance(this, UM6_ERROR_COV_00, 16),
171  temperature(this, UM6_TEMPERATURE, 1),
172  communication(this, UM6_COMMUNICATION, 1),
173  misc_config(this, UM6_MISC_CONFIG, 1),
174  status(this, UM6_STATUS, 1),
175  mag_ref(this, UM6_MAG_REF_X, 3),
176  accel_ref(this, UM6_ACCEL_REF_X, 3),
177  gyro_bias(this, UM6_GYRO_BIAS_XY, 3),
178  accel_bias(this, UM6_ACCEL_BIAS_XY, 3),
179  mag_bias(this, UM6_MAG_BIAS_XY, 3),
180  cmd_zero_gyros(this, UM6_ZERO_GYROS),
181  cmd_reset_ekf(this, UM6_RESET_EKF),
182  cmd_set_accel_ref(this, UM6_SET_ACCEL_REF),
183  cmd_set_mag_ref(this, UM6_SET_MAG_REF)
184  {
185  memset(raw_, 0, sizeof(raw_));
186  }
187 
188  // Data
189  const Accessor<int16_t> gyro_raw, accel_raw, mag_raw,
190  gyro, accel, mag, euler, quat;
191  const Accessor<float> covariance, temperature;
192 
193  // Configs
194  const Accessor<uint32_t> communication, misc_config, status;
195  const Accessor<float> mag_ref, accel_ref;
196  const Accessor<int16_t> gyro_bias, accel_bias, mag_bias;
197 
198  // Commands
199  const Accessor<uint32_t> cmd_zero_gyros, cmd_reset_ekf,
200  cmd_set_accel_ref, cmd_set_mag_ref;
201 
202  void write_raw(uint8_t register_index, std::string data)
203  {
204  if ((register_index - 1) + (data.length()/4 - 1) >= NUM_REGISTERS)
205  {
206  throw std::range_error("Index and length write beyond boundaries of register array.");
207  }
208  memcpy(&raw_[register_index], data.c_str(), data.length());
209  }
210 
211 private:
212  uint32_t raw_[NUM_REGISTERS];
213 
214  friend class Accessor_;
215 };
216 } // namespace um6
217 
218 #endif // UM6_REGISTERS_H
d
const Accessor< int16_t > quat
Definition: registers.h:189
const uint16_t length
Definition: registers.h:116
#define UM6_GYRO_PROC_XY
#define UM6_GYRO_BIAS_XY
double get_scaled(uint16_t field) const
Definition: registers.h:138
#define TO_RADIANS
Definition: registers.h:54
#define UM6_ACCEL_PROC_XY
const Accessor< float > mag_ref
Definition: registers.h:195
void write_raw(uint8_t register_index, std::string data)
Definition: registers.h:202
XmlRpcServer s
#define UM6_SET_ACCEL_REF
#define UM6_ERROR_COV_00
const uint8_t width
Definition: registers.h:111
Copied directly from the UM6_config.h file, available online here: http://sourceforge.net/p/um6firmware/code/34/tree/trunk/UM6%20Firmware/UM6_config.h#l14.
#define UM6_MAG_RAW_XY
Accessor(Registers *registers, uint8_t register_index, uint8_t array_length=0, double scale_factor=1.0)
Definition: registers.h:126
const uint8_t index
Definition: registers.h:107
void * raw() const
Definition: registers.cpp:39
Definition: comms.h:46
#define UM6_ACCEL_BIAS_XY
#define UM6_MAG_REF_X
void memcpy_network(void *dest, void *src, size_t count)
Definition: registers.h:65
const Accessor< float > temperature
Definition: registers.h:191
#define UM6_ACCEL_REF_X
#define UM6_ACCEL_RAW_XY
#define UM6_TEMPERATURE
#define UM6_QUAT_AB
const Accessor< int16_t > mag_bias
Definition: registers.h:196
#define NUM_REGISTERS
Definition: registers.h:59
#define UM6_ZERO_GYROS
void set_scaled(uint16_t field, double value) const
Definition: registers.h:149
Registers * registers_
Definition: registers.h:119
#define UM6_RESET_EKF
Accessor_(Registers *registers, uint8_t register_index, uint8_t register_width, uint8_t array_length)
Definition: registers.h:96
const Accessor< uint32_t > cmd_zero_gyros
Definition: registers.h:199
#define UM6_STATUS
#define UM6_EULER_PHI_THETA
const double scale_
Definition: registers.h:155
const Accessor< uint32_t > status
Definition: registers.h:194
#define UM6_MAG_BIAS_XY
#define UM6_MAG_PROC_XY
#define UM6_COMMUNICATION
#define UM6_SET_MAG_REF
#define UM6_GYRO_RAW_XY
#define UM6_MISC_CONFIG


um6
Author(s): Mike Purvis
autogenerated on Thu Sep 26 2019 03:18:02