Tuesday, December 7, 2010

Infra-red

Now picking up the IR data - the hardware seems to pick the four brightest spots (above a brightness threshold, so it may identify less than four) and report their position.  Buttons B and A turn the reporting of IR data on and off, respectively.  I'm also showing the difference in the accelerometer readings, rather than the absolute readings.  This is interesting - the values are integers, and the difference between being at rest and being shaken around quite violently is about 40-50.  Not fantastic sensitivity, if you're hoping to use it for positioning - I think it may be better to rig up some sort of infra-red "landing beacon" and navigate that way.

#!/usr/bin/python

import cwiid
from time import sleep

print "Press 1+2 on the Wiimote now"
wiimote = cwiid.Wiimote()

# Rumble to indicate a connection
wiimote.rumble = 1
print "Connection established"
sleep(0.2)
wiimote.rumble = 0

wiimote.enable(cwiid.FLAG_MESG_IFC)
wiimote.rpt_mode = cwiid.RPT_ACC | cwiid.RPT_BTN | cwiid.RPT_IR

loop = True
show_acc = False
show_infrared = False
last_acc = (0, 0, 0)
delta_acc = [0, 0, 0]
led_status = 0

while (loop):
    sleep(0.01)
    messages = wiimote.get_mesg()
    for mesg in messages:
        # Accelerometer:
        if mesg[0] == cwiid.MESG_ACC:
            if show_acc:
                acc = mesg[1]
                if acc != last_acc:
                    for i in range(3):
                        delta_acc[i] = acc[i] - last_acc[i]
                    print "Acc: {0[0]:5} {0[1]:5} {0[2]:5}".format(delta_acc)
                    last_acc = acc
        # Button:
        elif mesg[0] == cwiid.MESG_BTN:
            if mesg[1] & cwiid.BTN_HOME:
                print "Ending Program"
                loop = False
            if mesg[1] & cwiid.BTN_PLUS:
                show_acc = True
            if mesg[1] & cwiid.BTN_MINUS:
                print "Ending accelerometer display"
                show_acc = False
            if mesg[1] & cwiid.BTN_1:
                wiimote.rumble = 1
            if mesg[1] & cwiid.BTN_2:
                wiimote.rumble = 0
            if mesg[1] & cwiid.BTN_UP:
                led_status = led_status ^ 0x01 
                wiimote.led = led_status
            if mesg[1] & cwiid.BTN_DOWN:
                led_status = led_status ^ 0x02 
                wiimote.led = led_status
            if mesg[1] & cwiid.BTN_LEFT:
                led_status = led_status ^ 0x04 
                wiimote.led = led_status
            if mesg[1] & cwiid.BTN_RIGHT:
                led_status = led_status ^ 0x08 
                wiimote.led = led_status
            if mesg[1] & cwiid.BTN_A:
                print "Ending IR display"
                show_infrared = False
            if mesg[1] & cwiid.BTN_B:
                show_infrared = True
        elif mesg[0] == cwiid.MESG_IR:
            if show_infrared:
                sources = mesg[1]
                found = False
                output = "IR:  "
                for spot in sources:
                    if spot:
                        output = output + \
                            "S {0} P {1:12}".format(spot["size"], spot["pos"])
                        found = True
                if found:
                    print output
                else:
                    print "No IR data"
        else:
            print mesg

No comments:

Post a Comment