Skip to content

Commit

Permalink
Allow aligning the mag to any rotation
Browse files Browse the repository at this point in the history
Introduce 3 new variables which allow setting the decidegrees
for the mag sensor alignment. When any of these 3 variables
are non-zero, mag is assumed to be mounted off-board and
"align_mag" as well as the board alignment are ignored.

Settings are named align_mag_roll, align_mag_pitch and
align_mag_yaw.

Fixes #86
Fixes #1029
  • Loading branch information
fiam committed Feb 24, 2018
1 parent 2aa552d commit 1a4a3e8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/main/fc/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,18 @@ groups:
condition: USE_DUAL_MAG
min: 0
max: 1
- name: align_mag_roll
field: rollDeciDegrees
min: -1800
max: 3600
- name: align_mag_pitch
field: pitchDeciDegrees
min: -1800
max: 3600
- name: align_mag_yaw
field: yawDeciDegrees
min: -1800
max: 3600

- name: PG_BAROMETER_CONFIG
type: barometerConfig_t
Expand Down
35 changes: 32 additions & 3 deletions src/main/sensors/compass.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ PG_RESET_TEMPLATE(compassConfig_t, compassConfig,
.mag_hardware = MAG_HARDWARE_DEFAULT,
.mag_declination = 0,
.mag_to_use = 0,
.magCalibrationTimeLimit = 30
.magCalibrationTimeLimit = 30,
.rollDeciDegrees = 0,
.pitchDeciDegrees = 0,
.yawDeciDegrees = 0,
);

#ifdef USE_MAG
Expand Down Expand Up @@ -368,8 +371,34 @@ void compassUpdate(timeUs_t currentTimeUs)
}
}

applySensorAlignment(mag.magADC, mag.magADC, mag.dev.magAlign);
applyBoardAlignment(mag.magADC);
if (compassConfig()->rollDeciDegrees != 0 ||
compassConfig()->pitchDeciDegrees != 0 ||
compassConfig()->yawDeciDegrees != 0) {

// Externally aligned compass
struct fp_vector v = {
.X = mag.magADC[X],
.Y = mag.magADC[Y],
.Z = mag.magADC[Z],
};

fp_angles_t r = {
.angles.roll = DECIDEGREES_TO_RADIANS(compassConfig()->rollDeciDegrees),
.angles.pitch = DECIDEGREES_TO_RADIANS(compassConfig()->pitchDeciDegrees),
.angles.yaw = DECIDEGREES_TO_RADIANS(compassConfig()->yawDeciDegrees),
};

rotateV(&v, &r);

mag.magADC[X] = v.X;
mag.magADC[Y] = v.Y;
mag.magADC[Z] = v.Z;

} else {
// On-board compass
applySensorAlignment(mag.magADC, mag.magADC, mag.dev.magAlign);
applyBoardAlignment(mag.magADC);
}

magUpdatedAtLeastOnce = 1;
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/sensors/compass.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ typedef struct mag_s {
extern mag_t mag;

typedef struct compassConfig_s {
int16_t mag_declination; // Get your magnetic decliniation from here : http://magnetic-declination.com/
int16_t mag_declination; // Get your magnetic declination from here : http://magnetic-declination.com/
// For example, -6deg 37min, = -637 Japan, format is [sign]dddmm (degreesminutes) default is zero.
sensor_align_e mag_align; // mag alignment
sensor_align_e mag_align; // on-board mag alignment. Ignored if externally aligned via *DeciDegrees.
uint8_t mag_hardware; // Which mag hardware to use on boards with more than one device
flightDynamicsTrims_t magZero;
uint8_t mag_to_use;
uint8_t magCalibrationTimeLimit; // Time for compass calibration (seconds)
int16_t rollDeciDegrees; // Alignment for external mag on the roll (X) axis (0.1deg)
int16_t pitchDeciDegrees; // Alignment for external mag on the pitch (Y) axis (0.1deg)
int16_t yawDeciDegrees; // Alignment for external mag on the yaw (Z) axis (0.1deg)
} compassConfig_t;

PG_DECLARE(compassConfig_t, compassConfig);
Expand Down

0 comments on commit 1a4a3e8

Please sign in to comment.