Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Best way to gather large chunks of sensor data?

I am trying to build an app that, while active, continuously collects the data from the sensors on board. What I want to get is accelerometer and gyro data for extended periods of time (>1 min possible an hour) but when I use the examples for sensor data with batches the app crashes after just a few seconds. There are a few things that I feel could be improved. Such as I currently do a separate .onreading for each sensor...

 

hrm.onreading = function() {
  sensorData = reducer(sensorData, "ADD_HEARTRATE", hrm.readings)
}
gyro.onreading = function() {  
  sensorData = reducer(sensorData, "ADD_GYROSCOPE", gyro.readings)
};
accel.onreading = function() {  
  sensorData = reducer(sensorData, "ADD_ACCELEROMETER", accel.readings)
};

obviously one unified would be better. There isn't a general fitbit web api for these sensor data which is why the idea of the ionic's sdk appealed to me so much. 

 

Is there an example of an app that does something similar to this or a set of best practices for gathering large amounts of data? Perhaps sending it to internal storage? 

Best Answer
0 Votes
4 REPLIES 4

Hey @nstrayer, I don't have an answer for your unified onreading solution, but I just wanted to confirm that the code you posted is not your actual working code (as it doesn't appear to be correct and could be the reason for your crashes).  If you would post a link to your actual code we might be able to pick out what's causing the crashing.

Best Answer
0 Votes

Oh, the code I posted was just meant to be an example of multiple onreads. 

 

Here's the full code for index.js

let document = require("document");
import { HeartRateSensor } from "heart-rate";
import { Gyroscope } from "gyroscope";
import { Accelerometer } from "accelerometer";


// constants
const sensorOpts = {
  frequency: 2, // times a second to get reading
  batch: 4      // number of readings before emiting batch
}; 

// Object to store the history of the sensors
let sensorData = {
  heartrate: [],
  gyroscope: [],
  accelerometer: [],
};

// Fetch UI elements we will need to change
const accelLabel = document.getElementById("accel");
const gyroLabel = document.getElementById("gyro");
const hrLabel = document.getElementById("hr");

// initialize the sensor objects
const hrm = new HeartRateSensor(sensorOpts);
const gyro = new Gyroscope(sensorOpts);
const accel = new Accelerometer(sensorOpts);

// for each sensor update the variables on each reading
hrm.onreading = function() {
  sensorData.heartrate = [...sensorData.heartrate, ...hrm.readings]
  console.log('adding heartrate data')
}

gyro.onreading = function() {  
  sensorData.gyroscope = [...sensorData.gyroscope, ...gyro.readings];
  console.log('adding gyroscope data')
};

accel.onreading = function() {  
  sensorData.accelerometer = [...sensorData.accelerometer, ...accel.readings];
  console.log('adding Accelerometer data')
};

function updateDisplay(){
  hrLabel.innerText = `HR readings: ${sensorData.heartrate.length}`;
  gyroLabel.innerText = `Gyro readings: ${sensorData.gyroscope.length}`;
  accelLabel.innerText = `Accel readings: ${sensorData.accelerometer.length}`
};

// Begin monitoring the sensors
hrm.start();
gyro.start();
accel.start();
setInterval(updateDisplay, 300);

and here's the index.gui

<svg>
  <text id="welcome" x="0" y="50"  class="large-value">Sensors!</text>
  <text id="gyro"    x="0" y="180" class="label">Gyro readings: ___</text>
  <text id="accel"   x="0" y="240" class="label">Accel readings: ___</text>
  <text id='hr'      x="0" y="120" class="label">HR readings: ___</text>
</svg>

This works for about 15 seconds and then crashes. I'm assuming it's just because the watch runs out of ram, although at 15 seconds we only have a few hundred total values stored, so perhaps it's something else. 

 

Thanks a ton for your help!

 

Best Answer
0 Votes

I'd probably reduce your example down to 1 sensor, does it still crash?

 

You can use the memory pressure API to see if you're running out of memory.

 

Also, in the onreading, are you iterating each reading?

 

accel.onreading = function (e) {
    accel.readings.forEach(function (reading) {
        console.log(reading.x);
//etc }); };
Best Answer
0 Votes

I just saw the memory pressure API. I will use that. 

One sensor crashes, just slower. 

 

For each reading, I appending the new reads to my overall array. I'm just using the array decomposition ES6 syntax to do it as opposed to concat or a foreach append. 

Best Answer
0 Votes