PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Supporting 2.6 and up
testpresta2
Phidgetsian
Posts: 13
Joined: Fri Jan 19, 2018 2:16 pm
Contact:

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Postby testpresta2 » Wed Jan 31, 2018 12:01 pm

What a shit.
I have checked everything.
When i set an output to 1, i made a loop to wait until the state of this channel is 1. Then i put a time.sleep(1) and when i read the state again, it comes to 0 !!
I think there is a bug somewhere but it is not my job to fix that. I think i will trash this relay board and buy something else

User avatar
mparadis
Site Admin
Posts: 690
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Postby mparadis » Wed Jan 31, 2018 12:56 pm

It would be helpful if you post your code in its current state so we can try to see what is wrong.

User avatar
Patrick
Lead Developer
Posts: 3141
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Postby Patrick » Wed Jan 31, 2018 1:59 pm

If the state is reverting to 0, this means either the channel was closed, or the 1014 experienced a detach. Raspberry Pi USB ports are notoriously under-powered, so you may want to check for USB detaches (you can see these in dmesg) - in that case, you probably need a powered hub.

If you need the relays to maintain state across open/close cycles, you should use phidget21 instead of phidget22. Phidget21 support is maintained here: https://www.phidgets.com/docs21/

-Patrick

testpresta2
Phidgetsian
Posts: 13
Joined: Fri Jan 19, 2018 2:16 pm
Contact:

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Postby testpresta2 » Thu Feb 01, 2018 1:51 am

How do you explain that only one channel of 4 is disconnecting. If my raspberry cut the usb power, it will close the 4 channels ?

testpresta2
Phidgetsian
Posts: 13
Joined: Fri Jan 19, 2018 2:16 pm
Contact:

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Postby testpresta2 » Thu Feb 01, 2018 4:42 am

I have lost half a day with your 21 library version.

I have made a program that randomly put some ports to 1 or 0, then wait a fews seconds and loop.

The states are not always applied.

This board is really a joke for me.

I wont work with Phidgets

testpresta2
Phidgetsian
Posts: 13
Joined: Fri Jan 19, 2018 2:16 pm
Contact:

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Postby testpresta2 » Thu Feb 01, 2018 4:44 am

Here is my code:

Code: Select all

from Phidgets.Devices.InterfaceKit import InterfaceKit
import time
import random

class GestionRelais:

    def __init__(self):
        self.carte0 = InterfaceKit()
        self.carte0.openPhidget(REPLACE_WITH_YOUR_BOARD1_SERIAL_NUMBER)
        self.carte0.waitForAttach(10000)

        self.carte1 = InterfaceKit()
        self.carte1.openPhidget(REPLACE_WITH_YOUR_BOARD2_SERIAL_NUMBER)
        self.carte1.waitForAttach(10000)

        self.carte2 = InterfaceKit()
        self.carte2.openPhidget(REPLACE_WITH_YOUR_BOARD3_SERIAL_NUMBER)
        self.carte2.waitForAttach(10000)

    def affiche_etat(self,titre):
        print("%s\t[[%d, %d, %d, %d], [%d, %d, %d, %d], [%d, %d, %d, %d]]" % (
            titre,
            self.get_etat(0, 0), self.get_etat(0, 1), self.get_etat(0, 2), self.get_etat(0, 3),
            self.get_etat(1, 0), self.get_etat(1, 1), self.get_etat(1, 2), self.get_etat(1, 3),
            self.get_etat(2, 0), self.get_etat(2, 1), self.get_etat(2, 2), self.get_etat(2, 3)))

    def get_etat(self,carte,port):
        while True:
            try:
                if carte==0:
                    return 1 if self.carte0.getOutputState(port) else 0
                elif carte == 1:
                    return 1 if self.carte1.getOutputState(port) else 0
                elif carte == 2:
                    return 1 if self.carte2.getOutputState(port) else 0
                else:
                    return
            except:
                print("Erreur.get_etat")
            time.sleep(0.2)

    def check_etat(self, etat_attendu,titre):
        for carte in range(0,3):
            for port in range(0,4):
                if self.get_etat(carte, port)!=etat_attendu[carte][port]:
                    print("%s=\t%s" % (titre, etat_attendu))
                    self.affiche_etat("actuel=")
                    return

    def set_etat(self,carte,port,etat):

        b_etat = (etat==1)

        while True:
            try:
                if carte==0:
                    self.carte0.setOutputState(port, b_etat)
                elif carte == 1:
                    self.carte1.setOutputState(port, b_etat)
                elif carte == 2:
                    self.carte2.setOutputState(port, b_etat)
                else:
                    return

                # le port ne passe pas immediatement a la valeur indiquee
                # D'ou tempo + boucle de verification
                time.sleep(0.1)

                if self.get_etat(carte, port)==etat:
                    return

            except:
                print("Erreur.set_etat")
            time.sleep(0.2)

gestion = GestionRelais()

