rPi Hardware Servo Control

Today’s lunch consisted of cold chicken and some fun with servos. Servos, Python, Linux, IO, chicken…nice little Friday.

The goal of this abbreviated session was to figure out how PWM works in the trenches. However sold I am on the rPi in general, I have an uneasy feeling that the CPU is going to be a pain in my ass with respect to stuff I’ve taken for granted on simpler hardware. Whereas the Arduino platform and my beloved IOIO were effectively real-time operating systems, the rPi’s IO pins’ calls are no different from any other PID that is spawned. The amazing Linux-y stuff does in fact have a downside…i.e. no guaranteed execution times.

Anyhow, this is what I am currently poking at. Today’s session was a case-in-point as a matter of fact.

schwervo

schwervo

The setup is as simple as it gets…what I call the ‘junior servo’ hooked up to 3.3V, a ground, and to the single PWM-capable pin (18) on the rPi. I have previously pushed Servo Jr. with 3.3V with the IOIO, so I had a level of confidence I wouldn’t tear down the box. On a bit of a side note, I did verify that ripping 5V off of the board and attempting to push a full sized hobby servo did in fact take the rPi to its knees. Be sure to power any servos externally for any real tests or prototyping.

I again chose to use Python to mash around. Two data points aren’t many, but will definitely allude to a trend you will likely continue to see here: I’m more comfortable with Python than C, and that is our chosen rPi workhorse at Swan Tron Global HQ. Sadly, as opposed to playing around with LEDs from my last rPi post, the GPIO library doesn’t have any PWM support at this point. The workaround is to call out the pin directly…this is pretty easily done with any of the main rPi distros. I am *fairly* certain I am still on Occidentalis v0.2 from Adafruit.

Here is another non-snippet-but-here’s-a-screenshot-worthy two minute py script I put together to push the servo:

bonus cap reflection

bonus cap reflection

As I feared, the hardware instance is begging for well-designed software in order to produce anything predictable. The following video was taken from a fresh boot (READ: after I wiped the thing out with a continuous drive servo) sans any networking. As bare-bones as you can get…the OS executing a Python script and writing to one pin…what you see is:

Executable 1) Software calling for servo to alternate between 0 and 180 degrees, with a delay between position changes

Executable 2) Software calling for servo to step between 0 and 180 degrees with slight delay between position changes

Executable 3) Software calling for servo to step through scenario #2, albeit with a negligible delay between position change calls

Take a look:

Result 1) With adequate time between writes, the servo itself sets the cadence, and smoothly

Result 2) 180 position writes manifests as a jumpy mess, even with ample pauses introduced

Result 3) …and boom goes the dynamite

With the hardware being handcuffed by the CPU, timing on the software level is the name of the game. Sort of fragile, but still wholly awesome.

The next step is going to be a full-out network setup and some real memory profiling. I am going to figure out how to access this thing via ssh and see what that leaves me to work with. The step after that is going to be driving multiple servos…provided software PWM is a possibility. We’ll see where that ends up.

The pipe-dream? Think Roomba with an onboard rPI. SSH access via WiFi on a device that can decouple itself from a charging dock. Basically, I am hung up on the notion of being able to ssh into a robot…I’m not sure it gets much cooler than that.

As always, stay tuned. As also always, feel free to contact me @ [ joe at swan tron dot com ] with any feedback or questions.

Python IO on rPi

My next ‘real’ project involves implementing a web interface to control some of the things around swan tron global HQ, the half of the garage I reserved for my lab. I’ve been a huge fan of the Raspberry Pi since the mission was announced. Linux and IO…you have my attention. Add in the $35 price tag and my penchant for destroying hardware…huge fan is correct.

Before I start planning my real project, I need to figure out how this differs from the microcontrollers I’ve used in the past to deal with similar implementations. I know the CPU is going to show itself, but not to what extent. Time to start poking around.

I decided to horse around and knock out a few birds with one stone…python library, digital out, and basic pin function. Oh, and LEDs. Everyone loves LEDs.

the 80s called, they want their printer cable back

cobbler and bread

Prior to this effort, I have mainly explored the rPi via x. I’ve spent a decent amount of time inspecting the file structure (Debian, no surprises) and the xwin UI. It is no speed demon, but it does the trick. I have the wireless adapter configured and set to auto-start, which was most of the battle. It basically performs as advertised, which on paper is very awesome for the price.

