posted on Tuesday, April 08, 2008 1:53 AM |

image3784778g[1] I've wasted literally an hour messy around with a section of code I've been attempting to make elegant (aka pretty).

Why is this so important to me besides the possibility of being obsessive compulsive?  I think it makes the code easier to read along with more maintainable.  While you can't reduce the amount of code you repeat 100%, I think you should be able to do it for most instances.  My gut told me this was one of those times and I found a clever solution.  While Dan may be banging his head against the wall since I have another deadline for him ... I think it is well worth it.

So I have 3 Acceleration variables within my ImuData class.  In my Kalman filter class, I had to loop through these vars and do some simple calculations and updating.  This should be done with a function (wow, I know).  The problem is updating vars from a function.  The ref keyword for referencing won't work here, I could use an out but then I'd have far too many repeats for initializing the variables going in the function.  So what did I do?

I fixed the ImuData class.  Instead of all member variables, I created a base arrays. 

An Example, I may still rename the variables to type_Axis instead of Axis_type:

public double X_Acceleration
{
    get { return Acceleration[0]; }
    set { Acceleration[0] = value; }
}
public double Y_Acceleration
{
    get { return Acceleration[1]; }
    set { Acceleration[1] = value; }
}
public double Z_Acceleration
{
    get { return Acceleration[2]; }
    set { Acceleration[2] = value; }
}

public double[] Acceleration
{
    get { return _acceleration; }
    set { _acceleration = value; }
}
private double[] _acceleration = { 0, 0, 0 };

which lets me do this in the kalman filter class:

private void applyCorrectionFactorsForGyro(int index)
{
    data.GyroVoltCorrection[index] = ((2.5 / data.GyroVolt[index]) * 1024) / 5;

    data.Gyro[index] *= data.GyroVoltCorrection[index];
    data.GyroTemp[index] *= data.GyroVoltCorrection[index];
}

mmmm, pretty and no code repeat.  (and yes, that picture does freak me out but it is a decent ad)

Tags [ Building Skateboard Video ]

Your Comments.

No comments posted yet.

Post Comment
Title *
Name *
Email
Url
Comment *  
Please add 3 and 6 and type the answer here: