-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLessonIMUBangBangMag.ino
190 lines (137 loc) · 3.51 KB
/
LessonIMUBangBangMag.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
/*
Using the BNO080 IMU
By: Nathan Seidle
SparkFun Electronics
Date: December 21st, 2017
SparkFun code, firmware, and software is released under the MIT License.
Please see LICENSE.md for further details.
Feel like supporting our work? Buy a board from SparkFun!
https://www.sparkfun.com/products/14586
This example shows how to output the parts of the calibrated gyro.
It takes about 1ms at 400kHz I2C to read a record from the sensor, but we are polling the sensor continually
between updates from the sensor. Use the interrupt pin on the BNO080 breakout to avoid polling.
Hardware Connections:
Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
Plug the sensor onto the shield
Serial.print it out at 115200 baud to serial monitor.
*/
#include <Wire.h>
#include "SparkFun_BNO080_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_BNO080
BNO080 myIMU;
#define BLUELED 13
#define YELLOWLED 14
#define LIMIT 0.3
#define ARRAYSIZE 4
float signals[ARRAYSIZE]={0.0};
float gX,gY,gZ, mX,mY,mZ;
void setup()
{
Serial.begin(115200);
Serial.println();
Wire.begin();
pinMode(BLUELED, OUTPUT);
pinMode(YELLOWLED, OUTPUT);
while(myIMU.begin()==false){
Serial.println("IMU ERROR");
delay(100);
}
Serial.println("IMU Success");
Wire.setClock(400000); //Increase I2C data rate to 400kHz
myIMU.enableGyro(50); //Send data update every 50ms
//myIMU.enableMagnetometer(50); //Send data update every 50ms
Serial.println(F("x, y, z"));
}
float smooth(float tmp){
int count;
for(count=0;count<(ARRAYSIZE-1);count++){
signals[count]=signals[count+1];
}
signals[count]=tmp;
float favg=0.0;
for(count=0;count<ARRAYSIZE;count++){
favg=signals[count];
}
favg/=ARRAYSIZE;
return favg;
}
void deactivate(){
digitalWrite(BLUELED, LOW);
digitalWrite(YELLOWLED, LOW);
}
float activate(float gVal, float mVal){
gVal=smooth(gVal);
Serial.print(gVal);Serial.print(", ");Serial.println(mVal);
if(mVal>=0){
if(gVal>LIMIT)
digitalWrite(BLUELED, HIGH);
if(gVal<-LIMIT)
digitalWrite(YELLOWLED, HIGH);
}
else{
if(gVal>LIMIT)
digitalWrite(YELLOWLED, HIGH);
if(gVal<-LIMIT)
digitalWrite(BLUELED, HIGH);
}
}
void rotation(){
//Look for reports from the IMU
if (myIMU.dataAvailable() == true)
{
gX = myIMU.getGyroX();
gY = myIMU.getGyroY();
gZ = myIMU.getGyroZ();
}
}
void mag(){
//Look for reports from the IMU
if (myIMU.dataAvailable() == true)
{
mX = myIMU.getMagX();
mY = myIMU.getMagY();
mZ = myIMU.getMagZ();
}
}
unsigned long lastTime=0;
int step=0;
void loop()
{
unsigned long currentTime=millis();
if(currentTime<lastTime+30)
return;
else
lastTime=currentTime;
Serial.println(step);
switch (step){
case 0:
myIMU.enableGyro(10);
break;
case 1:
rotation();
break;
case 2:
deactivate();
myIMU.enableMagnetometer(10);
break;
case 3:
mag();
activate(gX,mX);
break;
}
step++;
if (step>5) step=0;
}
void loop1(){
step++;
if(step%2==0){
Serial.println("ON");
digitalWrite(YELLOWLED, LOW);
digitalWrite(BLUELED, LOW);
}
else{
Serial.println("OFF");
digitalWrite(YELLOWLED, HIGH);
digitalWrite(BLUELED, HIGH);
}
delay(5000);
}