Page 6 of 8

Re: hitechnic Gyro sensor (NGY1044) noise

Posted: 07 Jul 2011, 22:30
by linusa
mattallen37 wrote:You should also note, that some "drift" is actually very correct. The earth rotates 360*/24 hours (once a day). That means that you should see a "drift" of 1 degree every 4 minutes. However, if the axis of the gyro is perfectly parallel to the axis of the earth, you should see no "drift".
I've thought about this before (and the good old coriolis effect), but I'm think I don't agree with you in detail.

If you measure angular position by integrating angular velocity, then yes: In the end, you should be off by 1 degree for every 4 minutes you integrated the velocity (as you "accidentally" included the earth's rotation into your final result).

On the other hand, if you go ahead and measure the offset of the gyro by taking some samples and averaging over them (statistical noise), than this offset shouldn't change. The earth's rotation will be caught all the time, and since it's a constant, it should cancel out.
If you place the gyro differently the 2nd time, you might get the "reverse direction".

Still, the offset is measured in degrees/second, and 1 deg/4min = 1deg/240sec = 0.0041... (times two for the worst case)...

We've had a bachelor thesis about integrating the gyro once (for position), and the same problems came up IIRC. I can only repeat: It's not good to take too many samples of the gyro in a tight loop for offset calculation, it somehow needs to "relax a bit".

Re: hitechnic Gyro sensor (NGY1044) noise

Posted: 08 Jul 2011, 00:22
by mattallen37
Linus, good points you bring out. I didn't really mean for it to have anything to do specifically with the HT Gyro, but more for an absolute position gyro.

BTW, is there any simple (or pre-made) NXC solution to convert from the rotation speed measured by the HT Gyro, into an absolute position? I don't really need anything to precise, but it would be interesting to examine some NXC code.

Re: hitechnic Gyro sensor (NGY1044) noise

Posted: 08 Jul 2011, 00:51
by linusa
mattallen37 wrote:Linus, good points you bring out. I didn't really mean for it to have anything to do specifically with the HT Gyro, but more for an absolute position gyro.
Yeah, I understood that, but these things should apply to all gyros. As long as gyro means "measuring angular velocity" (and not angular position aka absolute angle -- such a device would have to be called "rotation sensor" or "tilt sensor", for example).
mattallen37 wrote: BTW, is there any simple (or pre-made) NXC solution to convert from the rotation speed measured by the HT Gyro, into an absolute position? I don't really need anything to precise, but it would be interesting to examine some NXC code.
Well, x = v*t, and phi = omega * t.
Or, to be more exact (theoretical physics and all that):

Code: Select all

phi = integral(omega(t) dt)
with omega(t) = angular velocity (the thing that the gyro measures, sorry for stating the obvious).

So some simple numeric integration:

Code: Select all


float phi = 0;
int lastTicks = GetTickCount();
int curTicks;
while(true) { 

  // convert omega to unit "degrees per ms" here.
  float omega = GetGyro(sensorPort) / 1000.0;
  curTicks = GetTickCount();
  int deltaTTicks =  curTicks - lastTicks;
  phi += omega * deltaTTicks;
  lastTicks = curTicks;

  // use phi here...
}//end while
If this pseudo-NXC-code above is right (I hope so), it should do simple numeric integration. Note how it only uses GetTickCount() once per iteration to get maximum accuracy (change the name of the function if it's called differently in NXC, this is out of my head). Also you either have to convert the angular velocity to degrees per millisecond, or the ticks to seconds... Please check the code for yourself to verify I didn't make any mistakes...

Re: hitechnic Gyro sensor (NGY1044) noise

Posted: 08 Jul 2011, 02:31
by mattallen37
Cool, thanks for all that info.
linusa wrote:If this pseudo-NXC-code above is right (I hope so), it should do simple numeric integration. Note how it only uses GetTickCount() once per iteration to get maximum accuracy (change the name of the function if it's called differently in NXC, this is out of my head). Also you either have to convert the angular velocity to degrees per millisecond, or the ticks to seconds... Please check the code for yourself to verify I didn't make any mistakes...
Well, even if it isn't right, it does what I wanted :D

CurrentTick is what I used.

It seems to work perfectly, thanks a lot.

Here is the working NXC code I made out of your pseudo NXC code:

Code: Select all

#define offset 1.72

float phi = 0;
int lastTicks;
int curTicks;

task main(){
  SetSensorHTGyro(S2);
  lastTicks = CurrentTick();
  while(true){

    // convert omega to unit "degrees per ms" here.
    float omega = (SensorHTGyro(S2) - offset) / 1000.0;
    curTicks = CurrentTick();
    int deltaTTicks = curTicks - lastTicks;
    phi += omega * deltaTTicks;
    lastTicks = curTicks;

    ClearScreen();
    NumOut(0, LCD_LINE1, SensorHTGyro(S2));
    NumOut(0, LCD_LINE2, omega);
    NumOut(0, LCD_LINE3, deltaTTicks);
    NumOut(0, LCD_LINE4, phi);
    Wait(20);
   // use phi here...
  }//end while
}
The Gyro Offset being 1.72 is for my gyro (the one I am using now), and seems to work very well. I left the sensor stationary, and phi drifted 20 degrees in about 5 minutes. Thanks a lot for all the information and code.

Now, how would I integrate a compass into the equation for better horizontal absolute position? What about an accel sensor for other axis?

Re: hitechnic Gyro sensor (NGY1044) noise

Posted: 08 Jul 2011, 03:21
by linusa
Great, really cool that it works :-)

I just noticed that you're doing the slow LCD painting and a Wait(20) inside the polling loop. During this time you don't get any "rotation information", so if the gyro moves, this movement is lost to you. You should consider doing multi-tasking, and just have one loop that reads and updates phi as often as possible. On the other hand, not too often (or the gyro might get unstable or whater it is I do vaguely remember). It could be this: if you poll more often than the firmware updates its registers (I think that might be every 3ms for analog sensors), then you get "double" values, and if you integrate them, that's biased.

So, for a better test, introduce this extra polling tast, but keep a Wait(4) inside the loop for safety...

As to your other questions: later :-)

Re: hitechnic Gyro sensor (NGY1044) noise

Posted: 08 Jul 2011, 04:07
by mattallen37
linusa wrote:Great, really cool that it works :-)

