Peter_vdL

Code to Launch Foam Missiles Over USB!

by Peter_vdL Motorola 08-22-2011 02:44 PM - edited 08-29-2011 11:03 AM

 By  Peter van der Linden,   Android Technology Evangelist

and

      Kris Schaller,  Co-Op program member and Computer Science student

 

What USB devices do developers enjoy the most?  There's lots of fun stuff to choose between, with novelty thumb drives, multi-terabyte storage, USB fans and plasma balls.  You just know that a lot of software developers are going to put "USB-controlled missile launcher" near the top of their list.   And when you need to defend the borders of your cubicle with a missile launcher, you surely want to control it from your XOOM tablet. 

 

This blog post describes how to connect your XOOM to a USB foam missile launcher, with enough detail that just about anyone can do it.   First we'll describe the hardware, then the Android app software. 3 ... 2 ...1, let's blast off.

 

Project Hardware

Here is a list of all the hardware you need:

  1. Motorola XOOM tablet running Android 3.1 (API level 12) or later
  2. Motorola 89454N cable adapter for XOOM
  3. USB foam missile launcher

When you've acquired all this hardware, your set-up will look like this picture.

 

 cannon1.jpg

       The "missile launcher" from the Hong Kong-based gadget manufacturer, Dream Cheeky.

 

You can see a picture of the missile launcher on the XOOM screen.  That picture (app icon) is part of the dialog you get when you plug this USB device into the XOOM.  The Android framework lets you choose an app that will be started automatically on future plug-ins of this USB device, if you wish it. 

 

If you look closely, the missile launcher on the XOOM screen is a robust, olive green, chunky, "take no prisoners" missile launcher.  Whereas, the brightly-painted cannon on the desk looks like it was ordered from ACME surplus in a Roadrunner/coyote cartoon. 

 

The supply chain for USB novelty weaponry isn't everything it could be, and the circus cannon was all we could lay our hands on at short notice, OK?  The circus cannon is made by the same people who make the missile launcher (Dream Cheeky in Hong Kong - we presume the phrase means something significant in Cantonese?)  The circus cannon works with our Android software, but we haven't had a chance to test an actual missile launcher yet.  And if it hasn't been tested, you should assume it doesn't work.

 

We're going to continue calling the hardware a "missile launcher" because who wants to work on something that looks like it was liberated from a tot's playground by an excitable clown? The "missile launcher" we used was ordered from http://www.amazon.com/Dream-Cheeky-USB-Circus-Cannon/dp/B000LQZV0M/ .  

 

Missile Launcher

The Missile Launcher stands about 6 inches tall.   It has a circular base, on which is mounted a second ring.  The second ring holds the cannon barrel, and can rotate about 340 degrees.  The gun barrel can elevate about 45 degrees up from flat.  

 

The missiles are expelled from the gun barrel by compressed air.  It takes several seconds to pump up to operating pressure, and the compressor motor is gratifyingly noisy.   The swiveling and raise/lower motions are also accompanied by a loud grinding of gears and transmissions.  There's been a suggestion that the sound comes from a loudspeaker, not actual mechanical parts.  All we can say is this is not a stealth technology.  There is a 3 foot long USB cable with a standard type A plug.  You need the 89454N cable adapter kit, to connect the launcher to the USB micro type B receptacle on the XOOM.

 

USB on Android

The launcher comes with a CD containing the software to control it from Windows.  We carefully placed the Windows CD to one side, of the wastepaper bin.   We planned to take advantage of a new API, and control the missile launcher from an Android app, over a USB connection.

 

There's a briefing on USB basics in this blog post.   Wikipedia also has a good introduction to USB.  Google introduced the USB API in API level 12 (Android 3.1 Honeycomb).  That gives us the android.hardware.usb package, documented at http://developer.android.com/reference/android/hardware/usb/package-summary.html

The android.hardware.usb package lets you do two things:


  • use the standard USB protocol, but only if the android equipment can act as a USB Host.  Luckily, XOOMs implement enough of the USB "On The Go" protocol to do this, in many cases.  The Missile Launcher app needs hardware with this feature. 

