Skip to content

Commit 0bc482f

Browse files
authoredSep 21, 2024··
Provide gravity as an algorithm output
1 parent c2a6d49 commit 0bc482f

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed
 

‎Fusion/FusionAhrs.c

+16-11
Original file line numberDiff line numberDiff line change
@@ -380,38 +380,43 @@ void FusionAhrsSetQuaternion(FusionAhrs *const ahrs, const FusionQuaternion quat
380380
}
381381

382382
/**
383-
* @brief Returns the linear acceleration measurement equal to the accelerometer
384-
* measurement with the 1 g of gravity removed.
383+
* @brief Returns the direction of gravity in the sensor coordinate frame.
385384
* @param ahrs AHRS algorithm structure.
386-
* @return Linear acceleration measurement in g.
385+
* @return Direction of gravity in the sensor coordinate frame.
387386
*/
388-
FusionVector FusionAhrsGetLinearAcceleration(const FusionAhrs *const ahrs) {
387+
FusionVector FusionAhrsGetGravity(const FusionAhrs *const ahrs) {
389388
#define Q ahrs->quaternion.element
390-
391-
// Calculate gravity in the sensor coordinate frame
392389
const FusionVector gravity = {.axis = {
393390
.x = 2.0f * (Q.x * Q.z - Q.w * Q.y),
394391
.y = 2.0f * (Q.y * Q.z + Q.w * Q.x),
395392
.z = 2.0f * (Q.w * Q.w - 0.5f + Q.z * Q.z),
396393
}}; // third column of transposed rotation matrix
394+
return gravity;
395+
#undef Q
396+
}
397397

398-
// Remove gravity from accelerometer measurement
398+
/**
399+
* @brief Returns the linear acceleration measurement equal to the accelerometer
400+
* measurement with gravity removed.
401+
* @param ahrs AHRS algorithm structure.
402+
* @return Linear acceleration measurement in g.
403+
*/
404+
FusionVector FusionAhrsGetLinearAcceleration(const FusionAhrs *const ahrs) {
399405
switch (ahrs->settings.convention) {
400406
case FusionConventionNwu:
401407
case FusionConventionEnu: {
402-
return FusionVectorSubtract(ahrs->accelerometer, gravity);
408+
return FusionVectorSubtract(ahrs->accelerometer, FusionAhrsGetGravity(ahrs));
403409
}
404410
case FusionConventionNed: {
405-
return FusionVectorAdd(ahrs->accelerometer, gravity);
411+
return FusionVectorAdd(ahrs->accelerometer, FusionAhrsGetGravity(ahrs));
406412
}
407413
}
408414
return FUSION_VECTOR_ZERO; // avoid compiler warning
409-
#undef Q
410415
}
411416

412417
/**
413418
* @brief Returns the Earth acceleration measurement equal to accelerometer
414-
* measurement in the Earth coordinate frame with the 1 g of gravity removed.
419+
* measurement in the Earth coordinate frame with gravity removed.
415420
* @param ahrs AHRS algorithm structure.
416421
* @return Earth acceleration measurement in g.
417422
*/

‎Fusion/FusionAhrs.h

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ FusionQuaternion FusionAhrsGetQuaternion(const FusionAhrs *const ahrs);
9393

9494
void FusionAhrsSetQuaternion(FusionAhrs *const ahrs, const FusionQuaternion quaternion);
9595

96+
FusionVector FusionAhrsGetGravity(const FusionAhrs *const ahrs);
97+
9698
FusionVector FusionAhrsGetLinearAcceleration(const FusionAhrs *const ahrs);
9799

98100
FusionVector FusionAhrsGetEarthAcceleration(const FusionAhrs *const ahrs);

‎Python/Python-C-API/Ahrs.h

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ static int ahrs_set_quaternion(Ahrs *self, PyObject *value, void *closure) {
5252
return 0;
5353
}
5454

55+
static PyObject *ahrs_get_gravity(Ahrs *self) {
56+
FusionVector *const gravity = malloc(sizeof(FusionVector));
57+
*gravity = FusionAhrsGetGravity(&self->ahrs);
58+
59+
const npy_intp dims[] = {3};
60+
PyObject *array = PyArray_SimpleNewFromData(1, dims, NPY_FLOAT, gravity->array);
61+
PyArray_ENABLEFLAGS((PyArrayObject *) array, NPY_ARRAY_OWNDATA);
62+
return array;
63+
}
64+
5565
static PyObject *ahrs_get_linear_acceleration(Ahrs *self) {
5666
FusionVector *const linear_acceleration = malloc(sizeof(FusionVector));
5767
*linear_acceleration = FusionAhrsGetLinearAcceleration(&self->ahrs);
@@ -204,6 +214,7 @@ static int ahrs_set_heading(Ahrs *self, PyObject *value, void *closure) {
204214
static PyGetSetDef ahrs_get_set[] = {
205215
{"settings", NULL, (setter) ahrs_set_settings, "", NULL},
206216
{"quaternion", (getter) ahrs_get_quaternion, (setter) ahrs_set_quaternion, "", NULL},
217+
{"gravity", (getter) ahrs_get_gravity, NULL, "", NULL},
207218
{"linear_acceleration", (getter) ahrs_get_linear_acceleration, NULL, "", NULL},
208219
{"earth_acceleration", (getter) ahrs_get_earth_acceleration, NULL, "", NULL},
209220
{"internal_states", (getter) ahrs_get_internal_states, NULL, "", NULL},

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The magnetic rejection feature reduces the errors that result from temporary mag
3636

3737
### Algorithm outputs
3838

39-
The algorithm provides three outputs: quaternion, linear acceleration, and Earth acceleration. The quaternion describes the orientation of the sensor relative to the Earth. This can be converted to a rotation matrix using the `FusionQuaternionToMatrix` function or to Euler angles using the `FusionQuaternionToEuler` function. The linear acceleration is the accelerometer measurement with the 1 g of gravity removed. The Earth acceleration is the accelerometer measurement in the Earth coordinate frame with the 1 g of gravity removed. The algorithm supports North-West-Up (NWU), East-North-Up (ENU), and North-East-Down (NED) axes conventions.
39+
The algorithm provides four outputs: quaternion, gravity, linear acceleration, and Earth acceleration. The quaternion describes the orientation of the sensor relative to the Earth. This can be converted to a rotation matrix using the `FusionQuaternionToMatrix` function or to Euler angles using the `FusionQuaternionToEuler` function. Gravity is a direction of gravity in the sensor coordinate frame. Linear acceleration is the accelerometer measurement with gravity removed. Earth acceleration is the accelerometer measurement in the Earth coordinate frame with gravity removed. The algorithm supports North-West-Up (NWU), East-North-Up (ENU), and North-East-Down (NED) axes conventions.
4040

4141
### Algorithm settings
4242

0 commit comments

Comments
 (0)
Please sign in to comment.