-
Notifications
You must be signed in to change notification settings - Fork 1
/
Header _file
266 lines (245 loc) · 9.66 KB
/
Header _file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*!
@file ADXL345_Haeder.h
@author Lucien
@note The ADXL345 is a digital accelerometer with 13-bit resolution, capable
of measuring up to +/-16g. This driver communicate using I2C or SPI
@section license License
BSD (see license.txt)
@section HISTORY
v1.0 - First release
*/
#ifndef STM32Lxx_ADXL345_h
#define STM32Lxx_ADXL345_h
#include <stm32l4xx.h> // contains types useful for these MCU series
/* =========================================================================
I2C ADDRESS
-----------------------------------------------------------------------*/
#define ADXL345_ADDRESS (0xA6) // Assumes ALT address pin grouded
/* =========================================================================*/
/* =========================================================================
ADXL345 REGISTERS
-----------------------------------------------------------------------*/
#define ADXL345_REG_DEVID (0x00) // Device ID
#define ADXL345_REG_THRESH_TAP (0x1D) // Tap threshold
#define ADXL345_REG_OFSX (0x1E) // X-axis offset
#define ADXL345_REG_OFSY (0x1F) // Y-axis offset
#define ADXL345_REG_OFSZ (0x20) // Z-axis offset
#define ADXL345_REG_DUR (0x21) // Tap duration
#define ADXL345_REG_LATENT (0x22) // Tap latency
#define ADXL345_REG_WINDOW (0x23) // Tap window
#define ADXL345_REG_THRESH_ACT (0x24) // Activity threshold
#define ADXL345_REG_THRESH_INACT (0x25) // Inactivity threshold
#define ADXL345_REG_TIME_INACT (0x26) // Inactivity time
#define ADXL345_REG_ACT_INACT_CTL (0x27) // Axis enable control for activity and inactivity detection
#define ADXL345_REG_THRESH_FF (0x28) // Free-fall threshold
#define ADXL345_REG_TIME_FF (0x29) // Free-fall time
#define ADXL345_REG_TAP_AXES (0x2A) // Axis control for single/double tap
#define ADXL345_REG_ACT_TAP_STATUS (0x2B) // Source for single/double tap
#define ADXL345_REG_BW_RATE (0x2C) // Data rate and power mode control
#define ADXL345_REG_POWER_CTL (0x2D) // Power-saving features control
#define ADXL345_REG_INT_ENABLE (0x2E) // Interrupt enable control
#define ADXL345_REG_INT_MAP (0x2F) // Interrupt mapping control
#define ADXL345_REG_INT_SOURCE (0x30) // Source of interrupts
#define ADXL345_REG_DATA_FORMAT (0x31) // Data format control
#define ADXL345_REG_DATAX0 (0x32) // X-axis data 0
#define ADXL345_REG_DATAX1 (0x33) // X-axis data 1
#define ADXL345_REG_DATAY0 (0x34) // Y-axis data 0
#define ADXL345_REG_DATAY1 (0x35) // Y-axis data 1
#define ADXL345_REG_DATAZ0 (0x36) // Z-axis data 0
#define ADXL345_REG_DATAZ1 (0x37) // Z-axis data 1
#define ADXL345_REG_FIFO_CTL (0x38) // FIFO control
#define ADXL345_REG_FIFO_STATUS (0x39) // FIFO status
/* =========================================================================*/
/* ========================== DEFINE =======================================*/
/*!
* @brief
*/
#define ADXL345_MG2G_MULTIPLIER (0.004) // 4mg per lsb
/*!
* @brief Device name
*/
#define DEV_NAME "ADXL345"
/* =========================================================================*/
/*============================= TYPEDEF ===================================*/
/*!
* @brief Used with register 0x2C (ADXL345_REG_BW_RATE) to set bandwidth
*/
typedef enum {
ADXL345_DATARATE_3200_HZ = 0x0F, // 1600Hz Bandwidth 140?A IDD
ADXL345_DATARATE_1600_HZ = 0x0E, // 800Hz Bandwidth 90?A IDD
ADXL345_DATARATE_800_HZ = 0x0D, // 400Hz Bandwidth 140?A IDD
ADXL345_DATARATE_400_HZ = 0x0C, // 200Hz Bandwidth 140?A IDD
ADXL345_DATARATE_200_HZ = 0x0B, // 100Hz Bandwidth 140?A IDD
ADXL345_DATARATE_100_HZ = 0x0A, // 50Hz Bandwidth 140?A IDD
ADXL345_DATARATE_50_HZ = 0x09, // 25Hz Bandwidth 90?A IDD
ADXL345_DATARATE_25_HZ = 0x08, // 12.5Hz Bandwidth 60?A IDD
ADXL345_DATARATE_12_5_HZ = 0x07, // 6.25Hz Bandwidth 50?A IDD
ADXL345_DATARATE_6_25HZ = 0x06, // 3.13Hz Bandwidth 45?A IDD
ADXL345_DATARATE_3_13_HZ = 0x05, // 1.56Hz Bandwidth 40?A IDD
ADXL345_DATARATE_1_56_HZ = 0x04, // 0.78Hz Bandwidth 34?A IDD
ADXL345_DATARATE_0_78_HZ = 0x03, // 0.39Hz Bandwidth 23?A IDD
ADXL345_DATARATE_0_39_HZ = 0x02, // 0.20Hz Bandwidth 23?A IDD
ADXL345_DATARATE_0_20_HZ = 0x01, // 0.10Hz Bandwidth 23?A IDD
ADXL345_DATARATE_0_10_HZ = 0x00 // 0.05Hz Bandwidth 23?A IDD (default value)
} dataRate_t;
/*!
* @brief ADXL345 Status definition (used instead of HAL_StatusTypeDef)
* HAL_OK = 0x00, HAL_ERROR = 0x01, HAL_BUSY = 0x02, HAL_TIMEOUT = 0x03
*/
typedef HAL_StatusTypeDef ADXL345_StatusTypeDef;
/*!
* @brief Used when register must be set
*
*/
typedef enum {
X_AXIS = 0x00,
Y_AXIS = 0x01,
Z_AXIS = 0x02
} three_axis;
/*!
* @brief Used with register 0x31 (ADXL345_REG_DATA_FORMAT) to set g range
*
*/
typedef enum {
ADXL345_RANGE_16_G = (uint8_t)0x03, /*!< +/- 16g. */
ADXL345_RANGE_8_G = (uint8_t)0x02, /*!< +/- 8g. */
ADXL345_RANGE_4_G = (uint8_t)0x01, /*!< +/- 4g. */
ADXL345_RANGE_2_G = (uint8_t)0x00 /*!< +/- 2g (default value) */
} range_t;
/*!
* @brief Used to set up resolution
*/
typedef enum {
FULL = 1,
DEFAULT = 0
} resolution;
typedef enum {
ON = 1,
OFF = 0
}selftest;
/*!
* @brief write_i2c to the device
* @param Sensor address, register address to write_i2c, value to be written
* @return true: success false: write_i2c failed
*/
typedef ADXL345_StatusTypeDef (*ADXL345_write)(uint8_t sensor_address, int8_t reg_address, int8_t val);
/*!
* @brief Read from the device
* @param Register address to read, pointer to stored data
* @return True: success, false: read failed
*/
typedef ADXL345_StatusTypeDef (*ADXL345_read)(uint8_t sensor_address, uint8_t reg_address, uint8_t *data, uint8_t len);
/*!
* @brief Make a delay in ms
* @param value in ms
*/
typedef void (*ADXL345_delay)(uint32_t _delay);
/*!
* @brief sensor features
*
*/
typedef struct {
char sensor_name[8]; /*!<Contains the name : ADXL345. */
uint8_t dev_id; /*!<Contains sensor id. */
range_t dev_range; /*!<Contains current range. */
resolution full_res; /*!<When this bit is set 1, the device iss in full resolution mode. */
uint8_t data_rate; /*!<Contains data rate. */
/// user redefine functions ( according MCU used )
ADXL345_write write_device; /*!<Write i2c or SPI. */
ADXL345_read read_device; /*!<Read i2c or SPI. */
ADXL345_delay ms_delay; /*!<Define delay. */
/* main items of the device used to tilt/inclination calculation
filled with register 0x32 to 0x36 (X, Y, Z axis) and calculated */
int16_t x_value; /*!<x-axis is 10 to 13 bits (proportionnal to sine of inclination). */
int16_t y_value; // y-axis acceleration, due to the orthogonality, is proportional
// to the cosine of the angle of inclination
int16_t z_value; /*!<x-axis is 10 to 13 bits. */
float teta_x; // 3 axix calculation : x poxition
float psi_y; // 3 axix calculation : y poxition
float phi_z; // 3 axix calculation : z poxition
}
ADXL345_sensor;
/* =========================================================================*/
/* ============================ API ========================================*/
/*!
* @brief API to interact with the ADXL345 accelerometer
*
*/
/*!
* @brief caculate the angle on one axis : Typically to check horizontality, motionless.
* @note To notice :
* - the calculated angle of inclination is accurate only when the
* device is oriented such that the x-axis is always in the plane of gravity
* - Linear approximation for inclination angle between -30 and +30° is possible with
* a good sensibility (teta = x/126 rd)
* - No ability to distinguish angles of inclination in a complete 360° arc
*/
float inclination_calculation_1axis(ADXL345_sensor *sensor,three_axis axis);
/*!
* @brief caculate the angle on two axis : Typically to move a robot, at constant speed) on a plane surface
* x, y >0 : 0<teta<90 - x>0, y<0 : 90<teta<180 - x, y <0 : 180<teta<270 - x<0, y>0 : 270<teta<360
* @note An effective incremental sensitivity that is roughly constant.
* Complete 360° Tilt Sensing.
*/
void inclination_calculation_2axis(void);
/*!
* @brief caculate the angle on three axis : Typically to move a robot on all directions (at constant speed)
*
*/
void inclination_calculation_3axis(void);
/*!
* @brief set offset according the precision from +-2g to +-16g
*
*/
void setOffset(ADXL345_sensor sensor, int8_t average_value, three_axis axis);
/*!
* @brief set rsolution
*
*/
void set_resolution(ADXL345_sensor *sensor, resolution resol);
/*!
* @brief get rsolution
*
*/
resolution get_resolution(ADXL345_sensor *sensor);
/*!
* @brief set range from +-2g to +-16g
*
*/
void set_range(ADXL345_sensor *sensor, range_t range);
/*!
* @brief Get range (give true value only if setrange have been used before calling)
*
*/
range_t get_range(ADXL345_sensor *ps);
void set_data_rate(dataRate_t dataRate);
dataRate_t get_data_rate(void);
void init_self_test(ADXL345_sensor *ps, selftest value);
void ADXL345GetAvgN(int16_t *xi,int16_t *yi,int16_t *zi,uint8_t n);
ADXL345_StatusTypeDef ADXL345_begin(ADXL345_sensor *my_sensor, range_t my_range);
/*!
* @brief Get X, Y, Z
*
*/
void accel_lectureXYZ (ADXL345_sensor *sensor);
/**
* @brief Calculation in a sphere of angle with X axis
* @param sensor
*/
void accel_pitch (ADXL345_sensor *sensor);
/**
* @brief Calculation in a sphere of angle with Y axis
* @param sensor
*/
void accel_roll (ADXL345_sensor *sensor);
/**
* @brief Calculation in a sphere of angle with Z axis
* @param sensor
*/
void accel_yaw (ADXL345_sensor *sensor);
/**
* @brief Write the register to start mesurement
* @param sensor
*/
void start_mesurement (ADXL345_sensor *sensor);
#endif // STM32Lxx_ADXL345_h