Skip to content

Commit

Permalink
make accelerometer stable
Browse files Browse the repository at this point in the history
I've noticed an issue when pitching forward or backward. I expect only the "up" or "down" directions to be registered, but instead, "left" and/or "right" are also triggered for a brief period.
  • Loading branch information
pepper-jelly authored and pepper-jelly committed Jul 27, 2024
1 parent d30f930 commit 8a1d147
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions src/joyaccelerometersensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,41 +180,32 @@ JoySensorDirection JoyAccelerometerSensor::calculateSensorDirection()
double roll = calculateRoll();
double pitch_abs = abs(pitch);
double roll_abs = abs(roll);
if (pitch_abs * pitch_abs + roll_abs * roll_abs < m_dead_zone * m_dead_zone)
return SENSOR_CENTERED;

double range = M_PI / 4 - m_diagonal_range / 2;
bool inPitch = pitch_abs < range;
bool inPitch = pitch_abs < m_dead_zone;
bool inRoll = roll_abs < range;

if (inPitch && !inRoll)
{
if (roll > 0)
return SENSOR_LEFT;
else
return SENSOR_RIGHT;
} else if (!inPitch && inRoll)
{
if (pitch > 0)
return SENSOR_UP;
else
return SENSOR_DOWN;
} else // in both or in none
if (!inPitch)
{
if (pitch > 0)
if (!inRoll)
{
if (roll > 0)
return SENSOR_LEFT_UP;
else
return SENSOR_RIGHT_UP;
{
return pitch > 0 ? SENSOR_LEFT_UP : SENSOR_LEFT_DOWN;
} else
{
return pitch > 0 ? SENSOR_RIGHT_UP : SENSOR_RIGHT_DOWN;
}
} else
{
if (roll > 0)
return SENSOR_LEFT_DOWN;
else
return SENSOR_RIGHT_DOWN;
return pitch > 0 ? SENSOR_UP : SENSOR_DOWN;
}
} else if (!inRoll)
{
return roll > 0 ? SENSOR_LEFT : SENSOR_RIGHT;
}

return SENSOR_CENTERED;
}

/**
Expand Down

0 comments on commit 8a1d147

Please sign in to comment.