Android ADK Background Service

Tuesday, November 29th, 2011

I was playing around with the ADK and wondering how it would be possible to make the connection work while the app was in the background. The first way I tried was to not close the connection in onPause(). This worked, since the file input and output streams were able to continue … until they were garbage collected. When other Android apps run in the background, they use a Service. I tried this out with the ADK, and it works! Check out the video demonstration below.



Watch video on YouTube

The initialization and opening of the ADK is still done in the main activity. We use the Application class as a friendly singleton to transfer the streams, file descriptor, and usb accessory over to the Service.

  1. private void enableControls(boolean b) {
  2.         ((ServiceADKApplication) getApplication()).setInputStream(mInputStream);
  3.         ((ServiceADKApplication) getApplication()).setOutputStream(mOutputStream);
  4.         ((ServiceADKApplication) getApplication()).setFileDescriptor(mFileDescriptor);
  5.         ((ServiceADKApplication) getApplication()).setUsbAccessory(mAccessory);
  6.         // … snip …
  7. }

Creating the Service is rather straight forward. In ServiceADKActivity:

onCreate()

  1. startService(new Intent(this, ADKService.class));

onPause()

  1. try {
  2.         ADKService.self.startUpdater();
  3. } catch(Exception e) {
  4.         Log.d(TAG, "Starting the updater failed");
  5. }

onResume()

  1. try {
  2.         ADKService.self.stopUpdater();
  3. } catch(Exception e) {
  4.         Log.d(TAG, "Stopping the updater failed");
  5. }

When the Service is started, it creates an Updater thread, which runs every second.

  1. if (!updater.isRunning()) {
  2.         Log.d(TAG, "updater not running");
  3.         updater = new Updater();
  4.         updater.start();
  5. } else {
  6.         Log.d(TAG, "updater running");
  7. }

In background, this can run for a very long time. I let it be for 12 hours before I stopped it.

However… there is a very bizarre bug. Sometimes when the app returns to foreground, it can’t reconnect to the ADK. The main problem is with the USB Manager trying to open the accessory for some reason. The app has permissions to the accessory, and it prints out the connected accessory correctly. Here is my trace of the bug:

  1. @Override
  2.         public void onResume() {
  3.        
  4.         Log.v(TAG, "onResume");
  5.  
  6. // —- onResume is called —-
  7.        
  8.                 super.onResume();
  9.                
  10.                 try {
  11.                         ADKService.self.stopUpdater();
  12.                 } catch(Exception e) {
  13.                         Log.d(TAG, "Stopping the updater failed");
  14.                 }
  15.                
  16. // —- updater is indeed stopped —-
  17.  
  18.                 Intent intent = getIntent();
  19.                
  20.                 if (mInputStream != null && mOutputStream != null) {
  21.                         Log.v(TAG, "input and output stream weren’t null!");
  22.                         enableControls(true);
  23.                         return;
  24.                 }
  25.  
  26. // —- the file i&o streams are null —-
  27.                
  28.                 UsbAccessory[] accessories = mUsbManager.getAccessoryList();
  29.                
  30.                 Log.v(TAG, "all the accessories: " + accessories);
  31.  
  32. // —- shows the connected accessory —-
  33.                
  34.                 UsbAccessory accessory = (accessories == null ? null : accessories[0]);
  35.                 if (accessory != null) {
  36.                         if (mUsbManager.hasPermission(accessory)) {
  37.  
  38. // —- there is permission, going to open the accessory —-
  39.  
  40.                                 Log.v(TAG, "mUsbManager does have permission");
  41.                                 openAccessory(accessory);
  42.                         } else {
  43.                                 Log.v(TAG, "mUsbManager did not have permission");
  44.                                 synchronized (mUsbReceiver) {
  45.                                         if (!mPermissionRequestPending) {
  46.                                                 mUsbManager.requestPermission(accessory,
  47.                                                                 mPermissionIntent);
  48.                                                 mPermissionRequestPending = true;
  49.                                         }
  50.                                 }
  51.                         }
  52.                 } else {
  53.                         Log.d(TAG, "mAccessory is null");
  54.                 }
  55.                
  56.                
  57.         }
  58.  
  59.  
  60. private void openAccessory(UsbAccessory accessory) {
  61.                
  62.                 Log.e(TAG, "openAccessory: " + accessory);
  63.                
  64.                 Log.d(TAG, "this is mUsbManager: " + mUsbManager);
  65.                
  66. // —- prints out the address of usb manager fine —-
  67.  
  68.                 mFileDescriptor = mUsbManager.openAccessory(accessory);
  69.                
  70. // —- Error in log from UsbService: E/UsbService( 110): could not open /dev/usb_accessory
  71.  
  72.                 Log.d(TAG, "Tried to open");
  73.                
  74.                 if (mFileDescriptor != null) {
  75.                         mAccessory = accessory;
  76.                         FileDescriptor fd = mFileDescriptor.getFileDescriptor();
  77.                         mInputStream = new FileInputStream(fd);
  78.                         mOutputStream = new FileOutputStream(fd);
  79.                         mThread = new Thread(null, this, "DemoKit"); // meep
  80.                         mThread.start(); // meep
  81.                         Log.d(TAG, "accessory opened");
  82.                         enableControls(true);
  83.                 } else {
  84.                         Log.d(TAG, "accessory open fail");
  85.                         enableControls(false);
  86.                 }
  87. }

