├── GY_85.cpp ├── GY_85.h ├── README.md ├── examples └── ReadRawData │ └── ReadRawData.ino └── keywords.txt /GY_85.cpp: -------------------------------------------------------------------------------- 1 | #include "GY_85.h" 2 | 3 | void GY_85::SetAccelerometer() 4 | { 5 | //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register. 6 | Wire.beginTransmission( ADXL345 ); // start transmission to device 7 | Wire.write( 0x31 ); // send register address 8 | Wire.write( 0x01 ); // send value to write 9 | Wire.endTransmission(); // end transmission 10 | 11 | //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register. 12 | Wire.beginTransmission( ADXL345 ); // start transmission to device 13 | Wire.write( 0x2D ); // send register address //Power Control Register 14 | Wire.write( 0x08 ); // send value to write 15 | Wire.endTransmission(); // end transmission 16 | 17 | } 18 | 19 | int* GY_85::readFromAccelerometer() 20 | { 21 | static int axis[3]; 22 | int buff[6]; 23 | 24 | Wire.beginTransmission( ADXL345 ); // start transmission to device 25 | Wire.write( DATAX0 ); // sends address to read from 26 | Wire.endTransmission(); // end transmission 27 | 28 | Wire.beginTransmission( ADXL345 ); // start transmission to device 29 | Wire.requestFrom( ADXL345, 6 ); // request 6 bytes from device 30 | 31 | uint8_t i = 0; 32 | while(Wire.available()) // device may send less than requested (abnormal) 33 | { 34 | buff[i] = Wire.read(); // receive a byte 35 | i++; 36 | } 37 | Wire.endTransmission(); // end transmission 38 | 39 | axis[0] = ((buff[1]) << 8) | buff[0]; 40 | axis[1] = ((buff[3]) << 8) | buff[2]; 41 | axis[2] = ((buff[5]) << 8) | buff[4]; 42 | 43 | return axis; 44 | } 45 | //---------------------------------------- 46 | void GY_85::SetCompass() 47 | { 48 | //Put the HMC5883 IC into the correct operating mode 49 | Wire.beginTransmission( HMC5883 ); //open communication with HMC5883 50 | Wire.write( 0x02 ); //select mode register 51 | Wire.write( 0x00 ); //continuous measurement mode 52 | Wire.endTransmission(); 53 | } 54 | 55 | int* GY_85::readFromCompass() 56 | { 57 | static int axis[3]; 58 | 59 | //Tell the HMC5883 where to begin reading data 60 | Wire.beginTransmission( HMC5883 ); 61 | Wire.write( 0x03 ); //select register 3, X MSB register 62 | Wire.endTransmission(); 63 | 64 | 65 | //Read data from each axis, 2 registers per axis 66 | Wire.requestFrom( HMC5883, 6 ); 67 | if(6<=Wire.available()){ 68 | axis[0] = Wire.read()<<8; //X msb 69 | axis[0] |= Wire.read(); //X lsb 70 | axis[2] = Wire.read()<<8; //Z msb 71 | axis[2] |= Wire.read(); //Z lsb 72 | axis[1] = Wire.read()<<8; //Y msb 73 | axis[1] |= Wire.read(); //Y lsb 74 | } 75 | return axis; 76 | } 77 | 78 | //---------------------------------------- 79 | 80 | int g_offx = 0; 81 | int g_offy = 0; 82 | int g_offz = 0; 83 | 84 | void GY_85::SetGyro() 85 | { 86 | Wire.beginTransmission( ITG3200 ); 87 | Wire.write( 0x3E ); 88 | Wire.write( 0x00 ); 89 | Wire.endTransmission(); 90 | 91 | Wire.beginTransmission( ITG3200 ); 92 | Wire.write( 0x15 ); 93 | Wire.write( 0x07 ); 94 | Wire.endTransmission(); 95 | 96 | Wire.beginTransmission( ITG3200 ); 97 | Wire.write( 0x16 ); 98 | Wire.write( 0x1E ); // +/- 2000 dgrs/sec, 1KHz, 1E, 19 99 | Wire.endTransmission(); 100 | 101 | Wire.beginTransmission( ITG3200 ); 102 | Wire.write( 0x17 ); 103 | Wire.write( 0x00 ); 104 | Wire.endTransmission(); 105 | 106 | delay(10); 107 | 108 | GyroCalibrate(); 109 | } 110 | 111 | void GY_85::GyroCalibrate() 112 | { 113 | static int tmpx = 0; 114 | static int tmpy = 0; 115 | static int tmpz = 0; 116 | 117 | g_offx = 0; 118 | g_offy = 0; 119 | g_offz = 0; 120 | 121 | for( uint8_t i = 0; i < 10; i ++ ) //take the mean from 10 gyro probes and divide it from the current probe 122 | { 123 | delay(10); 124 | float* gp = readGyro(); 125 | tmpx += *( gp); 126 | tmpy += *(++gp); 127 | tmpz += *(++gp); 128 | } 129 | g_offx = tmpx/10; 130 | g_offy = tmpy/10; 131 | g_offz = tmpz/10; 132 | } 133 | 134 | float* GY_85::readGyro() 135 | { 136 | static float axis[4]; 137 | 138 | Wire.beginTransmission( ITG3200 ); 139 | Wire.write( 0x1B ); 140 | Wire.endTransmission(); 141 | 142 | Wire.beginTransmission( ITG3200 ); 143 | Wire.requestFrom( ITG3200, 8 ); // request 8 bytes from ITG3200 144 | 145 | int i = 0; 146 | uint8_t buff[8]; 147 | while(Wire.available()) 148 | { 149 | buff[i] = Wire.read(); 150 | i++; 151 | } 152 | Wire.endTransmission(); 153 | 154 | axis[0] = ((buff[2] << 8) | buff[3]) - g_offx; 155 | axis[1] = ((buff[4] << 8) | buff[5]) - g_offy; 156 | axis[2] = ((buff[6] << 8) | buff[7]) - g_offz; 157 | axis[3] = ((buff[0] << 8) | buff[1]); // temperature 158 | 159 | return axis; 160 | } 161 | 162 | void GY_85::init() 163 | { 164 | Wire.begin(); 165 | SetAccelerometer(); 166 | SetCompass(); 167 | SetGyro(); 168 | } 169 | -------------------------------------------------------------------------------- /GY_85.h: -------------------------------------------------------------------------------- 1 | #ifndef GY_85_h 2 | #define GY_85_h 3 | 4 | #if ARDUINO >= 100 5 | #include "Arduino.h" 6 | #else 7 | #include "WProgram.h" 8 | #endif 9 | 10 | #include 11 | 12 | //----------addresses----------// 13 | #define ADXL345 (0x53) // Device address as specified in data sheet //ADXL345 accelerometer 14 | #define DATAX0 (0x32) //X-Axis Data 0 15 | //#define DATAX1 0x33 //X-Axis Data 1 16 | //#define DATAY0 0x34 //Y-Axis Data 0 17 | //#define DATAY1 0x35 //Y-Axis Data 1 18 | //#define DATAZ0 0x36 //Z-Axis Data 0 19 | //#define DATAZ1 0x37 //Z-Axis Data 1 20 | #define HMC5883 (0x1E) //gyro 21 | #define ITG3200 (0x68) //compass 22 | 23 | 24 | class GY_85 { 25 | 26 | private: 27 | void GyroCalibrate(); 28 | void SetGyro(); 29 | void SetCompass(); 30 | void SetAccelerometer(); 31 | 32 | public: 33 | void init(); 34 | int* readFromAccelerometer(); 35 | int* readFromCompass(); 36 | float* readGyro(); 37 | 38 | //callback functions 39 | inline int accelerometer_x( int* a ){ return *( a ); } 40 | inline int accelerometer_y( int* a ){ return *( 1+a ); } 41 | inline int accelerometer_z( int* a ){ return *( 2+a ); } 42 | 43 | //----------------------------------- 44 | 45 | inline int compass_x( int* a ){ return *( a ); } 46 | inline int compass_y( int* a ){ return *( 1+a ); } 47 | inline int compass_z( int* a ){ return *( 2+a ); } 48 | 49 | //----------------------------------- 50 | 51 | inline float gyro_x( float* a ){ return *( a ) / 14.375; } 52 | inline float gyro_y( float* a ){ return *( 1+a ) / 14.375; } 53 | inline float gyro_z( float* a ){ return *( 2+a ) / 14.375; } 54 | inline float temp ( float* a ){ return 35+( *( 3+a )+13200 ) / 280; } 55 | }; 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GY-85 Arduino Library 2 | ===================== 3 | A basic GY-85 raw data getter for arduino. 4 | 5 | Wiring for arduino UNO: 6 | 7 | GY-85 -> Arduino 8 | -------------------- 9 | VCC_IN -> 5V 10 | GND -> GND 11 | SCL -> A5 12 | SDA -> A4 13 | 14 | For other arduino boards you have to check the I2C pins. 15 | 16 | Installation 17 | -------------------- 18 | Download library and move it to your Arduino/libraries folder -------------------------------------------------------------------------------- /examples/ReadRawData/ReadRawData.ino: -------------------------------------------------------------------------------- 1 | #include "GY_85.h" 2 | 3 | // Create module object 4 | GY_85 GY85; 5 | 6 | void setup() { 7 | // Initialize the serial communication: 8 | Serial.begin(9600); 9 | 10 | // Initialize module 11 | GY85.init(); 12 | } 13 | 14 | void loop() { 15 | // Read data from sensors 16 | int* accelerometerReadings = GY85.readFromAccelerometer(); 17 | int ax = GY85.accelerometer_x(accelerometerReadings); 18 | int ay = GY85.accelerometer_y(accelerometerReadings); 19 | int az = GY85.accelerometer_z(accelerometerReadings); 20 | 21 | int* compassReadings = GY85.readFromCompass(); 22 | int cx = GY85.compass_x(compassReadings); 23 | int cy = GY85.compass_y(compassReadings); 24 | int cz = GY85.compass_z(compassReadings); 25 | 26 | float* gyroReadings = GY85.readGyro(); 27 | float gx = GY85.gyro_x(gyroReadings); 28 | float gy = GY85.gyro_y(gyroReadings); 29 | float gz = GY85.gyro_z(gyroReadings); 30 | float gt = GY85.temp(gyroReadings); 31 | 32 | // Log it to serial port 33 | Serial.print("accelerometer"); 34 | Serial.print(" x:"); 35 | Serial.print(ax); 36 | Serial.print(" y:"); 37 | Serial.print(ay); 38 | Serial.print(" z:"); 39 | Serial.print(az); 40 | 41 | Serial.print("\t compass"); 42 | Serial.print(" x:"); 43 | Serial.print(cx); 44 | Serial.print(" y:"); 45 | Serial.print(cy); 46 | Serial.print(" z:"); 47 | Serial.print(cz); 48 | 49 | Serial.print("\t gyro"); 50 | Serial.print(" x:"); 51 | Serial.print(gx); 52 | Serial.print(" y:"); 53 | Serial.print(gy); 54 | Serial.print(" z:"); 55 | Serial.print(gz); 56 | Serial.print("\t gyro temp:"); 57 | Serial.println(gt); 58 | 59 | // Make delay between readings 60 | delay(100); 61 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For GY_85 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | GY_85 KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | init KEYWORD2 16 | readFromAccelerometer KEYWORD2 17 | readFromCompass KEYWORD2 18 | readGyro KEYWORD2 19 | accelerometer_x KEYWORD2 20 | accelerometer_y KEYWORD2 21 | accelerometer_z KEYWORD2 22 | compass_x KEYWORD2 23 | compass_y KEYWORD2 24 | compass_z KEYWORD2 25 | gyro_x KEYWORD2 26 | gyro_y KEYWORD2 27 | gyro_z KEYWORD2 28 | temp KEYWORD2 29 | 30 | ####################################### 31 | # Constants (LITERAL1) 32 | ####################################### 33 | 34 | #sample LITERAL1 --------------------------------------------------------------------------------