Skip to content

Commit

Permalink
disable check of flag valid position returned by the sensor
Browse files Browse the repository at this point in the history
My old device (Sony XA2) seems to not update the status as expected,
so disable the check to process data.
  • Loading branch information
janbar committed Sep 14, 2024
1 parent 8a55082 commit d216e68
Showing 1 changed file with 73 additions and 81 deletions.
154 changes: 73 additions & 81 deletions src/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,112 +283,104 @@ void TrackerModule::onLocationChanged(bool positionValid, double lat, double lon
osmscout::Timestamp now = std::chrono::system_clock::now();
osmscout::Bearing bearing;

if (positionValid)
// compute data for the last interval,
// starting with the last known position and up to now
if (!m_lastPosition)
{
// compute data for the last interval,
// starting with the last known position and up to now
if (!m_lastPosition)
{
// On the first run, initialize the starting data
m_currentAlt = alt;
m_currentSpeed = 0.0;
bearing = osmscout::Bearing::Degrees(m_azimuth);
}
else
{
}
else
{
// the duration should be non zero
auto sec = std::chrono::duration_cast<std::chrono::duration<double> >(now - m_lastPosition.time);
if (sec.count() > 0)
{
// i use accel to validate the new interval
// the new data won't be used when value is excessive
// the horizontal distance
double dh = osmin::Utils::sphericalDistance(m_lastPosition.coord.GetLat(), m_lastPosition.coord.GetLon(), lat, lon);
double dv = 0.0;
if (!std::isnan(alt) && !std::isnan(m_currentAlt))
dv = alt - m_currentAlt;
double s0 = std::sqrt(std::pow(dh, 2) + std::pow(dv, 2)) / sec.count();
double accel = std::fabs((s0 - (m_currentSpeed / 3.6)) / sec.count());
if (accel > MAX_ACCEL)
{
// reset elevation for the next run
m_currentAlt = alt;
}
else
{
// the vertical accuracy isn't good enough, therefore i estimate the speed using
// horizontal data only
double kmh = 3.6 * dh / sec.count();
// update tracking data when moving
if (kmh > MOVING_SPEED)
// i use accel to validate the new interval
// the new data won't be used when value is excessive
// the horizontal distance
double dh = osmin::Utils::sphericalDistance(m_lastPosition.coord.GetLat(), m_lastPosition.coord.GetLon(), lat, lon);
double dv = 0.0;
if (!std::isnan(alt) && !std::isnan(m_currentAlt))
dv = alt - m_currentAlt;
double s0 = std::sqrt(std::pow(dh, 2) + std::pow(dv, 2)) / sec.count();
double accel = std::fabs((s0 - (m_currentSpeed / 3.6)) / sec.count());
if (accel > MAX_ACCEL)
{
m_distance += dh;
m_duration += sec.count();
if (kmh > m_maxSpeed)
m_maxSpeed = kmh;
if (dv > ASC_THRESHOLD)
{
m_ascent += dv;
m_currentAlt = alt;
}
else if (dv < DES_THRESHOLD)
{
m_descent -= dv;
// reset elevation for the next run
m_currentAlt = alt;
}
}
m_currentSpeed = kmh;
emit dataChanged(m_currentSpeed, m_distance, m_duration, m_ascent, m_descent, m_maxSpeed);
}
else
{
// the vertical accuracy isn't good enough, therefore i estimate the speed using
// horizontal data only
double kmh = 3.6 * dh / sec.count();
// update tracking data when moving
if (kmh > MOVING_SPEED)
{
m_distance += dh;
m_duration += sec.count();
if (kmh > m_maxSpeed)
m_maxSpeed = kmh;
if (dv > ASC_THRESHOLD)
{
m_ascent += dv;
m_currentAlt = alt;
}
else if (dv < DES_THRESHOLD)
{
m_descent -= dv;
m_currentAlt = alt;
}
}
m_currentSpeed = kmh;
emit dataChanged(m_currentSpeed, m_distance, m_duration, m_ascent, m_descent, m_maxSpeed);
}
}
// when we are stationary the direction is calculated according to the azimuth of the device
// otherwise it is estimated according to the progress compared to the previous position
if (m_currentSpeed < COURSE_SPEED)
bearing = osmscout::Bearing::Degrees(m_azimuth);
bearing = osmscout::Bearing::Degrees(m_azimuth);
else
bearing = osmscout::Bearing::Radians(osmin::Utils::sphericalBearingFinal(m_lastPosition.coord.GetLat(), m_lastPosition.coord.GetLon(), lat, lon));
bearing = osmscout::Bearing::Radians(osmin::Utils::sphericalBearingFinal(m_lastPosition.coord.GetLat(), m_lastPosition.coord.GetLon(), lat, lon));

if (m_recording)
{
if (!m_lastRecord)
{
record();
m_lastRecord.time = m_lastPosition.time;
m_lastRecord.coord = m_lastPosition.coord;
m_lastRecord.bearing = m_lastPosition.bearing;
emit positionRecorded(m_lastRecord.coord);
}
else
{
double dh = osmin::Utils::sphericalDistance(m_lastRecord.coord.GetLat(), m_lastRecord.coord.GetLon(), lat, lon);
double da = std::fabs(bearing.AsRadians() - m_lastRecord.bearing.AsRadians());
if (da > M_PI_2)
da = std::fabs(da - M_PI);
// 0.17rad ~ 20deg
if (dh >= RECORD_INTERVAL || (da > 0.17 && dh > 5.0))
if (!m_lastRecord)
{
record();
m_lastRecord.time = m_lastPosition.time;
m_lastRecord.coord = m_lastPosition.coord;
m_lastRecord.bearing = m_lastPosition.bearing;
emit positionRecorded(m_lastRecord.coord);
record();
m_lastRecord.time = m_lastPosition.time;
m_lastRecord.coord = m_lastPosition.coord;
m_lastRecord.bearing = m_lastPosition.bearing;
emit positionRecorded(m_lastRecord.coord);
}
else
{
double dh = osmin::Utils::sphericalDistance(m_lastRecord.coord.GetLat(), m_lastRecord.coord.GetLon(), lat, lon);
double da = std::fabs(bearing.AsRadians() - m_lastRecord.bearing.AsRadians());
if (da > M_PI_2)
da = std::fabs(da - M_PI);
// 0.17rad ~ 20deg
if (dh >= RECORD_INTERVAL || (da > 0.17 && dh > 5.0))
{
record();
m_lastRecord.time = m_lastPosition.time;
m_lastRecord.coord = m_lastPosition.coord;
m_lastRecord.bearing = m_lastPosition.bearing;
emit positionRecorded(m_lastRecord.coord);
}
}
}
}
}

// save last position
m_lastPosition.bearing = bearing;
m_lastPosition.coord.Set(lat, lon);
m_lastPosition.elevation = alt;
m_lastPosition.time = now;
}
else
{
// update azimuth only
m_lastPosition.bearing = osmscout::Bearing::Degrees(m_azimuth);
}

// save last position
m_lastPosition.bearing = bearing;
m_lastPosition.coord.Set(lat, lon);
m_lastPosition.elevation = alt;
m_lastPosition.time = now;

emit positionChanged(m_state, m_lastPosition.coord, std::optional<osmscout::Bearing>(m_lastPosition.bearing));
}

Expand Down

0 comments on commit d216e68

Please sign in to comment.