SparkFun: ITG-3200,Triple-Axis Gyro

From eLinux.org
Revision as of 13:08, 26 September 2012 by Ruff (talk | contribs) (Sample C Code)
Jump to: navigation, search

thumb‎ Embedded Linux Class Ruffin White RHIT


Breakout board for InvenSense's ITG-3200

Overview

The ITG-3200 is a three-axis gyro that can be purchased from SparkFun. The datasheet describes it:

The ITG-3200 is the world’s first single-chip, digital-output, 3-axis MEMS gyro IC optimized for gaming, 3D mice, and 3D remote control applications. The part features enhanced bias and sensitivity temperature stability, reducing the need for user calibration. Low frequency noise is lower than previous generation devices, simplifying application development and making for more-responsive remote controls.

ITG-3200 breakout board pin-out

Inputs and Outputs

The ITG-3200 takes a supply voltage (Vs) of 1.8-3.6 V. The analog outputs are scaled proportionally to the supply voltage; at Vs = 3.6 V, the output will change by 2x for the same acceleration as compared to Vs = 1.8 V. Although the output sensitivity is scaled proportionally to the input voltage, noise is not, so higher supply voltages are advisable to reduce the impact of noise.

At all supply voltages, 0 g acceleration corresponds to an output voltage of Vs/2. At Vs = 3.6 V, the datasheet specs the typical sensitivity at 360 mV / g, with g as standard gravitational acceleration.

Connecting to the Bone

The Beagle Bone can be connected to the gyro via the I2C bus. Ground and Vcc on the breakout board should be connected to pins 1 and 2 respectively on the bone's P9 header, and the SCL and SDA pins should be connected to one of the I2C pairs on the bone. VIO should also be simply tied to Vcc, as this will be the same voltage the bone will use when acting as the master in the I2C buss. Two 4.7k resistors should used be connected between SCL and then Vcc and between SDA and Vcc. There are available resistor pads on the breakout board I you prefer to have surface mounts on the board instead of bread boarding them. I used I2C3 (P9, pins 19 and 20). The address of the magnetometer can be found by using the i2cdetect command from the shell. I get:

beagle$ i2cdetect -y -r 3
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- 69 -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

The address should be 0x69 (or 105 in decimal). This address can be changed by soldering an arrangement of resistors on the reserved pads on the breakout board.

Registers

the ITG-3200 has 14 8-bit registers:

Address Name Access
00 WHO_AM_I Read/Write
15 SMPLRT_DIV Read/Write
16 DLPF_FS Read/Write
17 INT_CFG Read/Write
1A INT_STATUS Read
1B TEMP_OUT_H Read
1C TEMP_OUT_L Read
1D GYRO_XOUT_H Read
1E GYRO_XOUT_L Read
1F GYRO_YOUT_H Read
20 GYRO_YOUT_L Read
21 GYRO_ZOUT_H Read
22 GYRO_ZOUT_L Read
3E PWR_MGM Read/Write

The sensor values for each axis are 16-bit and are stored across 2 registers each. GYRO_#OUT_H and GYRO_#OUT_L where # is just letter for the axis. Will have to get the two halves of the 16-bit signed measurement to read the entire value..

Sample C Code

The code shown below is sample code to demonstrate reading the registers via I2C and using the library.Specifically the code sets up some peripherals like buttons and interrupts and the I2C buss. The code will also zero out the gyro to account for the internal bias the gyro might have just being stationary and level. Then with by pressing the button the code will toggle through and repetitively read different poll the gyro and print and display the results to the terminal. Simply press ‘CTRL’ + ‘C’ to quit the program.

/*
 * MiniProject02.c
 *
 *  Created on: Sep 20, 2012
 *      Author: Ruffin White
 */

#include "Header.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include "ITG-3200.h"

int loop=1;


void signal_handler(int sig)
{
	printf( "Ctrl-C pressed, cleaning up and exiting..\n" );
	loop = 0;
}

