Hi,
only 2 beginner questions:
- what is the "square" function? - no Arduino language manual mentions it
- what is the "Math" library good for?
Example :
angle_x = abs(atan2(ax, sqrt(square(ay) + square(az)))/(pi/180));
thanks
Hi,
only 2 beginner questions:
Example :
angle_x = abs(atan2(ax, sqrt(square(ay) + square(az)))/(pi/180));
thanks
Where have you seen it used ?
As to the math library, see C math (math.h) Library Reference
I use sq()
as per https://docs.arduino.cc/language-reference/en/functions/math/sq/
for using MPU6050 :
link:
square (x) = x * x
yes, I know, but where did the author get the square function?
If I change "square" to "sq" the program doesn't work!? Mystery
ask the author.
Thanks, very good advice.
Apparently this is just a made-up function. It works and no one knows about it!
Probably a #define somewhere.
Can be rewritten as
angle_x = abs(atan2(ax, sqrt(ay*ay + az*az))/(pi/180));
The result is that the official function "square" does not exist?
this code works (limited in scope):
#include "Wire.h"
#include "Math.h"
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;
const float pi = 3.141592;
const int counter_w = 100;
int16_t ax, ay, az;
float x, y, z;
int counter;
float angle_x, angle_y, angle_z, _angle_x, _angle_y, _angle_z;
long ax_p, ay_p, az_p;
void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println("Spousteni zarizeni MPU6050");
accelgyro.initialize();
Serial.println("Test zarizeni");
Serial.println(accelgyro.testConnection() ? " " : "Zarizeni se nepodarilo pripojit");
}
void loop()
{
// zjistà všechny hodnoty z akcelerometru
accelgyro.getAcceleration(&ax, &ay, &az);
// sÄŤĂtáme potĹ™ebnĂ˝ poÄŤet hodnot
ax_p = ax_p + ax;
ay_p = ay_p + ay;
az_p = az_p + az;
// pocitadlo mereni
counter++;
// az bude mereni counter_w (100), tak:
if (counter == counter_w)
{
//zjistĂme prĹŻmernĂ© hodnoty
x = ax_p/counter;
y = ay_p/counter;
z = az_p/counter;
// vypočteme sklon a náklon [°]
angle_x = atan2(x, sqrt(square(y) + square(z)))/(pi/180);
angle_y = atan2(y, sqrt(square(x) + square(z)))/(pi/180);
angle_z = atan2(z, sqrt(square(x) + square(y)))/(pi/180);
// vynulujeme hodnoty
counter = 0;
ax_p = 0;
ay_p = 0;
az_p = 0;
// hodnoty si vypĂšme do portu
Serial.print(angle_x); Serial.print("\t");
Serial.print(angle_y); Serial.print("\t");
Serial.println(angle_z);
}
}
This will "work", but the equations below don't match any standard definition for 3D rotation angles, so the results are not very useful.
angle_x = atan2(x, sqrt(square(y) + square(z)))/(pi/180);
angle_y = atan2(y, sqrt(square(x) + square(z)))/(pi/180);
angle_z = atan2(z, sqrt(square(x) + square(y)))/(pi/180);
Anything in, garbage out.
I think so too. The output values ​​are imprecise and limited, sometimes some "limit" is exceeded and then the output is a nonsense value.
I'm not a mathematician , other equation suggestion ?
Mathematically correct equations and code for pitch and roll, the two tilt angles that can be measured using an accelerometer, are given in this tutorial.
Instead of square you could also use pow()
to raise a number to the power of 2. https://docs.arduino.cc/language-reference/en/functions/math/pow/
Is just ay
|ay|
Arduino uses AVR LibC standard libraries including math.h
#include <math.h>
float squaref( float x )
double square( double x )
Arduino AVR, double is a float.
The Teensy 4.1 has an ARM with FPU, if you want fast and decently precise results. The Teensy 3.5 might also only a bit slower.
This is called "modular" code. Write it one time, use it any time.
When you use something often, you do not need to re-write it all the time. Write a "function" (like "square(x)" one time and keep that function in an "include" file for use at any time.
Your sketch doesn't even compile for me with an Uno R3 selected. The above header file does not exist. It does not appear to be a part of either the I2Cdev library, nor the MPU6050 library.
So where does it come from? Given that your sketch uses #include "file"
rather than #include <file>
the intent would seem to be include header files from your sketch directory rather than from the libraries directory (though that's not actually the way it works in practice).