I just noticed that you're doing the slow LCD painting and a Wait(20) inside the polling loop. During this time you don't get any "rotation information", so if the gyro moves, this movement is lost to you. You should consider doing multi-tasking, and just have one loop that reads and updates phi as often as possible. On the other hand, not too often (or the gyro might get unstable or whater it is I do vaguely remember). It could be this: if you poll more often than the firmware updates its registers (I think that might be every 3ms for analog sensors), then you get "double" values, and if you integrate them, that's biased.

So, for a better test, introduce this extra polling tast, but keep a Wait(4) inside the loop for safety...
Yea, I know that it is not reading as often as it could be, and I understand the potential of it not seeing a sudden change. Because of this and my trial and error offset, I am quite surprised by the results.

I changed the code to this:

Code: Select all

float offset;

float phi = 0;
int lastTicks;
int curTicks;

task main(){
  SetSensorHTGyro(S2);
  lastTicks = CurrentTick();
  offset=0;
  repeat(500){
    offset += SensorHTGyro(S2);
    Wait(3);
  }
  offset/=500;
  lastTicks = CurrentTick() + 4;
  while(true){

    // convert omega to unit "degrees per ms" here.
    float omega = (SensorHTGyro(S2) - offset) / 1000.0;
    curTicks = CurrentTick();
    int deltaTTicks = curTicks - lastTicks;
    phi += omega * deltaTTicks;
    lastTicks = curTicks;

    ClearScreen();
    NumOut(0, LCD_LINE1, SensorHTGyro(S2));
    NumOut(50, LCD_LINE1, offset);
    NumOut(0, LCD_LINE2, omega);
    NumOut(0, LCD_LINE3, deltaTTicks);
    NumOut(0, LCD_LINE4, phi);
    Wait(2);
  }
}
It sets the offset automatically (since it seems to change based on something, like temp maybe).