Does anyone know why this may be? I have tried to work around it numerous times but to no luck yet.

You can grab the code off of my GitHub to play around and test it.

It would be great if we could fix this bug!

RoboBrrd Building Video 1

Sunday, November 27th, 2011

There was a lot of footage taken during the build of the latest RoboBrrd. Here is the first of these videos, there’s going to be a little over 10 in total!


Watch on YouTube

Hope you enjoy the video, the series of videos will be a great companion to the Instructable. More of the documentation will be arriving online as we sort through it! :)

I started learning Eagle last week, it was quick to jump in and get started learning. There’s also lots of cool parts libraries out there with Arduinos or other various components that you would need.

Here’s me learning Eagle:

Screen Shot 2011-11-22 at 11.52.07 PM

Here’s where the circuit is at right now. Have to add in the headers and such, and place it on the proto-screw shield!

Screen Shot 2011-11-27 at 11.37.42 PM

More later!

RoboBrrd and the NFC Hats

Monday, November 21st, 2011

I was goofing around the other day thinking of ways that I could use NFC with RoboBrrd. I got a few NFC tags and was also getting a robot achievement badge (thanks Adafruit!), so there had to be some way to connect these things with RoboBrrd!

Looking at one of my first robots, BubbleBoy, I borrowed the hula hoop idea from it… so why not also borrow the hat idea? Sweet! We could have NFC hats with RoboBrrd, where it does different things for the different hats! More super powers, new behaviours! Woot!

Here is an interactive video where you can go through all of the hats and see what they do!

Watch video on YouTube!

For the build, creating hats out of foam is actually a surprisingly difficult task.

IMG_4299 - Version 2

Eventually this is what I ended up with:

IMG_4303 - Version 2

The badges are attached with velcro!

IMG_4294 - Version 2

The purple robot achievement badge hat is RoboBrrd’s favourites!

IMG_4354 - Version 3

RoboBrrd celebrates with the red Maker 2011 hat:

IMG_4403 - Version 3

Chirping and chirping and waving with the top hat:

IMG_4416 - Version 3

But does RoboBrrd like the green hat? NONONONONONO

IMG_4326 - Version 2

The code for this you can find on GitHub, in the Impy_RoboBrrd-mod repo. This is where I will be putting all of the modifications that I make for RoboBrrd! :)

One of the fun parts of the code is how I handle being able to check the NFC to see if the hat was removed/replaced after each action. To do this, I just go through each step.

  1. checkNFC(); // checks the NFC, updates hat (0 = no hat)
  2.    
  3.     switch (hat) {
  4.         case 0:
  5.             eyesWhite();
  6.             break;
  7.         case 1: // top hat
  8.            
  9.             photovore = true;
  10.             photovoreCheck(); // check the LDRs each time
  11.            
  12.             switch (step) {
  13.                 // snip – does the different actions here
  14.             }
  15.            
  16.             step++;
  17.             if(step > 5) step = 0; // after the 5 steps it resets
  18.            
  19.             break;
  20.     }

Of course, this would be much better if we had multi-threading and all of that. There are ways you can do threads with the Arduino … sort of … but sometimes I find that goofing around with sequentialness makes you think a bit more.

I’m going to challenge myself to try to keep this Arduino Diecimila 168 inside of Impy RoboBrrd and not upgrade it to a MEGA. I think it will be fine, but it will be fun to have to deal with not enough pins.

For instance, to make the NFC shield (thanks seeedstudio!) work, I had to use four pins that were previously used for servos, which meant I had to move the LEDs to the analog input pins, which now means that there is only one analog input available now.

The NFC shield is mounted between the two ledges on the side faces:

IMG_4455 - Version 2

With some dual lock, so it is easy to remove:

IMG_4457

I want to eventually add in two tilt balls and some e-textile fabric sensory things. Adding in some multiplexers shouldn’t be too much of a problem for some things, like controlling the LEDs. It is kind of fun dealing with this sort of problems :)

On the Adafruit Show and Tell, ladyada had a cool idea that I never thought of, actually writing a paper about this. I’ll probably do this. I’ll probably have to make a make-believe deadline to actually get it done. There is an Open Hardware journal, maybe it would be cool to submit it to there. The only weird part is that when I was researching last night, I couldn’t find anything about NFC and human-robot interaction. Has anyone seen anything about this? Or perhaps, RFID?

Eventually with these NFC hats I want to add on some features, like being able to write how often it has been played with on the hat. Using the green hat as an example, perhaps every 5th time that RoboBrrd wears the hat, then it will like it!

That’s all for now. This week I’m going to try experimenting with laying out a board for RoboBrrd! OOOH! Hope you enjoy the interactive video!

