Skip to content

Commit

Permalink
fix in frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
jazminsofiaf committed Nov 6, 2021
1 parent f0861c4 commit 5e48be5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 38 deletions.
10 changes: 5 additions & 5 deletions lib/datacollection/measurements_collector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()}');
Expand Down Expand Up @@ -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) {
Expand All @@ -151,10 +151,10 @@ class MeasurementsCollector {

class _ParsedMeasurements {
final int eventNumber;
final double millis;
final int timestampMillis;
final List<String> values;

_ParsedMeasurements(this.eventNumber, this.millis, this.values);
_ParsedMeasurements(this.eventNumber, this.timestampMillis, this.values);

@override
String toString() {
Expand Down
20 changes: 10 additions & 10 deletions lib/datacollection/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class DeviceMeasurementsFile {
String deviceName, String deviceId, String word) async {
var creationDate = DateTime.now();
SensorMeasurements json = new SensorMeasurements(
deviceName, deviceId, word, <List<double>>[], <double>[]);
deviceName, deviceId, word, <List<double>>[], <int>[]);
String datetimeStr = format(creationDate);
var filename = "${deviceName.substring(0, 1)}_${word}_$datetimeStr";
var file = await new GloveEventsStorage().createFile(filename);
Expand Down Expand Up @@ -153,11 +153,11 @@ class SensorMeasurements {
final String deviceName;
final String deviceId;
final String word;
final List<double> millis;
final List<int> timestamps;
final List<List<double>> values;

SensorMeasurements(this.deviceName, this.deviceId, this.word,this.values,
this.millis);
this.timestamps);

bool add(GloveMeasurement gloveMeasurement) {
if (gloveMeasurement.deviceId != this.deviceId) {
Expand All @@ -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;
}

Expand All @@ -191,7 +191,7 @@ class SensorMeasurements {

factory SensorMeasurements.fromJson(dynamic json) {
List<List<double>> _values = <List<double>>[];
List<double> _millis = <double>[];
List<int> _timestamps = <int>[];

if (json['values'] != null) {
var jsonLists = json['values'] as List;
Expand All @@ -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,
);
}

Expand All @@ -219,7 +219,7 @@ class SensorMeasurements {
'device_id': deviceId,
'word': word,
'values': values,
'millis': millis
'timestamps': timestamps
};
}
}
4 changes: 2 additions & 2 deletions lib/edgeimpulse/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions lib/model/glove_measurement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> json)
: deviceId = json['device_id'], eventNum = json['event_num'],
millis = json['millis'],
timestampMillis = json['timestamp_millis'],
pinky = Finger.fromJson(json['pinky'] as Map<String, dynamic>),
ring = Finger.fromJson(json['ring'] as Map<String, dynamic>),
middle = Finger.fromJson(json['middle'] as Map<String, dynamic>),
Expand All @@ -54,7 +54,7 @@ class GloveMeasurement {

Map<String, dynamic> toJson() => {
'device_id': deviceId,
'millis': millis,
'timestampMillis': timestampMillis,
'event_num': eventNum,
'pinky' : pinky.toJson(),
'ring': ring.toJson(),
Expand All @@ -64,7 +64,7 @@ class GloveMeasurement {
};


static fromFingerMeasurementsList(int eventNum, double millis, String deviceId, List<String> fingerMeasurements) {
static fromFingerMeasurementsList(int eventNum, int timestampMillis, String deviceId, List<String> fingerMeasurements) {
Map<String, Finger> measurementsMap = new Map();

for (final item in fingerMeasurements) {
Expand All @@ -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) {
Expand Down
40 changes: 29 additions & 11 deletions lib/pages/ble_data_collection_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class _BleDataCollectionState extends State<BleDataCollectionPage>
});
}),
DataVisualizer(
key: Key("$_collections"), collector: _measurementsCollector),
key: Key("$_collections-$_isRecording"), collector: _measurementsCollector),
Expanded(
child: Align(
alignment: FractionalOffset.bottomCenter,
Expand Down Expand Up @@ -161,6 +161,7 @@ class _BleDataCollectionState extends State<BleDataCollectionPage>
void _stopRecording(BluetoothBackend bluetoothBackend) async {
developer.log('stopRecording');
bluetoothBackend.sendStopCommand();

_isRecording = false;
showDialog(
context: context,
Expand Down Expand Up @@ -313,10 +314,11 @@ class DataVisualizer extends StatefulWidget {
class _DataVisualizerState extends State<DataVisualizer>
with MeasurementsListener {
final MeasurementsCollector collector;
Map<String, GloveStats> _stats;

_DataVisualizerState(this.collector):_stats = Map();

_DataVisualizerState(this.collector);

Map<String, GloveStats> _stats = Map();

@override
void initState() {
Expand Down Expand Up @@ -350,23 +352,39 @@ class _DataVisualizerState extends State<DataVisualizer>
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;
}
}
2 changes: 1 addition & 1 deletion lib/pages/data_visualization_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class _MeasurementsChartState extends State<MeasurementsChart>
@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()));
Expand Down
6 changes: 3 additions & 3 deletions lib/pages/file_content_chart_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ class _MeasurementsChartState extends State<MeasurementsChart> {

@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()));
Expand Down

0 comments on commit 5e48be5

Please sign in to comment.