Monday, April 8, 2013

Android IOIO - motor control TB6612FNG


Summary
This is an Android example on how to connect a ioio mint, the motor driver TB6612FNG, and two motors.

Shortcut
The important stuff can all be found at: https://github.com/ergobot/ioioMotorDriver
Fritzing file - "ioioMotorControl.fzz"
Fritzing export - "ioioMotorControl_export.png"
Working example

Description

The ioio is a board to help our android devices interact with sensors, motors, etc.  By using a ioio, Android device, two motors, a motor driver, and a battery, we extend and change the function of our Android device. 

Alternatives - "What's so different?"
There are alternatives.  In fact, I used an Arduino first, with all the same stuff (except the ioio), in order to isolate any ioio problems.    
Unlike many alternatives,  We can design/write a user interface (an android  activity) that is able to directly interact with the outside world (sensors).  We can get an Android device to interact with the physical world by building an app.  The code written for the app is the same code that is used for the ioio.   The ioio becomes an extension of the Android device.  

Bonus - Bluetooth 
The ioio can communicate with the Android device through usb or bluetooth, no extra calls required.  In the past, I've taken apart google's bluetooth example for android so that I could use bluetooth with an Arduino (bluesmirf gold). I made it work but.... it wasn't very fun.   

This example shows how to wire together all of the components needed and write a little to get the components to work.  
  

Before we begin


I'm using the following (with links): 

ioio mint - http://www.adafruit.com/products/885 (originally from http://droidalyzer.com/tech.html)

Motor Driver 1A Dual TB6612FNG - https://www.sparkfun.com/products/9457

Battery (for the motors) - In this case, I used a 2S Lipo 7.4V battery.  A 9V battery would work fine as well.

Two motors - These are toy motors.  I know these motors will work because both motors will run on the same 9v battery.   
  
A breadboard - The breadboard was used to tie the grounds together.

Connect it all together
All of the connections are easy to examine using the fritzing diagram, the picture, or the written java files.  All of these items are available at: https://github.com/ergobot/ioioMotorDriver

1. Connect the ioio mint to the motor driver.  
ioio mint pin #30 to motor driver pin PWMA
ioio mint pin #31 to motor driver pin AIN2
ioio mint pin #32 to motor driver pin AIN1
ioio mint pin #42 to motor driver pin STBY
ioio mint pin #43 to motor driver pin BIN1
ioio mint pin #44 to motor driver pin BIN2
ioio mint pin #45 to motor driver pin PWMB

2. Connect the motors
one motor connected to AO1 and AO2
the other motor connected to B01 and B02

3. Grounds/Power
Connect the ioio mint pin gnd to the battery negative and to the motor driver pin GND (the GND between VCC and AO1)
ioio mint pin 3.3V to the motor driver pin VCC
motor driver pin VM to the battery positive

Fritzing Diagram


Get it to work

Machines of all types need instruction(s). 

If this is your first ioio project, go to the sparkfun ioio intro page:

Writing the code for this wasn't too bad.  I learned from several sites/references:
Making Android Accessories with IOIO by Simon Monk - there are awesome examples and clear instructions
http://www.jaychakravarty.com/?p=146 - This site has great information on how to do the same we're doing here. 
  
I'm not going to go over the code in great detail.  I looked at the two sources above and wrote the Motor.java and MotorDriver.java for simplicity.  These two classes were implemented in MainActivity.java.  

Quick example ---

1.  Declare a new MotorDriver  inside the Looper class


class Looper extends BaseIOIOLooper {
private MotorDriver motorDriver;

}

2. Inside the setup method of your looper, setup your motordriver


@Override
protected void setup() throws ConnectionLostException {
motorDriver = new MotorDriver(ioio_);
motorDriver.SetupMotorA(AIN1_PIN, AIN2_PIN, PWMA_PIN);
motorDriver.SetupMotorB(BIN1_PIN, BIN2_PIN, PWMB_PIN);
motorDriver.SetupStandBy(STBY_PIN);

}

Where AIN1_PIN, AIN2_PIN, PWMA_PIN.... etc are just the pin numbers (int type) you are using on the ioio for the motor driver


3.  In the main loop, use your motor driver


public void loop() throws ConnectionLostException {
// Example Forward movement
motorDriver.MotorA().FullPower();
motorDriver.MotorB().FullPower();
motorDriver.MotorA().Clockwise();
motorDriver.MotorB().Clockwise();
motorDriver.RefreshAll();
}

Example code:


Enjoy!!!
References (anything I used to write this)

first ioio project tutorial - https://www.sparkfun.com/tutorials/280
ioio project using motor driver TB6612FNG- http://www.jaychakravarty.com/?p=146
ioio mint product (adafruit) - http://www.adafruit.com/products/885
ioio mint source (droidalyzer) - http://droidalyzer.com/tech.html