SparkFun: ITG-3200,Triple-Axis Gyro Display

From eLinux.org
Revision as of 21:11, 23 October 2012 by Ruff (talk | contribs) (Overview)
Jump to: navigation, search

thumb‎ Embedded Linux Class Ruffin White RHIT


Breakout board for InvenSense's ITG-3200

Overview

This project show how the ITG-3200, a three-axis gyro sensor, can read and displayed using the beagle bone, a web browser and node.js. This project expands upon what was documented in SparkFun:_ITG-3200,Triple-Axis_Gyro.


ITG-3200 breakout board pin-out

Introduction

The goal here is to add a web-based graphical display to the gyroscopic sensor. Using examples of node.js servers that display realtime data from the beagle, I have altered the example code to display specifically the three axes of rotation for the gyroscope as well as in addition the internal temperature of the sensor itself.

buttonBox.js, buttonBox.html
This is a bone-based example that reads a gpio port, analog in and an i2c device and displays the output in a web browser.These will be there two main files that will contain many of my alterations.


Running the code

First off, you'll need to download the code through the get hub repository onto a local directory within your Beagle bone. You will then need to navigate to:

beagle$ cd ~/MiniProject04/node.js/realtime
beagle$ opkg update
beagle$ opkg install nodejs  (Don't install node, it's not what you think.)

On the Bone:

beagle$ node buttonBox.js

Connecting to the Bone

Then point a browser to beaglebone.local:8081. The default port is 8081. You can change it if you like.


How it works

The prior examples included the script within the HTML file that polled the server continuously for data updates. However I have altered the code within the HTML file as well as the .js file to allow the server to be so fully responsible for pushing the data to the client.

Another alteration I have made is to alter the I2C function that is called to acquisition the sensor data. Using the source code within mini project two, I have made a stripped-down version that simply returns all the relevant registers within the sensor. This is interpreted as a string within the server's .js file and is streamed to the client browser where the script parses the relevant information to each specific axis and temperature variable. This is then plotted on the grass and rendered on-screen over time.

This setup is accurate for the Sample Code up to the point about the brackeout board pinout. Just adjust the pin to the ITG-3200 accordingly to by description below. See EBC Exercise 12 I2C for more detail

Code

Node.js 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;
}

Screen Shots

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

Documents

SparkFun:_ITG-3200,Triple-Axis_Gyro

Schematic

Eagle Files

Quickstart Guide

ITG-3200 Datasheet

Code (ATmega328)

Example

Sparkfun

Github Repo




thumb‎ Embedded Linux Class Ruffin White RHIT