コード例 #1
0
class Led:
    @staticmethod
    def conn():
        try:
            return ALProxy("ALLeds", Config.ip_addr, Config.port_num)
        except Exception as e:
            print("Exception -> Led.conn():", e)
        return False

    @staticmethod
    def hexParaRGB(value):
        value = value.lstrip('#')
        lv = len(value)
        return tuple(
            int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))

    @staticmethod
    def mudar(r=0, g=0, b=0, hex="", group="AllLeds", async=True):
        try:
            if async:
                return AsyncThread.call(
                    lambda: Led.mudar(r, g, b, hex, group, False))
            if hex:
                rgb = Led.hexParaRGB(hex)
                r = rgb[0]
                g = rgb[1]
                b = rgb[2]
            Led.conn().fadeRGB(group,
                               256 * 256 * r + 256 * g + b,
                               0.100000,
                               _async=True)
            return True
        except Exception as e:
            print("Exception -> Led.mudar():", e)
        return False
コード例 #2
0
class Motor:

    almotion = False     

    @staticmethod
    def movimento():
        try:
            if Motor.almotion:
                Motor.almotion.stopMove()
                Motor.almotion = False
            Motor.almotion = ALProxy('ALMotion', Config.ip_addr, Config.port_num)
            Motor.almotion.stopMove()        
            return Motor.almotion    
        except Exception as e:
            print("Exception -> Motor.movimento():", e)
        return False

    @staticmethod
    def angulo(nome, tempo = 1.0, angulo = 0):
        try:
            motion = Motor.movimento()
            if motion:
                motion.angleInterpolationBezier(nome, tempo, angulo)
            return True
        except Exception as e:
            print("Exception -> Motor.angulo():", e)
        return False

    """
        MENCAO - By Matheus Johann Araujo
        As funcoes de nome "bracoEsquerdo(params...)" e
        "bracoDireito(params...)" foram desenvolvidas por: 
            Laura Gabrielle de Lira Silva e
            Wesley Oliveira de Souza            
    """
    @staticmethod
    def bracoEsquerdo(nome, angulo = 0, tempo = 1.0, rigidez = 100, async = True):
        if async:
            return AsyncThread.call(lambda: Motor.bracoEsquerdo(nome, angulo, tempo, rigidez, False))
        Motor.rigidezBracoEsquerdo(rigidez)
        angulo = [[float(angulo) * almath.TO_RAD]]
        tempo  = [[tempo]]
        if "antebraco" == nome:# Angulo -120 ate 120
            nome = ["LElbowYaw"]
        elif "mao" == nome:# Angulo -105 ate 105
            nome = ["LWristYaw"]
        elif "cotovelo" == nome:# Angulo -90 ate 0
            nome = ["LElbowRoll"]
        elif "ombro" == nome:# Angulo -18 ate 80
            nome = ["LShoulderRoll"]
        Motor.angulo(nome, tempo, angulo)
        return True
コード例 #3
0
class Comportamento:
    @staticmethod
    def conn():
        try:
            return ALProxy('ALBehaviorManager', Config.ip_addr,
                           Config.port_num)
        except Exception as e:
            print("Exception -> Comportamento.conn():", e)
        return False

    @staticmethod
    def listar():
        try:
            bm = Comportamento.conn()
            behaviors = bm.getUserBehaviorNames()
            for i in range(len(behaviors)):
                for j in range(2):
                    behaviors[i] = "/" + behaviors[i]
                    behaviors[i] = re.sub('/[^>]+?/', '', behaviors[i])
                behaviors[i] = re.sub('/', '', behaviors[i])
            return behaviors
        except Exception as e:
            print("Exception -> Comportamento.listar():", e)
        return []

    @staticmethod
    def iniciar(nome, runBehavior=True, async=True):
        try:
            if async:
                return AsyncThread.call(
                    lambda: Comportamento.iniciar(nome, runBehavior, False))
            if nome in Comportamento.listar():
                bm = Comportamento.conn()
                if runBehavior:
                    bm.runBehavior(nome)
                    return True
                else:
                    bm.startBehavior(nome)
                    return True
        except Exception as e:
            print("Exception -> Comportamento.iniciar():", e)
        return False
