I’m going to be posting a series of tutorials on how to build / program a drone. I’ve been working on a few quadrocopters and a RC Car drone. So I’m going to take you through the steps of how I got it up and running.
Lesson 1: pyOSC + TouchOSC
To be able to get values from touchOSC (or any OSC software) from a cell phone to your computer. It’s simple and makes for interesting interactive programs.
1. An Android or iOS cellphone with touchOSC or comparable software.
Notes: I really like touchOSC because I just know it works, but if you can’t get past the $5 price tag, try out other free ones like Control for (Android/iOS).
2. Lastest release of python 2.x find it for your operating system here:
I tested this small program on Mac OSX with no problems, then moved it over to the Raspberry Pi, which of course comes with Python preinstalled.
3. The pyOSC library, found here: https://pypi.python.org/pypi/pyOSC
4. If you don’t want to make the touchOSC template, then download mine that works specifically for this tutorial here: https://db.tt/bnxa0JkM
Once you’ve got everything set up, we’re ready to go.
First, make a folder for your OSC test program. Put it wherever you’d like.
Next, copy the file “OSC.py” from your pyOSC download location to this folder.
Finally, open up whichever application you use to write code (I use TextWrangler for Python and Xcode for other languages) and make a new file, call it something like “oscTest.py” and paste the following code into it:
You’ll need to modify a few lines to get it to work for your computer. First off, we need to modify lines 13 & 14. In 13, put the IP address of your computer.
On windows this is found in: Control Panel < Network and Internet < Network Connections < (Your Connected Network) < View Status < Details
Yeah I know, windows makes it hard.
On a Mac simply go to: Apple < System Preferences < Network
On line 14 you need to put the IP of your phone. If using touchOSC it automatically listed on the settings page as “Local IP address”, if not find it under: Settings < Wi-FI
Last Step! Make sure that touchOSC has all the same settings. Host is the IP address of your computer (line 13) and the ports should be set to what it says in lines 13 & 14 respectively. (For this example it’s 7000 and 9000).
Now just run the application!
If you installed python as listed above, then it comes with an application named Python Launcher. While it isn’t necessary to use it, I recommend it to start out with. So simply right mouse click on our code file “oscTest.py” and open it with Python Launcher. This should open up the command line and print “Starting OSCServer. Use ctrl-C to quit.” Then press buttons on your phone! The XY fader prints decimal values from 0 to 1 of where the cursor is, so you’l see something like: “X Value Is: .67898” and “Y Value Is: .34567”. When you get bored, hit “ctrl + c” and the application will close in the command line.
So this code may look intimidating, but it’s a nice example of python code, including a few functions that we call and some loops.
First off, in lines 27-29 we create a client and a server, right there on the computer.
When we press a button on the phone it is literally sending over the network the line “basic/joystick/<data>” So we want to be listening for just that message!
This is done in lines 61-67. We simply add the functions that we want to listen to. If you want to build your own custom interface in touchOSC all you need to do is create the object and name it, then add another line similar to “s.addMsgHandler("/basic/joystick", moveJoystick_handler)” where the first element is replacement by what the object is called and the second is replaced by the name of the function you want to call.
So again, what happens is this: we press a button, it sends that network message over Wi-Fi. Then since we are listening to it, it calls the function we defined above in lines 50-59. All we are doing there is simple taking the array called “Stuff” and printing it’s individual elements.
Lastly line 74-84 simply create a loop that runs until we close it ourselves. Not too shabby!
Now your ready to make your own templates in touchOSC and get your code to do cool things.