el codigo es el siguiente
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;
int16_t ax, ay, az;
float angleYZ, angleXZ, angletmp, angletmp1, angleXZ1;
int16_t gx, gy, gz;
#define LED_PIN 13
#define porcent 50
bool blinkState = false;
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
Wire.begin();
// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(115200);
// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
// configure Arduino LED for
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
// read raw accel/gyro measurements from device
angletmp=0;
angletmp1=1;
while(true){
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
ax = map(ax, -3800, 3800, -500 ,500);
ay = map(ay, -3800, 3800, -500, 500);
az = map(az, -3800, 3800, -500, 500);
angleYZ = atan((double)ay / (double)az);
angleYZ = angleYZ*(57.2958)*10;
angleXZ = atan((double)ax / (double)az);
angleXZ = angleXZ*(57.2958)*10;
//if(angletmp1 != 0){
/*for (int i=1; i <= 20; i++){
delayMicroseconds(50);
//angletmp = angletmp + angleXZ;
angleXZ = angleXZ + angleXZ1;
//angletmp1 = angleXZ`
}
angleXZ = angleXZ /20;
//} */
if(angleXZ >= (angletmp1 +20))
{
if(angleXZ >= (angletmp1 +30))
{
detras(100);
//angletmp1=angletmp1-0.1;
}else{
detras(30);
}}
else if(angleXZ <= (angletmp1 -20))
{
if(angleXZ <= (angletmp1 -30)){
delante(100);
//angletmp1=angletmp1+0.1;
}else{ delante(30);
}}
Serial.print("a/g:\t");
// Serial.print(ay); Serial.print("\t");
Serial.print(angletmp1); Serial.print("\t");
Serial.println(angleXZ); Serial.print("\t");
}}
void delante(int speed)
{
if(speed=100)
{
digitalWrite(11,HIGH);
}else{
analogWrite(11,speed);
}
digitalWrite(12, LOW);
delay(2);
Serial.print("delante\t");
}
void detras(int speed)
{
if(speed=100)
{
digitalWrite(12,HIGH);
}else{
analogWrite(12, speed);
}
digitalWrite(11,LOW);
delay(2);
Serial.print("detras\t");
}
como se puede apreciar el codigo eta lejos fde ser definitibo pero si el hardware no acompaña esta dificil, la alimentacion de los motores la e echo tanto con baterias totalmente independientes con optoacopladores de por medio como directo no dispongo de esquemas y los drivers usados son los mencionados anteriormente uno es el l298n modulo para arduino y el otro con iguales resultados es el l6203. no poseo esquemas pero ambos estan testeados y funcionan correctamente