コード例 #4
0
class ModoAutonomo:
    @staticmethod
    def conn():
        try:
            ma = ALProxy('ALAutonomousLife', Config.ip_addr, Config.port_num)
            print("ModoAutonomo.conn().getState():", str(ma.getState()))
            return ma
        except Exception as e:
            print("Exception -> ModoAutonomo.conn():", e)
        return False

    @staticmethod
    def alternar():
        try:
            ma = ModoAutonomo.conn()
            if ma.getState() != "disabled":
                return ModoAutonomo.desligar(ma)
            elif ma.getState() == "disabled":
                return ModoAutonomo.ligar(ma)
        except Exception as e:
            print("Exception -> ModoAutonomo.alternar():", e)
        return False

    @staticmethod
    def ligar(ma=False, async=True):
        try:
            if async:
                return AsyncThread.call(lambda: ModoAutonomo.ligar(ma, False))
            if not ma:
                ma = ModoAutonomo.conn()
            if ma.getState() == "disabled":
                ma.setState("safeguard")
            print("ModoAutonomo.conn().getState():", str(ma.getState()))
            return True
        except Exception as e:
            print("Exception -> ModoAutonomo.ligar():", e)
        return False
コード例 #5
0
                g = rgb[1]
                b = rgb[2]
            Led.conn().fadeRGB(group,
                               256 * 256 * r + 256 * g + b,
                               0.100000,
                               _async=True)
            return True
        except Exception as e:
            print("Exception -> Led.mudar():", e)
        return False

    @staticmethod
    def alternar(on=False, group="AllLeds", async=True):
        try:
            if async:
                return AsyncThread.call(lambda: Led.alternar(on, group, False))
            leds = Led.conn()
            if on:
                leds.on(group)
                return True
            else:
                leds.off(group)
                return True
        except Exception as e:
            print("Exception -> Led.alternar():", e)
        return False

    @staticmethod
    def botoesCabeca(b=0):
        return Led.mudar(b=b, group="BrainLeds")
コード例 #6
0
                bm = Comportamento.conn()
                if runBehavior:
                    bm.runBehavior(nome)
                    return True
                else:
                    bm.startBehavior(nome)
                    return True
        except Exception as e:
            print("Exception -> Comportamento.iniciar():", e)
        return False

    @staticmethod
    def parar(nome, posturaCrouch=True, async=False):
        try:
            if async:
                return AsyncThread.call(
                    lambda: Comportamento.parar(nome, posturaCrouch, False))
            if nome in Comportamento.listar():
                Led.corpoVermelho(255)
                bm = Comportamento.conn()
                bm.stopBehavior(nome)
                if posturaCrouch:
                    Motor.posturaCrouch()
                else:
                    time.sleep(1)
                Led.corpoVerde(255)
                time.sleep(1)
                Led.corpo(255, 255, 255)
                return True
        except Exception as e:
            print("Exception -> Comportamento.parar():", e)
        return False
