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

Fix the state update in the prediction step of the EKF #162

Merged
merged 3 commits into from
Nov 29, 2016

Conversation

estromb
Copy link
Contributor

@estromb estromb commented Nov 18, 2016

Currently, the update overwrites the state and then updates the other states with the overwritten ones. I added a few temp variables to save the old state through the update.

I also changed some declarations pertaining to this state update to static, so the wouldn't be re-declared at 100 Hz.

The code builds in the Bitcraze VM, but I don't have access to a Crazyflie to see if it runs.

float dx = S[STATE_PX] * dt;
float dy = S[STATE_PY] * dt;
float dz = S[STATE_PZ] * dt + zacc * dt2 / 2.0f; // thrust can only be produced in the body's Z direction
dx = S[STATE_PX] * dt;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change these variables to a global scope? They are really only used for readability, as the compiler will likely optimize them out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I just realized with your comment that these variables aren't global, but just declared static within their scope. Regardless, this is only typically done if the variable's value needs to persist between function calls (the allocation will be .data portion of memory instead of the stack with this change) .

@estromb
Copy link
Contributor Author

estromb commented Nov 18, 2016

I may have gone out on a limb with the static keyword, I admit. The idea behind it was to define it on the heap instead of the stack, since it was redeclared at every loop.

float dz = S[STATE_PZ] * dt + acc->z * dt2 / 2.0f; // thrust can only be produced in the body's Z direction
dx = S[STATE_PX] * dt + acc->x * dt2 / 2.0f;
dy = S[STATE_PY] * dt + acc->y * dt2 / 2.0f;
dz = S[STATE_PZ] * dt + acc->z * dt2 / 2.0f; // thrust can only be produced in the body's Z direction

// position update
S[STATE_X] += R[0][0] * dx + R[0][1] * dy + R[0][2] * dz;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the same bug is present in the else clause further below, and should also be updated. Good catch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks. I haven't implemented that case in my own work. I will add a commit for it.

@mikehamer
Copy link
Contributor

Just following up on this, these changes seem good.

I suggest you accept the pull request @ataffanel but remove the static keywords from the variables (on :622-:624) since there is no need for them to persist between calls and, I assume, the compiler will therefore more easily be able to optimize the variables away (?)

@ataffanel ataffanel merged commit 32a0ed5 into bitcraze:master Nov 29, 2016
ataffanel added a commit that referenced this pull request Nov 29, 2016
@ataffanel
Copy link
Member

Thanks, it is merged!

@mikehamer thanks for reviewing. I agree for static, these variables will likely be optimized by the compiler and never leave the registers.

@estromb estromb deleted the EKF_fix branch November 29, 2016 10:02
@estromb estromb restored the EKF_fix branch November 30, 2016 10:25
@krichardsson krichardsson added this to the Next version milestone Dec 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants