Program Guidance for Autonomous Vehicle, sort of in a hurry

Hello! I am a high schooler that bit off a little more than he could chew for his project. I am almost through, but I have no idea about my programs.

I am using a MPU 6050 and a HMC5883L with the I2Cdev Libraries. I have used them to calculate data in the Serial Monitor. Now, however, I am implementing them into an RC truck. These programs will control only the servo for now.

I know these programs won't be accurate; that's what I'm looking for! I am testing sensor accuracy.

Could you guys please help me review my programs? (I got another, simpler one to work, so now it's only these two)

MPU 6050 code:

#include "I2Cdev.h"
#include "MPU6050.h"
#include <Servo.h>

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif

Servo myservo;

int currentGravities;

int xFinal;

MPU6050 accelgyro;

int ax, ay, az;

long previousMillis = 0;            // variable to store last time it was updated

long startTime ;                    // start time for stop watch

long elapsedTime ;                  // elapsed time for stop watch

int fractional;                     // variable used to store fractional part of time

void setup() {
  // put your setup code here, to run once:
    myservo.attach(9);
    myservo.write(90);

    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif
    accelgyro.initialize();
}

void loop() {
  // put your main code here, to run repeatedly:
  accelgyro.getAcceleration(&ax, &ay, &az);

  currentGravities = (ax)/16384;

  startTime = (millis);

  xFinal = 0.5*(currentGravities)*(startTime*startTime);

  
}

That one ^ takes in sensor data, converts it into g's, and figures out the final position in meters using time^2 since starting the loop. I am going to use it to turn a servo using an if statement (if xFinal > (this position), turn servo)

How will I be able to turn the servo at different times during the program? Will the variable xFinal continue to update itself? How do I restart the timer (if it even works) after turning the servo?

This is my HMC5883L code: ( I have tested some of it but not all yet)

#include <Wire.h>
#include <Servo.h>

#define Addr 0x1E

Servo myservo;

void setup() {
  // put your setup code here, to run once:
  myservo.attach(9);
  myservo.write(90);
  
  // Set operating mode to continuous
  Wire.beginTransmission(Addr); 
  Wire.write(byte(0x02));
  Wire.write(byte(0x00));
  Wire.endTransmission();
}

void loop() {
  // put your main code here, to run repeatedly:
   int x, y, z;
   Wire.beginTransmission(Addr);
  Wire.write(byte(0x03));       // Send request to X MSB register
  Wire.endTransmission();

  Wire.requestFrom(Addr, 6);    // Request 6 bytes; 2 bytes per axis
  if(Wire.available() >=6) {    // If 6 bytes available
    x = Wire.read() << 8 | Wire.read();
    z = Wire.read() << 8 | Wire.read();
    y = Wire.read() << 8 | Wire.read();
  }
  // If compass module lies flat on the ground with no tilt,
  // just x and y are needed for calculation
  float heading=atan2(x, y)/0.0174532925;
  if(heading < 0) heading+=360;
  heading=360-heading; // N=0/360, E=90, S=180, W=270
  Serial.println(heading);  
  delay(500);

  // Drive Section
  
  if (heading < 180) {
    myservo.write(110);
    if (heading < 25 or heading > 335){
      myservo.write(90);
} else if (heading > 180) {
    myservo.write(80);
    if (heading < 25 or heading > 335){
      myservo.write(90);
    }
  }
}

  delay(3000);
}

Keep in mind, the section comment labeled Drive Section has the vehicle moving forward from the start of the program, ensuring that it will actually change heading after turning the servo.

Thank you for any help, I'm lost and pretty stressed that I won't get this done.

That one ^ takes in sensor data, converts it into g's, and figures out the final position in meters using time^2 since starting the loop.

See this overview for why that idea won't work in general, but your specific approach is also completely incorrect.

The (1/2)gt^2 formula is applicable only to motion in one dimension under constant acceleration.

I would only need to measure forward moving g's, so the single dimension calculation is ok. As for the accuracy, it's only going to give me better results! Will the program idea work though? It's only moving in straight lines.

How will you achieve a constant acceleration?

Will the program idea work though?

No. That will be obvious as soon as you try it.

:confused: What exactly should I do? I'm not sure how else to test the accuracy...

Hi,

  • Put down in point form what you want your autonomous vehicle to do?
  • Tell us the hardware that you have

Can you tell us your electronics, programming, Arduino, hardware experience?

Thanks... Tom.. :slight_smile:

I am testing sensor accuracy.

Does the sensor measure "g" (the acceleration due to Earth's gravity) accurately when it is held still?

Ok, so I want the vehicle to drive a certain distance in a straight line according to an accelerometer value.

Yes, it will give me exactly one g when holding still for the z axis.

Thanks for the help, people seemed to have given up on me!

jremington:
See this overview for why that idea won't work in general, but your specific approach is also completely incorrect.

The (1/2)gt^2 formula is applicable only to motion in one dimension under constant acceleration.

I love that page. Even the most accurate sensor gets errors of kilometers within a minute or two and that's just sitting stationary on the desk!

Think about that desk sitting inside a dragster car doing a 7-second pass. That's an incredibly violent motion and rather obvious that small accelerations on the desk will be difficult to measure. But that's only 1G acceleration.

I know it won't be accurate; that's what I'm testing! :slight_smile:

I just want to know if the general idea of the program will work...

Accurate? Kilometers error is not even in the same ballpark.

Encoders on the wheels will work much better.

Yeah, I figured that out about the encoders.

In theory, will the programs work?

In theory, will the programs work?

See reply #1