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,42 +112,46 @@ 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);
Log.v(TAG,"onSensorChanged(): Collected Data = final TimeStamp="+event.timestamp+", initial TimeStamp="+mStartTs); if (mSdData.mNsamp >= mSdData.rawData.length) {
double dT = 1e-9*(event.timestamp - mStartTs); Log.v(TAG, "onSensorChanged(): Collected Data = final TimeStamp=" + event.timestamp + ", initial TimeStamp=" + mStartTs);
mSdData.mSampleFreq = (int)(mSdData.mNsamp/dT); double dT = 1e-9 * (event.timestamp - mStartTs);
mSdData.mSampleFreq = (int) (mSdData.mNsamp / dT);
mSdData.haveSettings = true; mSdData.haveSettings = true;
Log.v(TAG,"onSensorChanged(): Collected data for "+dT+" sec - calculated sample rate as "+ mSampleFreq +" Hz"); Log.v(TAG, "onSensorChanged(): Collected data for " + dT + " sec - calculated sample rate as " + mSampleFreq + " Hz");
mMode = 1; mMode = 1;
mSdData.mNsamp = 0; mSdData.mNsamp = 0;
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];
float z = event.values[2]; float z = event.values[2];
//Log.v(TAG,"Accelerometer Data Received: x="+x+", y="+y+", z="+z); //Log.v(TAG,"Accelerometer Data Received: x="+x+", y="+y+", z="+z);
mSdData.rawData[mSdData.mNsamp] = sqrt(x*x + y*y + z*z); mSdData.rawData[mSdData.mNsamp] = sqrt(x * x + y * y + z * z);
mSdData.rawData3D[3*mSdData.mNsamp] = x; mSdData.rawData3D[3 * mSdData.mNsamp] = x;
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++) {
mSdData.rawData[i/2] = 1000.*mSdData.rawData[i]/9.81; mSdData.rawData[i / 2] = 1000. * mSdData.rawData[i] / 9.81;
mSdData.rawData3D[i/2] = 1000.*mSdData.rawData3D[i]/9.81; mSdData.rawData3D[i / 2] = 1000. * mSdData.rawData3D[i] / 9.81;
mSdData.rawData3D[i/2 +1] = 1000.*mSdData.rawData3D[i+1]/9.81; mSdData.rawData3D[i / 2 + 1] = 1000. * mSdData.rawData3D[i + 1] / 9.81;
mSdData.rawData3D[i/2 +2] = 1000.*mSdData.rawData3D[i+2]/9.81; mSdData.rawData3D[i / 2 + 2] = 1000. * mSdData.rawData3D[i + 2] / 9.81;
//Log.v(TAG,"i="+i+", rawData="+mSdData.rawData[i]+","+mSdData.rawData[i/2]); //Log.v(TAG,"i="+i+", rawData="+mSdData.rawData[i]+","+mSdData.rawData[i/2]);
} }
mSdData.mNsamp /= 2; mSdData.mNsamp /= 2;
@@ -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 { } else {
Log.v(TAG,"onSensorChanged(): ERROR - Mode "+mMode+" unrecognised"); mUseNextSample = true;
} }
} else {
Log.v(TAG, "onSensorChanged(): ERROR - Mode " + mMode + " unrecognised");
}
} }
} }