Hallo...
Yippie juhu, es läuft...
Bitte beachtet den Hinweis unten!
Ich habe dankenswerterweise von ft ein Beispiel bekommen.
Problem 1, es war in einer neueren Version von RoboPro Coding. Die von MS Store ist u.a. mit Blocky 14, die Onlineversion von RoboPro
https://dev.fischertechnik-cloud.com/de ... de/desktop hat Bolcky 15.
Die Unterschiede sind nicht so groß, aber ich habe es nicht 100% hinbekommen es umzuschreiben. Somit lief es nicht.
Problem 2, mein Gestensensor hat natürlich 10 Pins, der TXT 4.0 6 Pins. Man muss erst die jeweilige Belegung haben oder einen Adapter (den ich natürlich nicht hatte).
Das Beispiel bezieht sich auf den 20.Teil vom I2C Workshop, der Zeitschrift Raspberry Pi Geek:
https://www.raspberry-pi-geek.de/ausgab ... apds-9960/
Hier wird das APDS-9960 Sensor-Modul über I2C angesprochen. Das ist in dem ft-Gestensensor drinn.
Im RoboPro Coding - Programm ist ein Hauptprogramm, wo der Sensor Initialisiert wird und dann in einer Dauerschleife die Übertragenen Werte jede Sekunde auf dem Bildschirm ausgegeben werden. Es werden die Werte der Entfernung (Proximity) und der Farbe (Color) übertragen. Als Bus wird der I2C Bus "Nr.3" benutzt der an der Seite vom TXT 4.0 zugänglich ist.
Code: Alles auswählen
import time
def initAPDS9960():
global bus, DEVICE_ADDRESS
#https://www.raspberry-pi-geek.de/ausgaben/rpg/2018/08/i2c-workshop-teil-20-apds-9960/
import smbus
bus = smbus.SMBus(3)
DEVICE_ADDRESS = 0x39
#0x80 Aktivierungsregister, Sensor einschalten, Funktionen aktivieren
bus.write_byte_data(DEVICE_ADDRESS, 0x80, 0x07)
#0x8F Steuerregister 1 Funktionsparameter des ICs einstellen.
#0/1 Verstärkung Farb- und Lichtsensor (00=1x, 01=4x, 10=16x, 11=64x)
#2/3 Verstärkung Annäherungssensor (00=1x, 01=2x, 10=4x, 11=8x)
#4/5 reserviert
#6/7 Stromstärke LED (00=100mA, 01=50mA, 10=25mA, 11=12,5mA)
bus.write_byte_data(DEVICE_ADDRESS, 0x8f, 0x0f)
def readProximity():
global bus, DEVICE_ADDRESS, p
#proximity
p = bus.read_byte_data(DEVICE_ADDRESS, 0x9c)
def readColor():
global bus, DEVICE_ADDRESS, cf, rf, gf, bf, hf, sf, vf
import colorsys #hsv
#color
cl = bus.read_byte_data(DEVICE_ADDRESS, 0x94)
ch = bus.read_byte_data(DEVICE_ADDRESS, 0x95)
rl = bus.read_byte_data(DEVICE_ADDRESS, 0x96)
rh = bus.read_byte_data(DEVICE_ADDRESS, 0x97)
gl = bus.read_byte_data(DEVICE_ADDRESS, 0x98)
gh = bus.read_byte_data(DEVICE_ADDRESS, 0x99)
bl = bus.read_byte_data(DEVICE_ADDRESS, 0x9a)
bh = bus.read_byte_data(DEVICE_ADDRESS, 0x9b)
c = cl + (ch << 8)
r = rl + (rh << 8)
g = gl + (gh << 8)
b = bl + (bh<<8)
m = 65535/63
cf = c/m
rf = r/m
gf = g/m
bf = b/m
hf,sf,vf = colorsys.rgb_to_hsv(rf, gf, bf)
initAPDS9960()
while True:
readProximity()
readColor()
print("proximity:",p," crgb:", format(cf, '.2f'),format(rf, '.2f'),format(gf, '.2f'), format(bf, '.2f')," hsv:",format(hf, '.2f'),format(sf, '.2f'),format(vf, '.2f'))
time.sleep(1)
Das Programm ist ein Bolcky RoboPro Programm. Oben ist nur das Python Programm abgebildet. Nun das Blocky Programm (*)
Man muss es in APDS9960_smbus.ft umbenennen.
Code: Alles auswählen
PK
j{…WÅ%àÅ Å /APDS9960_smbus.blockly<xml xmlns="https://developers.google.com/blockly/xml" version="15">
<block type="start_block" id="C,[Z#!8M:O]SV%@n@}Tj" deletable="false" x="0" y="0">
<statement name="statement">
<block type="procedures_callnoreturn" id="~M-#Qmq7kM[|xL9aS;G:">
<mutation name="initAPDS9960"/>
<next>
<block type="loop_endless" id="[v.mwpHbcQKSI:?%u5*=">
<statement name="DO">
<block type="procedures_callnoreturn" id="OUL%CN}5d|u^PNH_nU[)">
<mutation name="readProximity"/>
<next>
<block type="procedures_callnoreturn" id="h[z(f3`lSom1{9?M`Aet">
<mutation name="readColor"/>
<next>
<block type="util_python" id="nR{)ZS*a`z_8Ek0Gsq/K">
<field name="value">print("proximity:",p," crgb:", format(cf, '.2f'),format(rf, '.2f'),format(gf, '.2f'), format(bf, '.2f')," hsv:",format(hf, '.2f'),format(sf, '.2f'),format(vf, '.2f'))</field>
<next>
<block type="util_wait_for" id="dytNq]a++sLK`yUOgWub">
<field name="format">s</field>
<value name="value">
<shadow type="math_number" id="tiqT;`F@DO|ZeD9QOSe1">
<field name="NUM">1</field>
</shadow>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</next>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" id=".h}tCd|b`}9t8uw`hv]Y" x="0" y="297">
<field name="NAME">initAPDS9960</field>
<statement name="STACK">
<block type="util_python" id="!U;VVyW|iCw8P.2{5a/E">
<field name="value">global bus, DEVICE_ADDRESS&#10;#https://www.raspberry-pi-geek.de/ausgaben/rpg/2018/08/i2c-workshop-teil-20-apds-9960/&#10;&#10;import smbus&#10;&#10;bus = smbus.SMBus(3)&#10;DEVICE_ADDRESS = 0x39&#10;&#10;#0x80 Aktivierungsregister, Sensor einschalten, Funktionen aktivieren&#10;bus.write_byte_data(DEVICE_ADDRESS, 0x80, 0x07)&#10;&#10;#0x8F Steuerregister 1 Funktionsparameter des ICs einstellen.&#10;#0/1 Verstärkung Farb- und Lichtsensor (00=1x, 01=4x, 10=16x, 11=64x)&#10;#2/3 Verstärkung Annäherungssensor (00=1x, 01=2x, 10=4x, 11=8x)&#10;#4/5 reserviert&#10;#6/7 Stromstärke LED (00=100mA, 01=50mA, 10=25mA, 11=12,5mA)&#10;&#10;bus.write_byte_data(DEVICE_ADDRESS, 0x8f, 0x0f)</field>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" id="{aEMkm!Wq{j/H3td@J-9" x="0" y="797">
<field name="NAME">readProximity</field>
<statement name="STACK">
<block type="util_python" id="a9$IASd~iSfz`riK?Ofx">
<field name="value">global bus, DEVICE_ADDRESS, p&#10;#proximity&#10;p = bus.read_byte_data(DEVICE_ADDRESS, 0x9c)</field>
</block>
</statement>
</block>
<block type="procedures_defnoreturn" id="QdxeW$X@QoLcrnl7GHO." x="0" y="982">
<field name="NAME">readColor</field>
<statement name="STACK">
<block type="util_python" id=",/Dzo)/){=+)`=(;;Qk:">
<field name="value">global bus, DEVICE_ADDRESS, cf, rf, gf, bf, hf, sf, vf&#10;import colorsys #hsv&#10;#color&#10;cl = bus.read_byte_data(DEVICE_ADDRESS, 0x94)&#10;ch = bus.read_byte_data(DEVICE_ADDRESS, 0x95)&#10;rl = bus.read_byte_data(DEVICE_ADDRESS, 0x96)&#10;rh = bus.read_byte_data(DEVICE_ADDRESS, 0x97)&#10;gl = bus.read_byte_data(DEVICE_ADDRESS, 0x98)&#10;gh = bus.read_byte_data(DEVICE_ADDRESS, 0x99)&#10;bl = bus.read_byte_data(DEVICE_ADDRESS, 0x9a)&#10;bh = bus.read_byte_data(DEVICE_ADDRESS, 0x9b)&#10;c = cl + (ch << 8)&#10;r = rl + (rh << 8)&#10;g = gl + (gh << 8)&#10;b = bl + (bh<<8)&#10;m = 65535/63&#10;cf = c/m&#10;rf = r/m&#10;gf = g/m&#10;bf = b/m&#10;hf,sf,vf = colorsys.rgb_to_hsv(rf, gf, bf)</field>
</block>
</statement>
</block>
</xml>PK
j{…WV‘[ /APDS9960_smbus.py
import time
def initAPDS9960():
global bus, DEVICE_ADDRESS
#https://www.raspberry-pi-geek.de/ausgaben/rpg/2018/08/i2c-workshop-teil-20-apds-9960/
import smbus
bus = smbus.SMBus(3)
DEVICE_ADDRESS = 0x39
#0x80 Aktivierungsregister, Sensor einschalten, Funktionen aktivieren
bus.write_byte_data(DEVICE_ADDRESS, 0x80, 0x07)
#0x8F Steuerregister 1 Funktionsparameter des ICs einstellen.
#0/1 Verstärkung Farb- und Lichtsensor (00=1x, 01=4x, 10=16x, 11=64x)
#2/3 Verstärkung Annäherungssensor (00=1x, 01=2x, 10=4x, 11=8x)
#4/5 reserviert
#6/7 Stromstärke LED (00=100mA, 01=50mA, 10=25mA, 11=12,5mA)
bus.write_byte_data(DEVICE_ADDRESS, 0x8f, 0x0f)
def readProximity():
global bus, DEVICE_ADDRESS, p
#proximity
p = bus.read_byte_data(DEVICE_ADDRESS, 0x9c)
def readColor():
global bus, DEVICE_ADDRESS, cf, rf, gf, bf, hf, sf, vf
import colorsys #hsv
#color
cl = bus.read_byte_data(DEVICE_ADDRESS, 0x94)
ch = bus.read_byte_data(DEVICE_ADDRESS, 0x95)
rl = bus.read_byte_data(DEVICE_ADDRESS, 0x96)
rh = bus.read_byte_data(DEVICE_ADDRESS, 0x97)
gl = bus.read_byte_data(DEVICE_ADDRESS, 0x98)
gh = bus.read_byte_data(DEVICE_ADDRESS, 0x99)
bl = bus.read_byte_data(DEVICE_ADDRESS, 0x9a)
bh = bus.read_byte_data(DEVICE_ADDRESS, 0x9b)
c = cl + (ch << 8)
r = rl + (rh << 8)
g = gl + (gh << 8)
b = bl + (bh<<8)
m = 65535/63
cf = c/m
rf = r/m
gf = g/m
bf = b/m
hf,sf,vf = colorsys.rgb_to_hsv(rf, gf, bf)
initAPDS9960()
while True:
readProximity()
readColor()
print("proximity:",p," crgb:", format(cf, '.2f'),format(rf, '.2f'),format(gf, '.2f'), format(bf, '.2f')," hsv:",format(hf, '.2f'),format(sf, '.2f'),format(vf, '.2f'))
time.sleep(1)
PK
j{…W lib/PK
j{…W£‚!4E E lib/controller.blockly<xml xmlns="https://developers.google.com/blockly/xml" version="15"/>PK
j{…W“×2 lib/controller.py
PK
j{…W÷ôÙ } }
.project.json{"uuid":"3c35bc4f-7b7c-5ba2-6ac4-5bb5376fe1a8","name":"APDS9960_smbus","mode":"ADVANCED","version":"1.0","controller":"TXT4"}PK
j{…WÅ%àÅ Å /APDS9960_smbus.blocklyPK
j{…WV‘[ ú /APDS9960_smbus.pyPK
j{…W 0 lib/PK
j{…W£‚!4E E R lib/controller.blocklyPK
j{…W“×2 Ë lib/controller.pyPK
j{…W÷ôÙ } }
û .project.jsonPK u £
Jetzt können wir die I2C Schnittstelle benutzen und Sensoren oder Erweiterungen anschließen. Es ist auch möglich einen TXT mit einem TXT 4.0 zu verbinden. Man kann auf dem TXT auch ein RoboPro Programm laufen lassen und damit die I2C Schnittstelle ansprechen.
Ich denke, dass es eine große Vielfalt und nun viele neue Einsatzmöglichkeiten und Modell geben wird.
Mit freundlichen Grüßen
Holger
(*)
Es ist die Frage wie man hier im Forum ft-Programme darstellt. Man kann den RoboPro Coding (Blocky) Text in den Editor kopieren speichern und dann in xxxxx.ft umbenennen.
Wichtiger Hinweis:
Man sollte schon wissen was man da macht. Man darf nicht Sachen einfach mischen. Z.B. ist die Spannung die man verwendet sehr wichtig. Man kann nicht einfach 3,3V mit 5V mischen. Das führt zur unwiederruflichen Zerstörung der Ein- und Ausgänge am TXT 4.0. Das betrift z.B. die Kopplung vom TX/TXT zum TXT 4.0 oder vom Arduino UNO und dem TXT 4.0. Es gibt "auch" 3,3V Versionen vom UNO aber viele der I2C-Sensoren laufen mit 5V und damit auch die I2C Schnittstelle auf 5V. Ansonsten Levelshifter 3,3V <-> 5V benutzen.
Als Bus wird der I2C Bus Nr.3 benutzt der an der Seite vom TXT 4.0 zugänglich ist. Niemals einen anderen Bus benutzen! Man könnte Werte z.B. vom Speicher überschreiben und den TXT 4.0 dauerhaft blockieren. Man darf auch keinen Kurzschluss auf dem Bus erzeugen. Immer erst prüfen, dann einschalten. Auch sollte man die Sensoren nicht im laufenden Betrieb anschließen.