Skip to content

Instantly share code, notes, and snippets.

@scottgwald
Last active August 29, 2015 13:57
Show Gist options
  • Save scottgwald/9612993 to your computer and use it in GitHub Desktop.
Save scottgwald/9612993 to your computer and use it in GitHub Desktop.
[wearscript] gather magnetic field data

How to use

Overall steps:

  • Copy/paste glass.html into the WearScript playground to run data collection for a while.
  • When ready, launch the Stop card in WearScript to halt data collection.
  • On your computer, clone this gist: git clone https://gist.github.com/9612993.git
  • Enter the directory with the code: cd 9612993
  • Plug in Glass / phone / tablet and make sure adb has recognized it adb devices.
  • Run script chmod +x unpack.sh && ./unpack.sh
  • This generates a data file called output.dat.
  • Done!
<html style="width:100%; height:100%; overflow:hidden">
<head>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.0/zepto.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>-->
</head>
<body style="width:100%; height:100%; overflow:hidden; margin:0">
<canvas id="canvas" width="640" height="360" style="display:block"></canvas>
<script>
function cb(data) {
// Changes canvas color with head rotation
if (data['type'] == WS.sensor('magneticField')) {
context.fillStyle = 'hsl(' + data['values'][0] + ', 90%, 50%)';
context.fillRect(0, 0, 640, 120);
context.fillStyle = 'hsl(' + data['values'][1] + ', 90%, 50%)';
context.fillRect(0, 120, 640, 120);
context.fillStyle = 'hsl(' + data['values'][2] + ', 90%, 50%)';
context.fillRect(0, 240, 640, 120);
}
}
function server() {
WS.log('Gather magnetic field data');
WS.say('Gather magnetic field data');
var sensors = ['magneticField'];
for (var i = 0; i < sensors.length; i++)
WS.sensorOn(WS.sensor(sensors[i]), .15, 'cb');
WS.dataLog(true, false, 1);
}
function main() {
if (WS.scriptVersion(1)) return;
context = document.getElementById('canvas').getContext("2d");
WS.serverConnect('{{WSUrl}}', 'server');
}
window.onload = main;
</script>
</body>
</html>
{
"name": "Magnetic Field Data"
}
adb pull /sdcard/wearscript/data
chmod +x unpackConcat.py
python unpackConcat.py
#! /usr/local/bin/python
import os
import msgpack
import glob
import collections
outfilePath = 'output.dat'
# For Nexus 4
sensorName = 'LGE Magnetometer Sensor'
# For Glass
# sensorName = 'MPL Magnetic Field'
print "Concatenating your magnetic field data from msgpack files"
print "Format: magX magY magZ timestamp unknown"
def flatten(l):
for el in l:
if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
for sub in flatten(el):
yield sub
else:
yield el
filePaths = glob.glob("*.msgpack")
testPath = filePaths[-1]
dat = msgpack.load(open(testPath))
# print dat
outfile = open(outfilePath, 'w')
isFirst = True;
for filePath in filePaths:
dat = msgpack.load(open(filePath))
if dat[2].has_key(sensorName):
for elem in dat[2][sensorName]:
elFlat = flatten(elem)
elemStr = map(str, elFlat)
bigStr = " ".join(elemStr)
if isFirst:
print "Sanity check. First line of data is: "
print bigStr
outfile.write(bigStr + "\n")
isFirst = False
outfile.close()
b = os.path.getsize(outfilePath)
l = sum(1 for line in open(outfilePath))
print "Wrote %s lines and %s bytes to %s" %(l, b, outfilePath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment