From 76adadba57c4d63cdb4327b73db467d30c5512d6 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Tue, 23 Oct 2012 10:10:03 +0300 Subject: [PATCH 1/2] Use serialport data from TI Launchpad instead of keystrokes. --- README.md | 14 ++++----- bicyclesim.sublime-project | 5 +--- client/bicyclesim-meteor.html | 2 +- client/main.sim.js | 53 +++++++++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index eb7b1ab..12e695e 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,9 @@ a speed sensor. ## Speed sensor -Most basic speed sensor would be "Keyboard" that sends a keypress whenever -the wheel has turned one revolution. -This kind of sensor can be built from a old keyboard by soldering a reed switch -to right conductors at keyboard circuit board. Some instructions here: -http://www.instructables.com/id/Hacking-a-USB-Keyboard/. +Most basic speed sensor would be "Keyboard" that sends a keypress whenever the wheel has turned one revolution. +~~This kind of sensor can be built from a old keyboard by soldering a reed switch to right conductors at keyboard circuit board. Some instructions here: http://www.instructables.com/id/Hacking-a-USB-Keyboard/.~~ +Doesn't work. The reed switch wont be closed long enough for the keyboard controller or computer to register wheel revolutions. -It is also possible to built equivalent device from Arduino Uno: -http://mitchtech.net/arduino-usb-hid-keyboard/. -Or more easily with the new Arduino Due: -http://www.i-programmer.info/news/91-hardware/4965-new-powerful-arduino-due-.html. +It should be possible to build working device from Arduino Uno (http://mitchtech.net/arduino-usb-hid-keyboard/) or Arduino Due (http://www.i-programmer.info/news/91-hardware/4965-new-powerful-arduino-due-.html). +Device should read the reed switch status every 1ms and send keypress maybe every 500ms (send 'a' if there was one wheel revolution since last keypress, send 'b' if two etc). diff --git a/bicyclesim.sublime-project b/bicyclesim.sublime-project index d2dd325..497e382 100644 --- a/bicyclesim.sublime-project +++ b/bicyclesim.sublime-project @@ -11,10 +11,7 @@ ".cache", ".meteor" ], - "path": "/home/juho/Source/bicyclesim" - }, - { - "path": "/home/juho/Source/bicyclesim-bundle" + "path": "." } ] } diff --git a/client/bicyclesim-meteor.html b/client/bicyclesim-meteor.html index bc76884..73ecedf 100644 --- a/client/bicyclesim-meteor.html +++ b/client/bicyclesim-meteor.html @@ -98,7 +98,7 @@
Asetukset - + diff --git a/client/main.sim.js b/client/main.sim.js index 6441696..b818bea 100644 --- a/client/main.sim.js +++ b/client/main.sim.js @@ -41,14 +41,14 @@ $(document).bind('keydown.space', function () { } }); -function speedo() { - speed_buffer.push(revs * c()); - revs = 0; +// function speedo() { +// speed_buffer.push(revs * c()); +// revs = 0; - Session.set('speed', speed_buffer.sum() / 5); -} +// Session.set('speed', speed_buffer.sum() / 5); +// } -setInterval(speedo, 500); +// setInterval(speedo, 500); // 5sec, 2 values / sec. var speed_buffer = createRingBuffer(5 * 2); @@ -72,8 +72,49 @@ Template.sim.helpers({ } }); +// This version is connected to a bicycle using a TI Launchpad which sends data to +// computer using a serialport. Nodejs service reads that data using node-serialport +// and then sends it through websocket. +var sock = new SockJS("http://localhost:9999/speed"); + function init_sim() { + +sock.onopen = function() { + debug('open'); +}; + +sock.onmessage = function(e) { + if (Session.equals('page', 'sim') && window.point) { + debug('message', e.data); + + var dist = parseInt(e.data, 10) * c(); + speed_buffer.push(dist); + + Session.set('speed', speed_buffer.sum() / 5); + + Session.set('distance', Session.get('distance') + localStorage.multiplier * dist); + window.traveled += localStorage.multiplier * dist; + + if (window.traveled >= window.point.distance) { + var next = Points.findOne({_id: window.point.next}); + + // Stay on current point if no next point exists + if (next) { + window.traveled -= next.distance; + + maps.travel(next._id, {route: true}); + window.point = next; + } + } + } + +}; + +sock.onclose = function() { + debug('close'); +}; + Meteor.autosubscribe(function () { if (Session.equals('page', 'sim')) { var route = Routes.findOne({_id: Session.get('route')}); From 922492edf2520580f9e70f2e94f58c21f546de82 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Tue, 23 Oct 2012 18:04:19 +0300 Subject: [PATCH 2/2] After adding a new point, only compute the distance and the heading between the new and the previous point (instead of all points). Add route save button. Routes should be saved after adding (or removing) points. Saving route will: - Update distance and heading between every point - Compute total length of route - Update encoded path used by static map images at frontpage --- client/bicyclesim-meteor.html | 4 +++- client/bicyclesim-meteor.styl | 8 ++++---- client/edit.js | 14 +++++++++++++- server/main.js | 17 ++++++++++++++--- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/client/bicyclesim-meteor.html b/client/bicyclesim-meteor.html index 73ecedf..c1521fb 100644 --- a/client/bicyclesim-meteor.html +++ b/client/bicyclesim-meteor.html @@ -192,6 +192,8 @@ {{#if can_edit}}{{/if}} {{/each}} - {{#if can_edit}}
  • {{/if}} + {{#if can_edit}} +
  • + {{/if}} diff --git a/client/bicyclesim-meteor.styl b/client/bicyclesim-meteor.styl index 6874fe9..66d5c71 100644 --- a/client/bicyclesim-meteor.styl +++ b/client/bicyclesim-meteor.styl @@ -157,16 +157,16 @@ a.thumbnail ol.points margin 0 li - font-size 0.9em + font-size 0.85em border-top 1px solid #ccc padding 2px 0px list-style-position inside - &:first-child, &.new-point + &:first-child, &.actions border-top 0 - &.new-point + &.actions list-style none span.location - font-size 0.8em + font-size 0.85em a.remove float right diff --git a/client/edit.js b/client/edit.js index b6f4095..13e3a9b 100644 --- a/client/edit.js +++ b/client/edit.js @@ -12,6 +12,7 @@ function new_point(latlng) { add_point(latlng, num, true); ++num; $('.sidebar').animate({scrollTop: $('.sidebar-inner').height()}, 'fast'); + Session.set('saved', false); }); } @@ -92,6 +93,10 @@ Template.points.can_edit = function () { return (route && Meteor.user() && route.owner === Meteor.user()._id); }; +Template.points.saved = function () { + return Session.get('saved'); +}; + Template.points.helpers(helpers); Template.points.events({ @@ -101,11 +106,17 @@ Template.points.events({ 'click a.remove': function () { Meteor.call('remove_point', this._id, function () { full_clear_required = true; + Session.set('saved', false); }); }, - 'click .new-point button': function () { + 'click .new-point': function () { new_point(maps.getLatLng()); }, + 'click .save': function () { + Meteor.call('update_route', Session.get('route'), function () { + Session.set('saved', true); + }); + }, 'change #points-autoadd': function (event) { Session.set('points-autoadd', event.currentTarget.checked); } @@ -124,6 +135,7 @@ Meteor.autorun(function () { if (Session.equals('page', 'edit') && Session.get('route')) { debug('sivu tai route vaihtui, route: ' + Session.get('route')); full_clear_required = true; + Session.set('saved', true); Meteor.deps.isolate(function () { var route = Routes.findOne({_id: Session.get('route')}); diff --git a/server/main.js b/server/main.js index 15fccab..854a162 100644 --- a/server/main.js +++ b/server/main.js @@ -41,7 +41,7 @@ Meteor.methods({ var route = Routes.findOne({_id: route_id}); if (!route) return; - var point_id = Points.insert({latlng: latlng, route: route_id, distance: 0, next: null}); + var point_id = Points.insert({latlng: latlng, heading: 0, route: route_id, distance: 0, next: null}); // Add new point after last point. Points.update({route: route_id, next: null, _id: {$ne: point_id}}, {$set: {next: point_id}}); @@ -49,7 +49,15 @@ Meteor.methods({ // If route didn't have any point, update first point, Routes.update({_id: route_id, first: null}, {$set: {first: point_id}}); - update_route(route_id); + // Update distance and heading of previous point + var prev = Points.findOne({next: point_id}); + if (prev) { + heading = computeFinalBearing(prev.latlng, latlng); + distance = computeDistance(prev.latlng, latlng); + Points.update({next: point_id}, {$set: {heading: heading, distance: distance}}); + } + + // update_route(route_id); }, remove_point: function (point_id) { var point = Points.findOne({_id: point_id}); @@ -61,6 +69,9 @@ Meteor.methods({ Points.remove({_id: point_id}); - update_route(point.route); + // update_route(point.route); + }, + update_route: function (route_id) { + update_route(route_id); } });