-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAccel.ino
219 lines (186 loc) · 5.44 KB
/
Accel.ino
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
#define accelAddress 0x40 // page 54 and 61 of datasheet
#define accel_capture_delta 200
int accelADC[3];
long accelBuf[3];
volatile bool accel_done=true;
volatile bool accel_capture_flag=false;
fixed accel_captured[3]=0;
quaternion accel_captured_imu=ident;
void accel_init(void)
{
disable_sensor_interrupts();
accel_time=0;
// Check if accel is connected
if (readWhoI2C(accelAddress) != 0x03) // page 52 of datasheet
error("Accelerometer not found!");
updateRegisterI2C(accelAddress, 0x10, 0xB6); //reset device
delay(10); //sleep 10 ms after reset (page 25)
updateRegisterI2C(accelAddress, 0x0D, 0x10); //enable writing to control registers (ee_w=1)
updateRegisterI2C(accelAddress, 0x20, 0x10, 0xF0); // set low pass filter to 10Hz (bw=1)
updateRegisterI2C(accelAddress, 0x35, 0x01, 0x01); // smp_skip=1
updateRegisterI2C(accelAddress, 0x35, 0x04, 0x0E); // Set range select bits for +/-2g
updateRegisterI2C(accelAddress, 0x21, 0x02); //new_data_int
updateRegisterI2C(accelAddress, 0x0D, 0x00); //disable writing to control registers (ee_w=0)
delay(100); //waiting for accel to stabilize
accel_clear_int();
attachInterrupt(AccelIntNum, accel_calibrate, RISING);
//accel_measure();
enable_sensor_interrupts();
}
void accel_calibrate()
{ //does not calibrate currently
Serial.println("Accel calibration complete");
accel_ready=true;
if(gyro_ready)
{
imu_init_orientation();
imu_init_position();
}
attachInterrupt(AccelIntNum, accel_int, RISING);
}
void accel_measure(void) //warning: you must call disable_sensor_interrupts() and interrupts() before attempting to call this function
{
sendByteI2C(accelAddress, 0x02);
Wire.requestFrom(accelAddress, 6);
accelADC[0] = readReverseWordI2C() >> 2;
accelADC[1] = readReverseWordI2C() >> 2;
accelADC[2] = readReverseWordI2C() >> 2;
}
void accel_capture_wait(void)
{
accel_capture_flag=true;
while(accel_capture_flag)
continue;
}
void accel_clear_int(void)
{
accel_measure();
}
void accel_update_eeprom(void)
{
updateRegisterI2C(accelAddress, 0x0D, 0x10, 0x10); //ee_w=1
for(byte i=0x40; i<0x5B; i+=2)
{
delay(100);
updateRegisterI2C(accelAddress, i, 0x00);
}
delay(1000);
accel_init();
}
/* // The below is the old accel interrupt without position capturing support:
void accel_int(void)
{
static unsigned long int accel_oldtime=0;
accel_interrupted=true;
digitalWrite(AccelLEDPin, HIGH);
if(gyro_interrupted)
digitalWrite(GyroLEDPin, LOW);
else
digitalWrite(StatusLEDPin, LOW);
accel_time=micros()-accel_oldtime;
accel_oldtime=micros();
disable_sensor_interrupts();
interrupts();
accel_measure();
if(gyro_ready)
imu_updatePosition((long)accelADC[0]<<18, (long)accelADC[1]<<18, (long)accelADC[2]<<18);
digitalWrite(AccelLEDPin, LOW);
if(gyro_interrupted)
digitalWrite(GyroLEDPin, HIGH);
else
digitalWrite(StatusLEDPin, HIGH);
accel_interrupted=false;
enable_sensor_interrupts();
}
*/
void accel_int(void)
{
int oldval[3]={0};
static int icount=0;
static int dcount=0;
static unsigned long int accel_oldtime=0;
accel_interrupted=true;
digitalWrite(AccelLEDPin, HIGH);
if(gyro_interrupted)
digitalWrite(GyroLEDPin, LOW);
else
digitalWrite(StatusLEDPin, LOW);
accel_time=micros()-accel_oldtime;
accel_oldtime=micros();
fixed acceleration[3];
if(accel_capture_flag)
for (byte axis = 0; axis < 3; axis++)
oldval[axis]=accelADC[axis];
disable_sensor_interrupts();
interrupts();
accel_measure();
if(gyro_ready)
{
for (byte axis = 0; axis < 3; axis++)
{
acceleration[axis]=accelBuf[axis]<<18;
for(byte m=0; m<accel_calibration_magnitude; m++)
acceleration[axis]+=accel_calibration[axis][m]*pow(acceleration[axis], m);
}
imu_updatePosition(acceleration);
}
if(accel_capture_flag)
{
if(icount==0)
{
for (byte axis = 0; axis < 3; axis++)
accelBuf[axis]=0;//1<<(ACCELCNTP-1);
accel_captured_imu=imu.q;
}
if(abs(accelADC[0]-oldval[0])+abs(accelADC[1]-oldval[1])+abs(accelADC[2]-oldval[2])>accel_capture_delta)
{
icount=0;
Serial.print("|");
//Serial.println(accelADC[0]-oldval[0]);
//Serial.println(accelADC[1]-oldval[1]);
//Serial.println(accelADC[2]-oldval[2]);
if(++dcount>79)
{
dcount=0;
Serial.println("");
}
digitalWrite(AccelLEDPin, LOW);
if(gyro_interrupted)
digitalWrite(GyroLEDPin, HIGH);
else
digitalWrite(StatusLEDPin, HIGH);
accel_interrupted=false;
noInterrupts();
enable_sensor_interrupts();
return;
}
for (byte axis = 0; axis < 3; axis++)
accelBuf[axis]+=accelADC[axis];
if(++icount==1<<ACCELCNTP)
{
accel_capture_flag=false;
icount=0;
dcount=0;
for (byte axis = 0; axis < 3; axis++)
{
accel_captured[axis]=accelBuf[axis]<<(18-ACCELCNTP);
for(byte m=0; m<accel_calibration_magnitude; m++)
accel_captured[axis]+=accel_calibration[axis][m]*pow(accel_captured[axis], m);
}
Serial.println("");
}
}
digitalWrite(AccelLEDPin, LOW);
if(gyro_interrupted)
digitalWrite(GyroLEDPin, HIGH);
else
digitalWrite(StatusLEDPin, HIGH);
accel_interrupted=false;
noInterrupts();
enable_sensor_interrupts();
}
void printpoint(const long x[3])
{ Serial.println(x[0]);
Serial.println(x[1]);
Serial.println(x[2]);
}