-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccelerometer_calibrate.ino
227 lines (196 loc) · 4.38 KB
/
accelerometer_calibrate.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
219
220
221
222
223
224
225
226
227
#include <SoftwareSerial.h>
SoftwareSerial Lcd(3,2);
// Turn on to output min/max X Y Z values
const bool CALIBRATE = true;
const bool LCD_ONLY = true;
// analog input pins
const int X_PIN = A0;
const int Y_PIN = A1;
const int Z_PIN = A2;
// ADXL334
// const int MIN = 401;
// const int MAX = 630;
// MMA7361
const int MIN = 224;
const int MAX = 758;
// MMA7361
// xMax:729 xMin:239 yMax:770 yMin:267 zMax:721 zMin:229
const int X_MIN = 239;
const int X_MAX = 729;
const int Y_MIN = 267;
const int Y_MAX = 770;
const int Z_MIN = 229;
const int Z_MAX = 721;
int xMax = 0;
int xMin = 1024;
int yMax = 0;
int yMin = 1024;
int zMax = 0;
int zMin = 1024;
void setup(){
// set up the LCD's number of rows and columns:
if (LCD_ONLY) {
Lcd.begin(9600);
}
Serial.begin(9600);
// set analogRead max to 3.3v instead of default 5v
analogReference(EXTERNAL);
pinMode(X_PIN, INPUT);
pinMode(Y_PIN, INPUT);
pinMode(Z_PIN, INPUT);
}
void loop() {
// sample the voltages
// delay(10);
// analogRead(X_PIN);
// delay(10);
int x = analogRead(X_PIN);
// analogRead(Y_PIN);
// delay(10);
int y = analogRead(Y_PIN);
// analogRead(Z_PIN);
// delay(10);
int z = analogRead(Z_PIN);
if (CALIBRATE) {
if (x > xMax) {
xMax = x;
}
if (x < xMin) {
xMin = x;
}
if (y > yMax) {
yMax = y;
}
if (y < yMin) {
yMin = y;
}
if (z > zMax) {
zMax = z;
}
if (z < zMin) {
zMin = z;
}
if (LCD_ONLY) {
moveToFirstLine();
Lcd.print(" ");
Lcd.print(xMax);
Lcd.print(" ");
Lcd.print(yMax);
Lcd.print(" ");
Lcd.print(zMax);
Lcd.print(" ");
moveToSecondLine();
Lcd.print(" ");
Lcd.print(xMin);
Lcd.print(" ");
Lcd.print(yMin);
Lcd.print(" ");
Lcd.print(zMin);
Lcd.print(" ");
} else {
Serial.print("xMax:");
Serial.print(xMax);
Serial.print(" xMin:");
Serial.print(xMin);
Serial.print(" yMax:");
Serial.print(yMax);
Serial.print(" yMin:");
Serial.print(yMin);
Serial.print(" zMax:");
Serial.print(zMax);
Serial.print(" zMin:");
Serial.println(zMin);
}
}
Serial.print("x:");
Serial.print(x);
Serial.print(" y:");
Serial.print(y);
Serial.print(" z:");
Serial.println(z);
// convert to range of -90 to +90 degrees
// int xAng = map(x, MIN, MAX, -90, 90);
// int yAng = map(y, MIN, MAX, -90, 90);
// int zAng = map(z, MIN, MAX, -90, 90);
int xAng = map(x, X_MIN, X_MAX, -90, 90);
int yAng = map(y, Y_MIN, Y_MAX, -90, 90);
int zAng = map(z, Z_MIN, Z_MAX, -90, 90);
// print angles
// Serial.print("xAng:");
// Serial.print(xAng);
// Serial.print(" yAng:");
// Serial.print(yAng);
// Serial.print(" zAng:");
// Serial.println(zAng);
// simulate mount on X axis
// if (zAng > 0) {
// if (xAng <= 0) {
// xAng = xAng + 90;
// } else {
// xAng = 90 - xAng;
// }
// } else {
// if (xAng <= 0) {
// xAng = -90 - xAng;
// } else {
// xAng = xAng - 90;
// }
// }
//
// if (xAng > 0) {
// if (zAng <= 0) {
// zAng = -90 - zAng;
// } else {
// zAng = 90 - zAng;
// }
// } else {
// if (zAng <= 0) {
// zAng = zAng + 90;
// } else {
// zAng = 90 - zAng;
// }
// }
if (!LCD_ONLY) {
Serial.print("xAng:");
Serial.print(xAng);
Serial.print(" yAng:");
Serial.print(yAng);
Serial.print(" zAng:");
Serial.println(zAng);
}
// convert radians to degrees
int pitch = -(RAD_TO_DEG * (atan2(-yAng, -zAng) + PI));
int roll = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
// convert left roll and forward pitch to negative degrees
if (pitch < -180) {
pitch = pitch + 360;
}
if (roll > 180) {
roll = roll - 360;
}
// write the pitch and roll to the second line
if (!CALIBRATE) {
updateDisplay(pitch, roll);
}
// Wait a quarter second so the numbers aren't flashing so fast
delay(250);
}
void updateDisplay(int first, int second) {
// convert int values to strings for output
String firstString = String(first);
String secondString = String(second);
if (!LCD_ONLY) {
Serial.print("Pitch:");
Serial.print(secondString);
Serial.print(" Roll:");
Serial.println(firstString);
}
}
void moveToFirstLine() {
Lcd.write(254);
Lcd.write(128);
}
void moveToSecondLine() {
Lcd.write(254);
Lcd.write(192);
}