-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKalman_MPU6050.ino
145 lines (102 loc) · 2.4 KB
/
Kalman_MPU6050.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
// vcc -- 3.3v
// scl a5
// sda a4
// int d2
#include<Wire.h>
#include <Servo.h>
Servo servo1;
#define DATA_LEN 14
#define DATA_IN_LEN 7
#define loopPin 4
// I2C address of the MPU-6050
const int MPU_addr=0x68;
// Mediciones
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int16_t datos[DATA_IN_LEN];
char out_msg[DATA_LEN];
bool kill=false;
char val, last_val;
char ch;
int servo_val = -180;
void setup()
{
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
pinMode(loopPin, INPUT);
servo1.attach(14);
servo_val = 64;
servo1.write(servo_val);
Serial.begin(9600);
last_val = -1;
}
void loop()
{
/*
delay(1000);
servo1.write(servo_val);
Serial.println(servo_val);
servo_val = servo_val + 10;
*/
if (Serial.available() > 0)
{
// get incoming byte:
ch = Serial.read();
if(kill == true)
{
kill = false;
servo_val = 64;
servo1.write(servo_val);
for (int idx = 0 ; idx < DATA_IN_LEN ; idx++)
{
out_msg[(idx*2)+0] = 0;
out_msg[(idx*2)+1] = 0;
}
for (int i = 0 ; i < DATA_LEN ; i++)
Serial.write(out_msg[i]);
}
else
{
if(ch == 'a')
servo_val = 64;
if(ch == 'r')
servo_val = 58;
if(ch == 'R')
servo_val = 0;
if(ch == 'c')
servo_val = 70;
if(ch == 'C')
servo_val = 120;
servo1.write(servo_val);
// Muestreo
// read sensor
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
for (int idx = 0 ; idx < DATA_IN_LEN ; idx++)
datos[idx] = Wire.read()<<8|Wire.read();
// Encode
for (int idx = 0 ; idx < DATA_IN_LEN ; idx++)
{
out_msg[(idx*2)+0] = char((datos[idx] >> 8) & 0xFF);
out_msg[(idx*2)+1] = char((datos[idx] ) & 0xFF);
}
// Send
for (int i = 0 ; i < DATA_LEN ; i++)
Serial.write(out_msg[i]);
}
}
// Controlo el stop
val = digitalRead(loopPin);
if (last_val != val)
{
last_val = val;
if (!val)
{
kill = true;
}
}
}