1
+ /*
2
+ ===============================================
3
+ Example sketch for CurieImu library for Intel(R) Curie(TM) devices.
4
+ Copyright (c) 2015 Intel Corporation. All rights reserved.
5
+
6
+ Based on I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050
7
+ class by Jeff Rowberg: https://github.com/jrowberg/i2cdevlib
8
+
9
+ ===============================================
10
+ I2Cdev device library code is placed under the MIT license
11
+ Copyright (c) 2011 Jeff Rowberg
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be included in
21
+ all copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
+ THE SOFTWARE.
30
+ ===============================================
31
+ */
32
+
33
+ #include " CurieImu.h"
34
+
35
+ int16_t ax, ay, az;
36
+ int16_t gx, gy, gz;
37
+
38
+ // uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated
39
+ // list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,
40
+ // not so easy to parse, and slow(er) over UART.
41
+ #define OUTPUT_READABLE_ACCELGYRO
42
+
43
+ // uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit
44
+ // binary, one right after the other. This is very fast (as fast as possible
45
+ // without compression or data loss), and easy to parse, but impossible to read
46
+ // for a human.
47
+ // #define OUTPUT_BINARY_ACCELGYRO
48
+
49
+ // uncomment "CALIBRATE_ACCELGRYO_OFFSETS" to perform auto-calibration of all 6 axes during start-up
50
+ // This requires the device to be resting in a horizontal position during the start-up phase
51
+ // #define CALIBRATE_ACCELGRYO_OFFSETS
52
+
53
+
54
+ #define LED_PIN 13
55
+ bool blinkState = false ;
56
+
57
+ void setup () {
58
+ // initialize Serial communication
59
+ Serial.begin (115200 );
60
+
61
+ // initialize device
62
+ Serial.println (" Initializing IMU device..." );
63
+ CurieImu.initialize ();
64
+
65
+ // verify connection
66
+ Serial.println (" Testing device connections..." );
67
+ Serial.println (CurieImu.testConnection () ? " CurieImu connection successful" : " CurieImu connection failed" );
68
+
69
+ #if CALIBRATE_ACCELGRYO_OFFSETS
70
+ // use the code below to calibrate accel/gyro offset values
71
+ Serial.println (" Internal sensor offsets BEFORE calibration..." );
72
+ Serial.print (CurieImu.getXAccelOffset ()); Serial.print (" \t " ); // -76
73
+ Serial.print (CurieImu.getYAccelOffset ()); Serial.print (" \t " ); // -235
74
+ Serial.print (CurieImu.getZAccelOffset ()); Serial.print (" \t " ); // 168
75
+ Serial.print (CurieImu.getXGyroOffset ()); Serial.print (" \t " ); // 0
76
+ Serial.print (CurieImu.getYGyroOffset ()); Serial.print (" \t " ); // 0
77
+ Serial.print (CurieImu.getZGyroOffset ()); Serial.print (" \t " ); // 0
78
+ Serial.println (" " );
79
+
80
+ // To manually configure offset compensation values, use the following methods instead of the autoCalibrate...() methods below
81
+ // CurieImu.setXGyroOffset(220);
82
+ // CurieImu.setYGyroOffset(76);
83
+ // CurieImu.setZGyroOffset(-85);
84
+ // CurieImu.setXAccelOffset(-76);
85
+ // CurieImu.setYAccelOffset(--235);
86
+ // CurieImu.setZAccelOffset(168);
87
+
88
+ // IMU device must be resting in a horizontal position for the following calibration procedure to work correctly!
89
+ Serial.print (" Starting Gyroscope calibration..." );
90
+ CurieImu.autoCalibrateGyroOffset ();
91
+ Serial.println (" Done" );
92
+ Serial.print (" Starting Acceleration calibration..." );
93
+ CurieImu.autoCalibrateXAccelOffset (0 );
94
+ CurieImu.autoCalibrateYAccelOffset (0 );
95
+ CurieImu.autoCalibrateZAccelOffset (1 );
96
+ Serial.println (" Done" );
97
+
98
+ Serial.println (" Internal sensor offsets AFTER calibration..." );
99
+ Serial.print (CurieImu.getXAccelOffset ()); Serial.print (" \t " ); // -76
100
+ Serial.print (CurieImu.getYAccelOffset ()); Serial.print (" \t " ); // -2359
101
+ Serial.print (CurieImu.getZAccelOffset ()); Serial.print (" \t " ); // 1688
102
+ Serial.print (CurieImu.getXGyroOffset ()); Serial.print (" \t " ); // 0
103
+ Serial.print (CurieImu.getYGyroOffset ()); Serial.print (" \t " ); // 0
104
+ Serial.print (CurieImu.getZGyroOffset ()); Serial.print (" \t " ); // 0
105
+ Serial.println (" " );
106
+
107
+ Serial.println (" Enabling Gyroscope/Acceleration offset compensation" );
108
+ CurieImu.setGyroOffsetEnabled (true );
109
+ CurieImu.setAccelOffsetEnabled (true );
110
+ #endif
111
+
112
+ // configure Arduino LED for
113
+ pinMode (LED_PIN, OUTPUT);
114
+ }
115
+
116
+ void loop () {
117
+ // read raw accel/gyro measurements from device
118
+ CurieImu.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
119
+
120
+ // these methods (and a few others) are also available
121
+ // CurieImu.getAcceleration(&ax, &ay, &az);
122
+ // CurieImu.getRotation(&gx, &gy, &gz);
123
+
124
+ #ifdef OUTPUT_READABLE_ACCELGYRO
125
+ // display tab-separated accel/gyro x/y/z values
126
+ Serial.print (" a/g:\t " );
127
+ Serial.print (ax); Serial.print (" \t " );
128
+ Serial.print (ay); Serial.print (" \t " );
129
+ Serial.print (az); Serial.print (" \t " );
130
+ Serial.print (gx); Serial.print (" \t " );
131
+ Serial.print (gy); Serial.print (" \t " );
132
+ Serial.println (gz);
133
+ #endif
134
+
135
+ #ifdef OUTPUT_BINARY_ACCELGYRO
136
+ Serial.write ((uint8_t )(ax >> 8 )); Serial.write ((uint8_t )(ax & 0xFF ));
137
+ Serial.write ((uint8_t )(ay >> 8 )); Serial.write ((uint8_t )(ay & 0xFF ));
138
+ Serial.write ((uint8_t )(az >> 8 )); Serial.write ((uint8_t )(az & 0xFF ));
139
+ Serial.write ((uint8_t )(gx >> 8 )); Serial.write ((uint8_t )(gx & 0xFF ));
140
+ Serial.write ((uint8_t )(gy >> 8 )); Serial.write ((uint8_t )(gy & 0xFF ));
141
+ Serial.write ((uint8_t )(gz >> 8 )); Serial.write ((uint8_t )(gz & 0xFF ));
142
+ #endif
143
+
144
+ // blink LED to indicate activity
145
+ blinkState = !blinkState;
146
+ digitalWrite (LED_PIN, blinkState);
147
+ }
0 commit comments