#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
#define pin1 3
#define pin2 5
void setup(){
Serial.begin(9600);
Serial.println("Initialize MPU");
mpu.initialize();
//Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
}
void loop(){
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
ax = map(ax, -17000, 17000, -1500, 1500);
//Serial.println(ax);
if(ax > 0){
if(ax<255){
Serial.println(ax);
analogWrite(pin2,ax);
}
else{
Serial.println("+255");
analogWrite(pin2,255);
}
}
if(ax<0){
if(ax>-255){
Serial.println(ax);
analogWrite(pin1, ax-ax-ax);
}
else{
Serial.println("-255");
analogWrite(pin1, 255);
}
}
delay(1000);
}
My team is working on a project which requires me to control the inclination of a plane using 4 lead screw mechanisms mounted on 4 corners of the plane.we are going to use a MPU 6050 for feedback ,so as to stop the motors when a particular angle is reached , while searching about the topic I came across this code but really couldn't understand why some commands have been used there it would prove helpful for me if someone explained just a few parts of the code and get me going.
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
ax = map(ax, -17000, 17000, -1500, 1500);
What is the map function doing here ? and why is there a variable ax and rest all of it are constants?
if(ax > 0){
if(ax<255){
Serial.println(ax);
analogWrite(pin2,ax);
}
else{
Serial.println("+255");
analogWrite(pin2,255);
}
}
if(ax<0){
if(ax>-255){
Serial.println(ax);
analogWrite(pin1, ax-ax-ax);
}
else{
Serial.println("-255");
analogWrite(pin1, 255);
}
what does this logic code here do?