int main(int argc, char** argv){

	//variable declarations
	struct pollfd fdset[1];
	int nfds = 1;
	int timeout = 100;
	int rc;
	char* buf[MAX_BUF];
	int gpio1, gpio2;
	int gpio1_fd, gpio2_fd;
	int gpio2_value = 0;
	int pattern =0;
	int value =0;

	int freq = 10;
	int duty = 25;

	short int gyroID, gyroTemp, gyroX, gyroY, gyroZ;

	// I2C Variables
	char *end;
	int res, i2cbus, address, size, file;
	int daddress;

	//check that at least two arguments are passed in
	if(argc < 4){
		printf("Usage: %s <input-gpio> <output-gpio> <i2c-bus>\n", argv[0]);
		printf("polls input-gpio, and writes value to output-gpio\n");
		fflush(stdout);
		return 1;
	}


	//set signal handler for Ctrl + C
	if (signal(SIGINT, signal_handler) == SIG_ERR)
		printf("\ncan't catch SIGINT\n");

	//assign gpio values
	gpio1 = atoi(argv[1]);
	gpio2 = atoi(argv[2]);


	//assign I2C values
	i2cbus   = atoi(argv[3]);
	address  = ITG3200_I2C_ADDRESS;
	file = initialize(i2cbus, address);
	zeroGyro(file);


	//argument 1 will be input
	export_gpio(gpio1);
	set_gpio_direction(gpio1, "in");
	set_gpio_edge(gpio1, "falling");
	gpio1_fd = gpio_fd_open(gpio1);

	//argument 2 will be output
	export_gpio(gpio2);
	set_gpio_direction(gpio2, "out");
	set_gpio_value(gpio2, gpio2_value);
	gpio2_fd = gpio_fd_open(gpio2);

	set_mux_value("gpmc_a2",6);


	while(loop){
		memset((void*)fdset, 0, sizeof(fdset));

		fdset[0].fd = gpio1_fd;
		fdset[0].events = POLLPRI;

		rc = poll(fdset, nfds, timeout);

		if (rc < 0){
			printf("\npoll() failed!\n");
		}

		if (rc == 0){
			printf(".");
		}

		if((fdset[0].revents & POLLPRI) == POLLPRI) {
			read(fdset[0].fd, buf, MAX_BUF);
			printf("interrupt value=%c\n", buf[0]);
			pattern++;
		if(pattern == 4){
		pattern = 0;
		}
		}

		switch(pattern){

			// blink led
			case 0:
				printf("Case 0\n");
				value = read_ain("ain6");
				printf("Voltage: %d\n",value);
				set_pwm("ehrpwm.1:0",10,25);
				if(gpio2_value){
					gpio2_value = 0;
				}
				else{
					gpio2_value = 1;
				}
				set_gpio_value(gpio2, gpio2_value);
				break;

			//PWM output
			case 1:
				printf("Case 1\n");

				gyroID = readWhoAmI(file);

				printf("gyroID: %6d\n", gyroID);
				break;

			//Read Gyro Temperature
			case 2:
				printf("Case 2\n");

				gyroTemp = readTemp(file);

				printf("gyroTemp: %6d\n", gyroTemp);
				break;

			//Read Gyro XYZ
			case 3:
				printf("Case 3\n");

				gyroX = readX(file);
				gyroY = readY(file);
				gyroZ = readZ(file);

				printf("gyroX: %6d\n", gyroX);
				printf("gyroY: %6d\n", gyroY);
				printf("gyroZ: %6d\n", gyroZ);
				break;

			default:
				break;
			}
		}
	close(file);
	gpio_fd_close(gpio1_fd);
	gpio_fd_close(gpio2_fd);
	unexport_gpio(gpio1);
	unexport_gpio(gpio2);
	fflush(stdout);
	return 0;
}

Header File

Features

Digital-output X-, Y-, and Z-Axis angular rate sensors (gyros) on one integrated circuit Digitally-programmable low-pass filter Low 6.5mA operating current consumption for long battery life Wide VDD supply voltage range of 2.1V to 3.6V Standby current: 5μA Digital-output temperature sensor Fast Mode I2C (400kHz) serial interface Optional external clock inputs of 32.768kHz or 19.2MHz to synchronize with system clock Pins broken out to a breadboard friendly 7-pin 0.1" pitch header

Dimensions

0.70 x 0.55" (17.78 x 13.97mm)

Documents

Schematic Eagle Files Quickstart Guide ITG-3200 Datasheet Code (ATmega328) Example Sparkfun
thumb‎ Embedded Linux Class Ruffin White RHIT