Go Down

Topic: totaling gyro reading (Read 817 times) previous topic - next topic

lkik

hey im sort of new to using arduino and i have a ic2 gyroscope as much as i try i cant seem to total the + and - values could some one give me a simple little example code that i can use to fit into what i have got i want to total up the + and - x value readings

UKHeliBob


hey im sort of new to using arduino and i have a ic2 gyroscope as much as i try i cant seem to total the + and - values could some one give me a simple little example code that i can use to fit into what i have got i want to total up the + and - x value readings

What have you got ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lkik



What have you got ?


arduino mega 2560
and
http://www.sainsmart.com/arduino-compatibles/module/accelerometer/sainsmart-mpu-6050-3-axis-gyroscope-module.html

UKHeliBob

To be more explicit

You said "code that i can use to fit into what i have got "

What code have you got ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lkik

just the example that came with the gyro



Code: [Select]
===============================================
*/

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#include "Wire.h"

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"

// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define LED_PIN 13
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(38400);

    // 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(LED_PIN, OUTPUT);
}

void loop() {
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    // these methods (and a few others) are also available
    //accelgyro.getAcceleration(&ax, &ay, &az);
    //accelgyro.getRotation(&gx, &gy, &gz);

    // display tab-separated accel/gyro x/y/z values
    Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
}

UKHeliBob

Which variables hold the + and - x values ?

There only seems to be one x value from the accelerometer (ax) and one from the gyro (gx).  Where do the 2 values that you wish to sum come from ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lkik

the (ax) and (gx) can be a + or - value

UKHeliBob

Are they the 2 values that you want to sum or maybe you read ax, remember it, read it again and sum the 2 values ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lkik

i want to be able to monitor the angle read by the gryo and use this to control motors to correct the angle
there is only one value that i can see

UKHeliBob

That was not what you originally asked.

Sounds like you need to read the angle of the gyro, then depending on whether the result is negative or positive then run the motor in the right direction to change the angle towards zero.

Is that right ?

NOTE - this is not real code but something like this perhaps :

Code: [Select]
read the angle
if it is zero do nothing
if it is positive then run the motor(s) forwards
if it is negative then run the motor(s) backwards
go back and read the angle again


In practice it will be more complicated.  How long do you run the motor(s) and how fast, for instance ?
Have you got the code to run the motor(s) in either direction ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lkik

the gryo is reading approx -284 all the time even when its not rotating
i want to be able to measure all of the changes in angle so to be able to return to a defined angle
just turning a motor on when the gryo reads high or low wont work because the gryo measures the change in angle

UKHeliBob

So, you do need to read a value then read it again later to see if it has changed.

Some more pseudo-code

Code: [Select]
START
read the angle and remember it.  Lets call it oldAngle

LOOP
wait
read the angle.  Lets call it newAngle
subtract newAngle from oldAngle.  Lets call the answer deltaAngle
if deltaAngle is zero do nothing
if deltaAngle is positive then run the motor(s) forwards
if deltaAngle is negative then run the motor(s) backwards
copy newAngle to oldAngle
start again at LOOP


NOTE - I am NOT suggesting that you use a goto command !!!!!!

Does the gyro reading change when you rotate it ?  If not then something is wrong.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PeterH


i want to be able to measure all of the changes in angle so to be able to return to a defined angle


Code: [Select]

    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    // these methods (and a few others) are also available
    //accelgyro.getAcceleration(&ax, &ay, &az);
    //accelgyro.getRotation(&gx, &gy, &gz);

I'm not sure what the other APIs give you but I suggest you investigate them. What you're trying to do doesn't seem unusual (or difficult to implement) and you may well find that the people who implemented the gyro library have done the work for you.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up