That's a critical point for software developers, so let's say it again with different words. To control a USB device, an Android computer has to be capable of acting as a USB host (bus controller).  Most Android handsets do not have this capability, but the XOOM tablet does.  The Missile Launcher app won't run on your Android phone.

 

  • use the Google-invented extension to the USB protocol (which is known as the "Open Accessory API") to connect your Android equipment acting as a USB Device, to some other USB hardware that (with an Arduino kit or something else) works as a USB Host.  


Google has also created a backward compatability library that supports the android.hardware.usb package on API level 10 (Android 2.3.3 Gingerbread).  This library, com.android.future.usb.accessory, is not downloadable from the web.  It relies on platform-dependent system library changes, and can therefore only be integrated by hardware manufacturers.  

 

If you want to work with USB on Gingerbread devices, you must choose a device which ships from the manufacturer with the backward compatability library already installed.  If you have the device, you can easily check if it supports the library.  Create the "hello world" app, and tuck these lines into the manifest file:

 

 <application android:label="TestUSBlib" android:icon="@drawable/icon">
     <uses-library android:name="com.android.future.usb.accessory"/>

 

 Then try to run the app.   If the library is not on the device, Eclipse will refuse to install it, displaying an error message dialog on your PC, and a log entry on the device.


Be aware that there are minor but annoying usage differences between the Google USB add on library and the platform APIs.  More details at http://developer.android.com/guide/topics/usb/accessory.html.


Project Software

 

We started out with the intention of programming the Open Accessory API and the Arduino kit, to act as a USB host that controlled both the missile launcher and the XOOM tablet.   However, we quickly discovered that was not necessary.  Google has already provided a sample Android app to control this missile launcher over USB!

 

If you have downloaded the code samples for API level 12, you already have the Google "Missile Launcher" app.  Here are the complete instructions on how to create the Android app to control the missile launcher from your XOOM:

 

  1. create a MOTODEV Studio or Eclipse android project based on <SDK>/samples/android-12/USB/MissileLauncher/ , build the app and install it on your XOOM.
  2. plug the missile launcher into the cable adapter, and the cable adapter into the XOOM.
  3. accept the "app launch" dialog that appears when a USB device is plugged in.  Then, subdue neighboring cubes with a barrage of missiles.
  4. Profit!!

Adding a new user interface
The missile launcher in the sample app has a rudimentary interface.  It senses the accelerometer to detect when you tilt the XOOM, and uses that as an analog for cannon movements.  There is a single button for missile launching.  The point of the app is to be an exemplar for USB, not GUIs.

goog.gui.png

GUI of <SDK>/samples/android-12/USB/MissileLauncher App

 

Although we didn’t need to write any code, we created a new and improved user interface for Google’s sample app anyway.  That's just us.  We control the launcher motion with buttons, instead of the accelerometer.   The new interface looks like this

newgui.png

New and improved user interface for the missile launcher

 

Pressing the "up" button elevates the gun barrel.  Pressing it a second time stops the motion.  All the other buttons work in the same way.   We supply a screen background of a fearsome military camouflage kind, to prove that the missile commander means business.   The compass rose is just there for eye candy - to provide some movement in the display.  

 

