Showing posts with label mindstorms. Show all posts
Showing posts with label mindstorms. Show all posts

Sunday, 10 October 2010

Over The Air Hack - a LEGO robot with an iPhone brain

IMG_6032

As promised, here are the details of my Over The Air hack — The Eyes Have It — which won prizes for “Best Hardware Hack” and “Best Use of Other Features” in the overnight hack-a-thon competition. I also took it along to Mobile Monday London’s Demo Night on the following Monday.

The hack was a LEGO Mindstorms robot with an iPhone brain that followed faces in front of it and steered towards them. It performed well on stage, following me as I gestured towards it, just like a small pet.

The hack was composed of two parts: an iPhone app that detected faces in the video feed from the front-facing camera; and the LEGO robot that took instructions from the iPhone and steered accordingly.

Communication

Getting the two parts to communicate was one of the trickiest areas to get right, and caused extra headaches before each presentation. Both LEGO Mindstorms and the iPhone can communicate over Bluetooth, but Apple has restricted Bluetooth communication to companies that will pay the Apple “Made For iPhone” license fee or that use particular hardware. Bluetooth comms is not available through the standard SDK and apparently needs some kind of “secret handshake” to work.

Unfortunately, Mindstorms came out a while before the iPhone and uses a different Bluetooth chip; and LEGO and Apple haven’t managed to do a deal to provide Mindstorms access from the iPhone (perhaps because LEGO has open sourced much of their software?). LEGO has now released an Android app to showcase mobile phone integration, so let's hope Apple can work with them to get some iPhone apps too.

So no Bluetooth, and Apple won’t let you talk over the dock connector either… That left feeding information to the robot through one of its five senses — a touch-sensitive button, an ultrasound distance sensor, a microphone, a light sensor or the motors themselves (which can detect rotation).

A quick search on LEGO Mindstorms iPhone brought up the iPhoneRobot which used the light sensor to pick up different greys on the screen. This was a great start, but it wasn’t quite what I was looking for. For a start, it required the LeJOS firmware on the Mindstorms brick — this is a cut-down Java VM that replaces the built-in LEGO firmware. I’ve left my Mindstorms with the default LEGO firmware as I use it with my 7 year-old son. He’s not quite ready for Java, but can easily understand the Labview-based visual programming that comes with the Mindstorms kit. Secondly, the robot itself wasn’t quite suitable — I wanted a robot that would recognise faces, so I needed the iPhone to be pointing upwards towards people’s heads rather than along the ground.

Motor skills — building the robot

Designing a new robot from scratch takes quite a while and I only had overnight. Unlike normal LEGO with bumps and holes, the Mindstorms kit uses the new-style LEGO Technic, which is mostly holes and connectors. Also, LEGO’s own sample models are pretty complex, as they are built to look like animals or people as well as interact.

Fortunately, I found a great site with designs for quick Mindstorms models that are easier to hack to do what you want. Some of the ideas are amazing — especially a Segway that actually balances! I started with the 3-Motor Chassis and added the distance sensor on the front to prevent crashes. I also added the button sensor on the side to help with starting and stopping the program, as the iPhone was in the way of the program buttons on the Mindstorms block.

Here’s some pictures of the final result, holding the iPhone pointing upwards. I’ll try and generate some build instructions later.

Computer vision — the iPhone app

Humans are exceptionally good at seeing faces. Our brains have been trained from birth to detect and analyse faces very quickly. We can tell which way people are looking from far away and even see faces in random patterns.

Computers have a harder time of it, although recent developments have massively improved what is possible. Companies such as Polar Rose have shown demonstrations of both face detection (finding out where any faces are in an image or video) and facial recognition (matching the detected faces against a database of known images) running in real-time on mobile phones. Unfortunately, their code is not available to an overnight hacker, though they’ve recently been bought by Apple so we may see interesting capabilities in future iPhones.

However, Intel launched an open source project back in 1999 called OpenCV (for Computer Vision) and not only is it still going strong, but the library is easily compatible with the iPhone and has a git repository of a ready-built library. OpenCV is all about providing well-optimised functions for real-time computer vision, so that developers do not have to reinvent the wheel. It includes face detection algorithms and a guy called Roy has posted some examples of how to get face detection working on an iPhone video feed.

Roy’s sample code was written for iOS 3, and iOS 4 provides much easier methods for accessing the video feed from the device. I updated Roy’s code to use the new AVCaptureVideoDataOutput class that provides direct access to uncompressed video frames from any iPhone camera. This bit took a little while longer than it should have done, as the video feed is provided as landscape (you’re recording a video, right?) whereas its preview feed is oriented the same way as the camera. This was not obvious, and made worse by the fact that face detection algorithms do not work when the image is rotated by 90°… There was a point in the early hours of Saturday morning when I thought there would be no face detection at all!

