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:
@@ -701,22 +701,31 @@ public class MainActivity extends AppCompatActivity {
|
||||
tv.setTextColor(warnTextColour);
|
||||
}
|
||||
tv = (TextView) findViewById(R.id.battTv);
|
||||
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) {
|
||||
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)
|
||||
+ 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
|
||||
// We start off with it set to OK, then check for several different abnormal conditions
|
||||
|
||||
@@ -47,8 +47,6 @@ import static java.lang.Math.sqrt;
|
||||
public class SdDataSourcePhone extends SdDataSource implements SensorEventListener {
|
||||
private String TAG = "SdDataSourcePhone";
|
||||
|
||||
|
||||
private final static int NSAMP = 250;
|
||||
private SensorManager mSensorManager;
|
||||
private Sensor mSensor;
|
||||
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;
|
||||
public double mSampleFreq = 0;
|
||||
|
||||
private boolean mUseNextSample = true;
|
||||
|
||||
|
||||
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.
|
||||
if (mMode == 0) {
|
||||
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");
|
||||
mStartEvent = event;
|
||||
mStartTs = event.timestamp;
|
||||
@@ -112,59 +112,67 @@ public class SdDataSourcePhone extends SdDataSource implements SensorEventListen
|
||||
} else {
|
||||
mSdData.mNsamp ++;
|
||||
}
|
||||
if (mSdData.mNsamp>=250) {
|
||||
Log.v(TAG,"onSensorChanged(): Collected Data = final TimeStamp="+event.timestamp+", initial TimeStamp="+mStartTs);
|
||||
double dT = 1e-9*(event.timestamp - mStartTs);
|
||||
mSdData.mSampleFreq = (int)(mSdData.mNsamp/dT);
|
||||
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);
|
||||
double dT = 1e-9 * (event.timestamp - mStartTs);
|
||||
mSdData.mSampleFreq = (int) (mSdData.mNsamp / dT);
|
||||
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;
|
||||
mSdData.mNsamp = 0;
|
||||
mStartTs = event.timestamp;
|
||||
}
|
||||
} else if (mMode==1) {
|
||||
// mMode=1 is normal operation - collect NSAMP accelerometer data samples, then analyse them by calling doAnalysis().
|
||||
float x = event.values[0];
|
||||
float y = event.values[1];
|
||||
float z = event.values[2];
|
||||
//Log.v(TAG,"Accelerometer Data Received: 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+1] = y;
|
||||
mSdData.rawData3D[3*mSdData.mNsamp+2] = z;
|
||||
mSdData.mNsamp++;
|
||||
if (mSdData.mNsamp==NSAMP) {
|
||||
// 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),
|
||||
// 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.
|
||||
double dT = 1e-9*(event.timestamp - mStartTs);
|
||||
int sampleFreq = (int)(mSdData.mNsamp/dT);
|
||||
Log.v(TAG,"onSensorChanged(): Collected "+NSAMP+" data points in "+dT+" sec (="+sampleFreq+" Hz) - analysing...");
|
||||
// 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.
|
||||
for (int i=0; i<mSdData.mNsamp; i++) {
|
||||
mSdData.rawData[i/2] = 1000.*mSdData.rawData[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 +2] = 1000.*mSdData.rawData3D[i+2]/9.81;
|
||||
//Log.v(TAG,"i="+i+", rawData="+mSdData.rawData[i]+","+mSdData.rawData[i/2]);
|
||||
} 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().
|
||||
float x = event.values[0];
|
||||
float y = event.values[1];
|
||||
float z = event.values[2];
|
||||
//Log.v(TAG,"Accelerometer Data Received: 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 + 1] = y;
|
||||
mSdData.rawData3D[3 * mSdData.mNsamp + 2] = z;
|
||||
mSdData.mNsamp++;
|
||||
if (mSdData.mNsamp == mSdData.rawData.length) {
|
||||
// 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),
|
||||
// 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.
|
||||
double dT = 1e-9 * (event.timestamp - mStartTs);
|
||||
int sampleFreq = (int) (mSdData.mNsamp / dT);
|
||||
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.
|
||||
// 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++) {
|
||||
mSdData.rawData[i / 2] = 1000. * mSdData.rawData[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 + 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.
|
||||
mSdData.mHR = -1;
|
||||
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 {
|
||||
mUseNextSample = true;
|
||||
}
|
||||
|
||||
} else {
|
||||
Log.v(TAG,"onSensorChanged(): ERROR - Mode "+mMode+" unrecognised");
|
||||
Log.v(TAG, "onSensorChanged(): ERROR - Mode " + mMode + " unrecognised");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user