Tuesday, March 25, 2008

code for dead reckoning

Here is the code to get raw print outs of the change in acceleration over the course of a second. The results are a little jittery but are roughly correct

/*
* Accelerometer dead reckoning
* by Arthur Johnston and Ryan Galgon
*
* Takes the acceleromter values and calculates the dead reckoning
* how far it has gone
*code assumes you are not moving in
*Sol-3 gravity at approximately 1 atmosphere, code is not guaranteed
*for non-standard operation or even standard operation
*/
//pins to get input from
int x = 0; // select the input pin for the potentiometer
int y = 1;
int z = 2; // variable to store the value coming from the sensor
int st=4;
//global value storage
int oldval[]={0,0,0};
int val[]={0,0,0};
//distances
//THESE MUST BE CHANGED TO int if anything but a second is
//used as the period
int xd=0;
int yd=0;
int zd=0;
//time period in microseconds don't forget to change multiplier
int period=1000;
//time period in seconds
int multiplier=1;
//ignore changes in acceleration under this value
int ignore=1;

//initial acceleration, these may not be needed
int ix;
int iy;
int iz;

//current speeds, these may not be needed.
int xs=0;
int ys=0;
int zs=0;
void setup() {
Serial.begin(9600);
//initializes position
oldval[0] = analogRead(x); // read the value from the sensor
oldval[1] = analogRead(y); // read the value from the sensor
oldval[2] = analogRead(z); // read the value from the sensor
//initilize all these speeds, code assumes you are not moving in
//Sol-3 gravity at approximately 1 atmosphere, code is not guaranteed
//for non-standard operation or even standard operation
ix=oldval[0];
iy=oldval[1];
iz=oldval[2];

}

void loop() {
val[0] = analogRead(x); // read the value from the sensor
val[1] = analogRead(y); // read the value from the sensor
val[2] = analogRead(z); // read the value from the sensor
print_out_raw();
calculate_speed_change();
calculate_position_change();
print_out_initial();
oldval[0]=val[0];
oldval[1]=val[1];
oldval[2]=val[2];
delay(period);
}
/* Since acceleration is the derivative of speed this code is actually
lying. using the equation acceleration=((speed0+speed1)/delta t)
and rewriting it gets the equation
speed1=(acceleration*delta t)+speed0, which is what we use*/
void calculate_speed_change(){

xs=xs + ((val[0]-oldval[0])/ignore)*ignore *multiplier;
ys=ys + ((val[1]-oldval[1])/ignore)*ignore *multiplier;
zs=zs + ((val[2]-oldval[2])/ignore)*ignore *multiplier;
//xs=xs + ((val[0]-iz)/ignore)*ignore *multiplier;
//ys=ys + ((val[1]-iy)/ignore)*ignore *multiplier;
//zs=zs + ((val[2]-iz)/ignore)*ignore *multiplier;
print_out_speed();
}

void print_out_speed(){
Serial.print("speed ");
Serial.print(xs,DEC);
Serial.print(" ");
Serial.print(ys,DEC);
Serial.print(" ");
Serial.println(zs,DEC);
}

void calculate_position_change(){
xd=xd+xs;
yd=yd+ys;
zd=zd+zs;
//xd=xd + ((xs)/ignore)*ignore *multiplier;
//yd=yd + ((ys)/ignore)*ignore *multiplier;
//zd=zd + ((zs)/ignore)*ignore *multiplier;
print_out_position();
}

void print_out_position(){
Serial.print("position ");
Serial.print(xd,DEC);
Serial.print(" ");
Serial.print(yd,DEC);
Serial.print(" ");
Serial.println(zd,DEC);
}


void print_out_raw(){
Serial.print("absolute ");
Serial.print(val[0]);
Serial.print(" ");
Serial.print(val[1]);
Serial.print(" ");
Serial.println(val[2]);
Serial.print("relative ");
Serial.print(val[0]-oldval[0]);
Serial.print(" ");
Serial.print(val[1]-oldval[1]);
Serial.print(" ");
Serial.println(val[2]-oldval[2]);
}


void print_out_initial(){
Serial.print("initial ");
Serial.print(ix,DEC);
Serial.print(" ");
Serial.print(iy,DEC);
Serial.print(" ");
Serial.println(iz,DEC);
}

1 comment:

keshy said...

Hi!

Well am gonna be doing something similar for the android platform using the accelerometer as the sensing device.

Can you give me more details on "are a little jittery"? As in how accurate were the readings and under what circumstances.

What would be your take on a more sensitive device such as the accelerometer?

Thanks