Interface and application programming
hello.reflect.45 to blender
Getting blender to read from the serial port in Debian 7.0 (Wheezy) wasn’t easy, but now it works really nice. Here is what I did:
Step 1. Download blender. I used blender 2.56a for Linux 64-bit (I will try, but I’m sure it works for 2.66 as well). I install and run it like this:
sudo mv ~/Downloads/blender-2.65a-linux-glibc211-x86_64.tar.bz2 /opt/
sudo tar -xf /opt/blender-2.65a-linux-glibc211-x86_64.tar.bz2
/opt/blender-2.65a-linux-glibc211-x86_64/blender
Step 2. I added an always sensor (in pulse mode)
to a cube that triggers the following python script:
sys.path.append('/usr/local/bin/blender-2.65a-linux-glibc211-x8664/2.65/python/lib/python3.3/site- packages/pyserial-2.6') sys.path.append('/usr/local/bin/blender-2.65a-linux-glibc211-x8664/2.65/python/lib/python3.3/site- packages/pyserial-2.6/serial') import bge import serial import GameLogic eps = 0.2 # filter time constant filter = 0.0 # filtered value nloop = 100.0 # number of loops accumulated amp = 25.0 # difference amplitude def reflect(): global filter, eps # # idle routine # byte2 = 0 byte3 = 0 byte4 = 0 GameLogic.SerialPort.flush() while 1: # # find framing # byte1 = byte2 byte2 = byte3 byte3 = byte4 byte4 = ord(GameLogic.SerialPort.read()) if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)): break onlow = ord(GameLogic.SerialPort.read()) onhigh = ord(GameLogic.SerialPort.read()) onvalue = (256*onhigh + onlow)/nloop print ("on", onvalue) offlow = ord(GameLogic.SerialPort.read()) offhigh = ord(GameLogic.SerialPort.read()) offvalue = (256*offhigh + offlow)/nloop print ("off", offvalue) filter = (1-eps)filter + epsamp*(on_value-off_value) print ("diff", filter) port = '/dev/ttyUSB0' pin = 0 try: GameLogic.SerialPort except: GameLogic.SerialPort = serial.Serial(port,9600) GameLogic.SerialPort.setDTR() reflect() # To get the controller thats running this python script: cont = bge.logic.getCurrentController() # bge.logic is automatically imported # To get the game object this controller is on: obj = cont.owner obj.worldPosition.z = filter/10It is basically Neil’s hello.reflect.45.py with some extras.
Step 3. Blender’s bundeled python doesn’t come with pyserial, so you have to install it manually. This thread helped me a lot for making this work. Big thank you to abhi! I downloaded pyserial 2.6 and copied it to
/opt/blender-2.66a-linux-glibc211-x86_64/2.66/python/lib/python3.3/site-packages/
I put the path to the pyserial module in the script
sys.path.append(‘/opt/blender-2.65a-linux-glibc211-x86_64/2.65/python/lib/python3.3/site-
packages/pyserial-2.6’)
Step 4. In the file
/opt/blender-2.65a-linux-glibc211-x86_64/2.65/python/lib/python3.3/site-packages/serial/serialposix.py
line 62,
I changed octal to hexadecimal notation
baudrate_constants = dict([ (0, 0x0000), (50, 0x0001), (75, 0x0002), (110, 0x0003),\
(134, 0x0004), (150, 0x0005), (200, 0x0006), (300, 0x0007),\
(600, 0x0008), (1200, 0x0009), (1800, 0x000A), (2400, 0x000B),\
(4800, 0x000C), (9600, 0x000D), (19200, 0x000E), (38400, 0x000F),\
('BOTHER', 0x1000), (57600, 0x1001), (115200, 0x1002), (230400, 0x1003),\
(460800, 0x1004), (500000, 0x1005), (576000, 0x1006), (921600, 0x1007),\
(1000000, 0x1008), (1152000, 0x1009), (1500000, 0x100A), (2000000, 0x100B),\
(2500000, 0x100C), (3000000, 0x100D), (3500000, 0x100E), (4000000, 0x100F) ])
Step 5. Change all the “except Exception, msg:” to “except Exception as msg:” and similar instructions.
Step 6. In the file
/opt/blender-2.65a-linux-glibc211-x86_64/2.65/python/lib/python3.3/site-packages/serial/serialutil.py”
line 292,
comment the if as shown below
#if isinstance(port, basestring):
self.portstr = port
#else:
#self.portstr = self.makeDeviceName(port)
Step 7. Change all the ” except TypeError, err:” to ” except TypeError as err:” and similar instructions.
This will work, but for not opening the serial port on every frame of bge cycle I used a separate init script called only once at the beginning.
hello.reflect.45 to puredata
I used the “SimpleExample.pd” from the tutorial Pure Data to Arduino Over Serial
Since it receives the 1234’s and the separate byte values all together it just makes some funny noises. I will work on separating it all nicely.