mem_etat = [[gestion.get_etat(0, 0), gestion.get_etat(0, 1), gestion.get_etat(0, 2), gestion.get_etat(0, 3)],
            [gestion.get_etat(1, 0), gestion.get_etat(1, 1), gestion.get_etat(1, 2), gestion.get_etat(1, 3)],
            [gestion.get_etat(2, 0), gestion.get_etat(2, 1), gestion.get_etat(2, 2), gestion.get_etat(2, 3)]]


while True:

    raz = random.randint(0,20)
    if raz==0:
        # print("RAZ")
        mem_etat = [ [0,0,0,0], [0,0,0,0], [0,0,0,0] ]
        for carte in range(0,3):
            for port in range(0,4):
                gestion.set_etat(carte,port,0)
        gestion.check_etat(mem_etat, "err raz")
        continue
    elif raz==1:
        # print("TOUT A 1")
        mem_etat = [ [1,1,1,1], [1,1,1,1], [1,1,1,1] ]
        for carte in range(0,3):
            for port in range(0,4):
                gestion.set_etat(carte,port,1)
        gestion.check_etat(mem_etat, "err tout")
        continue

    carte = random.randint(0,2)
    port = random.randint(0,3)
    etat = random.randint(0,1)
    tempo = float(random.randint(1,100))

    if mem_etat[carte][port] == etat:
        continue

    # print("Changement etat carte=%d port=%d etat=%d" % (carte, port, etat))

    # gestion.affiche_etat("etat avant")
    mem_etat[carte][port] = etat
    gestion.set_etat(carte,port,etat)
    # gestion.affiche_etat("etat apres")

    gestion.check_etat(mem_etat,"err avant")
    time.sleep(tempo/10.0)
    # gestion.check_etat(mem_etat,"err apres")


And here is what i get after 1 hour:

Erreur.set_etat
Erreur.set_etat
Erreur.set_etat
err avant= [[1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 1]]
actuel= [[0, 0, 0, 0], [1, 1, 1, 0], [1, 1, 1, 1]]
err avant= [[1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 0]]
actuel= [[0, 0, 0, 0], [1, 1, 1, 0], [1, 1, 1, 0]]
err avant= [[1, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 1]]
actuel= [[0, 0, 0, 0], [1, 1, 1, 0], [1, 1, 1, 1]]
Erreur.get_etat
err tout= [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
actuel= [[0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]]
err avant= [[1, 1, 1, 0], [1, 1, 1, 1], [1, 1, 1, 1]]
actuel= [[0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]]
Erreur.get_etat
Erreur.get_etat
Erreur.get_etat
err avant= [[1, 1, 1, 1], [0, 1, 1, 1], [1, 1, 1, 1]]
actuel= [[0, 0, 0, 0], [0, 1, 1, 1], [1, 1, 1, 1]]
err avant= [[0, 1, 1, 1], [0, 1, 1, 1], [1, 1, 1, 1]]
actuel= [[0, 0, 0, 0], [0, 1, 1, 1], [1, 1, 1, 1]]
Erreur.get_etat
Erreur.get_etat
Erreur.get_etat
err tout= [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
actuel= [[0, 0, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]]
err avant= [[1, 1, 1, 1], [0, 1, 1, 1], [1, 1, 1, 1]]
actuel= [[0, 0, 0, 0], [0, 1, 1, 1], [1, 1, 1, 1]]

User avatar
mparadis
Site Admin
Posts: 690
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Postby mparadis » Thu Feb 01, 2018 11:30 am

I just ran your code on my Windows computer for an hour with no errors. I agree with Patrick, the errors you're seeing likely have to do with a power issue on the Pi's USB hub causing the Phidgets to drop in and out. I recommend using an externally powered USB hub to ensure that each Phidget is getting the full 5V/500mA that USB is specified for.

testpresta2
Phidgetsian
Posts: 13
Joined: Fri Jan 19, 2018 2:16 pm
Contact:

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Postby testpresta2 » Fri Feb 02, 2018 12:33 am

How can i track a channel disconnexion ?
The documentation is very poor

Thanks

User avatar
mparadis
Site Admin
Posts: 690
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Postby mparadis » Fri Feb 02, 2018 8:27 am

Look at "setOnDetachHandler" in our API documentation, and look at how it's used in every python code sample:

Code: Select all

def DigitalOutputDetached(self):
    detached = self
    try:
        print("\nDetach event on Port %d Channel %d" % (detached.getHubPort(), detached.getChannel()))
    except PhidgetException as e:
        print("Phidget Exception %i: %s" % (e.code, e.details))
        print("Press Enter to Exit...\n")
        readin = sys.stdin.read(1)
        exit(1)   


This event will trigger whenever a channel is closed intentionally, physically disconnected, or detaching due to loss of power. You need to register the event handler to the object using setOnDetachHandler(DigitalOutputDetached) in this case.

testpresta2
Phidgetsian
Posts: 13
Joined: Fri Jan 19, 2018 2:16 pm
Contact:

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Postby testpresta2 » Fri Feb 02, 2018 9:31 am

In my previous code, i had no exception when the ports changed value...


Return to “Linux”

Who is online

Users browsing this forum: No registered users and 2 guests