-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPU6050.c
425 lines (393 loc) · 11.8 KB
/
MPU6050.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* MPU6050.c
*
* Created on: 03.01.2021
*
*/
#include "stm32l0xx.h"
#include "stm32l0xx_nucleo.h"
#include "global_constants.h"
#include "global_variables.h"
#include "global_functions.h"
#include "MPU6050.h"
#include <math.h>
static void setup_conf();
static void setup_gyro();
static void setup_acc();
static void read(uint8_t, uint8_t);
void failsafe_CONF();
void failsafe_I2C();
double M_rotacji[3][3] = { { (ACC_CALIBRATION_X_X - ACC_PITCH_OFFSET)
/ sqrt(
pow(ACC_CALIBRATION_X_Z - ACC_YAW_OFFSET, 2)
+ pow(ACC_CALIBRATION_X_Y - ACC_ROLL_OFFSET, 2)
+ pow(ACC_CALIBRATION_X_X - ACC_PITCH_OFFSET, 2)),
(ACC_CALIBRATION_X_Y - ACC_ROLL_OFFSET)
/ sqrt(
pow(ACC_CALIBRATION_X_Z - ACC_YAW_OFFSET, 2)
+ pow(ACC_CALIBRATION_X_Y - ACC_ROLL_OFFSET, 2)
+ pow(ACC_CALIBRATION_X_X - ACC_PITCH_OFFSET,
2)), (ACC_CALIBRATION_X_Z
- ACC_YAW_OFFSET)
/ sqrt(
pow(ACC_CALIBRATION_X_Z - ACC_YAW_OFFSET, 2)
+ pow(ACC_CALIBRATION_X_Y - ACC_ROLL_OFFSET, 2)
+ pow(ACC_CALIBRATION_X_X - ACC_PITCH_OFFSET,
2)) }, { (ACC_CALIBRATION_Y_X
- ACC_PITCH_OFFSET)
/ sqrt(
pow(ACC_CALIBRATION_Y_Z - ACC_YAW_OFFSET, 2)
+ pow(ACC_CALIBRATION_Y_Y - ACC_ROLL_OFFSET, 2)
+ pow(ACC_CALIBRATION_Y_X - ACC_PITCH_OFFSET, 2)),
(ACC_CALIBRATION_Y_Y - ACC_ROLL_OFFSET)
/ sqrt(
pow(ACC_CALIBRATION_Y_Z - ACC_YAW_OFFSET, 2)
+ pow(ACC_CALIBRATION_Y_Y - ACC_ROLL_OFFSET, 2)
+ pow(ACC_CALIBRATION_Y_X - ACC_PITCH_OFFSET,
2)), (ACC_CALIBRATION_Y_Z
- ACC_YAW_OFFSET)
/ sqrt(
pow(ACC_CALIBRATION_Y_Z - ACC_YAW_OFFSET, 2)
+ pow(ACC_CALIBRATION_Y_Y - ACC_ROLL_OFFSET, 2)
+ pow(ACC_CALIBRATION_Y_X - ACC_PITCH_OFFSET,
2)) }, { (ACC_CALIBRATION_Z_X
- ACC_PITCH_OFFSET)
/ sqrt(
pow(ACC_CALIBRATION_Z_Z - ACC_YAW_OFFSET, 2)
+ pow(ACC_CALIBRATION_Z_Y - ACC_ROLL_OFFSET, 2)
+ pow(ACC_CALIBRATION_Z_X - ACC_PITCH_OFFSET, 2)),
(ACC_CALIBRATION_Z_Y - ACC_ROLL_OFFSET)
/ sqrt(
pow(ACC_CALIBRATION_Z_Z - ACC_YAW_OFFSET, 2)
+ pow(ACC_CALIBRATION_Z_Y - ACC_ROLL_OFFSET, 2)
+ pow(ACC_CALIBRATION_Z_X - ACC_PITCH_OFFSET,
2)), (ACC_CALIBRATION_Z_Z
- ACC_YAW_OFFSET)
/ sqrt(
pow(ACC_CALIBRATION_Z_Z - ACC_YAW_OFFSET, 2)
+ pow(ACC_CALIBRATION_Z_Y - ACC_ROLL_OFFSET, 2)
+ pow(ACC_CALIBRATION_Z_X - ACC_PITCH_OFFSET,
2)) } };
uint8_t read_write_tab[14];
static volatile uint8_t read_write_quantity;
static double time_flag4_1;
//for debugging only:
static uint32_t pak1 = 0;
void I2C1_IRQHandler() {
static uint8_t i = 0;
if (I2C1->ISR |= I2C_ISR_RXNE) {
read_write_tab[i] = I2C1->RXDR;
i++;
if (i > read_write_quantity - 1) {
i = 0;
I2C1_read_write_flag = 1;
}
}
}
void EXTI4_15_IRQHandler() {
if ((EXTI->PR & EXTI_PR_PIF4)) {
EXTI->PR |= EXTI_PR_PIF4; // clear this bit setting it as
}
if ((EXTI->PR & EXTI_PR_PIF5)) {
EXTI->PR |= EXTI_PR_PIF5; // clear this bit setting it as
}
if ((EXTI->PR & EXTI_PR_PIF6)) {
EXTI->PR |= EXTI_PR_PIF6; // clear this bit setting it as
}
if ((EXTI->PR & EXTI_PR_PIF7)) {
EXTI->PR |= EXTI_PR_PIF7; // clear this bit setting it as
}
if ((EXTI->PR & EXTI_PR_PIF8)) {
EXTI->PR |= EXTI_PR_PIF8; // clear this bit setting it as
}
//Interrupt from IMU:
if ((EXTI->PR & EXTI_PR_PIF9)) {
EXTI->PR |= EXTI_PR_PIF9; // clear this bit setting it as 1
if (I2C1_read_write_flag != 0) {
EXTI->IMR &= ~EXTI_IMR_IM9;
USART2->CR1 &= ~USART_CR1_RXNEIE;
// USART2->CR1 &= ~USART_CR1_IDLEIE;
//for debugging only:
for (int i = 0; i < 14; i++) {
read_write_tab[i] = 0;
}
read_all();
pak1++;
}
// if (I2C1_read_write_flag != 1){
//
// I2C1->CR1&=~I2C_CR1_PE;
// DMA1_Channel3->CCR &= ~DMA_CCR_EN;
// if (ibus_received==0){
// USART2->CR1 |= USART_CR1_RXNEIE;
// USART2->CR1 |= USART_CR1_IDLEIE;
// }
// while (I2C1->CR1 & I2C_CR1_PE){
//
// }
// I2C1->CR1|=I2C_CR1_PE;
//
// I2C1_read_write_flag = 1;
// }
}
if ((EXTI->PR & EXTI_PR_PIF10)) {
EXTI->PR |= EXTI_PR_PIF10; // clear this bit setting it as
}
if ((EXTI->PR & EXTI_PR_PIF11)) {
EXTI->PR |= EXTI_PR_PIF11; // clear this bit setting it as
}
if ((EXTI->PR & EXTI_PR_PIF12)) {
EXTI->PR |= EXTI_PR_PIF12; // clear this bit setting it as
}
if ((EXTI->PR & EXTI_PR_PIF13)) {
EXTI->PR |= EXTI_PR_PIF13; // clear this bit setting it as
}
if ((EXTI->PR & EXTI_PR_PIF14)) {
EXTI->PR |= EXTI_PR_PIF14; // clear this bit setting it as
}
//FAILSAFE ARM SWITCH is set as DISARM:
if ((EXTI->PR & EXTI_PR_PIF15)) {
EXTI->PR |= EXTI_PR_PIF15; // clear(setting 1) this bit (and at the same time bit SWIER15)
TIM2->CCR1 = 1000 - 1; //wypelneinie motor 1
TIM2->CCR2 = 1000 - 1; //wypelneinie motor 2
TIM2->CCR3 = 1000 - 1; //wypelneinie motor 3
TIM2->CCR4 = 1000 - 1; //wypelneinie motor 4
PWM_M1 = &motor_off;
PWM_M2 = &motor_off;
PWM_M3 = &motor_off;
PWM_M4 = &motor_off;
switch (failsafe_type) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
}
void setup_MPU6050() {
setup_conf();
setup_gyro();
setup_acc();
}
void I2C_Start(uint16_t Number_of_Bytes) {
// Ile bajtów bêdzie wysy³ane:
I2C1->CR2 = ((~0xF0000 & (I2C1->CR2)) | Number_of_Bytes << 16);
// wys³anie bajtu START aby rozpocz¹c komunikacje:
I2C1->CR2 |= I2C_CR2_START;
time_flag4_1 = get_Global_Time();
while (I2C1->CR2 & I2C_CR2_START) {
// czekam az START w CR2 zosatnie wyczyszczony aby wys³ac kolejne bajty
failsafe_I2C();
}
}
void I2C_StartWrite(uint16_t Number_of_Bytes) {
// transfer direction 0-write 1-read:
I2C1->CR2 &= ~I2C_CR2_RD_WRN;
// inicjalizacja komunikacji:
I2C_Start(Number_of_Bytes);
}
void I2C_StartRead(uint16_t Number_of_Bytes) {
// transfer direction 0-write 1-read
I2C1->CR2 |= I2C_CR2_RD_WRN;
// inicjalizacja komunikacji:
I2C_Start(Number_of_Bytes);
}
void read_all() {
I2C1_read_write_flag = 0;
read(0x3B, 14);
}
static void setup_conf() {
//-------main MPU6050 setting-----------
// slave address shifted by 1:
I2C1->CR2 |= 0x68 << 1;
// start communication:
I2C_StartWrite(2);
// address of Power Management 1 register:
I2C1->TXDR = 0x6B;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
// set 0x80 in this register (RESET)
I2C1->TXDR = 0x80;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
//delay of 0.1[s] according the MPU6050 datasheet
delay_mili(150);
// slave address shifted by 1:
I2C1->CR2 |= 0x68 << 1;
// start communication:
I2C_StartWrite(2);
// address of Power Management 1 register:
I2C1->TXDR = 0x6B;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
// set 0x0 in this register (SLEEP -> 0)
I2C1->TXDR = 0x0;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
// slave address shifted by 1:
I2C1->CR2 |= 0x68 << 1;
// start communication:
I2C_StartWrite(2);
// address of (26) Configuration register:
I2C1->TXDR = 0x1A;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
// setting low pass filter in this register (turn off)
I2C1->TXDR = 0x01;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
// slave address shifted by 1:
I2C1->CR2 |= 0x68 << 1;
// start communication:
I2C_StartWrite(2);
// address of (36) I2C Master Control register:
I2C1->TXDR = 0x24;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
// clock divider in this register
I2C1->TXDR = 0xD;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
// slave address shifted by 1:
I2C1->CR2 |= 0x68 << 1;
// start communication:
I2C_StartWrite(2);
// address of (56) Interrupt Enable register:
I2C1->TXDR = 0x38;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
// setting interrupt source as Data Register
I2C1->TXDR = 0x0001;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
}
static void setup_gyro() {
//---------setting Gyro--------
//start communication:
I2C_StartWrite(2);
// address of Gyroscope Configuration register:
I2C1->TXDR = 0x1C;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent, failsafe if set time passed
failsafe_CONF();
}
// set +/-1000[deg/s]
I2C1->TXDR = 0x10;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent
failsafe_CONF();
}
}
static void setup_acc() {
//---------setting Accelerometer-----------
// start communication:
I2C_StartWrite(2);
// address of Accelerometer Configuration register:
I2C1->TXDR = 0x1B;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent
failsafe_CONF();
}
// set +/-8[g]
I2C1->TXDR = 0x10;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent
failsafe_CONF();
}
}
static void read(uint8_t address, uint8_t n) {
read_write_quantity = n;
I2C_StartWrite(1);
// 1st address of accelerometer measurements, every next reading will increase register number by 1
I2C1->TXDR = address;
time_flag4_1 = get_Global_Time();
while (!(I2C1->ISR & I2C_ISR_TXE)) {
// waiting as Data will be sent
failsafe_I2C();
}
I2C_StartRead(n);
// enable DMA reading:
DMA1_Channel3->CCR |= DMA_CCR_EN;
}
void rewrite_data() {
if (I2C1_read_write_flag != 0) {
for (int i = 0; i < 3; i++) {
//gyro:
Gyro_Acc[i] = read_write_tab[2 * i + 8] << 8
| read_write_tab[2 * i + 9];
//acc:
Gyro_Acc[i + 3] = read_write_tab[2 * i] << 8
| read_write_tab[2 * i + 1];
}
double temporary[6] = { 0 };
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 3; i++) {
temporary[j] += Gyro_Acc[i] * M_rotacji[i][j];
temporary[j + 3] += Gyro_Acc[i + 3] * M_rotacji[i][j];
}
}
Gyro_Acc[0] = temporary[0]- GYRO_ROLL_OFFSET;
Gyro_Acc[1] = temporary[1]- GYRO_PITCH_OFFSET;
Gyro_Acc[2] = temporary[2] - GYRO_YAW_OFFSET;
Gyro_Acc[3] = temporary[3]- ACC_ROLL_OFFSET;
Gyro_Acc[4] = temporary[4]-ACC_PITCH_OFFSET;
Gyro_Acc[5] = temporary[5]-ACC_YAW_OFFSET;
//temperature:
Gyro_Acc[6] = read_write_tab[6] << 8 | read_write_tab[7];
EXTI->IMR |= EXTI_IMR_IM9;
}
}
void failsafe_CONF() {
// waiting as Data will be sent or failsafe if set time passed
if ((get_Global_Time() - time_flag4_1) >= MAX_I2C_TIME) {
failsafe_type = 4;
EXTI->SWIER |= EXTI_SWIER_SWI15;
}
}
void failsafe_I2C() {
// waiting as Data will be sent or failsafe if set time passed
if ((get_Global_Time() - time_flag4_1) >= MAX_I2C_TIME) {
failsafe_type = 5;
EXTI->SWIER |= EXTI_SWIER_SWI15;
}
}