Fixed phone data source (battery low warning on MainActivity, and crash running off end of RawData). I am a bit suspicious that the frequency response is not right after downsampling, but ok for now as it is just a demo mode.

This commit is contained in:
Graham Jones
2023-12-21 15:59:22 +00:00
parent 92311545ea
commit 792ddf46d0
2 changed files with 76 additions and 59 deletions

View File

@@ -701,9 +701,18 @@ public class MainActivity extends AppCompatActivity {
tv.setTextColor(warnTextColour); tv.setTextColor(warnTextColour);
} }
tv = (TextView) findViewById(R.id.battTv); tv = (TextView) findViewById(R.id.battTv);
if (mConnection.mSdServer.mSdData.dataSourceName.equals("Phone")) {
tv.setText(getString(R.string.WatchBatteryEquals)
+ "---% / "
+ String.valueOf(mConnection.mSdServer.mSdData.phoneBatteryPc) + "%");
tv.setBackgroundColor(okColour);
tv.setTextColor(okTextColour);
} else {
tv.setText(getString(R.string.WatchBatteryEquals) tv.setText(getString(R.string.WatchBatteryEquals)
+ String.valueOf(mConnection.mSdServer.mSdData.batteryPc) + "% / " + String.valueOf(mConnection.mSdServer.mSdData.batteryPc) + "% / "
+ String.valueOf(mConnection.mSdServer.mSdData.phoneBatteryPc) + "%"); + String.valueOf(mConnection.mSdServer.mSdData.phoneBatteryPc) + "%");
if (mConnection.mSdServer.mSdData.batteryPc <= 10) { if (mConnection.mSdServer.mSdData.batteryPc <= 10) {
tv.setBackgroundColor(alarmColour); tv.setBackgroundColor(alarmColour);
tv.setTextColor(alarmTextColour); tv.setTextColor(alarmTextColour);
@@ -716,7 +725,7 @@ public class MainActivity extends AppCompatActivity {
tv.setBackgroundColor(okColour); tv.setBackgroundColor(okColour);
tv.setTextColor(okTextColour); tv.setTextColor(okTextColour);
} }
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Populate the Data Sharing Status Box // Populate the Data Sharing Status Box
// We start off with it set to OK, then check for several different abnormal conditions // We start off with it set to OK, then check for several different abnormal conditions

View File

@@ -47,8 +47,6 @@ import static java.lang.Math.sqrt;
public class SdDataSourcePhone extends SdDataSource implements SensorEventListener { public class SdDataSourcePhone extends SdDataSource implements SensorEventListener {
private String TAG = "SdDataSourcePhone"; private String TAG = "SdDataSourcePhone";
private final static int NSAMP = 250;
private SensorManager mSensorManager; private SensorManager mSensorManager;
private Sensor mSensor; private Sensor mSensor;
private int mMode = 0; // 0=check data rate, 1=running private int mMode = 0; // 0=check data rate, 1=running
@@ -56,6 +54,8 @@ public class SdDataSourcePhone extends SdDataSource implements SensorEventListen
private long mStartTs = 0; private long mStartTs = 0;
public double mSampleFreq = 0; public double mSampleFreq = 0;
private boolean mUseNextSample = true;
private PowerManager.WakeLock mWakeLock; private PowerManager.WakeLock mWakeLock;
@@ -104,7 +104,7 @@ public class SdDataSourcePhone extends SdDataSource implements SensorEventListen
// we initially start in mMode=0, which calculates the sample frequency returned by the sensor, then enters mMode=1, which is normal operation. // we initially start in mMode=0, which calculates the sample frequency returned by the sensor, then enters mMode=1, which is normal operation.
if (mMode == 0) { if (mMode == 0) {
if (mStartEvent==null) { if (mStartEvent==null) {
Log.v(TAG,"onSensorChanged(): mMode=0 - checking Sample Rate - mNSamp = "+mSdData.mNsamp); Log.v(TAG,"onSensorChanged(): mMode=0 - Starting Sample Rate Check - mNSamp = "+mSdData.mNsamp);
Log.v(TAG,"onSensorChanged(): saving initial event data"); Log.v(TAG,"onSensorChanged(): saving initial event data");
mStartEvent = event; mStartEvent = event;
mStartTs = event.timestamp; mStartTs = event.timestamp;
@@ -112,7 +112,8 @@ public class SdDataSourcePhone extends SdDataSource implements SensorEventListen
} else { } else {
mSdData.mNsamp ++; mSdData.mNsamp ++;
} }
if (mSdData.mNsamp>=250) { Log.v(TAG, "onSensorChanged - mMode=" + mMode + " mNSamp=" + mSdData.mNsamp);
if (mSdData.mNsamp >= mSdData.rawData.length) {
Log.v(TAG, "onSensorChanged(): Collected Data = final TimeStamp=" + event.timestamp + ", initial TimeStamp=" + mStartTs); Log.v(TAG, "onSensorChanged(): Collected Data = final TimeStamp=" + event.timestamp + ", initial TimeStamp=" + mStartTs);
double dT = 1e-9 * (event.timestamp - mStartTs); double dT = 1e-9 * (event.timestamp - mStartTs);
mSdData.mSampleFreq = (int) (mSdData.mNsamp / dT); mSdData.mSampleFreq = (int) (mSdData.mNsamp / dT);
@@ -123,6 +124,9 @@ public class SdDataSourcePhone extends SdDataSource implements SensorEventListen
mStartTs = event.timestamp; mStartTs = event.timestamp;
} }
} else if (mMode == 1) { } else if (mMode == 1) {
// The phone gives us 50 Hz sample frequency so we do a crude factor of 2 downsampling.
if (mUseNextSample) {
mUseNextSample = false;
// mMode=1 is normal operation - collect NSAMP accelerometer data samples, then analyse them by calling doAnalysis(). // mMode=1 is normal operation - collect NSAMP accelerometer data samples, then analyse them by calling doAnalysis().
float x = event.values[0]; float x = event.values[0];
float y = event.values[1]; float y = event.values[1];
@@ -133,14 +137,14 @@ public class SdDataSourcePhone extends SdDataSource implements SensorEventListen
mSdData.rawData3D[3 * mSdData.mNsamp + 1] = y; mSdData.rawData3D[3 * mSdData.mNsamp + 1] = y;
mSdData.rawData3D[3 * mSdData.mNsamp + 2] = z; mSdData.rawData3D[3 * mSdData.mNsamp + 2] = z;
mSdData.mNsamp++; mSdData.mNsamp++;
if (mSdData.mNsamp==NSAMP) { if (mSdData.mNsamp == mSdData.rawData.length) {
// Calculate the sample frequency for this sample, but do not change mSampleFreq, which is used for // Calculate the sample frequency for this sample, but do not change mSampleFreq, which is used for
// analysis - this is because sometimes you get a very long delay (e.g. when disconnecting debugger), // analysis - this is because sometimes you get a very long delay (e.g. when disconnecting debugger),
// which gives a very low frequency which can make us run off the end of arrays in doAnalysis(). // which gives a very low frequency which can make us run off the end of arrays in doAnalysis().
// FIXME - we should do some sort of check and disregard samples with long delays in them. // FIXME - we should do some sort of check and disregard samples with long delays in them.
double dT = 1e-9 * (event.timestamp - mStartTs); double dT = 1e-9 * (event.timestamp - mStartTs);
int sampleFreq = (int) (mSdData.mNsamp / dT); int sampleFreq = (int) (mSdData.mNsamp / dT);
Log.v(TAG,"onSensorChanged(): Collected "+NSAMP+" data points in "+dT+" sec (="+sampleFreq+" Hz) - analysing..."); Log.v(TAG, "onSensorChanged(): Collected " + mSdData.mNsamp + " data points in " + dT + " sec (=" + sampleFreq + " Hz) - analysing...");
// DownSample from the 50Hz received frequency to 25Hz and convert to mg. // DownSample from the 50Hz received frequency to 25Hz and convert to mg.
// FIXME - we should really do this properly rather than assume we are really receiving data at 50Hz. // FIXME - we should really do this properly rather than assume we are really receiving data at 50Hz.
for (int i = 0; i < mSdData.mNsamp; i++) { for (int i = 0; i < mSdData.mNsamp; i++) {
@@ -158,13 +162,17 @@ public class SdDataSourcePhone extends SdDataSource implements SensorEventListen
doAnalysis(); doAnalysis();
mSdData.mNsamp = 0; mSdData.mNsamp = 0;
mStartTs = event.timestamp; mStartTs = event.timestamp;
} else if (mSdData.mNsamp>NSAMP) { } else if (mSdData.mNsamp > mSdData.rawData.length) {
Log.v(TAG, "onSensorChanged(): Received data during analysis - ignoring sample"); Log.v(TAG, "onSensorChanged(): Received data during analysis - ignoring sample");
} }
} else {
mUseNextSample = true;
}
} else { } else {
Log.v(TAG, "onSensorChanged(): ERROR - Mode " + mMode + " unrecognised"); Log.v(TAG, "onSensorChanged(): ERROR - Mode " + mMode + " unrecognised");
} }
} }
} }