profmason.com

April 13, 2013

CM900: Using a robotis Gyro

Filed under: Daily — profmason @ 6:28 am

The robotis Gyro is extremely straight forward to use as it outputs 2x Analog Outputs centered at ~1.23V

Taking advantage of the 12bit ADC in the CM-9xx, the Robotis Gyro is interfaced as follows:

X – ADC1
Y – ADC2
Vcc – +5
Gnd – Gnd

The gyro is based on the STMicro LPR530A MEMS gyro. and has two axis as shown in the image from the documentation shown below:

The sensor has a range of 300 degree/second per volt which translates to 800 ADC counts per 300 degree/seconds or a resolution of 0.375 degree/seconds per count.

/*
Read Data from a Robotis Gyro
 Connect:
 X - ADC1
 Y - ADC2
 Vcc - +5
 Gnd - Gnd

 */

// These constants won't change.  They're used to give names
// to the pins used:

#define xInPin  1 // Analog input pins that the gyro is attached to
#define yInPin  2
#define xZeroValue 1545
#define yZeroValue 1548
int xGyroRaw, yGyroRaw= 0;
int xGyro, yGyro =0;
long xGyroIntegrated, yGyroIntegrated = 0;
short counter = 0;
short rotationThreshold = 20;

void setup() {
  // Configure the ADC pins
  pinMode(xInPin, INPUT_ANALOG);
  pinMode(yInPin, INPUT_ANALOG);
}

void readAngularRates()
{
  //Measure the raw gyro vlaues and calculate the scaled values
  xGyroRaw = analogRead(xInPin);
  yGyroRaw = analogRead(yInPin);
  xGyro = xGyroRaw - xZeroValue;
  yGyro = yGyroRaw - yZeroValue;
}

void IntegrateGyro()
{
  if (xGyro >= rotationThreshold || xGyro <= -rotationThreshold) {
    xGyroIntegrated += xGyro;
  }
  if (yGyro >= rotationThreshold || yGyro <= -rotationThreshold) {
    yGyroIntegrated += yGyro;
  }
}

void displayRawRates()
{
  // print the Raw Gyro Rates results to the serial monitor:
  SerialUSB.print("x = " );
  SerialUSB.print(xGyroRaw);
  SerialUSB.print(" y = " );
  SerialUSB.println(yGyroRaw);
}

void displayScaledRates()
{
  // print the Scaled Gyro results to the serial monitor:
  SerialUSB.print("x = " );
  SerialUSB.print(xGyro);
  SerialUSB.print(" y = " );
  SerialUSB.println(yGyro);
}

void displayIntegratedAngles()
{
  // print the Integrated Angle result to the serial monitor:
  SerialUSB.print("x = " );
  SerialUSB.print(xGyroIntegrated);
  SerialUSB.print(" y = " );
  SerialUSB.println(yGyroIntegrated);
}

void loop() {
  counter ++;
  readAngularRates();// read the gyro and refresh the variables
  IntegrateGyro(); //Integrate the values
  if (counter==10000)
  {
    counter = 0;
    displayScaledRates();
    displayIntegratedAngles();
  }
}

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress