Ive modified my test program as per suggestions.
1)I used the 3.3v reference for the ADC which should help combat rounding error
2)to get reliably timing for the integration I measure the time it takes to execute each loop continuosly and log it for later calculation. I still retain a small delay.
the modified arduino code is as follows
#define PIN_A0 0 //analog pin 0
#define PIN_A1 1 //analog pin 3
#define PIN_A5 5 //analog pin 5
int x, ref;
unsigned long time1, time2, deltatime; //used to work out duration
void setup()
{
Serial.begin(9600);
analogReference(EXTERNAL); //use the 3.3v reference for the ADC
}
void loop()
{
time1 = millis(); //get start time
x = analogRead(PIN_A5); //digitise gyro output
ref = analogRead(PIN_A1); //digitise reference
Serial.print(x); Serial.print(" ");
Serial.print(ref); Serial.print(" ");
delay(20);
time2 = millis(); //get end time
Serial.print(time2-time1); Serial.print("\n");
//time1 = millis();
//Serial.print(time1-time2); Serial.print("\n"); //about 1ms to do this command
}
I then process this in matlab (i like matlab because of its plotting function) with the following code
gyro_raw = input(:,1); %get the raw gyro data
ref = input(:, 2); %get the raw vref data
time = input(:, 3); %get the time (ms) between samples
time = time + 1; %add a 1ms to account for the final serial print
time = time/1000; %convert to seconds
gyro_angle = zeros(length(gyro_raw), 1); %create a matrix to store the angular position
angular_rate = ((gyro_raw - ref)*3.3/1024)/0.002;%get the angular rate
%get the angular position
%this is done by integrating and accounting for the time between samples
total = 0; %stores that angular position
for i =1:1:length(gyro_raw)
total = total + angular_rate(i)*time(i);%do the integration
gyro_angle(i) = total;%this will store the change in angle from some origin
end
plot(angular_rate);
figure, plot(gyro_angle);
What I did was rotate the gyroscope and plot the angular position over time. I still have the same problem where the angular position I measure from the gyroscope does not reflect the amount I rotate the gyroscope. For example I rotate the gyro past 90
0 but I measure a rotation less than 90
0, and this happens consistently. However if I change the sensitivity from 2mv/
0/s (as per datasheet) to say 1mv/
0/s I get better results. These are pretty rough experiments because I do the rotations by hand and observations by eye. I would attach the raw data I have collected and the plots I made, but I don't know how.
I have connected the IMU and arduino as follows
IMU board side connected to Arduino Side
pin5 (vref) analog in 1
pin7 (xrate) analog in 5
pin8 (gnd) the gnd pin below aref
pin9 (3.3v) 3.3v power output
At this point I think I am inclined to blame the hardware (I am aware this is rather poor form), and try a new gyroscope and hopefully that will solve everything