CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ADXL345 Angle Problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Zek_De



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

ADXL345 Angle Problem
PostPosted: Wed Dec 14, 2016 1:14 pm     Reply with quote

Friends,

Why does device show 86 or 87 degree as max angle?
Code:

void Acc_Values()
{
    Acc_Values_Raw();
    float32 x = 0.004*(AccRaw[x_eksen]);
    float32 y = 0.004*(AccRaw[y_eksen]);
    float32 z = 0.004*(AccRaw[z_eksen]);

    tetapitch = atan2(x,sqrt(y*y + z*z)) * 57.29577;
    psiroll   = atan2(y,sqrt(x*x + z*z)) * 57.29577;
}

if I use
Code:
psiroll   = atan2(y,z) * 57.29577;

it's okay but angle is locked at 90. Also how can I fit in 0 - 180 degree?
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 14, 2016 1:20 pm     Reply with quote

We NEED to see your raw data !
AccRaw[x_eksen], y, and z......

Without KNOWING what the raw data is, no one knows if it's a raw reading problem( sensor error ) or a 'math' error somewhere in those calculations.....


Whenever you have a problem like this you need to printout(display) ALL the numbers from raw data,mid calculations as well as the final result. That way you can see where the problem is.

Jay
Zek_De



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

PostPosted: Wed Dec 14, 2016 1:24 pm     Reply with quote

To Raw Values

Code:
void Acc_Values_Raw()
{
   unsigned int8 cikis=0;
   
   cikis = Single_Byte_Read(ADXL345_deviceAdress_w ,ADXL345_deviceAdress_r ,INT_SOURCE);
   
   
   while(bit_test(cikis,7)==0)
   {
      cikis = Single_Byte_Read(ADXL345_deviceAdress_w ,ADXL345_deviceAdress_r ,INT_SOURCE);
   }
   
         Multiple_Bytes_Read(ADXL345_deviceAdress_w ,ADXL345_deviceAdress_r ,DATAXL_INC , out);

         AccRaw[x_eksen] = make16(out[1],out[0]);
         AccRaw[y_eksen] = make16(out[3],out[2]);
         AccRaw[z_eksen] = make16(out[5],out[4]);         
}
Zek_De



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

PostPosted: Wed Dec 14, 2016 1:25 pm     Reply with quote

Oh sorry you mean you wanna see some values
guy



Joined: 21 Oct 2005
Posts: 297

View user's profile Send private message Visit poster's website

PostPosted: Wed Dec 14, 2016 1:43 pm     Reply with quote

Here's a piece of code that measures between -90 to +90 deg. on a MMA8453Q accelerometer. Each of the two axis used has better accuracy on different angles, therefore the calculation is made using both.
Could be useful:

Code:
void main()
{   
byte i;
  byte tiltFltr=0;
   int timer=0,adcvalue=0,callini=0;
   short tmp;
   float f,f2;
   signed int16 i16;
   
    ini();
   while(true)
  {
      delay_ms(5);
     
// handle tilt:
      updateXYZ();

       // x axis
      i16=acc_xyz[0]; 
      if(i16<0) i16=(-i16);
     
      if(i16<130) {
         f=acc_xyz[0];
          f=asin(f/260)*180/pi;
      }
      else if(i16<220) {
         f=acc_xyz[2];  // z axis
          f=acos(f/260)*180/pi;
         
         f2=acc_xyz[0];
          f2=asin(f2/260)*180/pi;
          if(acc_xyz[0]<0) f=-f;   // neg. angle
          f=(f+f2)/2;
     }
     else {
         f=acc_xyz[2];   // z axis
          f=acos(f/260)*180/pi;
          if(acc_xyz[0]<0) f=-f;   // neg. angle
     }
     if(f<-90) f=-90;
     if(f>90) f=90;
      printf("%2.1g",f);
      putc(176);   // deg. sign
      putc('\r');
      putc('\n');
   }
}
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 14, 2016 1:53 pm     Reply with quote

re:
Oh sorry you mean you wanna see some values

yes...

There are at least 7 reasons for the bad numbers...
Ttelmah



Joined: 11 Mar 2010
Posts: 19506

View user's profile Send private message

PostPosted: Wed Dec 14, 2016 3:26 pm     Reply with quote

Also you might want to look at this code:

<http://www.ccsinfo.com/forum/viewtopic.php?t=50920&highlight=atan>

The code I posted there is an atan2 function that is half the size and much faster than the CCS version, designed specifically to give degrees as an output directly from an input from -2047 to 2048. It's very many times faster than using atan2, more accurate than the chip, and gets rid of the need to scale the input numbers and the result.

One particular problem (which is why atan2 is available), is that using atan (which is what is actually being coded using the sin & cos here), the functions fail at the quadrant points, so may not be able to code with values as you get to 90degrees
Zek_De



Joined: 13 Aug 2016
Posts: 100

View user's profile Send private message

PostPosted: Thu Dec 15, 2016 2:47 am     Reply with quote

Okay thanks everyone
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group