Buttons aren't the best component to control the missile launcher hardware.  We're thinking of upgrading the GUI to use a horizontal slider for left/right bearing (azimuth) and a vertical slider for up/down elevation (altitude).  We'd also like to have two animated icons that depict barrel motion in real time.  That's a project for another day. (Or another developer - post a message if you're interested in doing this).


How an App tells the Framework about the USB devices it can control

Some of the work is done in the app manifest, saying “when someone plugs in a USB device, I might be interested.”  The AndroidManifest.xml file says that with these lines:

<activity>  ...

<intent-filter>
    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>

<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
               android:resource="@xml/device_filter" />

</activity>


The device_filter resource is in file res/xml/device_filter.xml, and it contains these lines

<resources>
    <!-- vendor and product ID for Dream Cheeky USB Missle Launcher -->
    <usb-device vendor-id="2689" product-id="1793" />
    <!-- vendor and product ID for Dream Cheeky Wireless USB Missle Launcher -->
    <usb-device vendor-id="2689" product-id="65281" />
</resources> 

That tells us that this app can control two products from vendor-id 2689, Dream Cheeky.   Vendor ids are issued by a central organization, USB.org.  Product ids are assigned by each manufacturer.  The two together uniquely identify any USB device, and let the framework find the software that knows how to control that device.  That's how an Android app tells the framework that the app knows how to control USB product-id 1793 and 65281 - two different models of launcher.   If you have a Mac computer, you can check the vendor id of any USB device by plugging the USB device in, then clicking Apple > About this Mac  > More info > USB.  That tells us, of the two supported devices, this cannon is product-id 1793.

 

 

Advice for App Writers and Artillery persons

So there you have it - the easiest app you'll never have to write - unless you also want a new and improved GUI that will strike fear into the black hearts of your cubicle neighbors.

 

      Peter van der Linden,   Android Technology Evangelist

and

      Kris Schaller,  Co-Op program member and Computer Science student

 

 

 

References

 

  1. http://community.developer.motorola.com/t5/MOTODEV-Blog/The-Open-Accessory-API-and-USB-On-The-Go/ba-...
  2. http://www.google.com/events/io/2011/sessions/android-open-accessory-api-and-development-kit-adk.htm...
  3. http://android-developers.blogspot.com/2011/05/bright-idea-android-open-accessories.html
  4. http://developer.android.com/reference/android/hardware/usb/package-summary.html
  5. http://developer.android.com/guide/topics/usb/adk.html





Comments
by James Robinson(anon) on 09-08-2011 09:51 AM

As irreverant and useful as ever! Great stuff!

by tonper on 11-22-2011 11:46 PM

What model of Xoom and version of Android did you use? I have tried this on my MZ601 3.1, but I can not get the USB host API to work at all.

by Peter_vdL Motorola 11-23-2011 01:42 PM - edited 11-23-2011 01:43 PM

Hi Ton,

 

I ran this on a XOOM MZ600 from the North American market.   It was tested under Honeycomb 3.0, 3.1, and 3.2.

 

You can check that the USB On The Go works by plugging a USB keyboard or mouse into the XOOM, using the "camera kit" adapter cable.

 

The most likely cause of failure is that you aren't using the exact cannon that we tested with - the Dream Cheeky USB Circus cannon.    I procured one of the the more military cannon, and immediately found that it would not work - it told the host device that it wanted to draw 1 Amp of current.  The XOOM will only drive a maximum of 0.5 Amp devices.

by tonper on 11-24-2011 12:38 AM

Hi Peter,

 

Thanks for your answer. However, I don't think that the current is my problem. Please look at my thread in the development forum where I have described my problem more in details.

Post a Comment
Be sure to enter a unique name. You can't reuse a name that's already in use.
Be sure to enter a unique email address. You can't reuse an email address that's already in use.
reCAPTCHA challenge image
Type the characters you see in the picture above.Type the words you hear.
About MOTODEV Blog
The MOTODEV blog keeps you updated on mobile app development news from MOTODEV and the Android developer community.

Subscribe to our RSS feed Subscribe via RSS

Follow Us:
Fan MOTODEV on Facebook Join the MOTODEV LinkedIn Group MOTODEV on YouTube

motodev profile

motodev RT @StackMob: Join StackMob and Salesforce at the MOTODEV Google + Hangout today at noon. Register here l2cloud-stackmob.eventbrite.com 7 days ago · reply · retweet · favorite

motodev profile

motodev G+ Hangout: Migrating ur legacy systems to the cloud & mobile is at @ 12pm PDT today. Join us and bring your questions. #l2cloud 7 days ago · reply · retweet · favorite

motodev profile

motodev RT @sidneyallen: Google + Hangout kicking off in 10 min moto.ly/l2cloud Join StackMob, Salesforce and MOTODEV Team 7 days ago · reply · retweet · favorite

Our Blog & Comment Policy
Opinions expressed here and in any corresponding comments are the personal opinions of the original authors, not of Motorola. The content is provided for informational purposes only and is not meant to be an endorsement or representation by Motorola or any other party.

Remember, when you comment, please stay on topic and avoid spam, profanity, and anything else that violates our user guidelines. All comments require approval by Motorola and can be rejected for any reason.

For customer support issues with your Motorola phone go to the Motorola customer support website.