From 5e48be53edb7b0dff83fff9cddf45411806615db Mon Sep 17 00:00:00 2001 From: Jazmin Ferreiro Date: Sat, 6 Nov 2021 20:18:49 -0300 Subject: [PATCH] fix in frequency --- .../measurements_collector.dart | 10 ++--- lib/datacollection/storage.dart | 20 +++++----- lib/edgeimpulse/api_client.dart | 4 +- lib/model/glove_measurement.dart | 12 +++--- lib/pages/ble_data_collection_page.dart | 40 ++++++++++++++----- lib/pages/data_visualization_page.dart | 2 +- lib/pages/file_content_chart_page.dart | 6 +-- 7 files changed, 56 insertions(+), 38 deletions(-) diff --git a/lib/datacollection/measurements_collector.dart b/lib/datacollection/measurements_collector.dart index b352bf3..b6ad1be 100644 --- a/lib/datacollection/measurements_collector.dart +++ b/lib/datacollection/measurements_collector.dart @@ -91,7 +91,7 @@ class MeasurementsCollector { GloveMeasurement gloveMeasurement = GloveMeasurement.fromFingerMeasurementsList( parsedMeasurements.eventNumber, - parsedMeasurements.millis, + parsedMeasurements.timestampMillis, deviceId, parsedMeasurements.values); // developer.log('map to -> ${gloveMeasurement.toJson().toString()}'); @@ -138,8 +138,8 @@ class MeasurementsCollector { return null; } int eventNum = int.parse(fingerMeasurements.removeAt(0)); - double millis = double.parse(fingerMeasurements.removeAt(0)); - return _ParsedMeasurements(eventNum, millis, fingerMeasurements); + int timestampMillis = int.parse(fingerMeasurements.removeAt(0)); + return _ParsedMeasurements(eventNum, timestampMillis, fingerMeasurements); } void _notifyListeners(GloveMeasurement measurement) { @@ -151,10 +151,10 @@ class MeasurementsCollector { class _ParsedMeasurements { final int eventNumber; - final double millis; + final int timestampMillis; final List values; - _ParsedMeasurements(this.eventNumber, this.millis, this.values); + _ParsedMeasurements(this.eventNumber, this.timestampMillis, this.values); @override String toString() { diff --git a/lib/datacollection/storage.dart b/lib/datacollection/storage.dart index 27e8b50..49a2ede 100644 --- a/lib/datacollection/storage.dart +++ b/lib/datacollection/storage.dart @@ -71,7 +71,7 @@ class DeviceMeasurementsFile { String deviceName, String deviceId, String word) async { var creationDate = DateTime.now(); SensorMeasurements json = new SensorMeasurements( - deviceName, deviceId, word, >[], []); + deviceName, deviceId, word, >[], []); String datetimeStr = format(creationDate); var filename = "${deviceName.substring(0, 1)}_${word}_$datetimeStr"; var file = await new GloveEventsStorage().createFile(filename); @@ -153,11 +153,11 @@ class SensorMeasurements { final String deviceName; final String deviceId; final String word; - final List millis; + final List timestamps; final List> values; SensorMeasurements(this.deviceName, this.deviceId, this.word,this.values, - this.millis); + this.timestamps); bool add(GloveMeasurement gloveMeasurement) { if (gloveMeasurement.deviceId != this.deviceId) { @@ -171,7 +171,7 @@ class SensorMeasurements { measurementList.addAll(extractFingerMeasurement(gloveMeasurement.middle)); measurementList.addAll(extractFingerMeasurement(gloveMeasurement.index)); this.values.add(measurementList); - this.millis.add(gloveMeasurement.millis); + this.timestamps.add(gloveMeasurement.timestampMillis); return true; } @@ -191,7 +191,7 @@ class SensorMeasurements { factory SensorMeasurements.fromJson(dynamic json) { List> _values = >[]; - List _millis = []; + List _timestamps = []; if (json['values'] != null) { var jsonLists = json['values'] as List; @@ -200,16 +200,16 @@ class SensorMeasurements { return valuesList.map((v) => v as double).toList(); }).toList(); } - if (json['millis'] != null) { - var jsonLists = json['millis'] as List; - _millis = jsonLists.map((v) => v as double).toList(); + if (json['timestamps'] != null) { + var jsonLists = json['timestamps'] as List; + _timestamps = jsonLists.map((v) => v as int).toList(); } return SensorMeasurements( json['device_name'] as String, json['device_id'] as String, json['word'] as String, _values, - _millis, + _timestamps, ); } @@ -219,7 +219,7 @@ class SensorMeasurements { 'device_id': deviceId, 'word': word, 'values': values, - 'millis': millis + 'timestamps': timestamps }; } } diff --git a/lib/edgeimpulse/api_client.dart b/lib/edgeimpulse/api_client.dart index 00c5ac2..0899d62 100644 --- a/lib/edgeimpulse/api_client.dart +++ b/lib/edgeimpulse/api_client.dart @@ -22,8 +22,8 @@ class EdgeImpulseApiClient { iat: datetime.toUtc().millisecondsSinceEpoch ~/1000.0// date when the file was created in seconds since epoch ); double elapsedTimeSum = 0.0; - for(var i = sensorMeasurements.millis.length -1 ; i > 0 ; i--){ - var elapsedTime = sensorMeasurements.millis[i] - sensorMeasurements.millis[i-1]; + for(var i = sensorMeasurements.timestamps.length -1 ; i > 0 ; i--){ + var elapsedTime = sensorMeasurements.timestamps[i] - sensorMeasurements.timestamps[i-1]; elapsedTimeSum += elapsedTime ; } double averageIntervalInMilliseconds = elapsedTimeSum / (sensorMeasurements.values.length * 1.0); diff --git a/lib/model/glove_measurement.dart b/lib/model/glove_measurement.dart index 793fad7..a822900 100644 --- a/lib/model/glove_measurement.dart +++ b/lib/model/glove_measurement.dart @@ -34,18 +34,18 @@ class GloveMeasurement { final String deviceId; final int eventNum; - final double millis; + final int timestampMillis; final Finger thumb; final Finger index; final Finger middle; final Finger ring; final Finger pinky; - GloveMeasurement(this.deviceId, this.eventNum,this.millis,this.pinky, this.ring, this.middle, this.index, this.thumb); + GloveMeasurement(this.deviceId, this.eventNum,this.timestampMillis,this.pinky, this.ring, this.middle, this.index, this.thumb); GloveMeasurement.fromJson(Map json) : deviceId = json['device_id'], eventNum = json['event_num'], - millis = json['millis'], + timestampMillis = json['timestamp_millis'], pinky = Finger.fromJson(json['pinky'] as Map), ring = Finger.fromJson(json['ring'] as Map), middle = Finger.fromJson(json['middle'] as Map), @@ -54,7 +54,7 @@ class GloveMeasurement { Map toJson() => { 'device_id': deviceId, - 'millis': millis, + 'timestampMillis': timestampMillis, 'event_num': eventNum, 'pinky' : pinky.toJson(), 'ring': ring.toJson(), @@ -64,7 +64,7 @@ class GloveMeasurement { }; - static fromFingerMeasurementsList(int eventNum, double millis, String deviceId, List fingerMeasurements) { + static fromFingerMeasurementsList(int eventNum, int timestampMillis, String deviceId, List fingerMeasurements) { Map measurementsMap = new Map(); for (final item in fingerMeasurements) { @@ -80,7 +80,7 @@ class GloveMeasurement { Finger? middle = measurementsMap[middleLetter]; Finger? index = measurementsMap[indexLetter]; Finger? thumb = measurementsMap[thumbLetter]; - return new GloveMeasurement(deviceId, eventNum, millis, pinky!, ring!, middle!, index!, thumb!); + return new GloveMeasurement(deviceId, eventNum, timestampMillis, pinky!, ring!, middle!, index!, thumb!); } Finger getFinger(FingerValue fingerName) { diff --git a/lib/pages/ble_data_collection_page.dart b/lib/pages/ble_data_collection_page.dart index ff8fd81..8f15ecc 100644 --- a/lib/pages/ble_data_collection_page.dart +++ b/lib/pages/ble_data_collection_page.dart @@ -77,7 +77,7 @@ class _BleDataCollectionState extends State }); }), DataVisualizer( - key: Key("$_collections"), collector: _measurementsCollector), + key: Key("$_collections-$_isRecording"), collector: _measurementsCollector), Expanded( child: Align( alignment: FractionalOffset.bottomCenter, @@ -161,6 +161,7 @@ class _BleDataCollectionState extends State void _stopRecording(BluetoothBackend bluetoothBackend) async { developer.log('stopRecording'); bluetoothBackend.sendStopCommand(); + _isRecording = false; showDialog( context: context, @@ -313,10 +314,11 @@ class DataVisualizer extends StatefulWidget { class _DataVisualizerState extends State with MeasurementsListener { final MeasurementsCollector collector; + Map _stats; + + _DataVisualizerState(this.collector):_stats = Map(); - _DataVisualizerState(this.collector); - Map _stats = Map(); @override void initState() { @@ -350,23 +352,39 @@ class _DataVisualizerState extends State setState(() { if (!_stats.containsKey(measurement.deviceId)) { _stats[measurement.deviceId] = GloveStats(); - } else { - _stats[measurement.deviceId]?.update(measurement.millis); } + _stats[measurement.deviceId]?.update(measurement.eventNum, measurement.timestampMillis); }); } } class GloveStats { - double accumulatedTimeMs = 0; - int eventNumber = 0; - - void update(double elapsedTimeMs) { + static const String TAG = 'GloveStats'; + int initialMs = -1; + int accumulatedTimeMs = 1; + int eventNumber = 1; + + void update(int gloveEventNum, int timestampMillis) { + if(this.initialMs < 0 ){ + this.initialMs = timestampMillis; + developer.log("set initialMs -> $initialMs", name: TAG); + } + if(gloveEventNum != this.eventNumber ){ + developer.log("se perdieron eventos ?", name: TAG); + developer.log("eventNumber: $eventNumber gloveEventNum: $gloveEventNum", name: TAG); + } eventNumber++; - accumulatedTimeMs = accumulatedTimeMs + elapsedTimeMs; + accumulatedTimeMs = timestampMillis; } double getFrequency() { - return 1000 * eventNumber / accumulatedTimeMs; + double elapsedTime = ((accumulatedTimeMs - initialMs) * 1.0); + developer.log("elapsedTime = $accumulatedTimeMs - $initialMs= $elapsedTime", name: TAG); + if(elapsedTime < 1){ + return 0.0; + } + double frequency = 1000.0 * eventNumber / elapsedTime; + developer.log("frequency= 1000 * $eventNumber /$elapsedTime = $frequency", name: TAG); + return frequency; } } diff --git a/lib/pages/data_visualization_page.dart b/lib/pages/data_visualization_page.dart index 5e86038..fe11b9c 100644 --- a/lib/pages/data_visualization_page.dart +++ b/lib/pages/data_visualization_page.dart @@ -196,7 +196,7 @@ class _MeasurementsChartState extends State @override void onMeasurement(GloveMeasurement measurement) { measurementsBuffer.add(measurement); - lastTimestampMs += measurement.millis; + lastTimestampMs += measurement.timestampMillis; Vector3 sensorValues = measurement.getFinger(finger).getSensorValues(sensor); _measurementsX.add(SeriesEntry(lastTimestampMs, sensorValues.getX())); _measurementsY.add(SeriesEntry(lastTimestampMs, sensorValues.getY())); diff --git a/lib/pages/file_content_chart_page.dart b/lib/pages/file_content_chart_page.dart index be02a45..d26745d 100644 --- a/lib/pages/file_content_chart_page.dart +++ b/lib/pages/file_content_chart_page.dart @@ -131,12 +131,12 @@ class _MeasurementsChartState extends State { @override Widget build(BuildContext context) { - var initialTimestamp = 0.0; + double initialTimestamp = 0.0; for (var i = 0; i < this._measurements.values.length; i++) { if (i == 0) { - initialTimestamp = this._measurements.millis[i]; + initialTimestamp = this._measurements.timestamps[i] as double; } - var timestamp = (this._measurements.millis[i] - initialTimestamp); + var timestamp = (this._measurements.timestamps[i] - initialTimestamp); var fingerValues = getFingerValues(this._measurements.values[i]); var sensorValues = this.getSensorValues(fingerValues); _measurementsX.add(SeriesEntry(timestamp, sensorValues.getX()));