Can someone please explain why servos led by gyroscope shift back?

//The Libraries:
#include <Wire.h>;
#include <Servo.h>;

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define gyropin A4
//Kerjigger sends from 4th apin



//The Components:
Servo xServo; //attaches swingyarm1
Servo yServo; //attaches swingyarm2
int val; //byte val
int Addr = 105;//gyro address
int x, y, z;   //ints
int prevx, prevy, prevz; //previous gyrovals x,y,z


void setup() {
  xServo.attach(10);
  yServo.attach(9);

  Wire.begin();
  Serial.begin(9600); 
  writeI2C(CTRL_REG1, 0x1F); //Turns on the kerjigger, disable power down. 
  writeI2C(CTRL_REG3, 0x08); //Enable Contral Ready signal for the kerjigger
  writeI2C(CTRL_REG4, 0x80); //Set scale (500 deg/sec) of the kerjigger
  delay(20); // syncronise the kerjigger
  
}
  
void loop() {
  getGyroValues();           //What does it read on x, y, z. 
  Serial.print("X:"); Serial.print(x/114);                 
  Serial.print("Y:"); Serial.print(y/114);                 
  Serial.print("Z:"); Serial.print(z/114);
  if(x!=prevx)
  {
    xServo.write(x);
  }
 
  if(y!=prevy)
  {
  yServo.write(y);
  }
  
delay(1500);
 prevx = x;
 prevy = y;

}

void getGyroValues() {     
  byte MSB, LSB;
  
  MSB = readI2C(0x29);
  LSB = readI2C(0x28);
  x = ((MSB << 8) | LSB);
  
  MSB = readI2C(0x2B);
  LSB = readI2C(0x2A);
  y = ((MSB << 8) | LSB);
  
  MSB = readI2C(0x2D);
  LSB = readI2C(0x2C);
  z = ((MSB << 8) | LSB);
}

int readI2C(byte regAddr) {   //asks to read information from kerjigger into arduino
  Wire.beginTransmission(Addr); //begin wire transmission to addr
  Wire.write(regAddr);  // register addr as correct shipping address
  Wire.endTransmission(); //terminate asking about address; boring
  Wire.requestFrom(Addr, 1); //ask for letter from your friend, kerjigger
  while(!Wire.available()) {}; //wait for the post
  return(Wire.read()); //letter attained, friendship levels up
}



void writeI2C (byte regAddr, byte val) {
  Wire.beginTransmission(Addr);
  Wire.write(regAddr);
  Wire.write(val);
  Wire.endTransmission();
}

What I’ve noticed through the serial monitor is that when the gyroscope is at a new angle, it will rewrite the x, y, z, values to 0, and the servos will shift back to their ambiguous “zero position”. Is that what my problem is? The gyroscope moves, the servos adjust, the monitor pings they’re 0, 0, and the servos attempt to center out again and after the delay head to their next position.

This is the first thing I’ve done with coding, arduino or otherwise.
An accelerometer/gyro/mag arrived today and, if I can get time, I’ll be playing with that instead of the Parallax L2g4200D I’m using now. Running an uno and 2 Hi0tec HS0322hd r/c servos.

Thanks for your help folks.

Links to the hardware are far more useful than descriptions. Perhaps your unspecified gyroscope is only reporting changes in attitude, and you are expected to integrate them to determine an actual orientation. It's hard to say without knowing which gyroscope you have.

It's a 3-axis Parallax L2g4200D.

It's a 3-axis Parallax L2g4200D.

That link doesn't work.

Sorry! L3g4200D. http://learn.parallax.com/KickStart/27911 Can't believe I did that twice.

OK. So, the values that the gyroscope provides ARE instantaneous values. They show how much the gyroscope IS moving, not how much it HAS moved.

You need to keep track of how much the device has moved, by integrating the "IS moving" values over time, if the "HAS moved" values are what you are interested in.

What are you using the gyroscope to do? It seems that you want to move some servos incrementally, not absolutely, based on the gyroscope readings. You are currently using the gyroscope reading as a absolute value for the servo positions.

Honestly I'm just playing with things to see what I can make the servos do as a learning experience. I don't want to start getting into more complicated hardware before learning basic manipulation and reading controls. Figured a rotating mechanism and information source would be an accessible place to start. Though I must admit, I don't understand the 0x2B stuff yet since I don't know what it relates to. I'm terribly excited to learn about that.

Thanks for the help.

If I understand correctly, if I store an array of previous values from the gyroscope, and do the math to tack on or remove the differences of the readout as it relates to the physically assumed/expected position of the servo over time, I can give it the mathematics to reach the new position instead of zeroing out, as long as I give the servos an initial value to work from and tell them that the initial gyroscope value, and then the most current saved, is considered the midpoint of the motion within the bounds of the physical limitations of the servo's remaining movement ability.

That's awesome.

Nearly all gyroscope types are "rate gyros", they produce a signal proportional to the rate of rotation only. MEMS gyros are all rate-gyros - this is why the output is spec'd in volts/(degree/second)

To approximate current absolute rotation then you have to integrate the output. In the digital domain that means summing the output to an accumulator variable at a fixed regular sample clock, perhaps 100Hz or so. This accumulator value is then scaled to degrees or radians or whatever you want.

To do this for one axis is easy, to do it for three requires a DCM or quarternion algorithm to handle 3-D rotations (there is no short-cut to this, 3D rotations are not simple).

The thing to do is find a library that does this for you, unless you like 3D vector maths.

I would like to learn about the mathematics. No point in not understanding every part of what I'm working with, and mathematics is something I love figuring out.

Quaternions look like they'd be fun.

MarkT: To approximate current absolute rotation then you have to integrate the output. In the digital domain that means summing the output to an accumulator variable at a fixed regular sample clock, perhaps 100Hz or so. This accumulator value is then scaled to degrees or radians or whatever you want.

Does this need to be qualified that the rate is constant or does the gyroscope give you information to account for rate changes?

Some DCM info here:

http://gentlenav.googlecode.com/files/DCMDraft2.pdf