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>
const int MPU_addr=0x68;
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.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,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);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
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?
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
...
...
...
Thank you everyone ![]()