# Need and explanation with MTU-6050 and angels

Ok. So, total noob here. Just wanted to try something with my new gyro and accelerometer MTU-6050. So I found this code:

``````#include<Wire.h>

int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

int minVal=265;
int maxVal=402;

double x;
double y;
double z;

void setup(){
Wire.begin();
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.write(0x3B);
Wire.endTransmission(false);

int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);

Serial.print("AngleX= "); Serial.println(x);

Serial.print("AngleY= "); Serial.println(y);

Serial.print("AngleZ= "); Serial.println(z);

Serial.println("-----------------------------------------"); delay(400); }
``````

But I have no idea, what the hell does it do and how does it even work. What are the 265 and 402 values?
So, to my main question. Can you guys please explain to me (total noob, so explain like you would do it to a child) how does it work and what do these lines in particular mean?

``````...
...
...
int minVal=265; //(why this numbers?)
int maxVal=402; //(why this numbers?)

double x; //?
double y; //?
double z; //?
...
...
...
int xAng = map(AcX,minVal,maxVal,-90,90); // re-map. but why is it minVal and maxVal when AcX is way bigger and why from -90 to 90?

int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);

x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);  // some math, can you explain it guys?
...
...
...
``````

Thank you everyone

Strat0s: But I have no idea, what the hell does it do and how does it even work.

Does it do what you want it to do? Does it do anything?

int minVal=265; //(why this numbers?) int maxVal=402; //(why this numbers?)

Ask whoever you found this code from. Or maybe there's something written at where you found the code that explains it.

``````double x; //?
double y; //?
double z; //?
``````

Standard variable declarations. Go read some C++ tutorials, as this is very basic and there should be no need to question what it's doing.

what the hell does it do and how does it even work.

Why did you even buy it, given that you have no idea what it does?

Strat0s: Ok. So, total noob here. Just wanted to try something with my new gyro and accelerometer MTU-6050. So I found this code:

``````x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);  // some math, can you explain it guys?
...
...
...
``````

Thank you everyone :)

The tilt angle is being found by using the acceleration values. You will find that the tilt angle derived from acceleration values to be a bit erratic as the accelerometers are sensitive. Do not get rid of that formula, you'll want to have it as you explore a way to derive a more stable tilt angle reading.

The gyros should be called rate gyros. The gyros are going to give you the rate at which they are precessing. Rate gyro info and accelerometer tilt angle can be used in a low pass complimentary filter to give more stable angle readings.

The first thing to get down pat will be to write code to calibrate your gyro and accelerometers. I calibrate for drift rate and angle offsets. I have the calibration routine run at midnight every night.

After figuring out how to calibrate the MPU unit, you may want to learn about writing to and reading from the MPU registers. You may find that the MPU's default settings are a bit to sensitive for your needs, I did. Reducing the sensitivity of the device gives a larger range of measurement, which I find more use full. Also, the default setting of the clock is free running, setting the clock to one of the gyros PLL's is more stable.

If you change the sampling rate you will need to adjust the scale factors being used.

One of the things I found that makes a difference in stability and accuracy is to apply calibration for the accelerometers during scale factor calculations and applying calibration data to the gyros during the complimentary / filter operations.

You'll want to become familiar with the MPU register datasheet https://www.invensense.com/wp-content/uploads/2015/02/MPU-6500-Register-Map2.pdf to get the most out of the MPU and to have many of your questions answered.

wvmarle: Does it do what you want it to do? Does it do anything?

Ask whoever you found this code from. Or maybe there's something written at where you found the code that explains it.

Standard variable declarations. Go read some C++ tutorials, as this is very basic and there should be no need to question what it's doing.

jremington: Why did you even buy it, given that you have no idea what it does?

The code works. And I know how does it work, I have mainly no idea why the hell are those 265 and 402 variables... That's about it.

Also I know how to read and write to the registers.

why the hell are those 265 and 402 variables

Ask the person who wrote the code.

That scaling of the accelerometer values seems bogus. atan2() uses the ratio of values to get the angle (like all trigonometry), so no scaling is needed.

I like the idea of complimentary filter, Idahowalker, but its actually a complementary filter, as the two kinds of readings complement each other. :)

MarkT: That scaling of the accelerometer values seems bogus. atan2() uses the ratio of values to get the angle (like all trigonometry), so no scaling is needed.

I like the idea of complimentary filter, Idahowalker, but its actually a complementary filter, as the two kinds of readings complement each other. :)

The scale factor I wrote of comes from pages 29 and 31 of the MPU6X00 register data sheet. Accelermeter and gyro data from the registers need to be scaled before use. Also, before use the data from the mpu60X00 needs to be have a 2’s complement value.

And I knew I had a 50/50 chance of getting it correct " complementary filter."