Outils pour utilisateurs

Outils du site


python:lumiere

MOD-LTR-501ALS

Code lumiere vers OSC

OSC sur local host 9001 /LUX/0 /LUX/1 et /LUX/DIST

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Lecture du module LTR50ALS capteur de lumière , UEXT Olimex 
# transmission des donnees en OSC

import sys,time,liblo
from smbus import SMBus

BUS_NUMBER = 2
PORT=9001 
ADRS=35 # 0x23 - LTR501ALS
# init pour le bus
b = SMBus(BUS_NUMBER) # 0 indicates /dev/i2c-0
data = {}

try:    
        print('init1')
        target = liblo.Address("localhost",9001)
except liblo.AddressError, err:
        print str(err)
        sys.exit()

# init du module
try:
        b.write_i2c_block_data(2, ADRS,[0x80,0x03]) # active mode 64k lux
        b.write_i2c_block_data(2, ADRS,[0x81,0x03]) # PS active mode, x1 GAIN
        b.write_i2c_block_data(2, ADRS,[0x82,0x6B]) # LED 60Hz, 50% duty, 50mA
        b.write_i2c_block_data(2, ADRS,[0x83,0x7F]) # 127 pulses
        b.write_i2c_block_data(2, ADRS,[0x84,0x02]) # PS 100ms measure rate
        b.write_i2c_block_data(2, ADRS,[0x85,0x04]) # ALS Integration 100ms, repeat rate 500ms
except IOError, err:
      print ("ioerr")
#exept:
#       print str(err init)
#       sys.exit()

while True:
        try:
                status = b.read_byte_data(ADRS,0x8c)
#               print("0x%02X",status & 0xFF)
                if(status & 0x04):
                        data_block = b.read_i2c_block_data(ADRS,0x88,4)
                        adc_0 = (data_block[1] * 2**8 + data_block[0])
                        adc_1 = (data_block[3] * 2**8 + data_block[2])
                        print "ADC_0= %d" % adc_0
                        print "ADC_1= %d" % adc_1
                        liblo.send(target,"/LUXADC_0", adc_0)
                        liblo.send(target,"/LUXADC_1", adc_1)
                elif(status & 0x01):
                        data_block = b.read_i2c_block_data(ADRS,0x8D,2)
                        PS = (data_block[1] * 2**8 + data_block[0])
                        distance = 10 -(10.0/2047)*PS
                        distance = format(distance,'.2f')
                        print "DIST= %d" % distance
                        liblo.send(target,"/DIST", distance)
                else:
#                       print("0x%02X",status & 0xFF)
                        time.sleep(0.02)
        except IOError, err:
                      print ("ioerr read")

        except KeyError:
                pass
        except KeyboardInterrupt:
                quit()
        except StopIteration:
                session = None
                print "GPSD has terminated"
        except:
#               print('echec')
                continue


#       time.sleep(1.12)

lumiere2osc.py script bash de lancement, sort si le capteur n'est pas present et l'initialise correctement ( a faire dans en python ;-) )

#!/bin/bash
MOD-LTR501ALS --i2c=/dev/i2c-1 -a 0x23  --als-mode=ACTIVE --ps-mode=ACTIVE
RET=$?
if [ "$RET" == 0 ]
then
        lumiere2OSC.py
fi
python/lumiere.txt · Dernière modification: 2017/04/11 15:13 (modification externe)