deltaTTicks is 4, so I get the same effect as what you talked about. I have been running this code now for about 5 minutes stationary, and it is returning about -0.6 degrees (make that 8 minutes, and -0.4 degrees). The greatest error was about 0.8. I am thoroughly impressed by it!

If I do multitasking, I should use a mutex for the timing, right?
linusa wrote:As to your other questions: later :-)
Can hardly wait :D

Re: hitechnic Gyro sensor (NGY1044) noise

Posted: 08 Jul 2011, 07:21
by aswin0
mattallen37 wrote:You should also note, that some "drift" is actually very correct. The earth rotates 360*/24 hours (once a day). That means that you should see a "drift" of 1 degree every 4 minutes. However, if the axis of the gyro is perfectly parallel to the axis of the earth, you should see no "drift".
This is only true on the poles. On the equator you won't measure an angular rate because of earth rotation.

Re: hitechnic Gyro sensor (NGY1044) noise

Posted: 08 Jul 2011, 07:48
by HaWe
In the moment I have constant and stable GyroIntegrals and -Headings with my code I posted above (changed because of John's advice to my Waits) for more than 1 hour when not moving the robot.
As I now completely switched to float calculation and Mean instead of Median, now it's about 5° drift per hour. But it's more accurate even for fast turns.

Turning the robot by steps of 90° gives very accurate Gyro readings (gyro error <= 1°, far better than the compass which shows up to 30° error deviation)).
Turning the robot 360° the GyroHeadings again match perfectly to the CompassHeadings.
Program uses MT, no mutexes in use or needed .

All electrical and ferromagnetical parts are more than 20 cm away from the compass.(The Compass really needs to be calibrated!)

All tests have been repeated several times inside my house and outside in the garden (because of possible magnetic interferences).

I have to see how changes of battery level will affect the Gyro readings.

ps
the voltage influence seems to be severe, it leads to a drift up to 2° per second!

pps
I'm testing close to Frankfurt/Main, neither on the North Pole, nor in Equatorial Africa, and not in the vicinity of Betelgeuze^^)

Re: hitechnic Gyro sensor (NGY1044) noise

Posted: 08 Jul 2011, 16:30
by mattallen37
aswin0 wrote:
mattallen37 wrote:You should also note, that some "drift" is actually very correct. The earth rotates 360*/24 hours (once a day). That means that you should see a "drift" of 1 degree every 4 minutes. However, if the axis of the gyro is perfectly parallel to the axis of the earth, you should see no "drift".
This is only true on the poles. On the equator you won't measure an angular rate because of earth rotation.
It depends on the orientation of the gyro, right? If the gyro axis is perpendicular to the earth axis, it doesn't matter where you are.

Re: hitechnic Gyro sensor (NGY1044) noise

Posted: 08 Jul 2011, 17:14
by HaWe
I still don't quite see how to calculate a Gyro drift by changing voltages, using aswins formula
offset= 0.004981 * mVolt + 558.7103
https://sourceforge.net/apps/phpbb/mind ... t=30#p1278

At the start I'm using the following

Code: Select all

int OldBatteryLevel;

void FindGyroOffset()
{
    float tot = 0;
    OldBatteryLevel=BatteryLevel();  // to remember the voltage when calibrating
    for (int i=0; i<500; ++i)
    {
       tot += SensorHTGyro(GYRO, 0); // tot var is 500 gyro readings
       if (!(i%50)) Beep(880,10);
       Wait(5);                     // 5ms between analog readings
    }
    Beep(440,100);
    GyroOffset=tot/500;
} 
- and then, how should I update GyroOffset by the current BatteryLevel and aswin's formula?

(the drift by voltage changing is really the mean problem!)