Skip to content

Commit 8422f96

Browse files
author
Russell Hay
committed
Acceleration streams to an epoch chart
1 parent 1adde3a commit 8422f96

File tree

8 files changed

+55
-63
lines changed

8 files changed

+55
-63
lines changed

client/pyr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function render() {
3030
}
3131

3232
var socket = io.connect()
33-
socket.on('position', function (data) {
33+
socket.on('pyr', function (data) {
3434
pitch = data.pitch
3535
yaw = -data.yaw
3636
roll = -data.roll

client/raw.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
<html>
22
<head>
33
<title>Raw IMU - example</title>
4+
<link rel="stylesheet" href="epoch.min.css">
45
<style>
5-
body { margin: 0; }
6-
canvas { width: 100%; height: 100% }
6+
.epoch { height: 200px; }
77
</style>
88
</head>
99
<body>
10+
<div id="controls">
11+
<button id="start_record" data-event="start">Start</button>
12+
<button id="stop_record" data-event="stop" class="disabled">Stop</button>
13+
</div>
14+
<div id="charts">
15+
<div id="accel" class="epoch small_chart"></div>
16+
<div id="gyro" class="epoch small_chart"></div>
17+
<div id="mag" class="epoch small_chart"></div>
18+
<div id="display">
19+
</div>
1020
<script src="/socket.io/socket.io.js"></script>
1121
<script src="raw.js"></script>
1222
</body>

client/raw.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
/* jshint browserify: true */
2+
/* jshint jquery: true */
3+
/* jshint unused: false */
24
/* global io */
3-
var three = require('three'),
4-
scene = new three.Scene(),
5-
camera = new three.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000),
6-
renderer = new three.WebGLRenderer(),
7-
light = new three.DirectionalLight(0xffff00, 0.5),
8-
pitch = 0,
9-
yaw = 0,
10-
roll = 0
5+
var d3 = require('d3')
6+
window.jQuery = require('jquery')
7+
require('../contrib/epoch/epoch.min.js')
118

12-
renderer.setSize( window.innerWidth, window.innerHeight)
13-
document.body.appendChild(renderer.domElement)
9+
var accelData = [
10+
{
11+
label: 'x',
12+
values: [ { time: 0, y: 0 } ]
13+
},
14+
{
15+
label: 'y',
16+
values: [ { time: 0, y: 0 } ]
17+
},
18+
{
19+
label: 'z',
20+
values: [ { time: 0, y: 0 } ]
21+
}
22+
]
1423

15-
var geometry = new three.BoxGeometry(1, 1, 1),
16-
material = new three.MeshLambertMaterial( { color: 0x00ff00 }),
17-
cube = new three.Mesh(geometry, material)
18-
19-
scene.add( cube )
20-
camera.position.z = 5
21-
light.position.set(0,1, 5)
22-
scene.add(light)
23-
24-
function render() {
25-
requestAnimationFrame( render )
26-
cube.rotation.x = roll
27-
cube.rotation.y = yaw
28-
cube.rotation.z = pitch
29-
renderer.render( scene, camera)
30-
}
24+
var accelChart = $('#accel').epoch({
25+
type: 'time.line',
26+
data: accelData
27+
})
3128

3229
var socket = io.connect()
3330
socket.on('position', function (data) {
34-
pitch = data.pitch
35-
yaw = -data.yaw
36-
roll = -data.roll
31+
accelChart.push([
32+
{time: data.time, y: data.accel_x},
33+
{time: data.time, y: data.accel_y},
34+
{time: data.time, y: data.accel_z}
35+
])
3736
})
38-
39-
render()

gulp/raw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ gulp.task('raw-app', [], function() {
1616
.pipe(gulp.dest('public'))
1717
})
1818

19-
gulp.task('raw', ['raw-app'], function() {
19+
gulp.task('raw', ['raw-app', 'epoch'], function() {
2020
var manifest = gulp.src('./public/raw-manifest.json')
2121

2222
return gulp.src('./client/raw.html')

lib/imuduino.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ function Imuduino(peripheral_id, packet_type) {
1616
}
1717

1818
peripheral_id = peripheral_id || service_info.peripheralUUID
19-
this.parser = require(parser_mappings[packet_type] || DEFAULT_PARSER)
19+
packet_type = (packet_type && parser_mappings[packet_type]) || DEFAULT_PARSER
20+
console.log(packet_type)
21+
this.parser = require(packet_type)
2022

2123
var self = this
2224
this.buffer = []

lib/pitch-roll-yaw.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function parse_string(input, cb) {
99
var data = input.split('|')
1010
var p = _.zipObject(PROPS,
1111
_.map(data, function (n) { return parseFloat(n) }))
12+
p.type = 'pyr'
1213
if (cb instanceof Function) {
1314
cb(p)
1415
}

lib/raw-imu.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ function parse_int(data, index) {
2222
return data.readInt16LE(index)
2323
}
2424

25-
function parse_packet(data, type, fields) {
25+
function parse_packet(data, type_, fields) {
2626
var retval = {
27-
'type': type
27+
type: type_
2828
}
2929
var index = 1
3030
for(var i=0; i < fields.length; i++) {

server.js

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
var ble_package_name = './lib/imuduino',
2-
record = false,
3-
record_filename = process.env.npm_package_config_playback_file || 'imu.record'
4-
5-
switch (process.env.npm_package_config_mode) {
6-
case 'record':
7-
console.log('running in record mode')
8-
record = true
9-
break
10-
case 'playback':
11-
console.log('running with playback: ' + record_filename)
12-
ble_package_name = './lib/playback_imuduino'
13-
break
14-
}
1+
var ble_package_name = './lib/imuduino'
152

163
var express = require('express'),
174
app = express(),
185
server = require('http').Server(app),
196
io = require('socket.io')(server),
207
ble = require(ble_package_name),
218
_ = require('lodash'),
22-
fs = require('fs'),
239
connection = null,
2410
radianScale = (Math.PI / 180.0)
2511

@@ -29,24 +15,20 @@ console.log('server url http://localhost:' + 4200)
2915

3016
app.use(express.static('public'))
3117

32-
if (record) {
33-
if (fs.existsSync(record_filename)) {
34-
fs.renameSync(record_filename, record_filename + '.old')
35-
}
36-
}
3718

3819
function degreeToRadians(n) {
20+
if (isNaN(n)) {
21+
return n
22+
}
3923
return n * radianScale
4024
}
4125

4226
var IMUduino = new ble()
4327
IMUduino.on('packet', function (p) {
4428
p = _.mapValues(p, degreeToRadians)
45-
if (record) {
46-
p.duration = (new Date().getTime()) - start
47-
fs.appendFileSync(record_filename, JSON.stringify(p) + '\n')
48-
}
49-
io.emit('position', p)
29+
p.time = new Date().getTime()
30+
p.duration = (new Date().getTime()) - start
31+
io.emit(p.type || 'unknown', p)
5032
start = new Date().getTime() // Times are deltas
5133
})
5234

0 commit comments

Comments
 (0)