Anyway, following Roy’s recommendations, I scaled down the input image and adjusted the parameters of the OpenCV face detection call. At the moment I transpose the image before sending it to the detection algorithm, but I suspect it would be faster to use a rotated Haar feature set (the bits that the algorithm picks out in each image to match faces). I also didn’t use Roy’s changes to use integer arithmetic rather than floating point — it turns out that the iPhone 4G has enough grunt to cope with the standard OpenCV code.

You can get the iPhone code from my github repository and try it for yourself. Note that it’s hardwired to use the front-facing camera at the moment. If you don’t have an iPhone 4 just change the AVCaptureDevice to point to the ID of the other camera and the rest of the code should still work (though possibly a little more slowly…).

Light and dark — the LEGO program

So now I had a robot base and an iPhone app that could see faces. The next step was to connect the two together using the light sensor.

LEGO provide a drag and drop programming interface for Mindstorms that lets you build up programs using blocks such as “move motor”, “wait for sensor input” and control logic of loops and if/else switches. It’s quite capable and makes simple programs relatively easy, but using variables and arithmetic can be a little cumbersome.

The main issue in getting the robot to drive was calibrating the light sensor, especially when each demo was under different lighting. After a fair amount of tweaking (some just minutes before presenting at Mobile Monday London’s demo night), the best results turned out to be when I crammed a small piece of cardboard into the hinge that held the light sensor onto the iPhone…

You can download the “.rbtx” file from my github repository, but for those who don’t have the LEGO software, the algorithm is essentially:

  1. Calibrate the sensor when pointing at the black and white squares to either side of the control square on the iPhone screen
    • The robot prompts for each reading with its display and waits for you to press the button between each sensor reading
    • The program reads the raw values from the sensor and calculates its own scaling values, as the built-in calibration routines turn on the light on the sensor — this works for reading black lines on a white sheet of paper but isn’t so good at reading the backlit screen of the iPhone…
  2. Wait for another button press to start the robot moving — so you can step back and make sure your face is in frame
  3. Read the raw value of the light sensor, convert it into a value between -90 and +90, and then steer that amount, then repeat
    • The program checks that the light sensor value is within a reasonable range before steering, otherwise the robot tends to go round in tight circles and you have to run round it like a lunatic trying to get your face in the camera frame!
  4. When the distance sensor picks up something closer than 6 inches, stop, play a sound and show a beating heart on the display (“I’ve found you!”)
  5. Start moving again when the button is pressed

To give you an idea of what this looks like in the Mindstorms NXT software, here’s a picture of the program!

Prizes

Thanks to Monotype for the beautiful poster of Gill Sans Bold Extra Condensed. They were going to give me two, but were happy to swap one for a copy of FontExplorer Pro instead, so I can see my digital fonts presented almost as prettily. Apparently, it’s now available for Windows as well as Mac OS X.

Wednesday, 20 May 2009

Kizoom team at Yahoo! Open Hack London 2009

For the first time, I managed to convince some other Kizoom developers to come along to a weekend developer event. Dan, Matthew, Neo and Joyce joined me for Yahoo!’s Open Hack London 2009, and Neo’s friend San brought a much welcomed Lego Mindstorms set to the party.

Our team. Image courtesy of Nate Lanxon, CNET UK

And it was a pretty good party (though strictly a geeky one), complete with beer, music, snacks, giant Jenga and scalextric. There was even a fantastic live band (this being a geek party, the band consisted of guitar and Nintendo Gameboy…) and an iPhone orchestra:

