Skip to content

Commit

Permalink
Miscellaneous fixes from dev branch
Browse files Browse the repository at this point in the history
Includes:
Add FOREGROUND_SERVICE permission on Android, fixes #32
Fix check for data files when syncing
GPS skip no longer signaled as warning
Fix user log length
Tentative fix for accelerometer speed issue #35
  • Loading branch information
LorenzCK committed Apr 8, 2019
1 parent 40ecd7d commit 5f90a27
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application android:icon="@drawable/ic_launcher" android:theme="@style/AppTheme" android:label="@string/Vernacular_P0_app_name">
<provider android:name="android.support.v4.content.FileProvider" android:authorities="it.uniurb.smartroadsense.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" />
Expand Down
34 changes: 23 additions & 11 deletions src/Shared/Data/SensorPack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public abstract class SensorPack {
#endif

public const int AccelerometerDesiredHz = 100;
public const int AccelerometerDesiredDelayMs = 1000 / AccelerometerDesiredHz;
public const int AccelerometerDesiredDelayMs = (int)(1000 / AccelerometerDesiredHz);
public const int AccelerometerToleratedDelayMs = (int)(AccelerometerDesiredDelayMs * 0.8);

public const int LocationDesiredHz = 1;
public const int LocationDesiredDelayMs = 1000 / LocationDesiredHz;
Expand Down Expand Up @@ -253,7 +254,7 @@ protected void ReportNewLocation(double latitude, double longitude, float speed,
IsMovingSlowly = false;

if (measuredSpeed >= GpsTooFastSpeed) {
Log.Warning(new ArgumentOutOfRangeException(nameof(measuredSpeed)), "Unforeseen GPS skip of {0} m in {1} ms", measuredDistance, intervalTime.TotalMilliseconds);
Log.Debug("Unforeseen GPS skip of {0} m in {1} ms", measuredDistance, intervalTime.TotalMilliseconds);
ResetGpsLocation();
return;
}
Expand All @@ -270,13 +271,30 @@ protected void ReportNewLocation(double latitude, double longitude, float speed,
_gpsLastAccuracy = accuracy;
}

private DateTime? _lastAcceleration = null;

/// <summary>
/// Reports new acceleration values from the sensor.
/// </summary>
/// <param name="accX">X-axis acceleration in m/s^2.</param>
/// <param name="accY">Y-axis acceleration in m/s^2.</param>
/// <param name="accZ">Z-axis acceleration in m/s^2.</param>
protected void ReportNewAcceleration(double accX, double accY, double accZ) {
//NOTE: cannot use the sensor timestamp data, as this is HW dependent on Android
// see https://code.google.com/p/android/issues/detail?id=56561
var timestamp = DateTime.UtcNow;

// Check accelerometer frequency
if(_lastAcceleration.HasValue) {
int accDelayMs = (int)((timestamp - _lastAcceleration.Value).TotalMilliseconds);
if(accDelayMs < AccelerometerToleratedDelayMs) {
// Higher frequency than what was asked for, drop
Log.Debug("Dropping accelerometer sample ({0}ms delay)", accDelayMs);
return;
}
}
_lastAcceleration = timestamp;

//Scale each axis individually
accX *= _accelerometerScaleFactor;
accY *= _accelerometerScaleFactor;
Expand Down Expand Up @@ -307,15 +325,6 @@ protected void ReportNewAcceleration(double accX, double accY, double accZ) {
return;
}

//TODO: check for acceleration sampling frequency here

//TODO: computation is now sync-locked to the accelerometer instead of the intended
// 100 Hz frequency. This should be changed.

//NOTE: cannot use the sensor timestamp data, as this is HW dependent on Android
// see https://code.google.com/p/android/issues/detail?id=56561
var timestamp = DateTime.UtcNow;

//Check for unfixed GPS timeout
if (timestamp - _gpsLastUpdate > GpsUnfixedTimeout) {
LocationSensorStatus = LocationSensorStatus.Fixing;
Expand Down Expand Up @@ -392,6 +401,7 @@ public void StartSensing() {
_gpsLastUpdate = DateTime.UtcNow;
_lastTimeFastEnough = DateTime.MinValue;
_lastTimeMoved = DateTime.MinValue;
_lastAcceleration = null;

_accelerometerScaleFactor = Settings.CalibrationScaleFactor;
Log.Debug("Sensor pack using scale factor of {0:P2}", _accelerometerScaleFactor);
Expand Down Expand Up @@ -435,6 +445,8 @@ private void ResetGpsLocation() {
_gpsLastBearing = 0f;
_gpsLastAccuracy = 0;

_lastAcceleration = null;

_engine.Reset();
}

Expand Down
5 changes: 4 additions & 1 deletion src/Shared/SyncManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ private async Task<SyncResult> SynchronizeInner(CancellationToken token, SyncPol

IList<DatabaseQueries.TrackAndCount> tracks = await Task.Run(() => {
using(var db = DatabaseUtility.OpenConnection()) {
return db.GetAllPendingTracks();
return (from t in db.GetAllPendingTracks()
let trackFilepath = FileNaming.GetDataTrackFilepath(t.TrackId)
where File.Exists(trackFilepath)
select t).ToList();
}
});
if (tracks.Count == 0) {
Expand Down
10 changes: 7 additions & 3 deletions src/Shared/UserLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ await Task.Run(() => {
Log.Debug("Log persisted");
}

public const int MaximumLogSize = 100;
public const int MaximumLogSize = 200;

private static readonly object _rootLock = new object();

private static Queue<LogEntry> _log = new Queue<LogEntry>(MaximumLogSize);
private static Queue<LogEntry> _log = new Queue<LogEntry>(MaximumLogSize + 1);

/// <summary>
/// Adds an iconless message to the log.
Expand All @@ -140,13 +140,17 @@ public static void Add(Icon icon, string format, params object[] args) {

lock (_rootLock) {
_log.Enqueue(entry);

while(_log.Count > MaximumLogSize) {
_log.Dequeue();
}
}

OnNewEntryAdded(entry);
}

/// <summary>
/// Gets the entries of the log.
/// Gets all entries of the log.
/// </summary>
public static IReadOnlyList<LogEntry> Entries {
get {
Expand Down

0 comments on commit 5f90a27

Please sign in to comment.