Thursday, January 16, 2014

Controlling Arduino with iPhone using OSC

The iPhone's touch screen allows it to be used as a handy remote control user interface for all sorts of things.  For this project, I used my iPhone to control different electrical components.  The first goal was to use a button on the iPhone to turn an LED on and off and the second goal was to use a fader (potentiometer) control on the iPhone to control the speed of an electric motor (fan).

I used a technology called Open Sound Control (OSC) which is used in the music business to control various audio components (using MIDI).  Without getting into the benefits of OSC in too much detail, suffice it to say that this allows us to very quickly create different custom user interfaces for the purpose of controlling all sorts of things.  Think of the big sound boards with its sliders, buttons, toggles, faders, etc. that one finds in a recording studio - the ones that the sound engineers use.  This is what OSC gives you on your smart phone or tablet (which makes it portable).  It is used often by DJs at clubs that want to use their own, customized, familiar controls to control the music they are playing.  Connectivity is handled through WiFi.

TouchOSC


Of the various OSC apps available for iOS, I decided to go with TouchOSC based on the various reviews that I read.  In order to get OSC to work, a host machine must be set up to receive the OSC commands.  This is achieved by connecting both the iPhone and the host machine to the same WiFi network (yes, ad-hoc mode will work if you don't have a wireless router set up).  The host machine runs an event handler that catches the OSC commands and handles it accordingly by translating it into an instruction for the Arduino.  For the purpose of this project, the Arduino is connected directly to the host computer via USB cable and the serial port is used by the host computer to command the Arduino.  For a completely wireless setup, the Arduino can be linked to the host computer by WiFi, bluetooth or radio link depending on the application.  

There is a a downloadable layout editor provided for TouchOSC which allows one to quickly and easily produce a custom UI on your desktop computer and to sync with TouchOSC in order to upload the layout to the iPhone.

Host Computer


The Processing language was used for the purpose of coding the event handler for the host computer.  Processing is commonly used for interacting with Arduino as the two play very well together.  In fact, the Arduino IDE was derived from the Processing IDE which is why they look so similar.  While the Arduino language has its origins in C/C++, the Processing language comes from JAVA.  This means that the same sketch (the term used for a Processing and Arduino program) can be used on MacOS, Windows, Unix/Linux and can readily be compiled for use on Android OS.  Additionally, Processing is supported by a huge open source community and, consequently, there are libraries and sketches already available for many different things - including OSC handling.

Controlling an LED

 

Circuitry

For the purpose on this demonstration,  a simple circuit was set up containing an LED plus a resistor connected to the cathode (-ve terminal) of the LED.  The other end of the resistor was connected to ground on the Arduino while the LED's anode (+ve terminal) was connected to digital pin 9 on the Arduino.  By writing a HIGH or LOW value to pin 9, the LED can be turned on and off respectively.

Arduino Sketch

The Arduino sketch is very simple and pretty much just contains an event handler which monitors the serial port for any data (in this case only 1 byte of data is being communicated each time).  In this case, the data being sent is simply a "1" or a "0" from which the Arduino determines whether to send a HIGH or LOW voltage to pin 9.  A copy of the sketch can be download from here.

 

Processing Sketch

The Processing sketch is slightly more involved than the Arduino sketch (although only slightly).  Using Processing, the host computer must also run an event handler in order to receive commands from TouchOSC on the iPhone.  The handler catches the incoming message from TouchOSC, parses the message in order to determine which control sent the message and the corresponding value.  

TouchOSC

In this case, we just needed a simple on/off toggle switch.  A custom layout was created containing a toggle switch (which I named 'toggle1') and was set to send the values of '0' or '1' depending on the state of the toggle.  IMPORTANT:  Under Settings-->Connections-->OSC, the host name must be set to the IP address of the host computer.  The host computer should be configured with a static IP address to ensure that it won't somehow change unexpectedly.

So, in this case a typical message sent from TouchOSC will look something like "1/toggle1" followed by a value (in this case a 0 or 1).  By parsing this message, the processing code determines which control was changed (in this case it is only toggle1).  For a more general set up, the Processing sketch should be abstracted so that it can handle different types of controls.  In general (although not used in this case), the Processing sketch will send a different range of values to those that the Arduino is expecting so there is usually some mapping that is handled by Processing.  For example, you could have set a knob on TouchOSC to send the value of 0 to 180 (which indicates how many degrees the knob has been turned).  This range of 0 to 180 degrees might need to be mapped to a value of, say, 0 to 100 before sending it to the Arduino. 


Controlling an Electric Motor/Fan

 

Circuitry

I decided to build a test rig in order to test various motor/propeller combinations in order to determine the thrust output and the efficiency of different combinations while spinning at different RPMs.  The test rig is made from wood onto which an electric motor was mounted (NOTE:  it is a good idea to attach the propeller only AFTER one is sure that everything is working as expected so that nobody can get hurt).  I attached an ESC (30A) to the motor to which I attached a big 4S (14.8V) battery to power the motor and then attached the control wire's ground and signal wires to the Arduino's ground and digital pin 9 respectively.  Do NOT connect the power wire to the Arduino.

WARNING: These ESCs contain a battery elimination circuit (BEC) for the purpose of providing power from the flight battery to the receiver and thereby eliminating the need for a separate receiver battery.  Consequently, the red power line of the ESC's control wire is hot (it carries 5V at up to 3 amps).  It could be used to power the Arduino, but the Arduino is already getting its power from the host computer's USB port.  

IF YOU CONNECT YOUR ESC'S POWER LINE TO THE ARDUINO'S 5V PIN, YOU STAND A GOOD CHANCE OF BLOWING BOTH YOUR ARDUINO AND THE USB PORT ON YOUR COMPUTER.

 

Arduino Sketch

For the purpose of controlling the ESC, the Arduino's servo library was used.  A couple of things to bear in mind is that servos are controlled by sending a value of 0 to 255 along the control wire.  In order to achieve this, the signal wire on the servo must be connected to a pin on the Arduino that is capable of pulse-width modulation (PWM).  In the case of the Arduino nano that was used here, port 9 will work just fine.  Just to complicate matters a little, we are obviously not controlling a servo, but rather an ESC.  The difference is that, in the case of an ESC, sending a value of 0 to the ESC does NOT usually correspond to the zero-throttle setting of the ESC (it is actually a negative throttle setting).  

Why is this important.  Well, for one thing, when you first power up the ESC's they start up in safe mode in so that the motors do not suddenly run unexpectedly.  Upon powering up the ESC, it proceeds with a chime and then continues to beep to indicate that it is in safe mode.  In order to arm the ESC, the throttle must be reduced to zero at which time the beeping stops.  In order to find this value, the Arduino was powered up and then different values were sent via the serial port monitor to the ESC and it was discovered the a value of 50 armed the ESC - so a signal value of 50 corresponds to zero throttle (this is different for different ESCs) and 255 is full throttle.  Note that this relationship between signal input and motor speed is not a linear one.

 

Processing Sketch

The Processing sketch was modified slightly.  On the one hand, instead of the control being a toggle switch, it is now a potentiometer (called a fader).  This fader can be adjusted from 0 to 180 degrees (or whatever mapping one prefers).  In this case, the value received by the sketch must first be mapped to a range of values that the ESC expects to receive.  it should be noted that TouchOSC will send only integer values to the host computer (no fractional values are used).  If a higher resolution is desired for the fader knob, then the range of values from the fader could be set to be 0 to 1800.

The value of the fader (0 to 180) is then mapped to the range of output signals which is 50 to 255 (as mentioned above).

 

TouchOSC

Once again a custom UI was designed using layout editor.  In this case, it consisted of simply a fader knob which was set to output values in the range of 0 to 180.  Custom layout was uploaded to TouchOSC on the iPhone.







No comments:

Post a Comment