Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary doubles in Accelerometer & Compass #455

Open
microbit-carlos opened this issue Nov 25, 2024 · 2 comments · May be fixed by lancaster-university/codal-core#177
Open

Unnecessary doubles in Accelerometer & Compass #455

microbit-carlos opened this issue Nov 25, 2024 · 2 comments · May be fixed by lancaster-university/codal-core#177

Comments

@microbit-carlos
Copy link
Collaborator

In codal-core Accelerometer::recalculatePitchRoll():

void Accelerometer::recalculatePitchRoll()
{
    double x = (double) sample.x;
    double y = (double) sample.y;
    double z = (double) sample.z;

    roll = atan2(x, -z);
    pitch = atan2(y, (x*sin(roll) - z*cos(roll)));

    // Handle to the two "negative quadrants", such that we get an output in the +/- 18- degree range.
    // This ensures that the pitch values are consistent with the roll values.
    if (z > 0.0)
    {
        double reference = pitch > 0.0 ? (PI / 2.0) : (-PI / 2.0);
        pitch = reference + (reference - pitch);
    }

    status |= ACCELEROMETER_IMU_DATA_VALID;
}

And Compass::getFieldStrength():

int Compass::getFieldStrength()
{
    Sample3D s = getSample();

    double x = s.x;
    double y = s.y;
    double z = s.z;

    return (int) sqrt(x*x + y*y + z*z);
}

@finneyj we probably don't need the extra precision and can downgrade them to floats?

@finneyj
Copy link
Contributor

finneyj commented Feb 18, 2025

Thanks @microbit-carlos. Yes, I think this looks pretty safe!

I recall on v1 we had some rounding errors in the old compass calibration code that were causing some problems - this was probably a leftover from that...

@finneyj
Copy link
Contributor

finneyj commented Feb 18, 2025

p.s. Can you tell how much RAM/FLASH we save from this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants