Skip to content

Commit

Permalink
Add Horizon mode inverted self-leveling
Browse files Browse the repository at this point in the history
Co-authored-by: Ken Imhof <[email protected]>
  • Loading branch information
pmattila and KenImhof committed Apr 11, 2024
1 parent 73245ca commit ae2ba81
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/common/maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ static inline float tan_approx(float x)
__typeof__ (x) _x = (x); \
_x > 0 ? _x : -_x; })

#define SIGN(x) \
__extension__ ({ __typeof__ (x) _x = (x); \
(_x > 0) - (_x < 0); })

/*
* Basic math operations
Expand Down
9 changes: 9 additions & 0 deletions src/main/flight/imu.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,12 @@ bool isUpright(void)
return true;
#endif
}

bool isUpsidedown(void)
{
#ifdef USE_ACC
return sensors(SENSOR_ACC) && attitudeIsEstablished && getCosTiltAngle() < 0;
#else
return false;
#endif
}
1 change: 1 addition & 0 deletions src/main/flight/imu.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ void imuSetHasNewData(uint32_t dt);

bool shouldInitializeGPSHeading(void);
bool isUpright(void);
bool isUpsidedown(void);
15 changes: 14 additions & 1 deletion src/main/flight/leveling.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,20 @@ static float calcLevelErrorAngle(int axis)
#endif
angle = constrainf(angle, -level.AngleLimit, level.AngleLimit);

float error = angle - ((attitude.raw[axis] - angleTrim->raw[axis]) / 10.0f);
float currentAngle = ((attitude.raw[axis] - angleTrim->raw[axis]) / 10.0f);

if (isUpsidedown() && FLIGHT_MODE(HORIZON_MODE)) {
switch (axis) {
case FD_PITCH:
currentAngle = -currentAngle;
break;
case FD_ROLL:
currentAngle -= SIGN(currentAngle) * 180;
break;
}
}

float error = angle - currentAngle;

return error;
}
Expand Down

0 comments on commit ae2ba81

Please sign in to comment.