Remote servo control using Python, Arduino, OSC and TouchOSC App with Smartphone (iPhone iOS or Android)
This program is based on the code and instructions from SILVINO J. A. PRESA
<-THANK YOU! and adapted by me for remote control with the touchOSC app.
I am not an experience Python programmer, so I wrote a mickey-mouse code to control a servo with my iPhone.
There are for sure much clever way’s to do this, so feedback and improvements are very welcome.
For instance I use an UDP server to get the controller path from the touchOSC app and then I start an OSC server to control it. There must be a smarter way to do this.
so here we go:
index:
code | program | device |
---|---|---|
OSC_servo.py | python IDE | computer |
Servo.touchOSC | touchOSC editor | computer => phone touchOSC |
Servo_Control.ino | Arduino IDE | computer => Arduino |
1) Set up Arduino Circuit
2) Computer: load
Servo.touchosc
layout inTouchOSC editor
3) synchronize layout with Phone
TouchOSC
app.4) load
Servo_Control.ino
sketch inArduino IDE
and compile to Arduino device5) load
OSC_Control.py
in python IDE, and run it.6) Now you can control the servo with your Phone
on Mac or PC:
Run the program with Python 2.7, otherwise
Vpython
will not work.
On the Mac use the FrameWork version, otherwise theVpython graphic display
will not work:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
OSC: Unix terminal: $ pip install pyosc
or download:
if you have trouble installing it, you can also put the OSC.py
that is in the downloaded package, in the same folder you have OSC_Servo.py
Vpython:$ pip install vpython
$ pip install pyserial
$ pip install numpy
Double click the Servo.touchosc
file, and the TouchOSC editor
should open or open it from inside the editor.
Click the Sync
button, in the upper right of the window => touchOSC sync
OSC:
and fill in the IP address
, variable in the OSC_Control.py
=>serverAdr = "192.168.0.104"
<=IP address
!!! Port(outgoing)
is set to 8000
, variable in the OSC_Control.py
=>serverPort = 8000
Port(incoming)
is set to 9000
, variable in the OSC_Control.py
=>clientPort = 9000
Local IP address
is variable in OSC_Control.py
=>clientAdr = "192.168.0.103"
<=IP address
!!! Bellow the settings on my iPhone:
Return to <TouchOSC
upper left corner and choose the item under LAYOUT
, choose Add
Choose the host from the list.
Now you can choose the Servo
layout.
see also touchOSC wifi transfer
In the Arduino IDE on your computer: open the Servo_Control.ino
file and send it to the Arduino. Be sure that the right type Arduino and serial Port is selected => Menubar -> Tools -> Board: / Port:
Now you see also the name of the serialPort that you need to set in OSC_Control.py
=> variable: serialPort = "/dev/tty.wchusbserialfa130"
. On the Mac it is similar like this, on WIN is some thing like COMn
see also: serial port name
First compile your sketch, do not open the serial monitor, and then run OSC_Control.py
Other wise you get the message:
avrdude: ser_open(): can’t open device “/dev/cu.wchusbserialfa130”: Resource busy
to find your IP address: Mac or Win
!!!CHANGE THESE VARIABLEN according to your enviroment!!!
serialPort = '/dev/tty.wchusbserialfa130'
serverAdr = "192.168.0.104"
serverPort = 8000
clientAdr = "192.168.0.102"
clientPort = 9000
WARNING: if you not use a fix IP address, but get if from a DHCP server, the IP address can change, special on the Phone, that is re-conecting to your network if you return from an other place.
error message is e.g:
OSCServer: NoCallbackError on request from 192.168.0.103 No callback registered to handle OSC-address ‘/1/push2’
you see: if have my Phone IP defined as 192.168.0.102
but it is now: 192.168.0.103
Check again your touchOSC app to see if it is still the same.
it take a few moments to start, on my compter 20 sec., also its a bit slow to receive the fist messages. Keep turning the rotary till it gets it. Output looks like this:
conroller => output angle
bert@temminck.net, April 2017, Anápolis-GO, BRASIL