Offline
Newbie
Karma: 0
Posts: 6
|
 |
« on: January 06, 2013, 01:53:21 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1406
May all of your blinks be without delay
|
 |
« Reply #1 on: January 06, 2013, 02:38:54 am » |
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 ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #2 on: January 06, 2013, 03:35:34 am » |
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1406
May all of your blinks be without delay
|
 |
« Reply #3 on: January 06, 2013, 03:38:16 am » |
To be more explicit
You said "code that i can use to fit into what i have got "
What code have you got ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #4 on: January 06, 2013, 03:52:58 am » |
just the example that came with the gyro =============================================== */
// 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); }
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1406
May all of your blinks be without delay
|
 |
« Reply #5 on: January 06, 2013, 04:00:51 am » |
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 ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #6 on: January 06, 2013, 04:08:17 am » |
the (ax) and (gx) can be a + or - value
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1406
May all of your blinks be without delay
|
 |
« Reply #7 on: January 06, 2013, 04:29:21 am » |
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 ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #8 on: January 06, 2013, 05:09:42 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1406
May all of your blinks be without delay
|
 |
« Reply #9 on: January 06, 2013, 05:58:38 am » |
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 : 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 ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #10 on: January 06, 2013, 06:20:02 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1406
May all of your blinks be without delay
|
 |
« Reply #11 on: January 06, 2013, 07:52:11 am » |
So, you do need to read a value then read it again later to see if it has changed. Some more pseudo-code 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.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6362
-
|
 |
« Reply #12 on: January 06, 2013, 08:34:25 am » |
i want to be able to measure all of the changes in angle so to be able to return to a defined angle
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.
|
|
|
|
|
Logged
|
|
|
|
|
|