-
Notifications
You must be signed in to change notification settings - Fork 19
/
adxl345.c
181 lines (159 loc) · 4.3 KB
/
adxl345.c
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
/*
ADXL345 lib 0x02
copyright (c) Davide Gironi, 2012
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>
#include "adxl345.h"
#if ADXL345_GETATTITUDE == 1
#include <math.h>
#include <string.h>
#endif
//path to i2c fleury lib
#include ADXL345_I2CFLEURYPATH
#if ADXL345_LOWPASSENABLED == 1
static double axold = 0;
static double ayold = 0;
static double azold = 0;
static uint8_t firstread = 1;
#endif
#if ADXL345_GETATTITUDE == 1
/*
* estimate pitch and row using euleros angles
*/
void adxl345_getpitchroll(double ax, double ay, double az, double *pitch, double *roll) {
double magnitude = sqrt((ax * ax) + (ay * ay) + (az * az));
ax = ax / magnitude;
ay = ay / magnitude;
az = az / magnitude;
*pitch = -atan2(ax, sqrt(pow(ay,2) + pow(az, 2)));
*roll = atan2(ay, az);
}
#endif
/*
* initialize the accellerometer
*/
void adxl345_init(void) {
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
uint8_t range = ADXL345_RANGE | (ADXL345_FULLRANGE<<3);
i2c_write(0x31);
i2c_write(range);
//power register
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x2D);
i2c_write(0x0); //disable
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x2D);
i2c_write(0x16); //stand by
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x2D);
i2c_write(0x08); //enable
//interrupt
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x2E);
i2c_write(0x80); //data_ready on int2
_delay_ms(20);
i2c_stop();
}
/*
* write the calibration offset
*/
void adxl345_writeoffset(int8_t offsetx, int8_t offsety, int8_t offsetz) {
//x offset
i2c_start_wait(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x1E);
i2c_write(offsetx);
//y offset
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x1F);
i2c_write(offsety);
//z offset
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x20);
i2c_write(offsetz);
i2c_stop();
}
/*
* wait for xyz data to be ready
*/
void adxl345_waitfordataready(void) {
//wait until data is ready
unsigned char status = 0;
i2c_start_wait(ADXL345_ADDR | I2C_WRITE);
do {
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x30); //status register
i2c_rep_start(ADXL345_ADDR | I2C_READ);
status = i2c_readNak();
status &= 0b10000000;
} while (!status);
}
/*
* get xyz accellerometer values
*/
void adxl345_getdata(double *ax, double *ay, double *az) {
int16_t axraw = 0;
int16_t ayraw = 0;
int16_t azraw = 0;
adxl345_waitfordataready();
//read axis data
int16_t temp;
//X
i2c_start_wait(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x32);
i2c_rep_start(ADXL345_ADDR | I2C_READ);
axraw = i2c_readNak();
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x32+1);
i2c_rep_start(ADXL345_ADDR | I2C_READ);
temp = i2c_readNak();
axraw += (temp<<8);
//Y
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x34);
i2c_rep_start(ADXL345_ADDR | I2C_READ);
ayraw = i2c_readNak();
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x34+1);
i2c_rep_start(ADXL345_ADDR | I2C_READ);
temp = i2c_readNak();
ayraw += (temp<<8);
//Z
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x36);
i2c_rep_start(ADXL345_ADDR | I2C_READ);
azraw = i2c_readNak();
i2c_rep_start(ADXL345_ADDR | I2C_WRITE);
i2c_write(0x36+1);
i2c_rep_start(ADXL345_ADDR | I2C_READ);
temp = i2c_readNak();
azraw += (temp<<8);
i2c_stop();
//axisg = mx + b
//m is the scaling factor (g/counts), x is the sensor output (counts), and b is the count offset.
#if ADXL345_CALIBRATED == 1
*ax = (axraw/(double)ADXL345_CALRANGEVALX) + (double)ADXL345_CALOFFSETX;
*zy = (ayraw/(double)ADXL345_CALRANGEVALY) + (double)ADXL345_CALOFFSETY;
*az = (azraw/(double)ADXL345_CALRANGEVALZ) + (double)ADXL345_CALOFFSETZ;
#else
*ax = (axraw/(double)ADXL345_RANGEVAL);
*ay = (ayraw/(double)ADXL345_RANGEVAL);
*az = (azraw/(double)ADXL345_RANGEVAL);
#endif
//this is a simple low pass filter
#if ADXL345_LOWPASSENABLED == 1
if(!firstread)
*ax = (0.75)*(axold) + (0.25)*(*ax);
axold = *ax;
if(!firstread)
*ay = (0.75)*(ayold) + (0.25)*(*ay);
ayold = *ay;
if(!firstread)
*az = (0.75)*(azold) + (0.25)*(*az);
azold = *az;
firstread = 0;
#endif
}