NASA Astrobee Robot Software  Astrobee Version:
Flight software for the Astrobee robots operating inside the International Space Station.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G362P.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef EPSON_IMU_G362P_H_
20 #define EPSON_IMU_G362P_H_
21 
22 #define EPSON_POWER_ON_DELAY 800000 // usec
23 
24 #define STALL_NORMAL_MODE 20 // usec
25 #define STALL_BURST_MODE_BEFORE_DATA 45 // usec
26 #define STALL_BURST_MODE_BETWEEN_DATA 5 // usec
27 
28 // Window 0
29 #define REG_BURST_H 0x00
30 #define REG_BURST_L 0x01
31 #define REG_MODE_CTRL_H 0x02
32 #define REG_MODE_CTRL_L 0x03
33 #define REG_DIAG_STAT_H 0x04
34 #define REG_DIAG_STAT_L 0x05
35 #define REG_FLAG_H 0x06
36 #define REG_FLAG_L 0x07
37 #define REG_GPIO_H 0x08
38 #define REG_GPIO_L 0x09
39 #define REG_COUNT 0x0A
40 #define REG_TEMP_HIGH 0x0E
41 #define REG_TEMP_LOW 0x10
42 #define REG_XGYRO_HIGH 0x12
43 #define REG_XGYRO_LOW 0x14
44 #define REG_YGYRO_HIGH 0x16
45 #define REG_YGYRO_LOW 0x18
46 #define REG_ZGYRO_HIGH 0x1A
47 #define REG_ZGYRO_LOW 0x1C
48 #define REG_XACCL_HIGH 0x1E
49 #define REG_XACCL_LOW 0x20
50 #define REG_YACCL_HIGH 0x22
51 #define REG_YACCL_LOW 0x24
52 #define REG_ZACCL_HIGH 0x26
53 #define REG_ZACCL_LOW 0x28
54 
55 // Window 1
56 #define REG_SIG_CTRL_H 0x00
57 #define REG_SIG_CTRL_L 0x01
58 #define REG_MSC_CTRL_H 0x02
59 #define REG_MSC_CTRL_L 0x03
60 #define REG_SMPL_CTRL_H 0x04
61 #define REG_SMPL_CTRL_L 0x05
62 #define REG_FILTER_CTRL_H 0x06
63 #define REG_FILTER_CTRL_L 0x07
64 #define REG_UART_CTRL_H 0x08
65 #define REG_UART_CTRL_L 0x09
66 #define REG_GLOB_CMD_H 0x0A
67 #define REG_GLOB_CMD_L 0x0B
68 #define REG_BURST_CTRL1_H 0x0C
69 #define REG_BURST_CTRL1_L 0x0D
70 #define REG_BURST_CTRL2_H 0x0E
71 #define REG_BURST_CTRL2_L 0x0F
72 #define REG_WIN_CTRL_H 0x7E
73 #define REG_WIN_CTRL_L 0x7F
74 
75 // Scale Factors
76 #define SF_TEMP (0.0042725)
77 #define SF_GYRO (0.005)
78 #define SF_ACCL (0.125)
79 
80 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
81 
82 #define CHECK_REG_VALUE 1
83 
84 #include <epson_imu/GPIO.h>
85 
86 #include <time.h>
87 
88 #include <iostream>
89 
90 namespace epson_imu {
91 
92 enum Mode {
93  SAMPLING = 0x00,
95 };
96 
97 enum Window {
98  WIN0 = 0x00,
99  WIN1 = 0x01
100 };
101 
103  B16 = 0,
104  B32 = 1
105 };
106 
107 enum Filter {
127 };
128 
130  SPS2000 = 0x01,
131  SPS1000 = 0x02,
132  SPS500 = 0x03,
133  SPS250 = 0x04,
134  SPS125 = 0x05,
135  SPS62_5 = 0x06,
136  SPS31_25 = 0x07
137 };
138 
139 typedef struct _Data {
140  uint16_t flag_;
141  double temp_; // degree celcius
142  double gyro_[3]; // deg/s
143  double accl_[3]; // mG
144  uint16_t gpio_;
145  uint16_t count_;
146  uint16_t chksm_;
147 } Data;
148 
149 typedef struct _Register {
150  // Window ID
152  // Address
153  uint8_t address;
154  // Name
155  const char *name;
156  // Readable?
157  bool readable;
158  // Writable?
159  bool writable;
160 } Register;
161 
162 constexpr Register kRegisters[] = {
163  // Window ID, Address, Name, Readable, Writable
164  { Window::WIN0, REG_BURST_H, "BURST_H", false, true },
165  { Window::WIN0, REG_BURST_L, "BURST_H", false, false },
166  { Window::WIN0, REG_MODE_CTRL_H, "MODE_CTRL_H", true, true },
167  { Window::WIN0, REG_MODE_CTRL_L, "MODE_CTRL_L", true, true },
168  { Window::WIN0, REG_DIAG_STAT_H, "DIAG_STAT_H", true, false },
169  { Window::WIN0, REG_DIAG_STAT_L, "DIAG_STAT_L", true, false },
170  { Window::WIN0, REG_FLAG_H, "FLAG_H", true, false },
171  { Window::WIN0, REG_FLAG_L, "FLAG_L", true, false },
172  { Window::WIN0, REG_GPIO_H, "GPIO_H", true, true },
173  { Window::WIN0, REG_GPIO_L, "GPIO_L", true, true },
174  { Window::WIN0, REG_COUNT, "COUNT", true, false },
175  { Window::WIN0, REG_TEMP_HIGH, "TEMP_HIGH", true, false },
176  { Window::WIN0, REG_TEMP_LOW, "TEMP_LOW", true, false },
177  { Window::WIN0, REG_XGYRO_HIGH, "XGYRO_HIGH", true, false },
178  { Window::WIN0, REG_XGYRO_LOW, "XGYRO_LOW", true, false },
179  { Window::WIN0, REG_YGYRO_HIGH, "YGYRO_HIGH", true, false },
180  { Window::WIN0, REG_YGYRO_LOW, "YGYRO_LOW", true, false },
181  { Window::WIN0, REG_ZGYRO_HIGH, "ZGYRO_HIGH", true, false },
182  { Window::WIN0, REG_ZGYRO_LOW, "ZGYRO_LOW", true, false },
183  { Window::WIN0, REG_XACCL_HIGH, "XACCL_HIGH", true, false },
184  { Window::WIN0, REG_XACCL_LOW, "XACCL_LOW", true, false },
185  { Window::WIN0, REG_YACCL_HIGH, "YACCL_HIGH", true, false },
186  { Window::WIN0, REG_YACCL_LOW, "YACCL_LOW", true, false },
187  { Window::WIN0, REG_ZACCL_HIGH, "ZACCL_HIGH", true, false },
188  { Window::WIN0, REG_ZACCL_LOW, "ZACCL_LOW", true, false },
189  { Window::WIN0, REG_WIN_CTRL_H, "WIN_CTRL_H", true, true },
190  { Window::WIN0, REG_WIN_CTRL_L, "WIN_CTRL_L", false, false },
191  { Window::WIN1, REG_SIG_CTRL_H, "SIG_CTRL_H", true, true },
192  { Window::WIN1, REG_SIG_CTRL_L, "SIG_CTRL_L", true, true },
193  { Window::WIN1, REG_MSC_CTRL_H, "MSC_CTRL_H", true, true },
194  { Window::WIN1, REG_MSC_CTRL_L, "MSC_CTRL_L", true, true },
195  { Window::WIN1, REG_SMPL_CTRL_H, "SMPL_CTRL_H", true, false},
196  { Window::WIN1, REG_SMPL_CTRL_L, "SMPL_CTRL_L", true, true},
197  { Window::WIN1, REG_FILTER_CTRL_H, "FILTER_CTRL_H", true, true },
198  { Window::WIN1, REG_FILTER_CTRL_L, "FILTER_CTRL_L", true, false },
199  { Window::WIN1, REG_UART_CTRL_H, "UART_CTRL_H", true, true },
200  { Window::WIN1, REG_UART_CTRL_L, "UART_CTRL_L", true, true },
201  { Window::WIN1, REG_GLOB_CMD_H, "GLOB_CMD_H", true, true },
202  { Window::WIN1, REG_GLOB_CMD_L, "GLOB_CMD_H", true, false },
203  { Window::WIN1, REG_BURST_CTRL1_H, "BURST_CTRL1_H", true, true },
204  { Window::WIN1, REG_BURST_CTRL1_L, "BURST_CTRL1_L", true, true },
205  { Window::WIN1, REG_BURST_CTRL2_H, "BURST_CTRL2_H", true, false },
206  { Window::WIN1, REG_BURST_CTRL2_L, "BURST_CTRL2_L", true, true },
207  { Window::WIN1, REG_WIN_CTRL_H, "WIN_CTRL_H", true, true },
208  { Window::WIN1, REG_WIN_CTRL_L, "WIN_CTRL_L", false, false },
209 };
210 
211 class G362P {
212  private:
213  int fd_spi_dev_;
214  uint32_t spi_speed_hz_;
215 
216  gpio::GPIO *gpio_data_ready_;
217  gpio::GPIO *gpio_reset_;
218 
219  // Current operation mode.
220  Mode mode_;
221  SamplingRate sampling_rate_;
222 
223  // DRDY pin timeout
224  struct timespec timeout_data_ready_;
225 
226  // Enable/disable outputs
227  bool flag_enabled_, temp_enabled_, gyro_enabled_;
228  bool accl_enabled_, gpio_enabled_, count_enabled_, chksm_enabled_;
229 
230  // Data bit-width
231  OutputBits temp_bits_, gyro_bits_, accl_bits_;
232 
233  public:
234  // Create a G362P instance in SPI mode.
235  G362P(int fd_spi_dev, uint32_t spi_speed_hz, gpio::GPIO *gpio_data_ready, gpio::GPIO *gpio_reset);
236  ~G362P(void);
237 
238  // Indicates whether the IMU is currently ready.
239  bool IsReady(void);
240  // Check if the IMU has any hardware error.
241  bool HasHardError(void);
242  void HardReset(void);
243  void SoftReset(void);
244 
245  // Self test. Use this function to check whether the outputs of the sensor
246  // are within the pre-defined range and operating properly.
247  // Use this function while the sensor is not moving.
248  // The tests are done by the sensor itself.
249  // Returns true if the sensor passes the tests. False otherwise.
250  bool SelfTest(void);
251 
252  // Initializes the IMU by executing the power-on sequence.
253  bool PowerOn(void);
254  void SetWindow(Window window);
255  bool SetFilter(Filter filter);
256  bool SetSamplingRate(SamplingRate sampling_rate);
257  bool StartSampling(void);
258  bool StopSampling(void);
259  // Enable/disable UART.
260  void EnableUART(bool enable);
261  bool EnableOutput(bool flag, bool temp, bool gyro, bool accl, bool gpio, bool count, bool chksm);
262  bool SetOutputBits(OutputBits temp, OutputBits gyro, OutputBits accl);
263  // Write a 8-bit data into a register.
264  void WriteRegister(Window window, uint8_t address, uint8_t value);
265  // Read a 16-bit data from a register.
266  uint16_t ReadRegister(Window window, uint8_t address);
267  void DumpRegisters(void);
268  bool SetMode(Mode mode);
269  // Returns the current operation mode.
270  Mode GetMode(void);
271  // Read data uing the burst mode.
272  bool ReadData(Data *data);
273  // Burst read
274  void PrintData(const Data& data);
275 
276  uint16_t TransferWord(uint16_t word, uint16_t delay_usecs);
277 
278  private:
279  uint16_t MakeWord(uint8_t high, uint8_t low);
280  void BurstRead(Data *data);
281 };
282 
283 } // namespace epson_imu
284 
285 #endif // EPSON_IMU_G362P_H_
mode
uint8_t mode
Definition: signal_lights.h:74
REG_YGYRO_LOW
#define REG_YGYRO_LOW
Definition: G362P.h:45
REG_BURST_L
#define REG_BURST_L
Definition: G362P.h:30
REG_MSC_CTRL_H
#define REG_MSC_CTRL_H
Definition: G362P.h:58
epson_imu::_Register::window
Window window
Definition: G362P.h:151
REG_GPIO_H
#define REG_GPIO_H
Definition: G362P.h:37
epson_imu::G362P::StartSampling
bool StartSampling(void)
epson_imu::_Data::temp_
double temp_
Definition: G362P.h:141
REG_XGYRO_HIGH
#define REG_XGYRO_HIGH
Definition: G362P.h:42
epson_imu::MOV_AVG_TAP_32
@ MOV_AVG_TAP_32
Definition: G362P.h:112
REG_XACCL_LOW
#define REG_XACCL_LOW
Definition: G362P.h:49
epson_imu
Definition: epson_imu_nodelet.h:37
epson_imu::_Register::readable
bool readable
Definition: G362P.h:157
epson_imu::G362P::ReadRegister
uint16_t ReadRegister(Window window, uint8_t address)
Definition: G362P.cc:127
epson_imu::G362P::PowerOn
bool PowerOn(void)
Definition: G362P.cc:81
REG_BURST_CTRL2_L
#define REG_BURST_CTRL2_L
Definition: G362P.h:71
epson_imu::Data
struct epson_imu::_Data Data
epson_imu::G362P::HasHardError
bool HasHardError(void)
Definition: G362P.cc:64
epson_imu::SPS62_5
@ SPS62_5
Definition: G362P.h:135
epson_imu::FIR_TAP_128_FC_100
@ FIR_TAP_128_FC_100
Definition: G362P.h:124
REG_WIN_CTRL_L
#define REG_WIN_CTRL_L
Definition: G362P.h:73
epson_imu::FIR_TAP_64_FC_200
@ FIR_TAP_64_FC_200
Definition: G362P.h:121
REG_MODE_CTRL_L
#define REG_MODE_CTRL_L
Definition: G362P.h:32
REG_FLAG_L
#define REG_FLAG_L
Definition: G362P.h:36
epson_imu::_Register::writable
bool writable
Definition: G362P.h:159
epson_imu::G362P::SetFilter
bool SetFilter(Filter filter)
Definition: G362P.cc:205
REG_YGYRO_HIGH
#define REG_YGYRO_HIGH
Definition: G362P.h:44
epson_imu::FIR_TAP_128_FC_200
@ FIR_TAP_128_FC_200
Definition: G362P.h:125
epson_imu::FIR_TAP_32_FC_50
@ FIR_TAP_32_FC_50
Definition: G362P.h:115
epson_imu::FIR_TAP_32_FC_200
@ FIR_TAP_32_FC_200
Definition: G362P.h:117
epson_imu::MOV_AVG_TAP_8
@ MOV_AVG_TAP_8
Definition: G362P.h:110
REG_BURST_CTRL2_H
#define REG_BURST_CTRL2_H
Definition: G362P.h:70
REG_BURST_CTRL1_L
#define REG_BURST_CTRL1_L
Definition: G362P.h:69
epson_imu::Mode
Mode
Definition: G362P.h:92
epson_imu::G362P::GetMode
Mode GetMode(void)
Definition: G362P.cc:339
epson_imu::G362P::DumpRegisters
void DumpRegisters(void)
Definition: G362P.cc:138
epson_imu::G362P::SetMode
bool SetMode(Mode mode)
Definition: G362P.cc:302
epson_imu::Filter
Filter
Definition: G362P.h:107
epson_imu::Register
struct epson_imu::_Register Register
REG_TEMP_HIGH
#define REG_TEMP_HIGH
Definition: G362P.h:40
epson_imu::FIR_TAP_32_FC_100
@ FIR_TAP_32_FC_100
Definition: G362P.h:116
epson_imu::G362P::WriteRegister
void WriteRegister(Window window, uint8_t address, uint8_t value)
Definition: G362P.cc:122
epson_imu::OutputBits
OutputBits
Definition: G362P.h:102
REG_DIAG_STAT_H
#define REG_DIAG_STAT_H
Definition: G362P.h:33
REG_BURST_CTRL1_H
#define REG_BURST_CTRL1_H
Definition: G362P.h:68
epson_imu::SPS2000
@ SPS2000
Definition: G362P.h:130
REG_GLOB_CMD_H
#define REG_GLOB_CMD_H
Definition: G362P.h:66
epson_imu::B16
@ B16
Definition: G362P.h:103
epson_imu::_Data::gpio_
uint16_t gpio_
Definition: G362P.h:144
gpio::GPIO
Definition: GPIO.h:54
epson_imu::G362P::IsReady
bool IsReady(void)
Definition: G362P.cc:60
epson_imu::FIR_TAP_64_FC_400
@ FIR_TAP_64_FC_400
Definition: G362P.h:122
REG_BURST_H
#define REG_BURST_H
Definition: G362P.h:29
epson_imu::_Register
Definition: G362P.h:149
REG_FLAG_H
#define REG_FLAG_H
Definition: G362P.h:35
REG_ZGYRO_LOW
#define REG_ZGYRO_LOW
Definition: G362P.h:47
epson_imu::MOV_AVG_TAP_128
@ MOV_AVG_TAP_128
Definition: G362P.h:114
REG_UART_CTRL_L
#define REG_UART_CTRL_L
Definition: G362P.h:65
epson_imu::G362P::~G362P
~G362P(void)
Definition: G362P.cc:57
time.h
epson_imu::G362P::G362P
G362P(int fd_spi_dev, uint32_t spi_speed_hz, gpio::GPIO *gpio_data_ready, gpio::GPIO *gpio_reset)
Definition: G362P.cc:35
epson_imu::SamplingRate
SamplingRate
Definition: G362P.h:129
epson_imu::MOV_AVG_TAP_2
@ MOV_AVG_TAP_2
Definition: G362P.h:108
REG_ZACCL_HIGH
#define REG_ZACCL_HIGH
Definition: G362P.h:52
epson_imu::G362P::SetWindow
void SetWindow(Window window)
Definition: G362P.cc:118
REG_SIG_CTRL_L
#define REG_SIG_CTRL_L
Definition: G362P.h:57
epson_imu::SPS250
@ SPS250
Definition: G362P.h:133
epson_imu::G362P::EnableUART
void EnableUART(bool enable)
Definition: G362P.cc:231
epson_imu::_Data::chksm_
uint16_t chksm_
Definition: G362P.h:146
epson_imu::SPS1000
@ SPS1000
Definition: G362P.h:131
epson_imu::G362P::SelfTest
bool SelfTest(void)
REG_SIG_CTRL_H
#define REG_SIG_CTRL_H
Definition: G362P.h:56
epson_imu::G362P::PrintData
void PrintData(const Data &data)
Definition: G362P.cc:460
epson_imu::G362P::SetSamplingRate
bool SetSamplingRate(SamplingRate sampling_rate)
Definition: G362P.cc:158
REG_MSC_CTRL_L
#define REG_MSC_CTRL_L
Definition: G362P.h:59
epson_imu::WIN1
@ WIN1
Definition: G362P.h:99
epson_imu::SPS31_25
@ SPS31_25
Definition: G362P.h:136
epson_imu::G362P
Definition: G362P.h:211
epson_imu::kRegisters
constexpr Register kRegisters[]
Definition: G362P.h:162
epson_imu::G362P::SoftReset
void SoftReset(void)
Definition: G362P.cc:75
epson_imu::G362P::EnableOutput
bool EnableOutput(bool flag, bool temp, bool gyro, bool accl, bool gpio, bool count, bool chksm)
Definition: G362P.cc:260
REG_XACCL_HIGH
#define REG_XACCL_HIGH
Definition: G362P.h:48
epson_imu::G362P::StopSampling
bool StopSampling(void)
gpio
Definition: GPIO.h:28
REG_UART_CTRL_H
#define REG_UART_CTRL_H
Definition: G362P.h:64
epson_imu::SPS125
@ SPS125
Definition: G362P.h:134
epson_imu::MOV_AVG_TAP_4
@ MOV_AVG_TAP_4
Definition: G362P.h:109
REG_SMPL_CTRL_H
#define REG_SMPL_CTRL_H
Definition: G362P.h:60
epson_imu::MOV_AVG_TAP_64
@ MOV_AVG_TAP_64
Definition: G362P.h:113
epson_imu::MOV_AVG_TAP_16
@ MOV_AVG_TAP_16
Definition: G362P.h:111
epson_imu::G362P::HardReset
void HardReset(void)
Definition: G362P.cc:68
REG_DIAG_STAT_L
#define REG_DIAG_STAT_L
Definition: G362P.h:34
epson_imu::_Register::address
uint8_t address
Definition: G362P.h:153
epson_imu::_Register::name
const char * name
Definition: G362P.h:155
REG_SMPL_CTRL_L
#define REG_SMPL_CTRL_L
Definition: G362P.h:61
epson_imu::G362P::SetOutputBits
bool SetOutputBits(OutputBits temp, OutputBits gyro, OutputBits accl)
Definition: G362P.cc:236
REG_ZACCL_LOW
#define REG_ZACCL_LOW
Definition: G362P.h:53
REG_GPIO_L
#define REG_GPIO_L
Definition: G362P.h:38
epson_imu::SPS500
@ SPS500
Definition: G362P.h:132
epson_imu::_Data::accl_
double accl_[3]
Definition: G362P.h:143
epson_imu::FIR_TAP_64_FC_100
@ FIR_TAP_64_FC_100
Definition: G362P.h:120
epson_imu::G362P::ReadData
bool ReadData(Data *data)
Definition: G362P.cc:477
epson_imu::CONFIGURATION
@ CONFIGURATION
Definition: G362P.h:94
REG_COUNT
#define REG_COUNT
Definition: G362P.h:39
epson_imu::FIR_TAP_128_FC_400
@ FIR_TAP_128_FC_400
Definition: G362P.h:126
epson_imu::_Data::gyro_
double gyro_[3]
Definition: G362P.h:142
epson_imu::FIR_TAP_64_FC_50
@ FIR_TAP_64_FC_50
Definition: G362P.h:119
epson_imu::WIN0
@ WIN0
Definition: G362P.h:98
epson_imu::SAMPLING
@ SAMPLING
Definition: G362P.h:93
epson_imu::FIR_TAP_128_FC_50
@ FIR_TAP_128_FC_50
Definition: G362P.h:123
REG_YACCL_LOW
#define REG_YACCL_LOW
Definition: G362P.h:51
REG_FILTER_CTRL_H
#define REG_FILTER_CTRL_H
Definition: G362P.h:62
epson_imu::FIR_TAP_32_FC_400
@ FIR_TAP_32_FC_400
Definition: G362P.h:118
REG_FILTER_CTRL_L
#define REG_FILTER_CTRL_L
Definition: G362P.h:63
REG_ZGYRO_HIGH
#define REG_ZGYRO_HIGH
Definition: G362P.h:46
REG_XGYRO_LOW
#define REG_XGYRO_LOW
Definition: G362P.h:43
epson_imu::G362P::TransferWord
uint16_t TransferWord(uint16_t word, uint16_t delay_usecs)
Definition: G362P.cc:500
REG_WIN_CTRL_H
#define REG_WIN_CTRL_H
Definition: G362P.h:72
epson_imu::_Data::count_
uint16_t count_
Definition: G362P.h:145
REG_GLOB_CMD_L
#define REG_GLOB_CMD_L
Definition: G362P.h:67
REG_YACCL_HIGH
#define REG_YACCL_HIGH
Definition: G362P.h:50
epson_imu::_Data::flag_
uint16_t flag_
Definition: G362P.h:140
epson_imu::B32
@ B32
Definition: G362P.h:104
REG_MODE_CTRL_H
#define REG_MODE_CTRL_H
Definition: G362P.h:31
GPIO.h
epson_imu::_Data
Definition: G362P.h:139
epson_imu::Window
Window
Definition: G362P.h:97
REG_TEMP_LOW
#define REG_TEMP_LOW
Definition: G362P.h:41