コード例 #7
0
class Ouvinte:

    asr = False

    @staticmethod
    def conn():
        try:
            if not Ouvinte.asr:
                Ouvinte.asr = ALProxy("ALSpeechRecognition", Config.ip_addr,
                                      Config.port_num)
                try:
                    Ouvinte.asr.removeAllContext()
                    Ouvinte.asr.unsubscribe("Test_ASR")
                except:
                    pass
            return Ouvinte.asr
        except Exception as e:
            print("Exception -> Ouvinte.conn():", e)
        return Ouvinte.asr

    @staticmethod
    def idioma(linguagem="Brazilian"):
        try:
            Ouvinte.conn().setLanguage(linguagem)
            return True
        except Exception as e:
            print("Exception -> Ouvinte.idioma():", e)
        return False

    precisao = 0.30

    @staticmethod
    def vocabulario(palavras=["ola", "matheus", "teste", "xuxu"],
                    precisao=30,
                    detectarPalavra=False):
        try:
            Ouvinte.conn().pause(True)
            Ouvinte.conn().setVocabulary(palavras, detectarPalavra)
            Ouvinte.precisao = float(precisao / 100)
            Ouvinte.conn().pause(False)
            return True
        except Exception as e:
            print("Exception -> Ouvinte.vocabulario():", e)
        return False

    @staticmethod
    def parametro(sensibilidade="Sensitivity", hipoteses=0.2):
        try:
            Ouvinte.conn().setParameter(sensibilidade, hipoteses)
            return True
        except Exception as e:
            print("Exception -> Ouvinte.parametro():", e)
        return False

    threadIniciar = False

    @staticmethod
    def iniciar(callback, async=True):
        try:
            if async:
                Ouvinte.threadIniciar = AsyncThread.call(
                    lambda: Ouvinte.iniciar(callback, False))
                return Ouvinte.threadIniciar
            time.sleep(3)
            Ouvinte.conn().subscribe("Test_ASR")
        except Exception as e:
            print("Exception -> Ouvinte.iniciar()-subscribe:", e)
        while True:
            try:
                if Ouvinte.escutando:
                    leitura = Memoria.ler("WordRecognized")
                    if Ouvinte.precisao <= leitura[1]:
                        Ouvinte.conn().unsubscribe("Test_ASR")
                        callback(leitura)
                        Ouvinte.conn().subscribe("Test_ASR")
                    time.sleep(0.25)
            except Exception as e:
                print("Exception -> Ouvinte.iniciar()-while:", e)
コード例 #8
0
    @staticmethod
    def pausar(valor=0):
        try:
            if valor == 0:
                Ouvinte.conn().pause(Ouvinte.escutando)
                Ouvinte.escutando = not Ouvinte.escutando
            else:
                Ouvinte.conn().pause(valor)
                Ouvinte.escutando = not valor
            print("Ouvinte.pausar()", not Ouvinte.escutando)
            return True
        except Exception as e:
            print("Exception -> Ouvinte.pausar():", e)
        return False

    @staticmethod
    def parar(timer=0, async=True):
        try:
            if async:
                return AsyncThread.call(lambda: Ouvinte.parar(timer, False))
            time.sleep(timer)
            if Ouvinte.conn() and Ouvinte.threadIniciar:
                if Ouvinte.threadIniciar.running():
                    Ouvinte.threadIniciar.stop()
                Ouvinte.conn().unsubscribe("Test_ASR")
                return True
        except Exception as e:
            print("Exception -> Ouvinte.parar():", e)
        return False
