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,22 +701,31 @@ public class MainActivity extends AppCompatActivity {
tv.setTextColor(warnTextColour); tv.setTextColor(warnTextColour);
} }
tv = (TextView) findViewById(R.id.battTv); tv = (TextView) findViewById(R.id.battTv);
tv.setText(getString(R.string.WatchBatteryEquals) if (mConnection.mSdServer.mSdData.dataSourceName.equals("Phone")) {
+ String.valueOf(mConnection.mSdServer.mSdData.batteryPc) + "% / " tv.setText(getString(R.string.WatchBatteryEquals)
+String.valueOf(mConnection.mSdServer.mSdData.phoneBatteryPc)+"%"); + "---% / "
if (mConnection.mSdServer.mSdData.batteryPc <= 10) { + String.valueOf(mConnection.mSdServer.mSdData.phoneBatteryPc) + "%");
tv.setBackgroundColor(alarmColour);
tv.setTextColor(alarmTextColour);
}
if (mConnection.mSdServer.mSdData.batteryPc > 10) {
tv.setBackgroundColor(warnColour);
tv.setTextColor(warnTextColour);
}
if (mConnection.mSdServer.mSdData.batteryPc >= 20) {
tv.setBackgroundColor(okColour); tv.setBackgroundColor(okColour);
tv.setTextColor(okTextColour); tv.setTextColor(okTextColour);
}
} else {
tv.setText(getString(R.string.WatchBatteryEquals)
+ String.valueOf(mConnection.mSdServer.mSdData.batteryPc) + "% / "
+ String.valueOf(mConnection.mSdServer.mSdData.phoneBatteryPc) + "%");
if (mConnection.mSdServer.mSdData.batteryPc <= 10) {
tv.setBackgroundColor(alarmColour);
tv.setTextColor(alarmTextColour);
}
if (mConnection.mSdServer.mSdData.batteryPc > 10) {
tv.setBackgroundColor(warnColour);
tv.setTextColor(warnTextColour);
}
if (mConnection.mSdServer.mSdData.batteryPc >= 20) {
tv.setBackgroundColor(okColour);
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,59 +112,67 @@ 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) {
// mMode=1 is normal operation - collect NSAMP accelerometer data samples, then analyse them by calling doAnalysis(). // The phone gives us 50 Hz sample frequency so we do a crude factor of 2 downsampling.
float x = event.values[0]; if (mUseNextSample) {
float y = event.values[1]; mUseNextSample = false;
float z = event.values[2]; // mMode=1 is normal operation - collect NSAMP accelerometer data samples, then analyse them by calling doAnalysis().
//Log.v(TAG,"Accelerometer Data Received: x="+x+", y="+y+", z="+z); float x = event.values[0];
mSdData.rawData[mSdData.mNsamp] = sqrt(x*x + y*y + z*z); float y = event.values[1];
mSdData.rawData3D[3*mSdData.mNsamp] = x; float z = event.values[2];
mSdData.rawData3D[3*mSdData.mNsamp+1] = y; //Log.v(TAG,"Accelerometer Data Received: x="+x+", y="+y+", z="+z);
mSdData.rawData3D[3*mSdData.mNsamp+2] = z; mSdData.rawData[mSdData.mNsamp] = sqrt(x * x + y * y + z * z);
mSdData.mNsamp++; mSdData.rawData3D[3 * mSdData.mNsamp] = x;
if (mSdData.mNsamp==NSAMP) { mSdData.rawData3D[3 * mSdData.mNsamp + 1] = y;
// Calculate the sample frequency for this sample, but do not change mSampleFreq, which is used for mSdData.rawData3D[3 * mSdData.mNsamp + 2] = z;
// analysis - this is because sometimes you get a very long delay (e.g. when disconnecting debugger), mSdData.mNsamp++;
// which gives a very low frequency which can make us run off the end of arrays in doAnalysis(). if (mSdData.mNsamp == mSdData.rawData.length) {
// FIXME - we should do some sort of check and disregard samples with long delays in them. // Calculate the sample frequency for this sample, but do not change mSampleFreq, which is used for
double dT = 1e-9*(event.timestamp - mStartTs); // analysis - this is because sometimes you get a very long delay (e.g. when disconnecting debugger),
int sampleFreq = (int)(mSdData.mNsamp/dT); // which gives a very low frequency which can make us run off the end of arrays in doAnalysis().
Log.v(TAG,"onSensorChanged(): Collected "+NSAMP+" data points in "+dT+" sec (="+sampleFreq+" Hz) - analysing..."); // FIXME - we should do some sort of check and disregard samples with long delays in them.
// DownSample from the 50Hz received frequency to 25Hz and convert to mg. double dT = 1e-9 * (event.timestamp - mStartTs);
// FIXME - we should really do this properly rather than assume we are really receiving data at 50Hz. int sampleFreq = (int) (mSdData.mNsamp / dT);
for (int i=0; i<mSdData.mNsamp; i++) { Log.v(TAG, "onSensorChanged(): Collected " + mSdData.mNsamp + " data points in " + dT + " sec (=" + sampleFreq + " Hz) - analysing...");
mSdData.rawData[i/2] = 1000.*mSdData.rawData[i]/9.81; // DownSample from the 50Hz received frequency to 25Hz and convert to mg.
mSdData.rawData3D[i/2] = 1000.*mSdData.rawData3D[i]/9.81; // FIXME - we should really do this properly rather than assume we are really receiving data at 50Hz.
mSdData.rawData3D[i/2 +1] = 1000.*mSdData.rawData3D[i+1]/9.81; for (int i = 0; i < mSdData.mNsamp; i++) {
mSdData.rawData3D[i/2 +2] = 1000.*mSdData.rawData3D[i+2]/9.81; mSdData.rawData[i / 2] = 1000. * mSdData.rawData[i] / 9.81;
//Log.v(TAG,"i="+i+", rawData="+mSdData.rawData[i]+","+mSdData.rawData[i/2]); 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 + 2] = 1000. * mSdData.rawData3D[i + 2] / 9.81;
//Log.v(TAG,"i="+i+", rawData="+mSdData.rawData[i]+","+mSdData.rawData[i/2]);
}
mSdData.mNsamp /= 2;
// Set HR and O2Sat values to fault value (-1) to avoid alarms if the user enables HR or O2Sat alarms.
mSdData.mHR = -1;
mSdData.mO2Sat = -1;
doAnalysis();
mSdData.mNsamp = 0;
mStartTs = event.timestamp;
} else if (mSdData.mNsamp > mSdData.rawData.length) {
Log.v(TAG, "onSensorChanged(): Received data during analysis - ignoring sample");
} }
mSdData.mNsamp /= 2;
// Set HR and O2Sat values to fault value (-1) to avoid alarms if the user enables HR or O2Sat alarms. } else {
mSdData.mHR = -1; mUseNextSample = true;
mSdData.mO2Sat = -1;
doAnalysis();
mSdData.mNsamp = 0;
mStartTs = event.timestamp;
} else if (mSdData.mNsamp>NSAMP) {
Log.v(TAG,"onSensorChanged(): Received data during analysis - ignoring sample");
} }
} else { } else {
Log.v(TAG,"onSensorChanged(): ERROR - Mode "+mMode+" unrecognised"); Log.v(TAG, "onSensorChanged(): ERROR - Mode " + mMode + " unrecognised");
} }
} }
} }