Impy RoboBrrd – RoboBrrds Dancing!

Friday, November 18th, 2011

Impy RoboBrrd
Vote for RoboBrrd in the laser cutter contest! ~( ^v^ )~

Here is a video of the RoboBrrds warming up for the Robot Party, which turned out to be a blast!

What is that on RoboBrrd’s head in the above photo, you ask?
Well, it’s a NFC enabled achievement hat! Put your achievement badge onto a foam hat + NFC, and see what you unlock when you place it on RoboBrrd’s head! Special behaviours? New chirps? A crazy RoboBrrd dance-a-thon? YEAH!

We’ll be documenting more of this ‘mod’ that you can add on to your RoboBrrd as well!

Don’t forget to vote for my RoboBrrd Instructable in the laser cutter contest! Thanks so much!

Impy RoboBrrd – All Photos on Flickr!

Wednesday, November 16th, 2011

Impy RoboBrrd
Vote for RoboBrrd in the laser cutter contest! ~( ^v^ )~

All of the photos that you see on the Instructable, and more, are now on Flickr!

So you can see what tools you need more clearly:

Impy RoboBrrd

One of my favourite things about this RoboBrrd was how shiny it was on the insides:

Impy RoboBrrd

Plus, the wiring is super tidy!

Impy RoboBrrd

With the hula hoop on, this reminds me of something like a RoboBrrd peace sign…

Impy RoboBrrd

Speaking of hula hoop, here are two capacitors that look like a happy face! They are placed on the two motor pins, going to ground.

Impy RoboBrrd

The legs ran away! Hahahaha

Impy RoboBrrd

Gluing the eyes on is super fun. It always amazes me how well the two materials, hot glue and googely eye, bond together. They are very sturdy.

Impy RoboBrrd

You can see all of the steps for the RoboBrrd on the Instructable here. Please vote and ask your friends to vote for RoboBrrd in the laser cutter contest! It means a lot :)

One of the cool things about RoboBrrd is that it is so silly that anyone can like it, and anyone can learn a little bit more about robotics from it. For example, an often overlooked part of robotics- laying out the electronics and wiring so that everything fits nicely, without wasting too much space or not having enough space. With this RoboBrrd, we took this into consideration when designing the base, and tried to not waste any extra space, while still having enough room for maintenance and wires. Cool eh! :D

We’ll be posting more updates throughout the week, so stay posted! :)

RoboBrrd Instructable and the race for a laser cutter!

Tuesday, November 15th, 2011


Vote for RoboBrrd in the laser cutter contest! ~( ^v^ )~

Wooohoo! We have documented everything and made it so that you can now follow an Instructable to build your own RoboBrrd! Huzzah!

Here is a teaser for the new RoboBrrd, nicknamed Impy (short for Important). :)


Watch video on YouTube.

Throughout the week we will be posting here some supplemental photos, videos, and CAD illustrations for the build of the RoboBrrd!

For example, the base! The base is covered in Step 5.

Here is a very clear way of seeing all of the craft sticks, highlighted with different colours to show the different parts.

Base CAD

We were able to make everything fit in a 16cmx14cm piece, including the battery and controller board. This is the bottom face to that base:

Base CAD

Add on a platform to that, and you will be able to have a place to mount the servos!

Base CAD

The base is 4cm tall, enough space for everything to fit, and for the rotational servo to poke out just a little bit. :)

Base CAD

Add on the top face, as you can see in the first CAD, and there you have the base! :)

Making this Instructable was quite a challenge, since there are many steps involved that have to be done in a set order. Hopefully you will find it exciting, and make your own RoboBrrd! They are quite a hoot!

If we do win the laser cutter, we will be able to zap out some pre-cut shapes for all of RoboBrrd’s faces. This would effectively cut down the construction time for the RoboBrrd immensely. With less construction time, this would be able to fit into a school’s lesson plan a lot easier- and there is a lot of things that can be learnt about robotics from building a RoboBrrd, like a voltage regulator circuit!

Share this with your friends, get your neighbour to vote even. Let’s get more people excited about robotics!

apps4arduino – Meters for Arduino on iOS, Introducing Wijourno!

Tuesday, November 8th, 2011

Meters for Arduino is now available for iOS! You can get it on the App Store here:
http://itunes.com/apps/metersforarduino :)

It communicates with Meters for Arduino on Mac, which just had a 1.1 update. You can get it on the Mac App Store here:
http://itunes.com/mac/metersforarduino :)


View the demo video on YouTube

Meters for Arduino on iOS works with the magic of Wijourno. Wijourno lets you communicate with your iOS devices and your Mac.

It’s a lot of fun, since you can send messages to specific devices, or broadcast a message to all devices. I’m imagining things where (for robots) you could have the iPad displaying lots of diagnostics and such, the iPhone would be the controller, and the Mac would be the data logger and connection to the internet!

Check out the apps4arduino site for more information, including some details on how you can use Wijourno in your own Apps. :) Can’t wait to see what people are going to make with this!