Rule 1 in coding something that can hurt you, take a base line reading first

I'm implementing simple damping code in my self-balancing skateboard code and I was looking at the result data and something didn't look quite right.

Here is the source before I caught it

GyroData returnData = (GyroData)dampenData[0].Clone(); // was new GyroData();
int length = dampenData.Length;

// only values we really care about damping
for (int i = 0; i < length; i++)
{
    returnData.Pitch += dampenData[i].Pitch;
    returnData.Yaw += dampenData[i].Yaw;
    returnData.Roll += dampenData[i].Roll;

    returnData.TiltX += dampenData[i].TiltX;
    returnData.TiltY += dampenData[i].TiltY;
    returnData.TiltZ += dampenData[i].TiltZ;
}

Anyone catch it?

I didn't either until I was looking at the base results.  I knew if the gyro was sitting still, I should be getting a reading of 315 off the X and 345 off the Y gyroscopes.  Instead I was getting 400+ on each.  If the dampening was working properly, it should be, 315 and 345 respectively.

Still not seeing where I made the mistake?

I was double counting the first index since I flipped to a Clone instead of a blank object.  I'm thinking cloning the object will be faster than having to reupdate the values I need to return but don't need to dampen.  Changing the loop to start at 1 instead of 0 fixed the issue.

I have no doubt this would have gone unnoticed for a long time.  Could have done Unit tests to catch this too.  You better believe I commented why it is index 1 also.

No comments posted yet.

Post a Comment

Please add 4 and 2 and type the answer here:
  • About
  • Clint Rutkas: I build stuff that will eventually hurt me. I'm your average nerd.
  • Follow Clint on Twitter