Skip to content

Commit

Permalink
Merge pull request #48 from Gloveland/graphic
Browse files Browse the repository at this point in the history
Graphic
  • Loading branch information
jazminsofiaf authored Nov 7, 2021
2 parents 06bbd86 + 9cd7635 commit 94b7a77
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 37 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.elapsedTime,
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 elapsedTime = double.parse(fingerMeasurements.removeAt(0));
return _ParsedMeasurements(eventNum, elapsedTime, 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 elapsedTime;
final int timestampMillis;
final List<String> values;

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

@override
String toString() {
Expand Down
22 changes: 13 additions & 9 deletions lib/datacollection/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ class DeviceMeasurementsFile {
static Future<DeviceMeasurementsFile> create(
String deviceName, String deviceId, String word) async {
var creationDate = DateTime.now();
var values = <List<double>>[];
double intervalSumInMillis = 0.0;
SensorMeasurements json = new SensorMeasurements(
deviceName, deviceId, word, values, intervalSumInMillis);
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 @@ -155,25 +153,25 @@ class SensorMeasurements {
final String deviceName;
final String deviceId;
final String word;
final List<int> timestamps;
final List<List<double>> values;
double intervalSumInMillis;

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

bool add(GloveMeasurement gloveMeasurement) {
if (gloveMeasurement.deviceId != this.deviceId) {
developer.log("wrong deviceId $gloveMeasurement.deviceId");
return false;
}
List<double> measurementList = [];
measurementList.addAll(extractFingerMeasurement(gloveMeasurement.thumb));
measurementList.addAll(extractFingerMeasurement(gloveMeasurement.pinky));
measurementList.addAll(extractFingerMeasurement(gloveMeasurement.ring));
measurementList.addAll(extractFingerMeasurement(gloveMeasurement.middle));
measurementList.addAll(extractFingerMeasurement(gloveMeasurement.index));
measurementList.addAll(extractFingerMeasurement(gloveMeasurement.thumb));
this.values.add(measurementList);
this.intervalSumInMillis = this.intervalSumInMillis + gloveMeasurement.elapsedTimeMs;
this.timestamps.add(gloveMeasurement.timestampMillis);
return true;
}

Expand All @@ -193,19 +191,25 @@ class SensorMeasurements {

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

if (json['values'] != null) {
var jsonLists = json['values'] as List;
_values = jsonLists.map((jsonList) {
var valuesList = jsonList as List;
return valuesList.map((v) => v as double).toList();
}).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,
json['interval_sum_in_millis'] as double,
_timestamps,
);
}

Expand All @@ -215,7 +219,7 @@ class SensorMeasurements {
'device_id': deviceId,
'word': word,
'values': values,
'interval_sum_in_millis': intervalSumInMillis,
'timestamps': timestamps
};
}
}
10 changes: 7 additions & 3 deletions lib/edgeimpulse/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ class EdgeImpulseApiClient {
//the algorithm used to sign this file. Either HS256 (HMAC-SHA256) or none (required)
iat: datetime.toUtc().millisecondsSinceEpoch ~/1000.0// date when the file was created in seconds since epoch
);

double averageIntervalInMilliseconds = sensorMeasurements.intervalSumInMillis / double.parse("${sensorMeasurements.values.length}");
double elapsedTimeSum = 0.0;
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);
developer.log("averageIntervalInMilliseconds: $averageIntervalInMilliseconds", name: TAG);
var payload = Payload(
deviceName: sensorMeasurements.deviceId,
Expand Down Expand Up @@ -52,7 +56,7 @@ class EdgeImpulseApiClient {
request.headers.set('x-api-key', secret.rightGloveApiKey);
}
request.headers.set('x-file-name', fileName);
request.headers.set('x-label', sensorMeasurements.word);
request.headers.set('x-label', sensorMeasurements.word);
request.add(utf8.encode(json.encode(edgeImpulseBody)));
HttpClientResponse response = await request.close();
String reply = await response.transform(utf8.decoder).join();
Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:lsa_gloves/datacollection/file_content.dart';
import 'package:lsa_gloves/pages/ble_connection_error_page.dart';
import 'package:lsa_gloves/pages/ble_devices_connection_page.dart';
import 'package:lsa_gloves/pages/file_content_chart_page.dart';
import 'package:lsa_gloves/pages/home_page.dart';
import 'package:provider/provider.dart';

Expand All @@ -24,6 +25,7 @@ class MyApp extends StatelessWidget {
return MaterialApp(
routes: {
FileContentPage.routeName: (context) => FileContentPage(),
FileContentChartPage.routeName: (context) => FileContentChartPage(),
BleGloveConnectionPage.routeName: (context) => BleGloveConnectionPage(),
BleConnectionErrorPage.routeName: (context) => BleConnectionErrorPage(),
},
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 elapsedTimeMs;
final int timestampMillis;
final Finger thumb;
final Finger index;
final Finger middle;
final Finger ring;
final Finger pinky;

GloveMeasurement(this.deviceId, this.eventNum,this.elapsedTimeMs,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'],
elapsedTimeMs = json['elapsed_time'],
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,
'elapsed_time': elapsedTimeMs,
'timestampMillis': timestampMillis,
'event_num': eventNum,
'pinky' : pinky.toJson(),
'ring': ring.toJson(),
Expand All @@ -64,7 +64,7 @@ class GloveMeasurement {
};


static fromFingerMeasurementsList(int eventNum, double elapsedTime, 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, elapsedTime, 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.elapsedTimeMs);
}
_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.elapsedTimeMs;
lastTimestampMs += measurement.timestampMillis;
Vector3 sensorValues = measurement.getFinger(finger).getSensorValues(sensor);
_measurementsX.add(SeriesEntry(lastTimestampMs, sensorValues.getX()));
_measurementsY.add(SeriesEntry(lastTimestampMs, sensorValues.getY()));
Expand Down
Loading

0 comments on commit 94b7a77

Please sign in to comment.