registers.h
Go to the documentation of this file.
1 
36 #ifndef UM7_REGISTERS_H_
37 #define UM7_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 <string>
50 #include <stdexcept>
51 
52 #include "um7/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 um7
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, DREG_GYRO_RAW_XY, 3),
163  accel_raw(this, DREG_ACCEL_RAW_XY, 3),
164  mag_raw(this, DREG_MAG_RAW_XY, 3),
165  gyro(this, DREG_GYRO_PROC_X, 3, 1.0 * TO_RADIANS),
166  accel(this, DREG_ACCEL_PROC_X, 3, 9.80665),
167  mag(this, DREG_MAG_PROC_X, 3, 1.0),
168  euler(this, DREG_EULER_PHI_THETA, 3, 0.0109863 * TO_RADIANS),
169  quat(this, DREG_QUAT_AB, 4, 0.0000335693),
170  temperature(this, DREG_TEMPERATURE, 1),
171  communication(this, CREG_COM_SETTINGS, 1),
172  comrate2(this, CREG_COM_RATES2, 1),
173  comrate4(this, CREG_COM_RATES4, 1),
174  comrate5(this, CREG_COM_RATES5, 1),
175  comrate6(this, CREG_COM_RATES6, 1),
176  misc_config(this, CREG_MISC_SETTINGS, 1),
177  status(this, CREG_COM_RATES6, 1),
178  mag_bias(this, CREG_MAG_BIAS_X, 3),
179  cmd_zero_gyros(this, CHR_ZERO_GYROS),
180  cmd_reset_ekf(this, CHR_RESET_EKF),
181  cmd_set_mag_ref(this, CHR_SET_MAG_REFERENCE)
182  {
183  memset(raw_, 0, sizeof(raw_));
184  }
185 
186  // Data
187  const Accessor<int16_t> gyro_raw, accel_raw, euler, mag_raw, quat;
188 
189  const Accessor<float> gyro, accel, mag, temperature;
190 
191  // Configs
192  const Accessor<uint32_t> communication, misc_config, status, comrate2,
193  comrate4, comrate5, comrate6;
194 
196 
197  // Commands
198  const Accessor<uint32_t> cmd_zero_gyros, cmd_reset_ekf, cmd_set_mag_ref;
199 
200  void write_raw(uint8_t register_index, std::string data)
201  {
202  if ((register_index - 1) + (data.length()/4 - 1) >= NUM_REGISTERS)
203  {
204  throw std::range_error("Index and length write beyond boundaries of register array.");
205  }
206  memcpy(&raw_[register_index], data.c_str(), data.length());
207  }
208 
209  private:
210  uint32_t raw_[NUM_REGISTERS];
211 
212  friend class Accessor_;
213 };
214 } // namespace um7
215 
216 #endif // UM7_REGISTERS_H_
d
#define CREG_COM_RATES4
#define DREG_ACCEL_RAW_XY
Accessor_(Registers *registers, uint8_t register_index, uint8_t register_width, uint8_t array_length)
Definition: registers.h:96
Registers * registers_
Definition: registers.h:119
#define CREG_MAG_BIAS_X
#define DREG_GYRO_RAW_XY
#define TO_RADIANS
Definition: registers.h:54
#define DREG_TEMPERATURE
XmlRpcServer s
const Accessor< int16_t > quat
Definition: registers.h:187
#define CHR_SET_MAG_REFERENCE
#define DREG_QUAT_AB
#define DREG_ACCEL_PROC_X
Copied directly from the UM7 version of the UM6_config.h file, available online here: http://sourcefo...
#define CREG_MISC_SETTINGS
Accessor(Registers *registers, uint8_t register_index, uint8_t array_length=0, double scale_factor=1.0)
Definition: registers.h:126
const uint8_t width
Definition: registers.h:111
const Accessor< uint32_t > status
Definition: registers.h:192
#define DREG_MAG_PROC_X
void set_scaled(uint16_t field, double value) const
Definition: registers.h:149
#define CREG_COM_SETTINGS
#define DREG_GYRO_PROC_X
#define CHR_ZERO_GYROS
const double scale_
Definition: registers.h:155
#define NUM_REGISTERS
Definition: registers.h:59
#define DREG_MAG_RAW_XY
const uint16_t length
Definition: registers.h:116
void * raw() const
Definition: registers.cpp:39
const Accessor< uint32_t > cmd_zero_gyros
Definition: registers.h:198
#define CREG_COM_RATES5
Definition: comms.h:46
void write_raw(uint8_t register_index, std::string data)
Definition: registers.h:200
void memcpy_network(void *dest, void *src, size_t count)
Definition: registers.h:65
#define CREG_COM_RATES2
const Accessor< float > mag_bias
Definition: registers.h:195
#define CREG_COM_RATES6
#define CHR_RESET_EKF
double get_scaled(uint16_t field) const
Definition: registers.h:138
#define DREG_EULER_PHI_THETA
const Accessor< float > temperature
Definition: registers.h:189
const uint8_t index
Definition: registers.h:107


um7
Author(s): Mike Purvis , Alex Brown
autogenerated on Tue Feb 11 2020 03:26:50