Support Forums
Getting python to interact with the real world(robotics/physical computing) - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Python Programming Language (https://www.supportforums.net/forumdisplay.php?fid=32)
+---- Thread: Getting python to interact with the real world(robotics/physical computing) (/showthread.php?tid=1952)



Getting python to interact with the real world(robotics/physical computing) - bsdpunk - 10-21-2009

As always this is brought to you by my blog http://bsdpunk.blogspot.com

[yt]http://www.youtube.com/watch?v=Hp30GKStna4[/yt]

For hardware you will need an arduino:
http://www.arduino.cc/

Arduino's are fairly cheap for getting into robotics/physical computing, you could spend as little as 20 dollars.

You need pygame and the python firmata installed. If you are on a yum based system you can install pygame by doing a:
yum -y install pygame

To install python firmata you will have to download it, untar it. Navigate to to directory in the terminal and then run this as root(or with the sudo command first):
python setup.py install
or
sudo python setup.py install

If you can't run them as root run each one with sudo in front of it:
PHP Code:
yum -y install git
git 
clone http://github.com/lupeke/python-firmata.git
cd python-firmata
python setup
.py install 
or
PHP Code:
sudo yum -y install git
sudo git 
clone http://github.com/lupeke/python-firmata.git
cd python-firmata
sudo python setup
.py install 


It is located here:
http://github.com/lupeke/python-firmata



Your biggest challenge, may be getting everything installed, from there it should be pretty easy.


This is how to get started, with python and your arduino, bringing up the beginning of a GUI. It's just a taste of what you can do and I hope to go further on the subject. You need pygame, and the python firmata installed(explained here), and your arduino must have the firmata software on it as explained here. Here is the code I stole/wrote for getting the arduino to blink when you click within pygame. This outline should get you started with fun stuff:


#! /usr/bin/env python

import pygame
from firmata import *

proArduino = Arduino('/dev/ttyUSB0')
proArduino.pin_mode(13, firmata.OUTPUT)
proArduino.delay(2)

LEFT = 1
x = y = 0
screen = pygame.display.set_mode((640, 480))
running = 1

def blinky(pin_n):
proArduino.digital_write(pin_n, firmata.LOW)
proArduino.delay(2)
proArduino.digital_write(pin_n, firmata.HIGH)
proArduino.delay(2)


while running:
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = 0
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
blinky(13)

screen.fill((100,0,0))
pygame.display.flip()




Right now I am experimenting with a lot of diffrent things(forking etc.), to see how the arduino and pygame can best interoperate.


RE: Getting python to interact with the real world(robotics/physical computing) - jolttz - 10-21-2009

Sweet, I can't wait till I get my arduino board Tongue


RE: Getting python to interact with the real world(robotics/physical computing) - bsdpunk - 10-22-2009

There fun to work with for sure.


RE: Getting python to interact with the real world(robotics/physical computing) - Nyx- - 10-22-2009

Thanks for making this bsdpunk ^__^