The IOIO Board

Recently I purchased a IOIO board so I can start playing around with android’s ability to interact with USB peripheral devices. Ever since I bought my first android device, I always wanted to tap into the power that these devices offer, so as soon as it arrived I started playing with it.

My first task was to solder all the female headers, and the power connector. Once I was done with that I quickly fired up Eclipse and compiled the example code Hello IOIO Power. The code was easy to understand so I decided that i would quickly modify it by adding a seek bar to the main UI, and mapping it to control a port on the board.

My Second task was to learn how to control a rc hobby servo. After some research, I learned is that servos require 4-6 volts DC are controlled  via Pulse Width Modulation (PWM). You will need set the PWM frequency for analog 30-50hz, and for digital servos, 300-400hz. Once you know this information all you need to do is adjust the pulse width of the PWM signal that is controlling the servo. The pulse width is required to be between 1-2 milliseconds. After learning this, I quickly google’d for the color code of one of my old vex servos and learned that the white was the signal, orange was Vin, and black was ground.

I Then finished updating the code, and wired up the IOIO for my first servo control test.

Taking what I learned from the example I Created a simple IOIOThread base class that was based of the thread from Hello IOIO Power. When ever I want to make a new program for the IOIO, I simply have to extend this class.


/**
 * IOIOThread.java
 * @date Jan 11, 2012
 * @author ricky barrette
 * @author Ytai Ben-Tsvi
 * @author Twenty Codes, LLC
 */
package com.TwentyCodes.android.IOIOTruck.ioio;

import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.IOIOFactory;
import ioio.lib.api.exception.ConnectionLostException;

/**
 * This is the thread that maintains the IOIO interaction.
 * 
 * It first creates a IOIO instance and wait for a connection to be
 * established.
 * 
 * Whenever a connection drops, it tries to reconnect, unless this is a
 * result of abort().
 * 
 * @author Ytai Ben-Tsvi
 * @author ricky barrette
 */
public abstract class IOIOThread extends Thread{

	private IOIO mIOIO;
	private boolean isAborted = false;
	private long mUpdateInterval = 10;
	private boolean isStatLedEnabled = false;

	/**
	 * Abort the connection.
	 * 
	 * This is a little tricky synchronization-wise: we need to be handle
	 * the case of abortion happening before the IOIO instance is created or
	 * during its creation.
	 */
	synchronized public void abort() {
		isAborted = true;
		if (mIOIO != null) {
			mIOIO.disconnect();
		}
	}

	/**
	 * @return the isStatLedEnabled
	 */
	public boolean isStatLedEnabled() {
		return isStatLedEnabled;
	}

	/**
	 * Called when the IOIO is connected.
	 * This is where you want to register and initialize each port
	 * @param ioio 
	 */
	public abstract void onConnected(IOIO ioio) throws ConnectionLostException;

	/**
	 * Called when the IOIO is disconnected.
	 */
	public abstract void onDisconnect();

	/**
	 * Called when the IOIO is to be updated.
	 * This is where you want to update the values for each port
	 */
	public abstract void onUpdate() throws ConnectionLostException;

	/**
	 * Thread Body
	 * (non-Javadoc)
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		while (true) {
			synchronized (this) {
				if (isAborted) {
					break;
				}
				mIOIO = IOIOFactory.create();
			}
			try {
				/*
				 * Here we will try to connect to the IOIO board.
				 * 
				 * the waitForConnect() is blocking until it can connect
				 */
				mIOIO.waitForConnect();

				/*
				 * Here we register and initialize each port 
				 */
				DigitalOutput statLed = mIOIO.openDigitalOutput(IOIOValues.STAT_LED_PORT, true);
				onConnected(mIOIO);

				/*
				 * Here we will update the ports every 10 ms (default)
				 */
				while (true) {
					onUpdate();
					statLed.write(!isStatLedEnabled);
					sleep(mUpdateInterval );
				}
			} catch (ConnectionLostException e) {
				onDisconnect();
			} catch (Exception e) {
				e.printStackTrace();
				mIOIO.disconnect();
				onDisconnect();
				break;
			} finally {
				try {
					mIOIO.waitForDisconnect();
					onDisconnect();
				} catch (InterruptedException e) {
				}
			}
		}
	}

	/**
	 * Sets the stat led on / off
	 * @param isStatLedEnabled the isStatLedEnabled to set
	 */
	public synchronized void setStatLedEnabled(boolean isStatLedEnabled) {
		this.isStatLedEnabled = isStatLedEnabled;
	}

	/**
	 * Sets the update interval of the IOIO thread
	 * @param ms
	 */
	public synchronized void setUpdateInverval(long ms){
		mUpdateInterval = ms;
	}

}

 

There is more to come, since I have also decided that the best way to put my knowledge to the test is to build an autonomous vehicle/robot that can navigate to a selected point on the map, using the phone’s GPS and compass.

Leave a Reply