<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Connorhd &#187; project</title>
	<atom:link href="http://connorhd.co.uk/tag/project/feed/" rel="self" type="application/rss+xml" />
	<link>http://connorhd.co.uk</link>
	<description>Interesting stuff.</description>
	<lastBuildDate>Sat, 30 Mar 2013 23:35:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Realtime UK train map</title>
		<link>http://connorhd.co.uk/2013/03/30/realtime-uk-train-map/</link>
		<comments>http://connorhd.co.uk/2013/03/30/realtime-uk-train-map/#comments</comments>
		<pubDate>Sat, 30 Mar 2013 21:18:18 +0000</pubDate>
		<dc:creator>Connorhd</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[d3]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[open data]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[planet compsoc]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[websockets]]></category>

		<guid isPermaLink="false">http://connorhd.co.uk/?p=339</guid>
		<description><![CDATA[A quick project to visualise some of the open data available about the UK train network.
First of all, the result, you should be able to see a map of the UK below, with coloured dots appearing to represent trains as they arrive at stations in realtime.
This requires a browser that supports SVG and websockets, I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p><strong>A quick project to visualise some of the open data available about the UK train network.</strong></p>
<p>First of all, the result, you should be able to see a map of the UK below, with coloured dots appearing to represent trains as they arrive at stations in realtime.</p>
<p><small>This requires a browser that supports SVG and websockets, I&#8217;ve only tested it works in Firefox and Chrome.</small><br />
<iframe src="http://connorhd.co.uk/project/trainmap/" style="border: 0; width: 500px; height: 600px" scrolling="no"></iframe><br />
<a href="http://connorhd.co.uk/project/trainmap/" target="_blank">Click for larger version</a> </p>
<p>So, whats going on here?</p>
<p>It starts with the <a href="http://www.networkrail.co.uk/data-feeds/">network rail operational data feeds</a> one of which  provides a stream of events for train movements (excellently documented by the <a href="http://nrodwiki.rockshore.net/index.php/Train_Movement">Open Rail Data wiki</a>). The interesting fields of data are the timestamp of the event, and the planned timestamp of the event, as well as the STANOX code. These allow me to locate the event, and to calculate the delay between when the event should have happened, and when it actually happened.</p>
<p>To display the events on a map the STANOX code needs to be translated into something more useful, to do that I grabbed one of the reference data files described <a href="http://nrodwiki.rockshore.net/index.php/ReferenceData">here</a>. I then use that to convert STANOX codes to TIPLOC codes (bear with me). Once I have the TIPLOC code I can find the Easting and Northing of stations using the <a href="http://data.gov.uk/dataset/naptan">NaPTAN dataset</a>. This doesn&#8217;t cover the locations of all events as it only has public stations and doesn&#8217;t include depot/switch points that trains also report at, but it turns out it locates about 70% of the events received, which is more than enough for a visualisation.</p>
<p>Unfortunately Eastings and Northings are not a very useful co-ordinate system, what I really want is WGS84 latitude and longitude (GPS co-ordinates), thankfully Chris Veness of <a href="http://www.movable-type.co.uk/">movable-type.co.uk</a> has written <a href="http://www.movable-type.co.uk/scripts/latlong-gridref.html">conversion functions in javascript</a>.</p>
<p>At this point I&#8217;m connecting to the National Rail data feed in node, looking up the location and coverting to the useful lat/long, so I have a realtime stream of train locations and how delayed they are in my console. The next step is to get this into a browser.</p>
<p>I don&#8217;t really have any experience with drawing maps in the browser, but my first guess that <a href="http://d3js.org/">d3</a> will be useful proves correct, and I quickly find this extremely useful tutorial: <a href="http://bost.ocks.org/mike/map/">Let’s Make a Map</a>. I take the <a href="http://bost.ocks.org/mike/map/step-7.html">end result</a> of this tutorial, slightly zoom and recentre the map (the national rail data doesn&#8217;t cover Northern Ireland, and not much goes on in the very north of Scotland). The result is a nice looking SVG UK map that I can draw points on using the d3 projection to convert the lat/long to x/y points.</p>
<p>The last thing to link together is to get the data from node to the browser, I wanted to use websockets given the streaming nature of the data, so I grab <a href="https://github.com/einaros/ws">ws for node</a>. I currently use lighttpd as a webserver, which can&#8217;t handle websocket proxying, so I also setup HAProxy in front of lighttpd to pass the websocket requests directly to node.</p>
<p>At this point it all works, I have dots appearing on my map in the browser as trains arrive at stations. To make things a little more interesting I <a href="https://github.com/mbostock/d3/wiki/Quantitative-Scales">use d3</a> to create a colour gradient for on time to very delayed trains, and I use a radial gradient with some transparency to make trains slightly colour an area of the map.</p>
<p>The result is what you (hopefully) see above, an animated map that shows locations of trains as they arrive, colour coded by how delayed the train is. The source for this is all available <a href="https://github.com/Connorhd/trainmap">on GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://connorhd.co.uk/2013/03/30/realtime-uk-train-map/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PassHash &#8211; A simple way to generate secure passwords</title>
		<link>http://connorhd.co.uk/2011/05/10/passhash-a-simple-way-to-generate-secure-passwords/</link>
		<comments>http://connorhd.co.uk/2011/05/10/passhash-a-simple-way-to-generate-secure-passwords/#comments</comments>
		<pubDate>Tue, 10 May 2011 18:29:46 +0000</pubDate>
		<dc:creator>Connorhd</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[passwords]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://connorhd.co.uk/?p=312</guid>
		<description><![CDATA[PassHash is a Chrome extension, Android app and website which allows you to generate strong, unique passwords using a master password and key based on the site you wish to use the password on.
Having secure passwords for every site you visit is difficult, in reality many people (including me) use the same password for more [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://connorhd.co.uk/wp-content/uploads/2011/05/icon128.png" alt="" title="" width="128" height="128" class="alignleft size-full wp-image-315" /><strong>PassHash is a <a href="https://chrome.google.com/webstore/detail/ebkoipncioolddgipnflfmidgdhobjdh">Chrome extension</a>, <a href="https://market.android.com/details?id=uk.co.connorhd.passhash">Android app</a> and <a href="http://passhash.connorhd.co.uk/">website</a> which allows you to generate strong, unique passwords using a master password and key based on the site you wish to use the password on.</strong></p>
<p>Having secure passwords for every site you visit is difficult, in reality many people (including me) use the same password for more than one place. This is bad as it increases the potential problem if your password is compromised, for example, if you share your email account&#8217;s password with another website, who then (accidentally or intentionally) give away your password, your email is no longer secure. There are a number of methods of reducing these problems and PassHash uses a technique which generates new passwords based on one secure password which you never share directly with anyone. It is worth noting that PassHash is far from unique, and there are many similar tools, as well as alternatives like <a href="http://lastpass.com/">LastPass</a>, however, none of the alternatives met my requirements, as I will describe below.</p>
<p>PassHash works by taking your master password (which should be a password which is complicated, long and only known by you), and combines it with a memorable key for each website you want to use it on (using the domain name is the default, i.e. amazon.com or ebay.co.uk). This combination is then transformed in a way that cannot be easily reversed (using <a href="http://en.wikipedia.org/wiki/SHA-1">SHA-1</a>), and modified to start with a lower case letter, followed by a number, followed by an upper case letter, and finally 9 more characters which can be lowercase, uppercase, or numeric. You can then use this password for the website with the key you gave, and know that you will be able to generate it again in the future, but no one else will.</p>
<p>So, why is PassHash better than any of the available alternatives?</p>
<ul>
<li>PassHash has no settings (unlike most of the alternatives), this might seem to be a bad thing, but settings are something else to remember, and something that could be wrong by default. PassHash attempts to have sensible defaults, and generate passwords accepted on as many websites as possible. Passwords generated are 12 characters long, and always include at least one of each of lowercase, uppercase and numeric characters. Longer passwords may not be allowed on all websites, and are harder to type if you need to manually enter them. Symbols are often not allowed in passwords and are also hard to type, especially if being input on a phone or other mobile device.</li>
<li>PassHash is accessible from anywhere, there is a Chrome extension for use on your own computer, as well as a website you can use anywhere. There is also an Android app which allows you to generate passwords without entering your master password on a computer you may not trust. As nothing is stored PassHash also works even when you have no internet connection.</li>
<li>The PassHash algorithm is simple and the source code is available, there is no need to trust that it generates passwords in a sensible way, you can check for yourself. As well as this, if I were to withdraw the Chrome extension anyone could release software to generate the same passwords.</li>
<li>PassHash doesn&#8217;t store any of your passwords &#8211; including your master password &#8211; anywhere, locally or remotely. Services such as LastPass are <a href="http://blog.lastpass.com/2011/05/lastpass-security-notification.html">always at risk of attack</a> and by using them you are trusting the LastPass developers have not made any mistakes which could result in your passwords not being properly encrypted.</li>
<li>PassHash is completely free, and always will be, with no potential for me to change this, as the source code is publicly available and no part of PassHash is provided as a service.</li>
<li>The PassHash chrome extension can automatically enter generated passwords into text boxes on websites, this avoids both showing the password on the screen and storing the password in the clipboard at any point.</li>
</ul>
<p>While this all sounds great, PassHash does have a couple of downsides.</p>
<ul>
<li>All of your passwords being based on one master passwords requires you to keep that password completely secret, if you compromise that password, none of your passwords are safe. Unlike services such as LastPass if you compromise your master password you will need to change your password on every site you used PassHash to generate passwords for. This means you must be particularly careful when entering your master password on any computer or device you do not fully trust.</li>
<li>Remembering passwords generated with PassHash is not really an option, which is not necessarily an issue as long as you have ways of accessing your passwords while not at your computer, which is why the website and Android app exist</li>
</ul>
<p>Website: <a href="http://passhash.connorhd.co.uk/">http://passhash.connorhd.co.uk/</a><br />
Source code: <a href="https://github.com/Connorhd/PassHash">GitHub</a></p>
]]></content:encoded>
			<wfw:commentRss>http://connorhd.co.uk/2011/05/10/passhash-a-simple-way-to-generate-secure-passwords/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Kindlizer &#8211; Adapting the web for the Kindle browser</title>
		<link>http://connorhd.co.uk/2011/03/20/kindlizer-adapting-the-web-for-the-kindle-browser/</link>
		<comments>http://connorhd.co.uk/2011/03/20/kindlizer-adapting-the-web-for-the-kindle-browser/#comments</comments>
		<pubDate>Sun, 20 Mar 2011 23:58:35 +0000</pubDate>
		<dc:creator>Connorhd</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[feed]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[kindle]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://connorhd.co.uk/?p=282</guid>
		<description><![CDATA[Kindlizer is an attempt to make the web browser on the Kindle 3 more useful. Currently it consists of a simple RSS feed reader built on some JavaScript which makes the Kindle browser behave more like a standard Kindle book.
Having recently acquired a Kindle e-book reader I immediately investigated less obvious ways of using it [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://kindlizer.appspot.com/">Kindlizer</a> is an attempt to make the web browser on the <a href="http://en.wikipedia.org/wiki/Amazon_Kindle#Kindle_3">Kindle 3</a> more useful. Currently it consists of a simple RSS feed reader built on some JavaScript which makes the Kindle browser behave more like a standard Kindle book.</strong></p>
<p>Having recently acquired a Kindle e-book reader I immediately investigated less obvious ways of using it than just reading books. Amazon themselves offer magazines, newspapers and blogs as well as normal books, however blogs particularly seem like something I shouldn&#8217;t need to pay for (especially considering I don&#8217;t have the 3G Kindle so won&#8217;t be making use of the included Whispernet fee). Of course there are a number of 3rd party services for Kindle out there, <a href="http://calibre-ebook.com/">calibre</a> is an e-book manager that will also download <a href="http://status.calibre-ebook.com/recipe_stats">many</a> popular news/magazines and send them directly to your Kindle. There is also <a href="http://kindlefeeder.com/">kindlefeeder.com</a> which will send aggregated RSS feeds to your Kindle. Other more generic services such as <a href="http://www.instapaper.com/">Instapaper</a> also have some form of Kindle support (Instapaper will send unread saved pages as a digest to your Kindle on a regular basis).</p>
<p>Kindlizer is slightly different to all of these services as it attempts to create an interface to be used directly on the Kindle, rather than sending static files to the Kindle to be read later. Amazon have announced a development kit for the Kindle called the <a href="https://kdk.amazon.com">KDK</a> to allow development of applications which run on the Kindle, however it is currently in a closed beta and appears to be quite restrictive in terms of free data usage. This means Kindlizer is restricted to the experimental web browser built into the Kindle.</p>
<p>The Kindle web browser is <a href="http://en.wikipedia.org/wiki/WebKit">Webkit</a> based which means it has excellent support for web standards, unfortunately in order for the browser to display all kinds of websites it has a less intuitive interface. The directional pad on the Kindle controls a mouse pointer which jumps around in a grid on the screen and snaps to clickable elements (why <a href="http://en.wikipedia.org/wiki/Spatial_navigation">spacial navigation</a> wasn&#8217;t used I have no idea), as well as this the page back and forward keys on the Kindle become page up/page down in the browser, meaning on each press the page is only scrolled a small portion. On top of this many pages are far too large to fit on the Kindle screen and are first shown zoomed out, requiring the user to first select a region to zoom in on.</p>
<p>Kindlizer attempts to fix some of these problems with the use of some JavaScript to trick the browser into showing the content one page at a time, with the page turn buttons on the Kindle moving forward or backward entire pages at once. On top of this CSS styling makes the documents appear with the same formatting as Kindle books, and large areas around clickable elements allows for far easier movement of the mouse cursor. The JavaScript is written to allow for arbitrary blocks of HTML to be split into pages and displayed on the Kindle, currently this is used in conjunction with the <a href="http://code.google.com/apis/feed/">Google Feed API</a> to allow users to view RSS feeds on the Kindle. The results so far are shown in the screenshots below:</p>
<div id="attachment_308" class="wp-caption aligncenter" style="width: 460px"><img src="http://connorhd.co.uk/wp-content/uploads/2011/03/screen_shot-40858.png" alt="" width="450" height="600" class="size-full wp-image-308" /><p class="wp-caption-text">Kindlizer homepage as viewed on the Kindle</p></div>
<div id="attachment_292" class="wp-caption aligncenter" style="width: 460px"><img src="http://connorhd.co.uk/wp-content/uploads/2011/03/screen_shot-40853.png" alt="" width="450" height="600" class="size-full wp-image-292" /><p class="wp-caption-text">Viewing this blog's RSS feed on Kindlizer</p></div>
<p>You can visit the (as of writing unfinished) <a href="http://kindlizer.appspot.com/">Kindlizer site</a> on Google App Engine <a href="http://kindlizer.appspot.com/">here</a>, alternatively there is a demo which shows this blog&#8217;s RSS feed and does not require login <a href="http://connorhd.co.uk/project/kindlizer/">here</a>. Neither of which work particularly well on any device other than a Kindle (they may kind of work on your browser, but you won&#8217;t be able to change page). The full source code for the App Engine site is open source and <a href="https://github.com/Connorhd/Kindlizer">available here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://connorhd.co.uk/2011/03/20/kindlizer-adapting-the-web-for-the-kindle-browser/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>TabCloud</title>
		<link>http://connorhd.co.uk/2010/10/23/tabcloud/</link>
		<comments>http://connorhd.co.uk/2010/10/23/tabcloud/#comments</comments>
		<pubDate>Sat, 23 Oct 2010 23:12:16 +0000</pubDate>
		<dc:creator>Connorhd</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[IBM]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[websockets]]></category>

		<guid isPermaLink="false">http://connorhd.co.uk/?p=241</guid>
		<description><![CDATA[TabCloud is a Chrome extension and Android app which allows you to save open Chrome windows and restore them on another computer or at a later date.
After being thoroughly disappointed by a talk from Mozilla labs about Weave (now called Sync) at last years FOSDEM (they appeared to have made very little progress in the [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="https://chrome.google.com/extensions/detail/npecfdijgoblfcgagoijgmgejmcpnhof">TabCloud</a> is a <a href="https://chrome.google.com/extensions/detail/npecfdijgoblfcgagoijgmgejmcpnhof">Chrome extension</a> and <a href="http://www.androidzoom.com/android_applications/tools/tabcloud_miyj.html?nav=googleDownload">Android app</a> which allows you to save open Chrome windows and restore them on another computer or at a later date.</strong></p>
<p>After being thoroughly disappointed by a talk from <a href="https://mozillalabs.com/">Mozilla labs</a> about <a href="https://mozillalabs.com/blog/2007/12/introducing-weave/">Weav</a>e (now called <a href="https://mozillalabs.com/sync/">Sync</a>) at last years <a href="http://archive.fosdem.org/2010/schedule/events/moz_sync_weave">FOSDEM</a> (they appeared to have made very little progress in the last 3 years, and were not even planning to offer any kind of real-time synchronisation). I had decided to attempt to implement the open window/tab synchronisation feature for Google Chrome as an extension. At the time I planned to host the server for this using <a href="http://nodejs.org/">node.js</a> and allow real-time synchronisation with changes automatically affecting any machines you currently had connected. After quickly writing a solution using <a href="http://en.wikipedia.org/wiki/WebSockets">websockets</a> it became obvious this was a harder problem than I anticipated &#8211; synchronisation is hard, mainly when things go wrong, i.e. how do you deal with conflicts when multiple machines have flakey connections, also the <a href="http://code.google.com/chrome/extensions/windows.html">Chrome extension API</a> makes it very difficult to distinguish the difference between closing all a windows tabs, and closing the window &#8211; so the project in that form was abandoned until I could find more time for it.</p>
<p>Fast-forward to the summer and I am working at <a href="http://www.ibm.com/uk/en/">IBM</a> as a technical intern for <a href="http://en.wikipedia.org/wiki/Extreme_Blue">Extreme Blue</a>, I have a work laptop, home computer and my personal laptop, I am still using Chrome as my main browser and I would really like a way of sharing my browser sessions between my machines. I had recently created <a href="http://linkpush.appspot.com">LinkPush</a> (something I intend to blog about shortly), which made me think of using <a href="http://code.google.com/appengine/">Google App Engine</a> to host a simplified session sharing plugin for Chrome using Google Accounts as a means of secure identification. The result &#8211; <a href="https://chrome.google.com/extensions/detail/npecfdijgoblfcgagoijgmgejmcpnhof">TabCloud</a> &#8211; is an extension which allows you to save an open window as a group of tabs, which can be restored on any other computer (or the same computer at a later date) via the extension interface. It also offers some other features such as naming windows, and re-arranging tabs between open windows and saved sessions by dragging and dropping the tab icons. I also released a very simple <a href="http://www.androidzoom.com/android_applications/tools/tabcloud_miyj.html?nav=googleDownload">Android app</a> which allows you to view your saved tabs as a list from your phone and open any individual link.</p>
<p><img src="http://connorhd.co.uk/wp-content/uploads/2010/10/tabcloud.png" alt="TabCloud Screenshot" title="tabcloud" width="477" height="290" class="alignnone size-full wp-image-251" /></p>
<p>The project isn&#8217;t really anything like my original plans, however I find it extremely useful, hopefully at some point I will find the time to make a truely real-time session synchronisation app (unless Google <a href="http://code.google.com/p/chromium/issues/detail?id=30519">beat me to it</a>). Firefox 4 beta comes with the current Mozilla Sync (also available as an extension for older Firefoxes), which periodically saves all your open tabs (as well as other data) to their server, which allows you to view open tabs on one machine from another (or mobile device), however (despite this being more what I originally aimed for) this seems a lot less useful in practice than the simpler functionality I have implemented with TabCloud.</p>
<p>If you are interested the whole project is open source and available on <a href="http://github.com/Connorhd/TabCloud">GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://connorhd.co.uk/2010/10/23/tabcloud/feed/</wfw:commentRss>
		<slash:comments>59</slash:comments>
		</item>
		<item>
		<title>A genetic algorithm example in JavaScript</title>
		<link>http://connorhd.co.uk/2010/10/09/a-genetic-algorithm-example-in-javascript/</link>
		<comments>http://connorhd.co.uk/2010/10/09/a-genetic-algorithm-example-in-javascript/#comments</comments>
		<pubDate>Sat, 09 Oct 2010 21:06:07 +0000</pubDate>
		<dc:creator>Connorhd</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[ai]]></category>
		<category><![CDATA[cs255]]></category>
		<category><![CDATA[genetic]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[project]]></category>

		<guid isPermaLink="false">http://connorhd.co.uk/?p=182</guid>
		<description><![CDATA[So, after several months of neglect I have finally decided to force myself to update my blog. This is the first of what will hopefully be several posts detailed the various projects I have been working on over the past few months. This is the smallest and potentially least useful of these projects, but something [...]]]></description>
			<content:encoded><![CDATA[<p>So, after several months of neglect I have finally decided to force myself to update my blog. This is the first of what will hopefully be several posts detailed the various projects I have been working on over the past few months. This is the smallest and potentially least useful of these projects, but something I recently made (and therefore easiest for me to write about) and hopefully mildly interesting.</p>
<p>In the 2nd year of my computer science degree a module called &#8220;Artificial Intelligence&#8221; briefly touched on the idea of <a href="http://en.wikipedia.org/wiki/Genetic_algorithm">Genetic Algorithms</a>. After discussion with a housemate as to how these could be used in a <a href="http://www2.warwick.ac.uk/fac/sci/dcs/teaching/modules/cs310">3rd year project</a> I decided to look into making a simple example of such an algorithm to run using JavaScript in the browser.</p>
<p>After a quick Google the best example I could find (that would be visual enough to be interesting to see and simple enough to write in JavaScript quickly) was to generate the string &#8220;Hello, World!&#8221;. Starting with randomly generated strings and genetically evolving them based on a fitness function of how close they were to the string I wanted. A little bit more detail on how that is achieved shortly, but first the result is shown below (click on &#8220;Hello, World!&#8221; to replay the demo) a more detailed output can be seen <a href="/project/genetic">here</a>.</p>
<div><script type="text/javascript">
var running = false;
function go () { running = true; var b=Math;function e(){return"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjiklmnopqrstuvwxyz,! ".charAt(b.floor(b.random()*55))}var f=true,g,h,i,j,k,l,m=0;
l=setInterval(function(){a:{var a,d;if(f){f=false;k=[];for(a=0;a<20;a++){var c="";for(d=0;d<13;d++)c+=e();k[a]=c}}else for(a=0;a<20;a++){if(a==10){d=g;g=h;h=d}splitAt=b.floor(b.random()*13);k[a]=g.substring(0,splitAt)+h.substring(splitAt,13);if(Math.random()<0.75){mutateAt=b.floor(b.random()*13)+1;k[a]=k[a].substring(0,mutateAt-1)+e()+k[a].substring(mutateAt,13)}}m+=20;g=k[0];h=k[1];i=j=-1E4;for(a=0;a<20;a++){d=k[a];for(c=count=0;c<13;c++){count-=Math.abs(d.charCodeAt(c)-"Hello, World!".charCodeAt(c));
if(d.substring(c,c+1)=="Hello, World!".substring(c,c+1))count+=100}score=count;if(score>i){g=k[a];i=score;if(g=="Hello, World!"){document.getElementById("output").innerHTML="Hello, World!";m=m-20+a;document.getElementById("stats").innerHTML=m+" strings genetically engineered";clearInterval(l); running = false; break a}}else if(score>j){h=k[a];j=score}}document.getElementById("output").innerHTML=g;document.getElementById("stats").innerHTML=m+" strings genetically engineered."}},50); }; go();
</script></div>
<pre style="margin: 20px; font-size: 4em; font-family: monospace; text-align: center; overflow: hidden" id="output" onclick="if (!running) go()">a</pre>
<div style="margin: 20px; font-size: 1.8em; font-family: monospace; text-align: center" id="stats">a</div>
<p>So, what is this actually doing? Viewing the <a href="/project/genetic">detailed output</a> you can see all of the strings generated, how they were combined (the algorithmic equivalent of reproduction) and the mutatations after they reproduced. To start 20 random strings are generated, then the top 2 strings (measured by number of letters that are correct, and then closeness of incorrect letters) are taken. It is worth noting this is perhaps not the best way for a genetic algorithm to work, and strictly all of the strings should have a chance of &#8220;reproducing&#8221; based on their fitness score, but this works well enough for a simple example. Once the top 2 are taken, they are split at a random point 20 times and combined (shown by the red and green colouring on the detailed output), these resulting strings then have a 75% chance of one of their characters randomly changing (shown with a yellow highlight on the detailed output) and we have 20 (potentially) completely new strings. The scoring process then repeats and more strings are created until we reach the goal string.</p>
<p>The result is we very quickly get a string very close to what we want, which eventually mutates to become exactly what we want, with a nice visual demonstration of what is happening, and hopefully a better understanding of how genetic algorithms work.</p>
]]></content:encoded>
			<wfw:commentRss>http://connorhd.co.uk/2010/10/09/a-genetic-algorithm-example-in-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>TapeMeasure &#8211; Another Android experiment</title>
		<link>http://connorhd.co.uk/2010/03/23/tapemeasure-another-android-experiment/</link>
		<comments>http://connorhd.co.uk/2010/03/23/tapemeasure-another-android-experiment/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 23:58:17 +0000</pubDate>
		<dc:creator>Connorhd</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[accelerometer]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[project]]></category>

		<guid isPermaLink="false">http://connorhd.co.uk/?p=158</guid>
		<description><![CDATA[I have again become interested in what you can do with Android applications. After some discussion with a housemate (who actually owns an Android, unlike me) about augmented reality applications and how they could be easily done (to some degree) if you could reasonably accurately track a phones location in a building (something that is [...]]]></description>
			<content:encoded><![CDATA[<p>I have again become interested in what you can do with Android applications. After some discussion with <a href="http://www.sinjakli.co.uk/">a housemate</a> (who actually owns an Android, unlike me) about augmented reality applications and how they could be easily done (to some degree) if you could reasonably accurately track a phones location in a building (something that is hard to do with GPS).  I wanted to see if the phones <a href="http://en.wikipedia.org/wiki/Accelerometer">accelerometer</a> could be used (at least for short distances) to work out how a phone had moved since entering a building. Rather than attempt to make an augmented reality app, I decided to go for a slightly easier project of making a tape measure, i.e. an app that could measure distance by moving the phone. Before anyone thinks this will actually be useful I should point out that this was a rather complete failure and the resulting app is fairly useless. However, the code may be useful to anyone looking at writing an Android app that uses any of the internal sensors, it would also be interesting to see if any other phones (I only tested the app on a G1) are any more successful.</p>
<p>So straight to the code, after (briefly) reading the Android documentation it seemed there was very little info about using the sensors, so I turned to Google, this article looked like what I wanted and linked to <a href="http://github.com/eburke/android_game_examples/blob/f09fa73e7012e6ae379180613d3db829fcd09a0f/GameExamples/src/com/stuffthathappens/games/Accel.java">this code</a> which is the basis of my app. So I updated the code to use the non-deprecated API, changing its functionality slightly and ended up with:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">uk.co.connorhd.android.tapemeasure</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.Sensor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorEventListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.hardware.SensorManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View.OnClickListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Button</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.TextView</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TapeMeasure <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span> SensorEventListener,
		OnClickListener <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> SensorManager sensorMgr<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Sensor sensorAccel<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> TextView accuracyLabel<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> TextView xLabel, yLabel, zLabel<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Button</span> calibrateButton<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> moved <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> speed <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> accel <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> accelDiff <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> a<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> lastUpdate <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> curAccel <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> accelUpdates <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		accuracyLabel <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">accuracy_label</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		xLabel <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">x_label</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		yLabel <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">y_label</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		zLabel <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">z_label</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		calibrateButton <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Button</span><span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">calibrate_button</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		calibrateButton.<span style="color: #006633;">setOnClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onPause<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onPause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		sensorMgr.<span style="color: #006633;">unregisterListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, sensorAccel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		sensorMgr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onResume<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onResume</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		sensorMgr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SensorManager<span style="color: #009900;">&#41;</span> getSystemService<span style="color: #009900;">&#40;</span>SENSOR_SERVICE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		sensorAccel <span style="color: #339933;">=</span> sensorMgr.<span style="color: #006633;">getSensorList</span><span style="color: #009900;">&#40;</span>Sensor.<span style="color: #006633;">TYPE_ACCELEROMETER</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">boolean</span> accelSupported <span style="color: #339933;">=</span> sensorMgr.<span style="color: #006633;">registerListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, sensorAccel,
				SensorManager.<span style="color: #006633;">SENSOR_DELAY_FASTEST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>accelSupported<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// on accelerometer on this device</span>
			sensorMgr.<span style="color: #006633;">unregisterListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, sensorAccel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			accuracyLabel.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">no_accelerometer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClick<span style="color: #009900;">&#40;</span><span style="color: #003399;">View</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>v <span style="color: #339933;">==</span> calibrateButton<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// Clicked button</span>
			moved <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			speed <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			accelDiff <span style="color: #339933;">=</span> accel<span style="color: #339933;">+</span>accelDiff<span style="color: #339933;">;</span>
			accel <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onAccuracyChanged<span style="color: #009900;">&#40;</span>Sensor sensor, <span style="color: #000066; font-weight: bold;">int</span> accuracy<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// this method is called very rarely, so we don't have to</span>
		<span style="color: #666666; font-style: italic;">// limit our updates as we do in onSensorChanged(...)</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sensor.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> Sensor.<span style="color: #006633;">TYPE_ACCELEROMETER</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>accuracy<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">case</span> SensorManager.<span style="color: #006633;">SENSOR_STATUS_UNRELIABLE</span><span style="color: #339933;">:</span>
				accuracyLabel.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">accuracy_unreliable</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">case</span> SensorManager.<span style="color: #006633;">SENSOR_STATUS_ACCURACY_LOW</span><span style="color: #339933;">:</span>
				accuracyLabel.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">accuracy_low</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">case</span> SensorManager.<span style="color: #006633;">SENSOR_STATUS_ACCURACY_MEDIUM</span><span style="color: #339933;">:</span>
				accuracyLabel.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">accuracy_medium</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">case</span> SensorManager.<span style="color: #006633;">SENSOR_STATUS_ACCURACY_HIGH</span><span style="color: #339933;">:</span>
				accuracyLabel.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">string</span>.<span style="color: #006633;">accuracy_high</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSensorChanged<span style="color: #009900;">&#40;</span>SensorEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> sensorType <span style="color: #339933;">=</span> event.<span style="color: #006633;">sensor</span>.<span style="color: #006633;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sensorType <span style="color: #339933;">==</span> Sensor.<span style="color: #006633;">TYPE_ACCELEROMETER</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			a <span style="color: #339933;">=</span> event.<span style="color: #006633;">values</span><span style="color: #339933;">;</span>
			accel <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>a<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>a<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>a<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>a<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">*</span>a<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>SensorManager.<span style="color: #006633;">GRAVITY_EARTH</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>accelDiff<span style="color: #339933;">;</span>
			curAccel <span style="color: #339933;">+=</span> accel<span style="color: #339933;">;</span>
			accelUpdates<span style="color: #339933;">++;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">long</span> curTime <span style="color: #339933;">=</span> event.<span style="color: #006633;">timestamp</span> <span style="color: #339933;">-</span> lastUpdate<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>curTime <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1000000000</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				curAccel <span style="color: #339933;">=</span> curAccel<span style="color: #339933;">/</span>accelUpdates<span style="color: #339933;">;</span>
				speed <span style="color: #339933;">+=</span> curAccel<span style="color: #339933;">*</span>curTime<span style="color: #339933;">*</span>10e<span style="color: #339933;">-</span>10<span style="color: #339933;">;</span>
				moved <span style="color: #339933;">+=</span> speed<span style="color: #339933;">*</span>curTime<span style="color: #339933;">*</span>10e<span style="color: #339933;">-</span>10<span style="color: #339933;">;</span>
&nbsp;
				lastUpdate <span style="color: #339933;">=</span> event.<span style="color: #006633;">timestamp</span><span style="color: #339933;">;</span>
&nbsp;
				xLabel.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Moved: %+2.20f&quot;</span>, moved<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				yLabel.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Speed: %+2.20f&quot;</span>, speed<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				zLabel.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Accel: %+2.20f Time:  %+2.3f&quot;</span>, curAccel, curTime<span style="color: #339933;">*</span>10e<span style="color: #339933;">-</span>10<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				curAccel <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
				accelUpdates <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This calculates the current acceleration of the phone, subtracts gravity (which is added to the raw data), then derives the speed and distance travelled. The calibrate button resets the speed and distance, and assumes the phone is not moving, generating a bias to add to the acceleration in the case of the phones accelerometer being badly calibrated. If you want to try the app it can be downloaded and installed from <a href="http://connorhd.co.uk/files/TapeMeasure.apk">here</a>. I suggest launching the app, placing the phone on a stable surface and pressing calibrate, if you get any interesting results let me know.</p>
<p>Despite the phone reporting the accelerometer accuracy as &#8220;high&#8221; (with no description of what high means) and returning values in excess of 10 significant figures,  the acceleration data with the phone sat on a table varied in the range of about 0.5m/s^2 (although this did appear to change depending on the room I was in). This resulted in a massively wrong distance value very quickly increasing to several metres without moving the phone, making any kind of testing almost impossible. It seems the current purpose of accelerometers is to work out if some violent action (such as shaking) has happened to the phone, and not anything much more interesting. All in all a bit of a waste of time, but maybe future phones will have better sensors, and hopefully someone will find the code snippet above useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://connorhd.co.uk/2010/03/23/tapemeasure-another-android-experiment/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>node_debug &#8211; A debugging console over HTTP</title>
		<link>http://connorhd.co.uk/2010/01/25/node_debug-a-debugging-console-over-http/</link>
		<comments>http://connorhd.co.uk/2010/01/25/node_debug-a-debugging-console-over-http/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 23:40:24 +0000</pubDate>
		<dc:creator>Connorhd</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[planet compsoc]]></category>
		<category><![CDATA[project]]></category>

		<guid isPermaLink="false">http://connorhd.co.uk/?p=43</guid>
		<description><![CDATA[I&#8217;d also like to use my blog to write about a few of my personal projects. Many of these aren&#8217;t very useful and very few actually reach any usable state, however I feel node_debug is actually something quite useful.
Before reading this post you should probably know what node.js is.
node_debug allows you to evaluate statements and view their [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d also like to use my blog to write about a few of my personal projects. Many of these aren&#8217;t very useful and very few actually reach any usable state, however I feel node_debug is actually something quite useful.</p>
<p>Before reading this post you should probably know what <a href="http://nodejs.org">node.js</a> is.</p>
<p><a href="http://github.com/Connorhd/node_debug">node_debug</a> allows you to evaluate statements and view their result (much like a <a href="http://en.wikipedia.org/wiki/Read-eval-print_loop">REPL</a>) from your favourite browser (assuming your favourite browser isn&#8217;t IE). It also allows you to browse objects if the result of your statement is an object, for example the global object in node is <code>process</code> so executing <code>process;</code> will return a browsable tree structure of the global scope.</p>
<p><a href="http://connorhd.co.uk/wp-content/uploads/2010/01/node_debug11.png"><img class="alignnone size-full wp-image-48" title="node_debug screenshot 1" src="http://connorhd.co.uk/wp-content/uploads/2010/01/node_debug11.png" alt="" width="447" height="324" /></a></p>
<p>Additionally it exports a log function that allows you to asynchronously output to any active console, for example <code>setTimeout(function () { debug.log("Hello") }, 5000);</code> will output <strong>Hello</strong> to any open consoles after 5 seconds, allowing useful debug output when using asynchronous functions.</p>
<p><a href="http://connorhd.co.uk/wp-content/uploads/2010/01/node_debug21.png"><img class="alignnone size-full wp-image-49" title="node_debug screenshot 2" src="http://connorhd.co.uk/wp-content/uploads/2010/01/node_debug21.png" alt="" width="448" height="323" /></a></p>
<p>To get started using node_debug you need to have a server running node, and to grab the latest node_debug from github. There is an example.js included to show how you would include it in your own scripts, the idea being you can simply include this in your own project in order to easily debug issues.</p>
<p>Future features I would like to add include:</p>
<ul>
<li>A debug.scope object that allows you to import other objects into the debuggers scope (currently only the global scope is available which can cause confusion due to closures in JavaScript hiding a lot of variables from the debugger).</li>
<li>debug.error/warn/info functions in line with the browser console object provided by firebug.</li>
<li>Detect disconnection from node and attempt reconnection.</li>
<li>Caching of asynchronous messages that can be shown when connecting to the console, in order to log things without having an active browser window all the time.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://connorhd.co.uk/2010/01/25/node_debug-a-debugging-console-over-http/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