But the most fun was the hacking. There were some absolutely stunning hacks produced over the weekend — better than both Hackday 2007 and Over The Air — and the Kizoom team had a great time playing with sticking things together for the hell of it. Mindstorms especially is great for hacks — you can run a Java VM on the programmable brick (see http://www.lejos,org) and then send it Java applets over Bluetooth. You then start attaching motors, microphones, light and distance sensors and building up the model with Lego pieces. Making silly things is fun and easy, and if you want to get serious you can even build a Rubik’s cube solver! We built a robot finger that pressed a button on a keyboard when it hears a loud noise (like playing a bongo drum!).

Since there were six of us, we pulled in other bits of hardware to play with as well — a Wiimote strapped to a scooter (ridden by Dan) sending its motion via Open Sound Control and Processing to a MacBook; and a JavaME app on a cameraphone with some image recognition to identify the omnipresent Hackday beanbags. Dan and Matthew built the hub of all these hacklets — a browser driven by Selenium, in turn driven by simple web services.

Aside from having a great time, both learning and building, we were very pleased to win the prize for “Most Awesome Hack”. I’d really like to include a video of our presentation, but that’s not available yet. In the meantime, here’s a few pictures of the various components:

The Fabulous Human Powered Browser Dan operating the scrollbar by riding the Wii-equipped scooter

The hacking itself was preceded by a morning of seminars on various Yahoo! and other APIs. I’ve included my notes below on the ones I attended in case anyone finds them useful.

GeoPlanet

Martin Barnes — Data Manager, Yahoo! Geo Technologies, UK (based in Shaftesbury Avenue)

barnes@yahoo-inc.com

Slides available

  • Most of the named places on Earth + relationships that tie them together
    • Not down to street level — just towns, villages & suburbs
  • Not a geocoder — provides you with some idea of the centroid of the object of interest
  • Official databases such as USPS also have weird and wacky data that doesn’t really exist in the real world — database
  • Integrates with Flickr & Fireagle so can expand on WOEIDs from these
  • Telephone area codes also included — don’t have exact boundaries, but can find out which cities are involved
  • WOEIDs sometimes get deprecated, but the API still responds to them — it just forwards you to the new authoritative WOEID
  • Each API key is limited to 50K queries a day
  • Have neighbour relationships for cities and towns
    • mainly for US, where they just spread one into the other
    • but also useful for other places
  • Can return GeoJSON: http://geojson.org/
  • GeoPlanet Forum constantly monitored
  • UK postcodes data is currently about 3 years out of date since Yahoo! are refusing to pay license…

Future plans

  • Plan to have data in some sort of escrow
    • depends on licensing deals with Navteq etc
  • Plan to integrate Geonames data
    • Original WhereHaus was based on Geonames but branched from it to get other sources
  • Plan to have user-based input, like Geonames

RDFa

Mark Birbeck mark.birbeck@webBackplane.com

  • Allows RDF to be embedded in web pages using attributes
  • Can attach license information to an individual image:
    • <img src="blah.jpg" rel="license" resource="http://creativecommons.org/license..."/>
  • No need for separate feeds
  • Helps for federated sites — just add markup to output from different systems
    • Can then pull together disparate outputs
  • Have built extensions to Drupal to add RDFa to contact details, etc.
  • See also OpenCalais http://www.opencalais.com/about by ThomsonReuters
    • identifies entities, facts and events in unstructured text, XML & HTML
  • See samples at http://code.google.com/p/ubiquity-rdfa/source/browse/#svn/trunk/_samples

Search Monkey

Neil Crosby — European Frontend Architect for Search at Yahoo

Slides available

  • extensions to Yahoo’s search engine: extend search results — making it easier for the user
  • users choose to install additional search monkeys from http://gallery.search.yahoo.com/
  • also supports XSLT that can transform whatever you want from the source page
    • e.g. if you have tables, you can pull them out as tuples
  • not done a huge amount of publicity to users about specific monkeys, but have turned on a bunch of monkeys automatically for all users
  • can also develop a monkey for your own site — can add to a header (see documentation somewhere)
    • in future may want to turn that on automatically

Blueprint

Ricardo Varela — Mobile Integration Engineering Lead, Yahoo ricardov@yahoo-inc.com

Blog: http://phobeo.com/

Presentation and handout available online.

I first learnt about Blueprint at Future of Mobile 2008 back in November last year, and Ricardo’s presentation was similar then. He understands the problems that are faced in mobile development, and Blueprint, like Luca Passani’s WURFL-based WALL, seems like a reasonable way to go forwards. However, it’s all down to the implementation and the edge cases and that’s a lot of hard work.

Quite a few people used Blueprint in their hacks, but didn’t actually demo on mobile phones (other than iPhones…). Dale Lane actually tried to make it work on Windows Mobile & Android G1 and had a horrible experience. When he visited his Blueprint site from his Windows Mobile browser, he actually got a message telling him to download Opera instead! That kind of ruins the point of a cross-browser system…

Yahoo!’s recent announcement that they’re killing all non-iPhone mobile native app development may point to Blueprint for mobile web getting stronger within Yahoo! But the promised native clients for BlackBerry and J2ME may now be dead in the water.

Anyway, here’s my notes from the session at Hackday:

  • Now gone to v1.1
  • Yahoo Mobile used to be text only, now graphics and styles etc.
  • some Nokia browsers won’t show tables unless you specify a doctype
  • video encoding for 40+ different mobile formats
    • currently only for videos hosted within yahoo.com
  • slideshare did mobile site with blueprint — created in 24hrs
  • similarly, http://m.fynbus.info/ — mobile version of buses in Fyn, DK
  • 1 out of 3 iPhone users has Facebook
  • Android and iPhone native wrappers still in development
    • were in development in November last year at Future of Mobile, so not that much progress…
  • this first release doesn’t have style customization
    • thinking about it — but can’t just support CSS, will have to have some subset
  • Some Blackberries are not standards-compliant for cookies
    • use commas not semicolons
    • if not in Blueprint now, can be added