Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Angle calculation from Gyroscope

Status
Not open for further replies.

RuH_iranga

Junior Member level 3
Joined
Nov 4, 2010
Messages
31
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Location
sri lanka
Activity points
1,480
I am doing a gyro related project and therefore I needed to calculate the rotated angle of gyro. Even there are several adjustments the code i generated was worked correctly and smoothly. coding was done using MikroC, and i used pololu LPR510al gyro. If someone interested i can post the code and you can futher develop it.

thanks
 

yes, it would be nice to publish the code. Also you can get ideas how to improve it
 
Here is the code:
Code:
int gyro_X;
int gyro_Y;
int gyro_REF;
float diff_X;
int diff_Y;

double velocity_X;
double velocity_Y;
float rotate_X;
double rotate_Y;
double tot_X=0;
double tot_Y;
double tot;
int offangle_X;
int i=0;
int j=0;
double arr[5];
int angle_X()
{

  if(((diff_X<=6)&&(diff_X>=0))||((diff_X>=-6)&&(diff_X<0)))
    {
                 diff_X=0;
    }
    velocity_X=diff_X*500/1024;
    rotate_X=velocity_X*0.056;        

         if(i<=3)
         {
         arr[i]=rotate_X;
                         if(i=0)
                         {
                         tot_X=arr[0];
                         }
                         if(i=1)
                         {
                         tot_X=(arr[0]+arr[1]*2)/3;
                         }
                         if(i=2)
                         {
                         tot_X=(arr[0]+arr[1]*2+arr[2]*2)/5;
                         }
                         if(i=3)
                         {
                         tot_X=(arr[0]+arr[1]*2+arr[2]*2+arr[3])/6;
                         }
         }
         else
         {
         arr[i]=rotate_X;
          for(j=0;j<=3; j++)
            arr[j]=arr[j+1];
            tot_X=(arr[0]+arr[1]*2+arr[2]*2+arr[3])/6;
            i=3;
            
         }
         i=i+1;




    tot=tot+tot_X;

    offangle_X=tot;
    return offangle_X;
}

void main()
{
     trisa=0xff;
     UsART_Init(9600);
     while(1)
     {
     gyro_X=Adc_read(1);
     gyro_Y=Adc_Read(2);
     gyro_REF=Adc_Read(3);

     diff_X=gyro_X-gyro_REF;
     diff_Y=gyro_Y-gyro_REF;

     delay_ms(10);
     angle_X();

    // Usart_Write(rotate_X);
     Usart_Write(offangle_X);
     }
}
 

there is small error in angle in this code. i rotate gyro from horizontal position to desired angle, and when it was rotated back to original position there is small difference of 4 degrees. I use 16f877a pic and please help me to overcome this problem. and i want to know whether this error is due to insufficient ADC.

please help me.
 

How quickly you rotate the gyro? If the rotating speed is low the PIC ADC will not have enough resolution to measure the signals. I would recommend you to increase the resolution to 12 -13 bits by using oversampling (on the price of slower response time).
 
Thank you very much. In my program I had to filter some values in "diff_X" variable, because it generates small unwanted values between reference pin and X-axis pin even in zero velocity. will this be affected to accuracy. I use POLOLU LPR510AL gyro. should i use a external ADC chip to improve accuracy and please be kind enough to recomond a product.

Pololu - LPR510AL Dual-Axis (Pitch and Roll or XY) Gyro with ±100°/s and ±400°/s Ranges

here is gyro details.

thank you again.
 

you can use external 16 bit ADC to improve the resolution but this will also increase the price of the solution. Google "oversampling" - this is a method to get more real bits from your existing ADC
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top