xyz2nxt
 

# xyz2NXT.py adopted from xyz2bluetooth.py by bjepson
#
# Uses the axyz library to read sensor values and send commands to LEGO NXT
#
# NXT runs remotec.rbt (from http://nxt-symbian.sourceforge.net/) and connected to N95 via Bluetooth
#
# Mod. 8 Apr 2008  Fixed indentation error
 
import socket
import e32
import axyz

def bt_connect():
global sock
sock =socket.socket(socket.AF_BT, socket.SOCK_STREAM)
address,services=socket.bt_discover()
target=(address,services.values()[0])
sock.connect(target)
print "Connected"

def bt_sendposition():
global x
global y
global z
global sock

# beep = chr(06) + chr(00) + chr(128) + chr(03) + chr(244) + chr(01) + chr(244) + chr(01)
fwd = chr(8) + chr(0) + chr(128) + chr(9) + chr(0)+ chr(4) + "fwd" + chr(0)
rwd = chr(8) + chr(0) + chr(128) + chr(9) + chr(0) + chr(4) + "rwd" + chr(0)
left = chr(9) + chr(0) + chr(128) + chr(9) + chr(0) + chr(5) + "left" + chr(0)
right = chr(10) + chr(0) + chr(128) + chr(9) + chr(0) + chr(6) + "right" + chr(0)
stop = chr(9) + chr(0) + chr(128) + chr(9) + chr(0) + chr(5) + "stop" + chr(0)

if z < 20 and z > -20:
if y < -20:
sock.send(left)
elif y > 20:
sock.send(right)
if y < 10 and y > -10:
if z < -20:
sock.send(rwd)
elif z > 20:
sock.send(fwd)
if y < 10 and y > -10:
if z < 20 and z > -20:
sock.send(stop)


def record_vars(x1,y1,z1):
global x
global y
global z
x = x1
y = y1
z = z1

axyz.connect(record_vars)
bt_connect()
while 1:
bt_sendposition()
e32.ao_sleep(.1)