mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-10 09:01:55 +01:00
cycling-sensor: added average RPM calculation
This commit is contained in:
parent
5a690b9119
commit
3da4b08e91
@ -6,6 +6,7 @@ import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -28,6 +29,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.cycling.data.AverageCalculator;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.cycling.data.DataAccumulator;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.cycling.protocol.CSCMeasurement;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.cycling.protocol.CSCProtocol;
|
||||
@ -36,6 +38,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
public class GenericCyclingSensorSupport extends AbstractBTLEDeviceSupport {
|
||||
public static String UUID_CSC_MESAUREMENT = "00002a5b-0000-1000-8000-00805f9b34fb";
|
||||
final int AVERAGE_CALCULATION_MIN_TIME_UNITS = 1024 * 3; // a bit more than 3 seconds since sensor counts a seconds in 1024 units
|
||||
private static final String TAG = "GenericCyclingSensorSup";
|
||||
|
||||
private int wheelCircumference;
|
||||
private int saveIntervalMinutes;
|
||||
@ -44,6 +47,7 @@ public class GenericCyclingSensorSupport extends AbstractBTLEDeviceSupport {
|
||||
|
||||
private CSCProtocol protocol;
|
||||
private DataAccumulator accumulator;
|
||||
private AverageCalculator averageCalculator;
|
||||
|
||||
public GenericCyclingSensorSupport() {
|
||||
super(LoggerFactory.getLogger(GenericCyclingSensorSupport.class));
|
||||
@ -51,6 +55,7 @@ public class GenericCyclingSensorSupport extends AbstractBTLEDeviceSupport {
|
||||
|
||||
protocol = new CSCProtocol();
|
||||
accumulator = new DataAccumulator();
|
||||
averageCalculator = new AverageCalculator(accumulator);
|
||||
}
|
||||
|
||||
private void loadPrefs(){
|
||||
@ -87,6 +92,9 @@ public class GenericCyclingSensorSupport extends AbstractBTLEDeviceSupport {
|
||||
long timeOfArrival = System.currentTimeMillis();
|
||||
CSCMeasurement measurement = protocol.parsePacket(timeOfArrival, value);
|
||||
accumulator.captureCSCMeasurement(measurement);
|
||||
double rpm = averageCalculator.calculateAverageRevolutionsPerSecond(3000);
|
||||
|
||||
Log.d(TAG, "handleMeasurement: " + rpm);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,34 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.service.devices.cycling.data;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.cycling.protocol.CSCMeasurement;
|
||||
|
||||
public class AverageCalculator {
|
||||
private final DataAccumulator accumulator;
|
||||
|
||||
public AverageCalculator(DataAccumulator accumulator) {
|
||||
this.accumulator = accumulator;
|
||||
}
|
||||
|
||||
public double calculateAverageRevolutionsPerSecond(long timeSpan){
|
||||
return calculateAverageRevolutionsPerSecond(accumulator.getMeasurementsInTimeSpan(timeSpan));
|
||||
}
|
||||
|
||||
public static double calculateAverageRevolutionsPerSecond(CSCMeasurement[] measurements){
|
||||
CSCMeasurement lastMeasurement = measurements[0];
|
||||
CSCMeasurement firstMeasurement = measurements[measurements.length - 1];
|
||||
|
||||
long timeUnitsDelta = lastMeasurement.lastWheelRevolutionTime - firstMeasurement.lastWheelRevolutionTime;
|
||||
if(timeUnitsDelta == 0){
|
||||
return 0;
|
||||
}
|
||||
while(timeUnitsDelta < 0){
|
||||
timeUnitsDelta += 1024 * 64;
|
||||
}
|
||||
int rotationsDelta = lastMeasurement.wheelRevolutions - firstMeasurement.wheelRevolutions;
|
||||
double factor = 1024;
|
||||
double rotationsPerTimeUnit = (double)rotationsDelta / timeUnitsDelta;
|
||||
double rotationsPerSecond = rotationsPerTimeUnit * factor;
|
||||
|
||||
return rotationsPerSecond;
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@ public class DataAccumulator {
|
||||
public CSCMeasurement[] getMeasurementsInTimeSpan(long timespanMillis) throws NotEnoughMeasurementsException{
|
||||
int listSize = 1;
|
||||
int accumulatedTimeDelta = 0;
|
||||
for(int i = measurements.size() - 2; i > 0; i--){
|
||||
for(int i = measurements.size() - 1; i > 0; i--){
|
||||
CSCMeasurement previousMeasurement;
|
||||
CSCMeasurement currentMeasurement;
|
||||
try{
|
||||
|
Loading…
Reference in New Issue
Block a user