Pages: [1]   Go Down
Author Topic: Gyro trouble  (Read 954 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With the code from this site http://arduino.cc/playground/Main/Gyro it really doesn't work. I have it hooked up to a servo and the servo barely rotates, and when it does it only rotates one way. Of course thats with the code just copied directly from the site (which i know I shouldn't do). I did get it working my self when I wrote the code my self (which was exactly the same), I was still copying the code for means of understanding it. But I still had a problem. the servo would rotate in one direction more the other, which is annoying. I narrowed it down to the drift. Although the drift doesn't rotate the servo while the gyro is stationary. I suspect its adding "weight" to one direction when I turn the Gyro, so in the end when I turn it around an back again the servo wouldn't rotate back to same place as before.

Heres the code from the site with the Servo implemented:
Code:
/* Keep track of gyro angle over time
 * Connect Gyro to Analog Pin 0
 *
 * Sketch by eric barch / ericbarch.com
 * v. 0.1 - simple serial output
 *
 */

#define GYROY A0
#include <Servo.h>

Servo myServo;

int gyroPin = A0;               //Gyro is connected to analog pin 0
float gyroVoltage = 5;         //Gyro is running at 5V
float gyroZeroVoltage = 1.35;   //Gyro is zeroed at 2.5V
float gyroSensitivity = .002;  //Our example gyro is 7mV/deg/sec
float rotationThreshold = 1.0;   //Minimum deg/sec to keep track of - helps with gyro drifting

float currentAngle = 180 / 2;          //Keep track of our current angle

void setup() {
  myServo.attach(11);
 
  Serial.begin (9600);
}

void loop() {
  //This line converts the 0-1023 signal to 0-5V
  float gyroRate = (analogRead(GYROY) * gyroVoltage) / 1023;
  Serial.println(analogRead(GYROY));

  //This line finds the voltage offset from sitting still
  gyroRate -= gyroZeroVoltage;

  //This line divides the voltage we found by the gyro's sensitivity
  gyroRate /= gyroSensitivity;

  //Ignore the gyro if our angular velocity does not meet our threshold
  if (gyroRate >= rotationThreshold || gyroRate <= -rotationThreshold) {
    //This line divides the value by 100 since we are running in a 10ms loop (1000ms/10ms)
    gyroRate /= 100;
    currentAngle += gyroRate;
  }

  //Keep our angle between 0-359 degrees
  if (currentAngle < 0)
    currentAngle += 180;
  else if (currentAngle > 179)
    currentAngle -= 180;

  myServo.write(currentAngle);
 
  //DEBUG
  Serial.println(gyroRate);

  delay(10);
}

and heres my code that I wrote that mimiks the first one

Code:
#define GYROX A0
#define GYROY A1
//#define VREF A2

#include <Servo.h>

int x, y;  // X and Y in-plane sensing

float voltage = 5.0;
float zeroVoltage = 1.35;
float gyroSensitivity = .002;
float rotationThreshold = 1.0;

float currentAngle = 180 / 2;
float previousAngle = 0;

Servo myServo;

void setup()
{
  myServo.attach(11);
  Serial.begin(9600);      // sets the serial port to 9600
}

void loop()
{
 
  float gyroRate = (analogRead(GYROY) * voltage) / 1023.0; 
 
  gyroRate -= zeroVoltage;

  gyroRate /= gyroSensitivity;
 
  if(gyroRate >= rotationThreshold || gyroRate <= -rotationThreshold)
  {
    gyroRate /= 100;
    currentAngle += gyroRate;
  }
 
  if (currentAngle < 0)
  {
    currentAngle += 180;
  }
  else if (currentAngle > 180)
  {
    currentAngle -= 180;
  }
 
  myServo.write(currentAngle);
  Serial.println(currentAngle);
 
  //delay(10);
 
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11718
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Firstly which gyro are you using?  What is its supply voltage?  The code you present is clearly wrong since it claims the zero-voltage is 1.35V yet the supply is 5V.  The zero-voltage should be half the supply.

Secondly you've commented out the delay(10) which is essential to the integration loop.
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The gyro i'm using is a IDG500 on IMU combo board http://www.sparkfun.com/products/9268
1.35 is the zero voltage on the datasheet. And the IMU us hooked up to the 3.3 volt on the Arduino because if I were to put on 5 volt it would frie.
But then i would change the voltage to 3.3 in the code and it wouldn't work. So yeah, i;m probably missing something
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11718
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Gosh, a non-ratiometric sensor, hadn't expected that.  OK, looking at that code some more I notice the gyroVoltage value is actually the ADC reference voltage - are you not using 3V3 for AREF then?

There is a rotationThreshold value; set this to zero, that's the main bug in the code.  You can help the accuracy my using BlinkWithoutDelay technique rather than the delay(10) call, since all that floating point will be taking a noticeable amount of time in the loop.
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

does any of you here knows how to interface rc hobby gyro like futaba gy401 with arduino? how do i make it work with servo? anybody knows it? i had search it on the internet but all those things come out only attaching the rc hobby gyro to the rc receiver. cant i do it without rc receiver??
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

linda89, the GY401 is ment to control a plane, it is not a sensor for a microcontroller.
You need to read it's output and process that trying to understand what the GY401 is doing.
If you want to have gyro information, perhaps you can buy a gyro sensor, and use that for the Arduino.
Logged

Pages: [1]   Go Up
Jump to: