‹ FSK More SC Workshop Material ›

AndroSensor

2014-10-18 01:14 supercollider

clean-up: #53

Below some code for reading and parsing data from the Android app AndroSensor.

With this free app, you can record a lot of different sensors in your Android phone (like GPS, accelerometer, mic, battery, light etc) and save it to a comma-separated file (CSV). Later one can copy over the data file from the Android phone to a computer and read it in SuperCollider. the CSV files are normally stored on the SD card in a folder called AndroSensor.

By default, the app only saves and display data at slow update rates, so go into the AndroSensor's settings and change update interval to very fast and recording interval to for example 0.05 seconds (the fastest).

// https://play.google.com/store/apps/details?id=com.fivasim.androsensor

//--read the data - edit the path to your CSV file
(
var path= "~/Desktop/Sensor_record_20141018_115750_AndroSensor.csv";  //edit here
var data= CSVFileReader.read(path.standardizePath, delimiter:$;, startRow:1);
var data2= data.flop;
var dict= ();
data2.do{|x|
  var key= x[0];
  var val= x.copyRange(1, x.size-1);
  while({key.last==Char.space or:{key.last==$:}}, {
    key= key.copyRange(0, key.size-2);
  });
  if(val[0].any{|x| #[$:, $/].includes(x)}.not, {
    val= val.asFloat;  //make single numbers into floats
    //}, {  //else do nothing - keep date and satellites as strings
  });
  dict.put(key.asSymbol, val);
};
~dict= dict;  //handle
)

//--list all stored keys...
~dict.keys.do{|x| x.postln}

//--access data stored at keys...
~dict['ACCELEROMETER X (m/s²)']
~dict['ACCELEROMETER Y (m/s²)']
~dict['ACCELEROMETER Z (m/s²)']
~dict['GRAVITY X (m/s²)']
~dict['GRAVITY Y (m/s²)']
~dict['GRAVITY Z (m/s²)']
~dict['GYROSCOPE X (rad/s)']
~dict['GYROSCOPE Y (rad/s)']
~dict['GYROSCOPE Z (rad/s)']
~dict['LIGHT (lux)']
~dict['MAGNETIC FIELD X (μT)']
~dict['MAGNETIC FIELD Y (μT)']
~dict['MAGNETIC FIELD Z (μT)']
~dict['ORIENTATION X (°)']
~dict['ORIENTATION Y (°)']
~dict['ORIENTATION Z (°)']
~dict['PROXIMITY (i)']
~dict['SOUND LEVEL (dB)']
~dict['LOCATION Latitude']
~dict['LOCATION Longitude']
~dict['LOCATION Altitude ( m)']
~dict['LOCATION Speed ( Kmh)']
~dict['LOCATION Accuracy ( m)']
~dict['Satellites in range']  //note as strings
~dict['Temperature (F)']
~dict['Level (%)']
~dict['Voltage (Volt)']
~dict['Time since start in ms']  //timestamps in milliseconds
~dict['YYYY-MO-DD HH-MI-SS_SSS']  //absolute timestamps - note as strings

//--plot the xyz accelerometer data...
(
[
  ~dict['ACCELEROMETER X (m/s²)'],
  ~dict['ACCELEROMETER Y (m/s²)'],
  ~dict['ACCELEROMETER Z (m/s²)']
].plot
)

//--plot the xyz orientation data...
(
[
  ~dict['ORIENTATION X (°)'],
  ~dict['ORIENTATION Y (°)'],
  ~dict['ORIENTATION Z (°)']
].plot
)

//--plot a lot of data (but not all)...
(
[
  'ACCELEROMETER X (m/s²)',
  'ACCELEROMETER Y (m/s²)',
  'ACCELEROMETER Z (m/s²)',
  'GRAVITY X (m/s²)',
  'GRAVITY Y (m/s²)',
  'GRAVITY Z (m/s²)',
  'GYROSCOPE X (rad/s)',
  'GYROSCOPE Y (rad/s)',
  'GYROSCOPE Z (rad/s)',
  'LIGHT (lux)',
  'MAGNETIC FIELD X (μT)',
  'MAGNETIC FIELD Y (μT)',
  'MAGNETIC FIELD Z (μT)',
  'ORIENTATION X (°)',
  'ORIENTATION Y (°)',
  'ORIENTATION Z (°)',
  'SOUND LEVEL (dB)'
].collect{|x| ~dict[x]}.plot;
)

//--sound example mapping accelerometer to simple sound
(
s.waitForBoot{
  var a= {|freq= 500, amp= 0, pan= 0| Pan2.ar(Pulse.ar(freq.lag(0.25), 0.2, amp.lag(0.25)), pan.lag(0.25))}.play;
  s.sync;
  Routine.run({
    var prevTime= 0;
    ~dict['Time since start in ms'].do{|x, i|
      a.set(
        \freq, ~dict['ACCELEROMETER Y (m/s²)'][i].linexp(-20, 20, 100, 1000),
        \amp, ~dict['ACCELEROMETER Z (m/s²)'][i].linlin(-20, 20, 0, 1),
        \pan, ~dict['ACCELEROMETER X (m/s²)'][i].linlin(-20, 20, -1, 1)
      );
      (x-prevTime*0.001).wait;
      prevTime= x;
    };
    "done".postln;
    a.release;
  });
};
)

Attached below is a demo CSV file of me first keeping the phone still, and then shaking it a bit + turning it around. The data recording is only about eight seconds long.

If you plot for example the 3D accelerometer data it will look like this...


‹ FSK More SC Workshop Material ›