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 |
on Mac or PC:
- Python IDE: I prefer PyCharm (mac/win)
- Arduino IDE
- touchOSC editor => scroll to the end of the page: Downloads -> choose your OS.
- touchOSC app: iOS or android price: $5
Run the program with Python 2.7, otherwise
Vpythonwill not work.
On the Mac use the FrameWork version, otherwise theVpython graphic displaywill not work:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
-
OSC: Unix terminal:
$ pip install pyoscor download: if you have trouble installing it, you can also put theOSC.pythat is in the downloaded package, in the same folder you haveOSC_Servo.py -
Vpython:
$ pip install vpython -
serial:
$ pip install pyserial -
numpy:
$ pip install numpy -
socket: I guess it is a standard Python package, but had a lot of trouble getting it working. Be sure you have no other socket.py some where in your directories.
- Arduino Board (Uno) available from many suppliers. $15, My UNO comes from China (Oops!)
- Servo Motor: I use TowerPro SG90, do a google search and you get a lot of hits. Around $5
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
- click the white spot on upper right of the window, choose the upper item
OSC:and fill in theIP address, variable in theOSC_Control.py=>serverAdr = "192.168.0.104"<=
YOU HAVE TO CHANGE this to your computerIP address!!!Port(outgoing)is set to8000, variable in theOSC_Control.py=>serverPort = 8000Port(incoming)is set to9000, variable in theOSC_Control.py=>clientPort = 9000Local IP addressis variable inOSC_Control.py=>clientAdr = "192.168.0.103"<=
YOU HAVE TO CHANGE this to your PhoneIP 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.inofile 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:64550: 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:
[email protected], April 2017, Anápolis-GO, BRASIL