My general setup for this was very simple. I bought a cobbler set from Adafruit, which acts simply to pull the IO pins out to a breadboard. Two LEDs, two resisters. A few jumpers. Very simple.

I booted the device, and used aptitude to update and snag the rPi GPIO library.
sudo apt-get update
sudo apt-get install python-rpi.gpio
That does the heavy lifting with the python implementation…sample code is available on the main python site. The update was I used vim to create a dirty python script…unworthy of a repository, but worth a pic:

fugly code

no comments, no worries

The digital outs worked as anticipated. Timing was a bit odd…I ended up playing around a bit with some pauses in order to tune the blinks as I wanted. It looks like coordinating pin outs might be tricky for more complex operations, but at least the library and hardware is up and running.

Don’t take my word for it:

The next steps will be interesting. Figuring out how to ssh into the box in order to run a script of this magnitude will be next, followed by profiling the voltage on the pinout. It should be able to throw my 5v relay with a little tweaking…if not I’ll figure out PWM and tackle the switching mechanically. Fun stuff to come…happy to be playing with the rPi finally. Stay tuned.

Spoofing User Agent Types from the Command line

I’m back on the command line…look out…

ubuntu terminal spoofing user agents

green on black...my favorite

So, I am still poking away at various ways to interface between the bash and various web components. This stuff is still shaking out of my Twitter to CLI to LCD project. I have been using some of the usual suspects (wget, python url libraries, etc) in an effort to find one that is best suited to sniffing and parsing html info. I have found that writing to serial is the most fail-proof means of mashing around data quickly, so that is my inspiration.

Anyhow, I was horsing around with curl on the command line. It turns out, that spoofing user agents is pretty simple to implement.

Take this two-line one liner for instance:

swantron@Dell15:~$ x=10
swantron@Dell15:~$ for (( y=1; y<=x; y++)) ; do curl --user-agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" swantron.com ; done

In a nutshell, this is acting to snag this site ten times, with credentials indicating the machine is running Windows 2000 and MS Internet Explorer 5.1. The success is indicated by my server statistics:

Before:

Windows 2000 IE 5.1

Windows 2000 IE 5.1

After:

IE 5.1 on Windows 2000

IE 5.1 on Windows 2000

Not too bad. I am considering using this as a testing tool for my site. Messing around with PHP and CSS…I can put together a quick regression using some legacy operating systems and browsers to make certain that I can still snag my content. Granted, the example setup may seem like gross overkill, but as those stats indicate, I do see quite a few requests from ancient machines. Makes you wonder..

Anyhow, this layout is in need of some serious testing. If anyone is interested in a copy, drop me a line. Cheers.

Evolution of a Project

I have two speeds when it comes to projects: percolate and bazooka. Whereas my swantron comment / to / LCD project was bazooka, this Twitter deal has been percolate. Haven’t been able to jump in completely, rather, I have been poking this one with a stick.

Still poking…

arduino controlling blue lcd

dual terminals and a terminal

Making progress…snagging my Twitter page via Python, and dumping it to LCD:

body and html closing tags on my lcd

+1 closing tags

I just need to hammer out some parsing, and I will be good to go. I really need to figure out some sweet regex and drop this whole ‘import (some junk)’ stuff. Who knows…at this rate, I just may.

Twitter CLI Teaser

I sort of forget what my original project entailed…something about tweeting via the command line, for Arduino purposes. You can pretty much just toss a bunch of words in a hat, and add ‘Arduino’ and ‘Command Line’ to them, and there you have one of my typical projects.

Anyhow, here we find ourselves. I am missing some dependencies with OAuth, but have managed to get twitter working from an interactive python shell:

cli twitter

command line-y

This section is as much for my reference as it is for public knowledge…I snagged the commands that I ran to snag my comments. It first prints swantr0n (my dev account) and then my primary Twitter feed, @swantron

