double xyz[3];
double ax, ay, az;
adxl.get_Gxyz(xyz);
ax = xyz[0];
ay = xyz[1];
az = xyz[2];
double xAngle = atan( ax / (sqrt(square(ay) + square(az))));
double yAngle = atan( ay / (sqrt(square(ax) + square(az))));
double zAngle = atan( sqrt(square(ax) + square(ay)) / az);
xAngle *= 180.00; yAngle *= 180.00; zAngle *= 180.00;
xAngle /= 3.141592; yAngle /= 3.141592; zAngle /= 3.141592;
adxl.get_Gxyz(xyz);
ax = xyz[0];
ay = xyz[1];
az = xyz[2];
double xAngle = atan( ax / (sqrt(square(ay) + square(az))));
double yAngle = atan( ay / (sqrt(square(ax) + square(az))));
double zAngle = atan( sqrt(square(ax) + square(ay)) / az);
xAngle *= 180.00; yAngle *= 180.00; zAngle *= 180.00;
xAngle /= 3.141592; yAngle /= 3.141592; zAngle /= 3.141592;
Yea I know this could have been better written, but it works. There. Done! My 3 hours of research resulted in 11 or so lines of code. There are lots of blogs and DIY posts that go into the physics of accelerometers and go on at length about vector addition, angles, radians etc. But if you're like me all I want to know is if my toy robot fell down and can't get up.
But credits are due. I did find something I could read and understand without a masters in math at www.arduinoos.com. My code is a distillation of code found on page 5.
Note the get.Gxyz method returns (what I understand) are voltage levels from 0 to about 1.1 volts. My ADXL345 is running on IIC, but I can only assume if you are reading analog voltages through analog input pins you would get something similar.
Now, if you use this code you might notice some slight problems. You see the accelerometer should be calibrated, and/or the readings should be normalized. But this code will get you going if your task is rather simple. If you're building a fleet of quadcopters this might not be enough code.
I'm reading angles from 0 to 89.8 degrees in both the positive and negative direction. Now I don't think I'm finished with my angle calculations because I expect my toy to get bounced around a bit and I'll have to sort that out, but this is a simple first start.
I wish I could have found a post like this last night when I started my research, so hopefully you will find this of use, which is the purpose of this blog. Adruinos for Gumps.
Thanks for your code, I was looking for the same conversion. Did you add any more code for calibration?
ReplyDeleteThanks for sharing, This was useful in my Corona project.
ReplyDeleteHere is the Lua code if anyone needs it http://simon.fearby.com/blog/?p=2134
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
DeleteI wish I could have found a post like this a little before and I found. Thank you!!
ReplyDeleteExcellent! Thanks very much. Anyone got a nice low pass filter snippet?
ReplyDeleteExcellent! Thanks very much. Anyone got a nice low pass filter snippet?
ReplyDeletetht really helped bro. thnxx
ReplyDelete
ReplyDeletedata conversion services are available and outsoured.
Data Conversion Services in India
ReplyDelete
ReplyDeleteBest Web Development Services are Available at reduced cost.
If you think your spouse is cheating, and you need to hire a real hacker to remotely monitor / hack their phone, recover your stolen bitcoin / any other cryptocurrency, or hack a database with guaranteed privacy, contact easybinarysolutions@gmail.com or whatsapp: +1 3478577580, they are efficient and confidential.
ReplyDelete