9DOF + Arduino + Processing

Hello

I am using an 9DOF Sensor Stick + Arduino Uno to caputure the data from the sensors and display them on a using serial monitor. The following links shows the code and the connections to be made.

Currently i am trying to display the results in a more friendly manner in other words i want to use Processing tool or any other tool to display a cube and whenever i move/change/rotate the orientation of the sensor stick i would like the cube to move respectively.

Any sort of help in doing this display thing is highly anticipated.

Cheers…

Hello bakkard90,
Here an example that does what you want but for Arduino Uno + 3DOF gyro L3G4200D. I believe, it is about to tweak it a little bit (replace with your sensor library, replace with your COM port, etc…).

The Arduino Uno code:

#include <Wire.h>
#include <L3G4200D.h>
L3G4200D gyro;
int sampleNum=500;
int dc_offset_x=0;
int dc_offset_y=0;
int dc_offset_z=0;
double noise=0;
unsigned long time;
int sampleTime=10;
int rate_x;
int rate_y;
int rate_z;
int prev_rate_x=0;
int prev_rate_y=0;
int prev_rate_z=0;
double x=0;
double y=0;
double z=0;
void setup() {
Serial.begin(9600);
Wire.begin();
gyro.enableDefault();
//Calculate initial DC offset and noise level of gyro
for(int n=0;n<sampleNum;n++){
gyro.read();
dc_offset_x+=(int)gyro.g.x;
dc_offset_y+=(int)gyro.g.y;
dc_offset_z+=(int)gyro.g.z;
}
dc_offset_x=dc_offset_x/sampleNum;
dc_offset_y=dc_offset_y/sampleNum;
dc_offset_z=dc_offset_z/sampleNum;
for(int n=0;n<sampleNum;n++){
gyro.read();
if((int)gyro.g.z-dc_offset_z>noise)
noise=(int)gyro.g.z-dc_offset_z;
else if((int)gyro.g.z-dc_offset_z<-noise)
noise=-(int)gyro.g.z-dc_offset_z;
}
noise=noise/100; //gyro returns hundredths of degrees/sec
delay(4000);
}
void loop() {
if(millis() - time > sampleTime)
{
time = millis(); // update the time to get the next sample
gyro.read();
rate_x=((int)gyro.g.x-dc_offset_x)/100;
rate_y=((int)gyro.g.y-dc_offset_y)/100;
rate_z=((int)gyro.g.z-dc_offset_z)/100;
if(rate_x >= noise || rate_x <= -noise)
x += ((double)(prev_rate_x + rate_x) * sampleTime * 1.32) / 2000;
if(rate_y >= noise || rate_y <= -noise)
y += ((double)(prev_rate_y + rate_y) * sampleTime * 1.32) / 2000;
if(rate_z >= noise || rate_z <= -noise)
z += ((double)(prev_rate_z + rate_z) * sampleTime * 1.32) / 2000;
// remember the current speed for the next loop rate integration.
prev_rate_x = rate_x;
prev_rate_y = rate_y;
prev_rate_z = rate_z;
// Keep our angle between 0-359 degrees
if (x < 0)
x += 360;
else if (x >= 360)
x -= 360;
if (y < 0)
y += 360;
else if (y >= 360)
y -= 360;
if (z < 0)
z += 360;
else if (z >= 360)
z -= 360;
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.print(z);
Serial.print('\n');
}
}

The Processing (2.0.2) code:

import processing.serial.*;
Serial myPort;    // The serial port
float xin=0;
float yin=0;
float zin=0;
float x, y, z;
void setup() {
  size(800, 800,P3D);
  myPort = new Serial(this, "COM28", 9600); 
  myPort.bufferUntil('\n'); 
}
void draw() {
  background(255);
  translate(width/2, height/2);;
  rotateX(xin/55);
  rotateY(yin/55);
  rotateZ(zin/55);
  pushMatrix();
  box(140);
  popMatrix();
}

void serialEvent(Serial myPort) 
{ 
  String inString = myPort.readString();
  float[] vals = float(split(inString,","));
  xin=vals[0];
  yin=vals[1];
  zin =vals[2];
}

Regards!

Here http://codeyoung.blogspot.nl/2009/11/adxl345-accelerometer-breakout-board.html is another example.

The code link at the end also contains a processing application to read the data and manipulate a model on the screen.

And here http://davidegironi.blogspot.nl/2013/02/avr-atmega-mpu6050-gyroscope-and.html#.UkqGbXdiKjI is an other example.