コード例 #9
0
class Audio:

    @staticmethod
    def conn(num):
        try:
            if num == 1:
                return ALProxy('ALAudioDevice', Config.ip_addr, Config.port_num)
            elif num == 2:
                return ALProxy('ALAudioPlayer', Config.ip_addr, Config.port_num)
            elif num == 3:
                tts = ALProxy('ALTextToSpeech', Config.ip_addr, Config.port_num)
                tts.setVolume(1)
                return tts
            elif num == 4:
                return ALProxy('ALAnimatedSpeech', Config.ip_addr, Config.port_num)
        except Exception as e:
            print("Exception -> Audio.conn():", e)
        return False

    @staticmethod
    def volume(vol = -1):
        try:
            vol = int(vol)
            if vol > -1 and vol < 101:
                Audio.conn(1).setOutputVolume(vol)
            return Audio.conn(1).getOutputVolume()
        except Exception as e:
            print("Exception -> Audio.setVolume():", e)
        return False
        
    @staticmethod
    def iniciarMusica(name = "", begin = 0, volume = 100, balance = 0, path = ""):
        try:
            volume = volume / 100
            if name != "" and path == "":
                path = os.getcwd()
                #path = os.path.dirname(os.path.realpath(__file__))
                path = path + "/" + name
                print("Audio.iniciarMusica", path)
            return Audio.conn(2).pCall("playFileFromPosition", path, float(begin), volume, balance)
        except Exception as e:
            print("Exception -> Audio.iniciarMusica():", e)
        return False

    @staticmethod
    def pararMusica(id):
        try:
            Audio.conn(2).stop(id)
            return True
        except Exception as e:
            print("Exception -> Audio.pararMusica():", e)
        return False

    @staticmethod
    def pararMusicas():
        try:
            Audio.conn(2).stopAll()
            return True
        except Exception as e:
            print("Exception -> Audio.pararMusicas():", e)
        return False

    @staticmethod
    def falar(texto, animado = False, speed = 80, voice_shaping = 100, configuracao = { "speakingMovementMode": "contextual", "bodyLanguageMode": "contextual" }, async = False):
        try:
            if async:
                return AsyncThread.call(lambda: Audio.falar(texto, animado, speed, voice_shaping, configuracao, False))
            if animado:
                modo = 4
            else:
                modo = 3
            tts = Audio.conn(modo)
            sentence = "\RSPD=" + str(speed) + "\ "
            sentence += "\VCT=" + str(voice_shaping) + "\ "
            sentence += str(texto)
            sentence +=  "\RST\ "
            if animado:
                id = tts.pCall("say", str(sentence), configuracao)
            else:
                id = tts.pCall("say", str(sentence))
            tts.wait(id, 0)
            return True
        except Exception as e:
            print("Exception -> Audio.falar():", e)
        return False
コード例 #10
0
    def ligar(ma=False, async=True):
        try:
            if async:
                return AsyncThread.call(lambda: ModoAutonomo.ligar(ma, False))
            if not ma:
                ma = ModoAutonomo.conn()
            if ma.getState() == "disabled":
                ma.setState("safeguard")
            print("ModoAutonomo.conn().getState():", str(ma.getState()))
            return True
        except Exception as e:
            print("Exception -> ModoAutonomo.ligar():", e)
        return False

    @staticmethod
    def desligar(ma=False, async=True):
        try:
            if async:
                return AsyncThread.call(
                    lambda: ModoAutonomo.desligar(ma, False))
            if not ma:
                ma = ModoAutonomo.conn()
            if ma.getState() != "disabled":
                ma.setState("disabled")
            print("ModoAutonomo.conn().getState():", str(ma.getState()))
            #ma.stopAll()
            return True
        except Exception as e:
            print("Exception -> ModoAutonomo.desligar():", e)
        return False
コード例 #11
0
        tempo  = [[tempo]]
        if "antebraco" == nome:# Angulo -120 ate 120
            nome = ["LElbowYaw"]
        elif "mao" == nome:# Angulo -105 ate 105
            nome = ["LWristYaw"]
        elif "cotovelo" == nome:# Angulo -90 ate 0
            nome = ["LElbowRoll"]
        elif "ombro" == nome:# Angulo -18 ate 80
            nome = ["LShoulderRoll"]
        Motor.angulo(nome, tempo, angulo)
        return True

    @staticmethod
    def bracoDireito(nome, angulo = 0, tempo = 1.0, rigidez = 100, async = True):
        if async:
            return AsyncThread.call(lambda: Motor.bracoDireito(nome, angulo, tempo, rigidez, False))
        Motor.rigidezBracoDireito(rigidez)
        angulo = [[float(angulo) * almath.TO_RAD]]
        tempo  = [[tempo]]
        if "antebraco" == nome:# Angulo -120 ate 120
            nome = ["RElbowYaw"]
        elif "mao" == nome:# Angulo -105 ate 105
            nome = ["RWristYaw"]
        elif "cotovelo" == nome:# Angulo -90 ate 0
            nome = ["RElbowRoll"]
        elif "ombro" == nome:# Angulo -18 ate 80
            nome = ["RShoulderRoll"]
        Motor.angulo(nome, tempo, angulo)
        return True

    @staticmethod