Tuesday, January 17, 2017

Toggling OTG Function in a USB Cable


The new control system for FTC (new as of 2015) uses Android phones to control the robot.  One android phone hooks up to joysticks, reads those joystick values, and send them via a wifi-direct connection to a second phone.  The second phone attaches to custom hardware from Modern Robotics which controls a variety of motors and servos, or reads sensor values.  See Figure 1.
Figure 1
In the configuration shown in Figure 1, both phones act as HOSTS... they must provide power and dictate communications to passive electronic devices (joysticks) or active electronics programmed to be USB slaves (Modern Robotics devices).  A phone was not originally intended to be a HOST, rather it is by default a slave to a computer (the master) to which is may be connected to transfer files or charge.  Similarly, the phone was not originally designed to power other devices... the USB jack on a phone was designed to accept power so that the phone's battery could be charged.

A protocol call On-The-Go (OTG) was developed to allow phones to act as HOSTS under certain circumstances.  When a special cable is plugged into the phone, it acts as a HOST.  When a normal USB cable is plugged into the phone, it acts as a client and can be charged and receive information from a computer (eg be programmed).  See figures 2 and 3.  These two modes of operation are currently mutually exclusive (although there is talk of a new OTG standard which will allow the phone to charge even when in OTG mode... this standard currently only works on certain Android phones).
Figure 2: Configuration for CHARGING and
 PROGRAMMING.
Figure 3: Configuration for Running and
Controlling the Robot











In FTC, our students spend a lot of time toggling the phone between master and slave.  Whenever they program the phone from Android Studio or need to charge the phone, they use a regular USB cable so the phone is the slave.  Whenever they want to test the robot, the phone is the master and an OTG cable is required.  Each toggle requires the cable to be swapped out.  It became clear to me that this was an issue about halfway through the 2015-16 season as students started reporting that the micro USB connectors on the phones (ZTE speeds) were breaking.  We moved to better phones (Moto G2 and G3), but even with those there have been failures.  While the logical fix to the problem is to coach kids to be careful and gentle when swapping out cords, in practice this is a futile endeavor.
The difference between a standard USB cable for an Android phone and an OTG cable for an Android phone is that the fifth pin (called the "ID" pin) is left open for a standard cable, and grounded for an OTG cable (Figure 4).  Hence a logical "fix" to the two cable problem seemed to me to make a cable with a switch that shorted the ID pin to ground in one position, and leaves it unconnected in the other (Figure 5).
Figure 4
Figure 5


I purchased USB DIY connector shells from Adafruit (figures 6, 7).  This is the link for the standard size, and this is the link for the micro shell.  I located pinouts for each of the shells (figure 8) and soldered solid core wire to each connector.  Soldering to the micro USB connectors is tough... the size of the solder pad and the spacing between them are both small.  It takes a steady hand and good hand-eye coordination, as well as a lighted magnifying glass.
Figure 6

Figure 7
I tested the concept by wiring the two USB connectors together on a breadboard.  I could "toggle" the state of the ID pin by putting the wire for that pin into the breadboard line for GND, or leaving it out.  The test proved successful, so I soldered all the wires together using  a switch as shown in Figure 5.  Figure 9 shows one of two finished cables that I made. 
Figure 8
Testing showed that the cable worked.  I needed a female to female connector and then a male to mini connector to test the connection to the Modern Robotics hardware.  I used the female to female connector along with a male to male cord to connect to the computer, and to test charging the phones.  All of the functions worked!

Figure 9

I attempted to replicate the procedure and make a second cable and had much more trouble.  The soldering was messed up on the micro connector once and I had to start over.  The breadboarding test showed erratic behavior.  I purchased a micro USB female breakout (Figure 10) to make sure that my soldering was done well.  When I couldn't find a problem there, I just soldered the wires directly to the standard USB connector and that seemed to fix the problem.
Figure 10

In all, this was a successful experiment to show a possible method to fix the problem of having to swap out cables.  However, I was not convinced on the durability of the custom cables that I made, and they are not easy to remake.  Hence, these cables are used on our school's soccer bots (Figures 11 and 12), but not on competition robots.  The CAD files for Moto G3 phones for use with these custom cables are found here.  I suspect that a new family of cables similar in principle to the one I made will be on the market soon, making my work obsolete.  Still, it was a fun challenge.
Figure 11

Figure 12