swantron@mini9:~$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import twitter
>>> api = twitter.Api()
>>> feedSwantron = api.GetUserTimeline(‘swantron’)
>>> feedSwantr0n = api.GetUserTimeline(‘swantr0n’)
>>> print [ x.text for x in feedSwantr0n]
['development account for @swantron | tune into http://t.co/SpDFBkK for projects', 'hello world']
>>> print [ x.text for x in feedSwantron]
['@vftb ...you better believe it', 'Twitter Project Teaser : http://t.co/7ZrOLUn | one step closer to tweeting from the #CLI ... might take a run at make it tweet', 'Might need to implement some sleeves into my wardrobe... @adafruit just flipped the script with open source cuff links', 'unsafe at any speed... updating #ubuntu on the road http://t.co/Q8gfc56', 'Upgrading Mini 9 to #Ubuntu Natty http://t.co/7yT9w81 | posting to kill time... review here ( http://t.co/RM675lX ) lunch-aft on the morrow', 'http://swantron.com/basic-mower-cord-repair/ #DIY lawn mower repair from a #math major. spoiler: it works', "#arduino no you didn't http://swantron.com/html-to-python-to-arduino-to-lcd/ #LCD project, stepped up a bit", 'working on a python script to dump info from web to LCD http://swantron.com/python-teaser/ #arduino', 'http://youtu.be/MpomhE6dgww video of #boc unboxing (for the win)', '#woot bag-o-crap unboxing @ http://swantron.com/woot-boc-unboxing/ not too shabby', 'http://swantron.com/compiz-error-in-ubuntu-natty/ still finding some compiz issues in #ubuntu 11.04', '#Arduino project @ http://swantron.com/command-line-lcd-arduino-interface/ | driving LCD screen via command line', '@RobotGrrl... what version are you running? I had no problems with IOIO / Droid 2 (froyo 2.2)', 'Easy LCD #Arduino Display | http://swantron.com/easy-lcd-arduino-display/ | feels good to have a warm soldering iron and functional hardware']
>>>

I will keep plugging away until I get this thing hammered out. If not, I can print this suff to my LCD for a real-time Twitter display. Could be cool either way.

Twitter Project Teaser

A while back, I tossed a little concept video out to the nets regarding CLI interface with my site’s comments and an LCD screen. I figured that I could do a similar thing with my Twitter account…spurred largely by Adafruit’s Make it Tweet challenge. Well, it turns out that Twitter has a few hurdles to jump, in regards to posting tweets to an account.

My failed usage from the command line was utilising curl

> curl -u swantron:pass -d status=”command line test” http://twitter.com/statuses/update.xml

I was getting an error message, that basic authentication was not supported. For the loss.

It turns out that you need a few keys in order to authenticate, which requires registering an app with Twitter… like this

twitter dev account

dev-y

There we go…keys in hand. Now, I need to figure out how to sew this together using curl or wget. If all else fails, I am pretty sure there is a py library that I can snag. Stay tuned…this one could be fun.

HTML to Python to Arduino to LCD

Last week found me standing tall upon my shell script soapbox, shouting command line praises to all who would listen.

Thou ought direct thine output aftways, to-wards thine USB port of thee. And that is well and righteous.

Well, that still is the case. My latest project has made it glaringly obvious that sometimes a little Python script will render a whole bunch of shell scripting moot. Namely, parsing HTML. Let’s see a picture…

outdoor hacking

bad lab mobile

Lunch hour project: parse the comments from swantron.com; feed said comments to an LCD screen.

I was horsing around with wget from a CLI a few days ago. I found myself trying to smash through the resultant file via pure regular expressions…which is incredibly clumsy. Well, as luck would have it, my go-to after my main go-to is Python, and this type of thing has been issue enough to warrant a library. BeautifulSoup. It acts to parse the HTML info into items, that can be smashed around as I see(med) fit.

My setup was simple: py script to snag my comments and write serial, Arduino sketch to drive a LCD and read/write serial. And a source of shade. And a WiFi signal to snag.

mobile mobile

bad lab mobile-mobile

Check, check, check, etc. Video time:

Pretty slick…hit the fold for the code, as promised, and a summary.
Continue reading

Python Teaser

After a deal of success with the command line interface-to-Arduino project, I am in the middle of stepping it up a notch. I ended up on a few blogs due to that last effort, which indicates that I am doing something worthy of note. Hats off to me, perhaps. Well, my tool belt is what it is…if I can’t do it via shell scripting, I reach next for Python. In an attempt to parse stuff that I have been ‘getting’ via wget, I found myself trying to do some clunky regex from the command line. Guess what…I can do that without as much heartache via Python via BeautifulSoup…

beautiful soup

so...beautiful


Thus far, I have python doing the website getting and Beautiful Soup doing the parsing of my site…basically just grabbing some info at this point that I can modify. I have my LCD driven via my trusty Arduino, and that is that. Instead of redirecting standard out to serial, I am writing to serial via python. Easy breezy.

I have some sloppy sloppy code at this point, and need to add some hardware and stuffs to my prototype. Stay tuned…this could be a good one.