Last active
May 7, 2022 10:14
-
-
Save e-Gizmo/7818380 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ITG3205 3Axis Gyroscope | |
Sample codes function | |
for Gyroscope ITG3205. | |
Codes from: | |
http://www.varesano.net | |
Modified by: | |
e-Gizmo Mechatronix Central | |
http://www.e-gizmo.com | |
For comments and suggestions: | |
https://gist.github.com/egizmocodes/7818380 | |
*/ | |
#include <Wire.h> // I2C library, gyroscope | |
// Gyroscope ITG3200 | |
#define GYRO 0x68 // when AD0 is connected to GND ,gyro address is 0x68. | |
//#define GYRO 0x69 when AD0 is connected to VCC ,gyro address is 0x69 | |
#define G_SMPLRT_DIV 0x15 | |
#define G_DLPF_FS 0x16 | |
#define G_INT_CFG 0x17 | |
#define G_PWR_MGM 0x3E | |
#define G_TO_READ 8 // 2 bytes for each axis x, y, z | |
// offsets are chip specific. | |
int g_offx = 120; | |
int g_offy = 20; | |
int g_offz = 93; | |
int hx, hy, hz, turetemp; | |
////////// GYROSCOPE INITIALIZATION ////////// | |
void initGyro() | |
{ | |
/***************************************** | |
* ITG 3200 | |
* power management set to: | |
* clock select = internal oscillator | |
* no reset, no sleep mode | |
* no standby mode | |
* sample rate to = 125Hz | |
* parameter to +/- 2000 degrees/sec | |
* low pass filter = 5Hz | |
* no interrupt | |
******************************************/ | |
writeTo(GYRO, G_PWR_MGM, 0x00); | |
writeTo(GYRO, G_SMPLRT_DIV, 0x07); // EB, 50, 80, 7F, DE, 23, 20, FF | |
writeTo(GYRO, G_DLPF_FS, 0x1E); // +/- 2000 dgrs/sec, 1KHz, 1E, 19 | |
writeTo(GYRO, G_INT_CFG, 0x00); | |
} | |
void getGyroscopeData(int * result) | |
{ | |
/************************************** | |
Gyro ITG-3200 I2C | |
registers: | |
temp MSB = 1B, temp LSB = 1C | |
x axis MSB = 1D, x axis LSB = 1E | |
y axis MSB = 1F, y axis LSB = 20 | |
z axis MSB = 21, z axis LSB = 22 | |
*************************************/ | |
int regAddress = 0x1B; | |
int temp, x, y, z; | |
byte buff[G_TO_READ]; | |
readFrom(GYRO, regAddress, G_TO_READ, buff); //read the gyro data from the ITG3200 | |
result[0] = ((buff[2] << 8) | buff[3]) + g_offx; | |
result[1] = ((buff[4] << 8) | buff[5]) + g_offy; | |
result[2] = ((buff[6] << 8) | buff[7]) + g_offz; | |
result[3] = (buff[0] << 8) | buff[1]; // temperature | |
} | |
void setup() | |
{ | |
Serial.begin(9600); | |
Wire.begin(); | |
initGyro(); | |
} | |
//////////////// MAIN LOOP ////////////////// | |
void loop() | |
{ | |
byte addr; | |
int gyro[4]; | |
getGyroscopeData(gyro); | |
hx = gyro[0] / 14.375; | |
hy = gyro[1] / 14.375; | |
hz = gyro[2] / 14.375; | |
turetemp = 35+ ((double) (gyro[3] + 13200)) / 280; // temperature | |
Serial.print(" X="); | |
Serial.print(hx); | |
Serial.print(" Y="); | |
Serial.print(hy); | |
Serial.print(" Z="); | |
Serial.print(hz); | |
Serial.print(" F="); | |
Serial.print(turetemp); | |
Serial.print((char)223); | |
Serial.println("C"); | |
delay(100); | |
} | |
//////////// Transmission Functions: /////////////// | |
void writeTo(int DEVICE, byte address, byte val) { | |
Wire.beginTransmission(DEVICE); //start transmission to ACC | |
Wire.write(address); // send register address | |
Wire.write(val); // send value to write | |
Wire.endTransmission(); //end transmission | |
} | |
//reads num bytes starting from address register on ACC in to buff array | |
void readFrom(int DEVICE, byte address, int num, byte buff[]) { | |
Wire.beginTransmission(DEVICE); //start transmission to ACC | |
Wire.write(address); //sends address to read from | |
Wire.endTransmission(); //end transmission | |
Wire.beginTransmission(DEVICE); //start transmission to ACC | |
Wire.requestFrom(DEVICE, num); // request 6 bytes from ACC | |
int i = 0; | |
while(Wire.available()) //ACC may send less than requested (abnormal) | |
{ | |
buff[i] = Wire.read(); // receive a byte | |
i++; | |
} | |
Wire.endTransmission(); //end transmission | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't think adding 35 at line 91 is correct.
The data sheet says that 35 degrees is the conditions at which the offset was measured, not the offset itself
https://www.sparkfun.com/datasheets/Sensors/Gyro/PS-ITG-3200-00-01.4.pdf
Also, I don't think shifting the msb unsigned byte (lines 68-71) will result in correct negative values, note that the sensor values are supposedly 2's compliment 16 bit values.