Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
i2cdevlib/MPU6050_raw.inoatmasterjrowberg/i2cdevlibGitHub
Thisrepository Search
Explore
Features
Enterprise
jrowberg / i2cdevlib
Code
Branch:master
Issues 71
Pricing
Watch
Pullrequests 11
Wiki
Pulse
298
Signup
Star
1,139
Signin
Fork
2,806
Graphs
i2cdevlib/Arduino/MPU6050/Examples/MPU6050_raw/MPU6050_raw.ino
Findfile
Copypath
fc92547onSep2,2013
jrowbergUpdatingI2Cdevclassandvariousdevices
1contributor
152lines(126sloc) 5.99KB
1
2
3
4
5
6
7
8
9
10
Blame
History
//I2Cdeviceclass(I2Cdev)demonstrationArduinosketchforMPU6050class
//10/7/2011byJeffRowberg<jeff@rowberg.net>
//Updatesshould(hopefully)alwaysbeavailableathttps://github.com/jrowberg/i2cdevlib
//
//Changelog:
//20130508addedmultipleoutputformats
//addedseamlessFastwiresupport
//20111007initialrelease
/*============================================
11
12
13
I2CdevdevicelibrarycodeisplacedundertheMITlicense
Copyright(c)2011JeffRowberg
14
15
Permissionisherebygranted,freeofcharge,toanypersonobtainingacopy
16
17
ofthissoftwareandassociateddocumentationfiles(the"Software"),todeal
intheSoftwarewithoutrestriction,includingwithoutlimitationtherights
touse,copy,modify,merge,publish,distribute,sublicense,and/orsell
18
19
copiesoftheSoftware,andtopermitpersonstowhomtheSoftwareis
furnishedtodoso,subjecttothefollowingconditions:
20
21
22
Theabovecopyrightnoticeandthispermissionnoticeshallbeincludedin
allcopiesorsubstantialportionsoftheSoftware.
23
24
THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND,EXPRESSOR
25
26
IMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOFMERCHANTABILITY,
FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALLTHE
27
28
29
AUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM,DAMAGESOROTHER
LIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOROTHERWISE,ARISINGFROM,
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
Raw
OUTOFORINCONNECTIONWITHTHESOFTWAREORTHEUSEOROTHERDEALINGSIN
THESOFTWARE.
===============================================
*/
//I2CdevandMPU6050mustbeinstalledaslibraries,orelsethe.cpp/.hfiles
//forbothclassesmustbeintheincludepathofyourproject
#include"I2Cdev.h"
#include"MPU6050.h"
//ArduinoWirelibraryisrequiredifI2CdevI2CDEV_ARDUINO_WIREimplementation
//isusedinI2Cdev.h
#ifI2CDEV_IMPLEMENTATION==I2CDEV_ARDUINO_WIRE
#include"Wire.h"
#endif
//classdefaultI2Caddressis0x68
//specificI2Caddressesmaybepassedasaparameterhere
//AD0low=0x68(defaultforInvenSenseevaluationboard)
//AD0high=0x69
MPU6050accelgyro;
//MPU6050accelgyro(0x69);//<useforAD0high
int16_tax,ay,az;
int16_tgx,gy,gz;
https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/Examples/MPU6050_raw/MPU6050_raw.ino
1/3
3/13/2016
i2cdevlib/MPU6050_raw.inoatmasterjrowberg/i2cdevlibGitHub
56
57
//uncomment"OUTPUT_READABLE_ACCELGYRO"ifyouwanttoseeatabseparated
58
//listoftheaccelX/Y/ZandthengyroX/Y/Zvaluesindecimal.Easytoread,
//notsoeasytoparse,andslow(er)overUART.
59
60
#defineOUTPUT_READABLE_ACCELGYRO
61
62
63
//uncomment"OUTPUT_BINARY_ACCELGYRO"tosendall6axesofdataas16bit
64
//withoutcompressionordataloss),andeasytoparse,butimpossibletoread
65
//forahuman.
//#defineOUTPUT_BINARY_ACCELGYRO
66
67
//binary,onerightaftertheother.Thisisveryfast(asfastaspossible
68
69
70
#defineLED_PIN13
boolblinkState=false;
71
72
73
74
75
76
77
78
voidsetup(){
//joinI2Cbus(I2Cdevlibrarydoesn'tdothisautomatically)
#ifI2CDEV_IMPLEMENTATION==I2CDEV_ARDUINO_WIRE
Wire.begin();
#elifI2CDEV_IMPLEMENTATION==I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400,true);
#endif
79
80
81
//initializeserialcommunication
82
//it'sreallyuptoyoudependingonyourproject)
Serial.begin(38400);
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
//(38400chosenbecauseitworksaswellat8MHzasitdoesat16MHz,but
//initializedevice
Serial.println("InitializingI2Cdevices...");
accelgyro.initialize();
//verifyconnection
Serial.println("Testingdeviceconnections...");
Serial.println(accelgyro.testConnection()?"MPU6050connectionsuccessful":"MPU6050connectionfailed");
//usethecodebelowtochangeaccel/gyrooffsetvalues
/*
Serial.println("Updatinginternalsensoroffsets...");
//76
2359 1688
0
0
0
Serial.print(accelgyro.getXAccelOffset());Serial.print("\t");//76
Serial.print(accelgyro.getYAccelOffset());Serial.print("\t");//2359
Serial.print(accelgyro.getZAccelOffset());Serial.print("\t");//1688
Serial.print(accelgyro.getXGyroOffset());Serial.print("\t");//0
Serial.print(accelgyro.getYGyroOffset());Serial.print("\t");//0
Serial.print(accelgyro.getZGyroOffset());Serial.print("\t");//0
Serial.print("\n");
accelgyro.setXGyroOffset(220);
accelgyro.setYGyroOffset(76);
accelgyro.setZGyroOffset(85);
Serial.print(accelgyro.getXAccelOffset());Serial.print("\t");//76
Serial.print(accelgyro.getYAccelOffset());Serial.print("\t");//2359
Serial.print(accelgyro.getZAccelOffset());Serial.print("\t");//1688
Serial.print(accelgyro.getXGyroOffset());Serial.print("\t");//0
Serial.print(accelgyro.getYGyroOffset());Serial.print("\t");//0
Serial.print(accelgyro.getZGyroOffset());Serial.print("\t");//0
Serial.print("\n");
*/
//configureArduinoLEDfor
117
118
119
pinMode(LED_PIN,OUTPUT);
}
120
121
122
voidloop(){
//readrawaccel/gyromeasurementsfromdevice
123
124
125
126
127
128
129
130
accelgyro.getMotion6(&ax,&ay,&az,&gx,&gy,&gz);
//thesemethods(andafewothers)arealsoavailable
//accelgyro.getAcceleration(&ax,&ay,&az);
//accelgyro.getRotation(&gx,&gy,&gz);
#ifdefOUTPUT_READABLE_ACCELGYRO
//displaytabseparatedaccel/gyrox/y/zvalues
Serial.print("a/g:\t");
https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/Examples/MPU6050_raw/MPU6050_raw.ino
2/3
3/13/2016
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
i2cdevlib/MPU6050_raw.inoatmasterjrowberg/i2cdevlibGitHub
Serial.print(ax);Serial.print("\t");
Serial.print(ay);Serial.print("\t");
Serial.print(az);Serial.print("\t");
Serial.print(gx);Serial.print("\t");
Serial.print(gy);Serial.print("\t");
Serial.println(gz);
#endif
#ifdefOUTPUT_BINARY_ACCELGYRO
Serial.write((uint8_t)(ax>>8));Serial.write((uint8_t)(ax&0xFF));
Serial.write((uint8_t)(ay>>8));Serial.write((uint8_t)(ay&0xFF));
Serial.write((uint8_t)(az>>8));Serial.write((uint8_t)(az&0xFF));
Serial.write((uint8_t)(gx>>8));Serial.write((uint8_t)(gx&0xFF));
Serial.write((uint8_t)(gy>>8));Serial.write((uint8_t)(gy&0xFF));
Serial.write((uint8_t)(gz>>8));Serial.write((uint8_t)(gz&0xFF));
#endif
//blinkLEDtoindicateactivity
blinkState=!blinkState;
digitalWrite(LED_PIN,blinkState);
}
https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/Examples/MPU6050_raw/MPU6050_raw.ino
3/3