Wishful Coding

Didn't you ever wish your
computer understood you?

Awesome Tower Defense Robot

I found this robot on Youtube, and guess what? It comes with building instructions and software.

I’m still working on my own building instructions, but now you have something to play with.

Happy new year!

Lego NXT With a Gamepad

I built this robot a while back, but I can’t stand the software that comes with the NXT, my own compiler isn’t ready yet, and any other remote controls I found didn’t work particularly well, if at all.

If you want it done right, you have to do it yourself – Bernd Paysan

So I threw together PyGame and nxt-python, and wrote my own little application to control the NXT. Notice that it looks very similar to this PyMouse code

I have no building plans for the robot, it’s the standard wheel base with my own pincer. I do have the code:

import pygame
from nxt import locator, motor
from time import sleep

# edit this to reflect your joystick axis and buttons
axis = {'x':0, 'y':1, 'x*':3, 'y*':5}

b = locator.find_one_brick()

left = motor.Motor(b, motor.PORT_B)
right = motor.Motor(b, motor.PORT_A)
action = motor.Motor(b, motor.PORT_C)

closed = False

def limit(nr):
    if nr > 50 or nr < -50:
        return min(127, max(-128, nr))
    else:
        return 0

def move(fwd=0, turn=0):
    lp = int((fwd - turn) * -100)
    rp = int((fwd + turn) * -100)
    left.run(limit(lp))
    right.run(limit(rp))

def pincer(button):
    global closed
    try:
        if button and not closed:
            closed = True
            action.turn(-40, 70, emulate=False)
        elif not button and closed:
            closed = False
            action.turn(30, 70, emulate=False, brake=False)
    except motor.BlockedException:
        print action.get_tacho()

pygame.init()
j = pygame.joystick.Joystick(0) # first joystick
j.init()
print 'Initialized Joystick : %s' % j.get_name()
try:
    while True:
        pygame.event.pump()
        sleep(0.1)
        
        # get_axis returns a value between -1 and 1
        move(j.get_axis(axis['y']), j.get_axis(axis['x']))
        pincer(j.get_button(0))
        
except KeyboardInterrupt:
    j.quit()

Twisted on Dotcloud

A while back, I wrote a Twisted email server that would sit between you and Twitter and let you reed and send tweets via email.

With the advent of free cloud hosting, I thought it’d be fun to put it online. I’ll share with you how it’s done, for those of you who are looking for a place to put your Twisted app.

The major problem with these easy hosts is that they assume a simple WSGI app or a background worker, but then the background worker is not accessible from the outside.

First I tried Heroku, then Dotcloud, and finally a couple of others, and just when I had given up, @solomonstre came in:

@pepijndevos dotcloud has beta support for arbitrary tcp/udp ports. Want to try? :)

After some fruitless tries, he shared this repo, which contains scaffolding for a web-accessible Python worker.

The core parts of this thing are dotcloud.yml, where you define a setup script, the ports you want, and the command to run your app.

worker:
  type: custom
  buildscript: builder
  ports:
    smpt: tcp
    pop3: tcp
  process: ~/run
  approot: twemail
  environment:
    PYTHON_VERSION: 2.7

builder contains a whole lot of pip/virtualenv code, while run contains something like twistd -ny yourapp.tac.