Exemplo n.º 1
0
class LedsActuator:
    def __init__(self, proxy):
        assert proxy.__class__ is ALProxy;
        self.__proxy = proxy;
        self.__ledsNames, self.__colorsNames = self.__getLedsNames();
        self.__animation = Animation();
        self.__parallelism = False;

    def setParallelism(self, value):
        self.__parallelism = value;

    #test OK
    def getLedsNames(self):
        return self.__ledsNames;

    #test OK
    def __getLedsNames(self):
        names = [];
        colorsNames = {};
        for i in range(1,9):
            name = "RightFaceLed%s" %(i);
            names.append(name);
            degree = (i-1)*45;
            red = "Face/Led/Red/Right/%sDeg/Actuator/Value" %(degree);
            green = "Face/Led/Green/Right/%sDeg/Actuator/Value" %(degree);
            blue = "Face/Led/Blue/Right/%sDeg/Actuator/Value" %(degree);
            tab = [red,green,blue];
            colorsNames[name] = tab;
        for i in range(8,0,-1):
            name = "LeftFaceLed%s" %(i);
            names.append(name);
            degree = (i-1)*45;
            red = "Face/Led/Red/Left/%sDeg/Actuator/Value" %(degree);
            green = "Face/Led/Green/Left/%sDeg/Actuator/Value" %(degree);
            blue = "Face/Led/Blue/Left/%sDeg/Actuator/Value" %(degree);
            tab = [red,green,blue];
            colorsNames[name] = tab;

        return names, colorsNames;

    #test OK
    def getLedName(self, ledsNumber):
        assert ledsNumber < len(self.__ledsNames);
        return self.__ledsNames[ledsNumber];

    #test OK
    def getLedsNumber(self):
        return len(self.__ledsNames);

    #test OK
    def allLedsOn(self):
        name = "FaceLeds";
        self.__proxy.on(name);

    #test OK
    def rightLedsOn(self):
        name = "RightFaceLeds";
        self.__proxy.on(name);

    #test OK
    def leftLedsOn(self):
        name = "LeftFaceLeds";
        self.__proxy.on(name);

    #test OK
    def allLedsOff(self):
        name = "FaceLeds";
        self.__proxy.off(name);

    #test OK
    def rightLedsOff(self):
        name = "RightFaceLeds";
        self.__proxy.off(name);

    #test OK
    def leftLedsOff(self):
        name = "LeftFaceLeds";
        self.__proxy.off(name);

    #test OK
    def getIntensity(self, ledNumber):
        name = self.getLedName(ledNumber);
        return self.__proxy.getIntensity(name);

    #test OK
    #renvoie red, green, blue
    def getColor(self, ledNumber):
        red, green, blue = 0,0,0;

        name = self.getLedName(ledNumber);
        tabColor = self.__colorsNames[name];
        #print(self.__colorsNames)

        redIntensity = self.__proxy.getIntensity(tabColor[0]);
        greenIntensity = self.__proxy.getIntensity(tabColor[1]);
        blueIntensity = self.__proxy.getIntensity(tabColor[2]);

        red = int(redIntensity*255);
        green = int(greenIntensity*255);
        blue = int(blueIntensity*255);

        return red, green, blue;

    #test OK
    def setIntensity(self, ledNumber, intensity):
        name = self.getLedName(ledNumber);
        self.__proxy.setIntensity(name, intensity);

    #test OK
    def setIntensities(self, ledNumber, redIntensity, greenIntensity, blueIntensity):
        name = self.getLedName(ledNumber);
        tabColor = self.__colorsNames[name];
        self.__proxy.setIntensity(tabColor[0], redIntensity);
        self.__proxy.setIntensity(tabColor[1], greenIntensity);
        self.__proxy.setIntensity(tabColor[2], blueIntensity);

    #test OK
    def fadeIntensity(self, ledsNumber, intensity, duration):
        name = self.getLedName(ledsNumber);
        self.__proxy.fade(name, intensity, duration);

    #test OK
    def setColor(self, ledsNumber, red, green, blue):
        name = self.getLedName(ledsNumber);
        self.__setColor(name, red, green, blue);

    #test OK
    def __setColor(self, ledName, red, green, blue):
        assert type(red) is int;
        assert type(green) is int;
        assert type(blue) is int;
        assert red >= 0 and red <= 255;
        assert green >= 0 and green <= 255;
        assert blue >= 0 and blue <= 255;
        tabColor = self.__colorsNames[ledName];

        redIntensity = 0;
        greenIntensity = 0;
        blueIntensity = 0;

        if red != 0 :
            redIntensity = red/255.0;
        if green != 0 :
            greenIntensity = green/255.0;
        if blue != 0 :
            blueIntensity = blue/255.0;

##        print(tabColor)
##        print("ok")
##        print(self.__colorsNames)
        self.__proxy.setIntensity(tabColor[0], redIntensity);
        self.__proxy.setIntensity(tabColor[1], greenIntensity);
        self.__proxy.setIntensity(tabColor[2], blueIntensity);


    #test OK
    def fadeColor(self, ledNumber, red, green, blue, duration):
        name = self.getLedName(ledNumber);
        color = self.__RGBToInt(red, green, blue);

        self.__proxy.fadeRGB (name, color, duration);

    #test OK
    def addLedAnimation(self, ledNumber, red, green, blue, time):
        name = self.getLedName(ledNumber);
        rgb = self.__RGBToInt(red, green, blue);
        self.__animation.addValue(name, rgb, time);

    #test OK
    def resetAnimation(self):
        self.__animation.reset();

    #test OK
    def playAnimation(self):
        names = self.__animation.getNames();
        values = self.__animation.getValues();
        times = self.__animation.getTimes();
        threads = [];

        i = 0;
        for name in names :
            valuesTab = values[i];
            timesTab = times[i];
            thread = threading.Thread(None, self.__playLedAnimation, None, (name, valuesTab, timesTab), {});
            threads.append(thread);
            i=i+1;

        for thread in threads:
            thread.start();

    #test OK
    def __playLedAnimation(self, name, values, times):
        assert len(values) == len(times);
        durations = self.__getDurations(times);
        for i in range(len(times)):
            duration = durations[i];
            color = values[i];
            time.sleep(duration);
            red,green,blue = self.__intToRGB(color);
            self.__setColor(name, red, green, blue);

    #test OK
    def __getDurations(self, timesTab):
        durationTab = [];
        previousTime = -1;

        if len(timesTab)>0:
            previousTime = timesTab[0];
            durationTab.append(previousTime);
            for i in range(1, len(timesTab)):
                currentTime = timesTab[i];
                if currentTime <= previousTime :
                    string = "Times must be inscreasing at index %s : %s" %(i, timesTab[i])
                    raise Exception("TimeError", string);
                else:
                    duration = currentTime - previousTime;
                    previousTime = currentTime;
                    durationTab.append(duration);

        return durationTab;

    #test OK
    def displayLedsNumber(self, ledsWord):
        for i in range(len(self.__ledsNames)):
            print(ledsWord,i, ":", self.__ledsNames[i]);

    #test OK
    def displayAnimation(self, ledWord, valueWord, timeWord, characterNumbers):
        names = self.__animation.getNames();
        values = self.__animation.getValues();
        times = self.__animation.getTimes();

        for i in range(len(names)):
            ledNumber = self.__ledsNames.index(names[i]);
            print(ledWord,ledNumber," : ");
            for j in range(len(values[i])):
                string = "    %s : %s" %(valueWord, hex(values[i][j]));
                string = self.addSpaces(string, characterNumbers);
                print(string,"-",timeWord,":",times[i][j]);

    #test OK
    #doublon !!!!
    def addSpaces(self, string, characterTotalNumbers):
        characterNumbers = len(string);
        if characterTotalNumbers > characterNumbers:
            spaceNumbers = characterTotalNumbers - characterNumbers;
            string = string+spaceNumbers*' ';
        return string;

    #test OK
    def __displayColorsNames(self):

        for name in self.__ledsNames:
            print(name,' :');
            tab = self.__colorsNames[name];
            print("   ",tab[0]);
            print("   ",tab[1]);
            print("   ",tab[2]);


    #test OK
    def __RGBToInt(self, red, green, blue):
        assert type(red) is int;
        assert type(green) is int;
        assert type(blue) is int;
        assert red >= 0 and red <= 255;
        assert green >= 0 and green <= 255;
        assert blue >= 0 and blue <= 255;

        color = int('%02x%02x%02x' %(red, green, blue), 16);

        #to check
        #print(hex(color));

        return color;

    #test OK
    def __intToRGB(self, rgb):
        blue, green, red = [(rgb >> (8*i)) & 255 for i in range(3)];
        return red,green,blue;

    def test(self):
        rgb = self.__RGBToInt(85,127,12);
        print(self.__intToRGB(rgb));
Exemplo n.º 2
0
class ALProxy():
    # staticNao = NaoCommunicationVirtual.AbstractNaoEvenement
    # (remplace les fonctions du robot réel pour les events)
    staticNao=None
    # virtualNao = Nao3D
    # (remplace les membres du robot réel)
    virtualNao=None

    vocabList = []
    typeRobot="T14"

    membres={0:"HeadYaw",1:"HeadPitch",
                    2:"LShoulderPitch",3:"LShoulderRoll",4:"LElbowYaw",5:"LElbowRoll",6:"LWristYaw",7:"LHand",
                    8:"RShoulderPitch",9:"RShoulderRoll",10:"RElbowYaw",11:"RElbowRoll",12:"RWristYaw",13:"RHand"}

    membresVirtual={0:"teteG2", 1:"teteG0",#ok
                            2:"bicepsG0",3:"bicepsG2",4:"coudeG1",5:"coudeG2",6:"mainG1",7:"doigt1G0",#ok
                            8:"bicepsD0",9:"bicepsD2",10:"coudeD1",11:"coudeD2",12:"mainD1",13:"doigt1D0"#ok
                            }

    jointsAll={"T14":["HeadYaw", "HeadPitch",
                       "LShoulderPitch", "LShoulderRoll", "LElbowYaw", "LElbowRoll", "LWristYaw", "LHand",
                       "RShoulderPitch", "RShoulderRoll", "RElbowYaw", "RElbowRoll", "RWristYaw", "RHand"],

               "H21":['HeadYaw', 'HeadPitch',
                       'LShoulderPitch', 'LShoulderRoll', 'LElbowYaw', 'LElbowRoll',
                       'LHipYawPitch', 'LHipRoll', 'LHipPitch', 'LKneePitch', 'LAnklePitch', 'LAnkleRoll',
                       'RHipYawPitch', 'RHipRoll', 'RHipPitch', 'RKneePitch', 'RAnklePitch', 'RAnkleRoll',
                       'RShoulderPitch', 'RShoulderRoll', 'RElbowYaw', 'RElbowRoll'],
               #not tested, may not work :
               "H25":['HeadYaw', 'HeadPitch',
                       'LShoulderPitch', 'LShoulderRoll', 'LElbowYaw', 'LElbowRoll', "LWristYaw", "LHand",
                       'LHipYawPitch', 'LHipRoll', 'LHipPitch', 'LKneePitch', 'LAnklePitch', 'LAnkleRoll',
                       'RHipYawPitch', 'RHipRoll', 'RHipPitch', 'RKneePitch', 'RAnklePitch', 'RAnkleRoll',
                       'RShoulderPitch', 'RShoulderRoll', 'RElbowYaw', 'RElbowRoll',"RWristYaw", "RHand"]
                }
    parallelism = False;


    def __init__(self,name=0,adress=0,port=0):
        self.name=name
        self.membre=None

        # Différence par rapport à naoqi :
        # Dans l'interface OpenGL, les rotations des membres droits sont inversé par la symétrie
        # Donc en fait les angles pour la partie gauche et la partie droite sont les memes
        # alors que dans naoqi, les parties droite et gauche sont gérées indépendemment
        # Solution :
        # Après copie de la liste renvoyée par le naoqi réel,
        # on copie tous les éléments de la partie gauche pour les coller et remplacer ceux de
        # la partie droite.
        # Données : [angleMin, angleMax, vitesseMin, vitesseMax]
        self.limitsAll={"T14":[[-2.08566856384 , 2.08566856384 , 8.26797389984 , 1.20000004768 ], [-0.671951770782 , 0.514872133732 , 7.19407272339 , 1.20000004768 ],
                               [-2.08566856384 , 2.08566856384 , 8.26797389984 , 1.20000004768 ], [-0.314159274101 , 1.32645022869 , 7.19407272339 , 1.20000004768 ], [-2.08566856384 , 2.08566856384 , 8.26797389984 , 1.20000004768 ], [-1.54461634159 , -0.0349065847695 , 7.19407272339 , 1.20000004768 ], [-1.82386910915 , 1.82386910915 , 24.6229305267 , 0.759999990463 ], [0.0 , 1.0 , 8.32999992371 , 0.550000011921 ],
                               [-2.08566856384 , 2.08566856384 , 8.26797389984 , 1.20000004768 ], [1.32645022869, -0.314159274101 , 7.19407272339 , 1.20000004768 ], [2.08566856384 , -2.08566856384 , 8.26797389984 , 1.20000004768 ], [ -0.0349065847695, -1.54461634159 , 7.19407272339 , 1.20000004768 ], [1.82386910915 , -1.82386910915 , 24.6229305267 , 0.759999990463 ], [0.0 , 1.0 , 8.32999992371 , 0.550000011921 ]],

                        "H21":[[-2.0856685638427734, 2.0856685638427734, 8.267973899841309, 1.2000000476837158], [-0.6719517707824707, 0.514872133731842, 7.194072723388672, 1.2000000476837158],
                               [-2.0856685638427734, 2.0856685638427734, 8.267973899841309, 1.2000000476837158], [-0.3141592741012573, 1.326450228691101, 7.194072723388672, 1.2000000476837158], [-2.0856685638427734, 2.0856685638427734, 8.267973899841309, 1.2000000476837158], [-1.5446163415908813, -0.03490658476948738, 7.194072723388672, 1.2000000476837158],
                               [-1.1452850103378296, 0.7407177090644836, 4.161737442016602, 3.200000047683716], [-0.37943458557128906, 0.7904596328735352, 4.161737442016602, 3.200000047683716], [-1.535889744758606, 0.4839797914028168, 6.40239143371582, 3.200000047683716], [-0.09232791513204575, 2.112546443939209, 6.40239143371582, 3.200000047683716], [-1.1894419193267822, 0.9225810170173645, 6.40239143371582, 3.200000047683716], [-0.3977605402469635, 0.7689920663833618, 4.161737442016602, 3.200000047683716],
                               [-1.1452850103378296, 0.7407177090644836, 4.161737442016602, 3.200000047683716], [-0.37943458557128906, 0.7904596328735352, 4.161737442016602, 3.200000047683716], [-1.535889744758606, 0.4839797914028168, 6.40239143371582, 3.200000047683716], [-0.09232791513204575, 2.112546443939209, 6.40239143371582, 3.200000047683716], [-1.1894419193267822, 0.9225810170173645, 6.40239143371582, 3.200000047683716], [-0.3977605402469635, 0.7689920663833618, 4.161737442016602, 3.200000047683716],
                               [-2.0856685638427734, 2.0856685638427734, 8.267973899841309, 1.2000000476837158], [-0.3141592741012573, 1.326450228691101, 7.194072723388672, 1.2000000476837158], [-2.0856685638427734, 2.0856685638427734, 8.267973899841309, 1.2000000476837158], [-1.5446163415908813, -0.03490658476948738, 7.194072723388672, 1.2000000476837158]],
                        #not tested
                        "H25":[[-2.08566856384 , 2.08566856384 , 8.26797389984 , 1.20000004768 ], [-0.671951770782 , 0.514872133732 , 7.19407272339 , 1.20000004768 ],
                               [-2.08566856384 , 2.08566856384 , 8.26797389984 , 1.20000004768 ], [-0.314159274101 , 1.32645022869 , 7.19407272339 , 1.20000004768 ], [-2.08566856384 , 2.08566856384 , 8.26797389984 , 1.20000004768 ], [-1.54461634159 , -0.0349065847695 , 7.19407272339 , 1.20000004768 ], [-1.82386910915 , 1.82386910915 , 24.6229305267 , 0.759999990463 ], [0.0 , 1.0 , 8.32999992371 , 0.550000011921 ],
                               [-1.1452850103378296, 0.7407177090644836, 4.161737442016602, 3.200000047683716], [-0.37943458557128906, 0.7904596328735352, 4.161737442016602, 3.200000047683716], [-1.535889744758606, 0.4839797914028168, 6.40239143371582, 3.200000047683716], [-0.09232791513204575, 2.112546443939209, 6.40239143371582, 3.200000047683716], [-1.1894419193267822, 0.9225810170173645, 6.40239143371582, 3.200000047683716], [-0.3977605402469635, 0.7689920663833618, 4.161737442016602, 3.200000047683716],
                               [-1.1452850103378296, 0.7407177090644836, 4.161737442016602, 3.200000047683716], [-0.37943458557128906, 0.7904596328735352, 4.161737442016602, 3.200000047683716], [-1.535889744758606, 0.4839797914028168, 6.40239143371582, 3.200000047683716], [-0.09232791513204575, 2.112546443939209, 6.40239143371582, 3.200000047683716], [-1.1894419193267822, 0.9225810170173645, 6.40239143371582, 3.200000047683716], [-0.3977605402469635, 0.7689920663833618, 4.161737442016602, 3.200000047683716],
                               [-2.08566856384 , 2.08566856384 , 8.26797389984 , 1.20000004768 ], [-0.314159274101 , 1.32645022869 , 7.19407272339 , 1.20000004768 ], [-2.08566856384 , 2.08566856384 , 8.26797389984 , 1.20000004768 ], [-1.54461634159 , -0.0349065847695 , 7.19407272339 , 1.20000004768 ], [-1.82386910915 , 1.82386910915 , 24.6229305267 , 0.759999990463 ], [0.0 , 1.0 , 8.32999992371 , 0.550000011921 ]]
                        }

        self.ledMnM={'RightFaceLed8':"eye1D", 'RightFaceLed7':"eye2D",
                     'RightFaceLed6':"eye3D", 'RightFaceLed5':"eye4D",
                     'RightFaceLed4':"eye5D", 'RightFaceLed3':"eye6D",
                     'RightFaceLed2':"eye7D", 'RightFaceLed1':"eye8D",

                     'LeftFaceLed8':"eye8G", 'LeftFaceLed7':"eye7G",
                     'LeftFaceLed6':"eye6G", 'LeftFaceLed5':"eye5G",
                     'LeftFaceLed4':"eye4G", 'LeftFaceLed3':"eye3G",
                     'LeftFaceLed2':"eye2G", 'LeftFaceLed1':"eye1G"
                     }

        self.ledIntensities={'RightFaceLed1':[.0,.0,.0],
                             'RightFaceLed2':[.0,.0,.0],
                             'RightFaceLed3':[.0,.0,.0],
                             'RightFaceLed4':[.0,.0,.0],
                             'RightFaceLed5':[.0,.0,.0],
                             'RightFaceLed6':[.0,.0,.0],
                             'RightFaceLed7':[.0,.0,.0],
                             'RightFaceLed8':[.0,.0,.0],
                             'LeftFaceLed8':[.0,.0,.0],
                             'LeftFaceLed7':[.0,.0,.0],
                             'LeftFaceLed6':[.0,.0,.0],
                             'LeftFaceLed5':[.0,.0,.0],
                             'LeftFaceLed4':[.0,.0,.0],
                             'LeftFaceLed3':[.0,.0,.0],
                             'LeftFaceLed2':[.0,.0,.0],
                             'LeftFaceLed1':[.0,.0,.0]}

        self.ledColors={'RightFaceLed1': ['Face/Led/Red/Right/0Deg/Actuator/Value', 'Face/Led/Green/Right/0Deg/Actuator/Value', 'Face/Led/Blue/Right/0Deg/Actuator/Value'], 'RightFaceLed3': ['Face/Led/Red/Right/90Deg/Actuator/Value', 'Face/Led/Green/Right/90Deg/Actuator/Value', 'Face/Led/Blue/Right/90Deg/Actuator/Value'], 'RightFaceLed2': ['Face/Led/Red/Right/45Deg/Actuator/Value', 'Face/Led/Green/Right/45Deg/Actuator/Value', 'Face/Led/Blue/Right/45Deg/Actuator/Value'], 'RightFaceLed5': ['Face/Led/Red/Right/180Deg/Actuator/Value', 'Face/Led/Green/Right/180Deg/Actuator/Value', 'Face/Led/Blue/Right/180Deg/Actuator/Value'], 'RightFaceLed4': ['Face/Led/Red/Right/135Deg/Actuator/Value', 'Face/Led/Green/Right/135Deg/Actuator/Value', 'Face/Led/Blue/Right/135Deg/Actuator/Value'], 'RightFaceLed7': ['Face/Led/Red/Right/270Deg/Actuator/Value', 'Face/Led/Green/Right/270Deg/Actuator/Value', 'Face/Led/Blue/Right/270Deg/Actuator/Value'], 'RightFaceLed6': ['Face/Led/Red/Right/225Deg/Actuator/Value', 'Face/Led/Green/Right/225Deg/Actuator/Value', 'Face/Led/Blue/Right/225Deg/Actuator/Value'], 'RightFaceLed8': ['Face/Led/Red/Right/315Deg/Actuator/Value', 'Face/Led/Green/Right/315Deg/Actuator/Value', 'Face/Led/Blue/Right/315Deg/Actuator/Value'], 'LeftFaceLed1': ['Face/Led/Red/Left/0Deg/Actuator/Value', 'Face/Led/Green/Left/0Deg/Actuator/Value', 'Face/Led/Blue/Left/0Deg/Actuator/Value'], 'LeftFaceLed3': ['Face/Led/Red/Left/90Deg/Actuator/Value', 'Face/Led/Green/Left/90Deg/Actuator/Value', 'Face/Led/Blue/Left/90Deg/Actuator/Value'], 'LeftFaceLed2': ['Face/Led/Red/Left/45Deg/Actuator/Value', 'Face/Led/Green/Left/45Deg/Actuator/Value', 'Face/Led/Blue/Left/45Deg/Actuator/Value'], 'LeftFaceLed5': ['Face/Led/Red/Left/180Deg/Actuator/Value', 'Face/Led/Green/Left/180Deg/Actuator/Value', 'Face/Led/Blue/Left/180Deg/Actuator/Value'], 'LeftFaceLed4': ['Face/Led/Red/Left/135Deg/Actuator/Value', 'Face/Led/Green/Left/135Deg/Actuator/Value', 'Face/Led/Blue/Left/135Deg/Actuator/Value'], 'LeftFaceLed7': ['Face/Led/Red/Left/270Deg/Actuator/Value', 'Face/Led/Green/Left/270Deg/Actuator/Value', 'Face/Led/Blue/Left/270Deg/Actuator/Value'], 'LeftFaceLed6': ['Face/Led/Red/Left/225Deg/Actuator/Value', 'Face/Led/Green/Left/225Deg/Actuator/Value', 'Face/Led/Blue/Left/225Deg/Actuator/Value'], 'LeftFaceLed8': ['Face/Led/Red/Left/315Deg/Actuator/Value', 'Face/Led/Green/Left/315Deg/Actuator/Value', 'Face/Led/Blue/Left/315Deg/Actuator/Value']}

        self.language="french"
        self.volume=0
        self.listLimits={"Body":[]}

        self.animation = Animation()

    #test ok
    @staticmethod
    def associateVirtualRobot(nao):
        ALProxy.virtualNao=nao

    @staticmethod
    def associateStaticNao(nao):
        ALProxy.staticNao=nao

    @staticmethod
    def getFunction(functionName):
        functionsByName={
            "onWordRecognized":ALProxy.staticNao._wordRecognizedEvent,
            "onPictureDetected":ALProxy.staticNao._pictureDetectedEvent,
            "onFaceDetected":ALProxy.staticNao._faceDetectedEvent,
            "onTactileDetected":ALProxy.staticNao._tactileEvent,
            "onWordDetected":ALProxy.staticNao._speechDetectedEvent}
        return functionsByName[functionName]

    @staticmethod
    def eventCall(function, args):
        ##try:
        if function != "onWordDetected":
            ALProxy.getFunction(function)(*args)
            return 1
        else :
            for mot in args[0].split():
                if mot.strip() in ALProxy.vocabList :
                    ALProxy.getFunction(function)()
                    return 1
                else :
                    print(mot.strip()+" is not in Vocabulary list")
        ##except:
        ##    print("Pas de AbstractNaoEvenement instancié")
        return 0

    @staticmethod
    def setType(robot="T14"):
        ALProxy.typeRobot=robot
        #reconstruction des membres
        ALProxy.membres={}
        for a in range(len(ALProxy.jointsAll[robot])):
            ALProxy.membres[a]=ALProxy.jointsAll[robot][a]

        if robot=="T14":
            ALProxy.membresVirtual={0:"teteG2", 1:"teteG0",#ok
                                2:"bicepsG0", 3:"bicepsG2", 4:"coudeG1", 5:"coudeG2", 6:"mainG1", 7:"doigt1G0",#ok
                                8:"bicepsD0", 9:"bicepsD2", 10:"coudeD1", 11:"coudeD2", 12:"mainD1", 13:"doigt1D0"#ok
                                }
        elif robot=="H21":
            ALProxy.membresVirtual={0:"teteG2", 1:"teteG0",#ok
                                2:"bicepsG0", 3:"bicepsG2", 4:"coudeG1", 5:"coudeG2",#ok
                                6:'hancheG1', 7:'cuisseG1', 8:'cuisseG0', 9:'molletG0', 10:'piedG0', 11:'piedG1',
                                12:'hancheD1', 13:'cuisseD1', 14:'cuisseD0', 15:'molletD0', 16:'piedD0', 17:'piedD1',
                                18:"bicepsD0", 19:"bicepsD2",20:"coudeD1",21:"coudeD2"
                                }
        elif robot=="H25":
            ALProxy.membresVirtual={0:"teteG2", 1:"teteG0",#ok
                                2:"bicepsG0", 3:"bicepsG2", 4:"coudeG1", 5:"coudeG2", 6:"mainG1", 7:"doigt1G0",
                                8:'hancheG1', 9:'cuisseG1', 10:'cuisseG0', 11:'molletG0', 12:'piedG0', 13:'piedG1',
                                14:'hancheD1', 15:'cuisseD1', 16:'cuisseD0', 17:'molletD0', 18:'piedD0', 19:'piedD1',
                                20:"bicepsD0", 21:"bicepsD2", 22:"coudeD1", 23:"coudeD2", 24:"mainD1", 25:"doigt1D0"
                                }


    @staticmethod
    def getKeys():
        l = []
        Virtual={0:"teteG2", 1:"teteG0",#ok
                                2:"bicepsG0", 3:"bicepsG2", 4:"coudeG1", 5:"coudeG2", 6:"mainG1", 7:"doigt1G0",
                                8:'hancheG1', 9:'cuisseG1', 10:'cuisseG0', 11:'molletG0', 12:'piedG0', 13:'piedG1',
                                14:'hancheD1', 15:'cuisseD1', 16:'cuisseD0', 17:'molletD0', 18:'piedD0', 19:'piedD1',
                                20:"bicepsD0", 21:"bicepsD2", 22:"coudeD1", 23:"coudeD2", 24:"mainD1", 25:"doigt1D0"
                                }
        for a in range(len(Virtual)):
            l.append(Virtual[a])
        return l


    @staticmethod
    def setParallelism(paral):
        ALProxy.parallelism = paral

    #test ok
    def setLanguage(self, language):
        self.language=language

    #test ok
    def getLanguage(self):
        return self.language

    #test ok
    def say(self, text):
        #print("NAO dit : "+str(text))
        self.virtualNao.addSpeaking(text)
        if not ALProxy.parallelism:
            self.timePerCharacter=0.1
            TimerT.sleep(len(text)*self.timePerCharacter)

    def setVolume(self, value):
        self.volume=value

    def getVolume(self):
        return self.volume

    #test ok
    def getJointNames(self, part="Body"):
        return self.jointsAll[self.typeRobot]

    #test ok
    def getLimits(self, part="Body"):
        return self.limitsAll[self.typeRobot]

    #test ok
    def getStiffnesses(self, part="Body"):
        r=[]
        for a in self.membres.keys():
            num=self.getNumberFromName(a)
            nom=self.membresVirtual[num][:-1]
            r.append(self.virtualNao.getMembre(nom).stiffness)
        return r

    def getStiffness(self, numeroMoteur):
            print("not implemented")

    def setStiffnesses(self, part="Body", value=1):
        names=self.getNamesFromPart(part)
        for a in names:
            self.virtualNao.getMembre(a[:-1]).stiffness=value

    def setStiffness(self, numeroMoteur, taux):
            print("not implemented")

    def getAngles(self, part="Body", isStg=True):
        return [0,0,0,0]

    #a verifier
    def getMinAngle(self, numero):
        return self.limitsAll[self.typeRobot][0]

    #a verifier
    def getMaxAngle(self, numero):
        return self.limitsAll[self.typeRobot][1]

    def angleInterpolation(self, name, motorAngle, time, isAbsolute):
        num=self.getNumberFromName(name)
        nom=self.membresVirtual[num][:-1]
        n=int(self.membresVirtual[num][-1])
        #print(self.virtualNao.getMembre(nom).rotate, nom)
        if nom[:5] == "doigt":
            self.virtualNao.getMembre("doigt3"+nom[-1]).setAngleFromPercent(0,100-motorAngle)
            self.virtualNao.getMembre("doigt2"+nom[-1]).setAngleFromPercent(0,100-motorAngle)
            self.virtualNao.getMembre("doigt1"+nom[-1]).setAngleFromPercent(0,100)#=fixe
        else :
            self.virtualNao.getMembre(nom).setAngle(n,motorAngle/3.14*180,time)

        if not ALProxy.parallelism:
            TimerT.sleep(time)

    def getAnimationData(self):
        names = self.animation.getNames()
        values = self.animation.getValues()
        times = self.animation.getTimes()
        return names, angles, times

    def addMotionAnimation(self, numeroMoteur, position, temps):
        self.animation.addValue(self.getNameFromNumber(numeroMoteur), position, temps);

    def resetAnimation(self):
        self.animation.reset();

    #test OK
    def playAnimation(self):
        names = self.animation.getNames();
        values = self.animation.getValues();
        times = self.animation.getTimes();
        threads = [];

        i = 0;
        for name in names :
            valuesTab = values[i];
            timesTab = times[i];
            thread = threading.Thread(None, self.__playMotorAnimation, None, (name, valuesTab, timesTab), {});
            threads.append(thread);
            i=i+1;

        for thread in threads:
            thread.start();

    #test OK
    def __playMotorAnimation(self, name, values, times):
        assert len(values) == len(times);
        durations = self.__getDurations(times);
        for i in range(len(times)):
            duration = durations[i];
            motorAngle = values[i];
            time.sleep(duration);
            self.angleInterpolation(name, motorAngle, duration, True);

    #test OK
    def __getDurations(self, timesTab):
        durationTab = [];
        previousTime = -1;

        if len(timesTab)>0:
            previousTime = timesTab[0];
            durationTab.append(previousTime);
            for i in range(1, len(timesTab)):
                currentTime = timesTab[i];
                if currentTime <= previousTime :
                    string = "Times must be inscreasing at index %s : %s" %(i, timesTab[i])
                    raise Exception("TimeError", string);
                else:
                    duration = currentTime - previousTime;
                    previousTime = currentTime;
                    durationTab.append(duration);

        return durationTab;

    def getNumberFromName(self, name):
        num=0
        for x in self.membres:
            if name==self.membres[x]:
                return x
        print("error numberName")
        return num

    def getNameFromNumber(self, number):
        return self.membres[number]

    def getNamesFromPart(self, part):
        if part.lower()=="body":
            return self.membresVirtual.values()
        for x in self.membres.keys():
            if self.membres[x]==part:
                return [self.membresVirtual[x]]
        print("error Names part", part)
        return []


    def getAvailableLanguages(self):
        return ["fr"]

    ###############" LED

    def getListFromName(self, name):
        result=[]
        if name in self.ledMnM.keys():
            return [self.ledMnM[name]]
        elif "Right" in name:
            for x in self.ledMnM.keys():
                if "Right" in x: result.append(self.ledMnM[x])
        elif "Left" in name:
            for x in self.ledMnM.keys():
                if "Left" in x: result.append(self.ledMnM[x])
        else:result=self.ledMnM.values()
        return result

    def getListNamesFromName(self, name):
        result=[]
        if name in self.ledMnM.keys():
            return [name]
        elif "Right" in name:
            for x in self.ledMnM.keys():
                if "Right" in x: result.append(x)
        elif "Left" in name:
            for x in self.ledMnM.keys():
                if "Left" in x: result.append(x)
        else:result=self.ledMnM.values()
        return result

    def setIntensity(self, name, intensity):
        #print(intensity)
        if name in self.ledMnM.keys():
            a=self.ledMnM[name]
            color=[intensity,intensity,intensity]
            self.ledIntensities[name]=color
        else:
            for b in self.ledColors.keys():
                if name in self.ledColors[b]:
                    #detection de la composante de couleur
                    pos=self.ledColors[b].index(name)
                    name=b
                    a=self.ledMnM[b]
                    self.ledIntensities[name][pos]=intensity
                    break
        self.virtualNao.setEye(self.ledIntensities[name],a)

    def getIntensity(self,name):
        return self.ledIntensities[name]

    def on(self,name):
        a=self.getListFromName(name)
        self.virtualNao.setEyes(1,a)

    def off(self,name):
        a=self.getListFromName(name)
        self.virtualNao.setEyes(0,a)

    def fade(self,name, intensity, duration):
        pass

    def fadeRGB (self, name, color, duration):
        pass

    def setWordListAsVocabulary(self, vocabList):
        ALProxy.vocabList = vocabList

    def startSpeechRecognition(self):
        pass
    ### SOUND
    def playFile(self, fichier):
        self.virtualNao.addSinging(fichier,5)
        if not ALProxy.parallelism:
            self.timePerCharacter=1
            TimerT.sleep(5)

    def stop(self):
        pass

    def playSine(self, frequence, gain, pan, duree):
        self.virtualNao.addSinging(" frequence : "+str(frequence), duree)
        if not ALProxy.parallelism:
            self.timePerCharacter=1
            TimerT.sleep(duree)
Exemplo n.º 3
0
class Button():

    def __init__(self, text, on_click_deferred, on_click_return_value, window_values, surface, hover_animation_surface = None, mouse_down_animation = None, spritesheet_dimensions=(2, 2), font_size='big'): #spritesheet for potential fancy animations later
        self.x, self.y, self.w, self.h = window_values

        self.font_size = font_size

        self.text = text

        self.surface = surface

        self.text_label = render_text(text, (0,0,0), font_size)
        self.get_text_draw_coords()

        self.on_click_deferred = on_click_deferred
        self.on_click_return_value = on_click_return_value

        hover_animation_surface = None

        if hover_animation_surface:
            self.on_hover_animation = Animation(TileSheet(hover_animation_surface, 292, 120, spritesheet_dimensions[0], spritesheet_dimensions[1]), 10, 4)
        else:
            self.on_hover_animation = None

        if mouse_down_animation:
            self.mouse_down_animation = Animation(TileSheet(mouse_down_animation, 292, 120, 1, 1), 10, 1)
        else:
            self.mouse_down_animation = None

        self.on_hover_animation_active = False
        self.mouse_down_animation_active = False

    def on_click(self):
        if self.mouse_down_animation:
            self.mouse_down_animation.done = True
        self.on_click_deferred.value = self.on_click_return_value

    def on_mouse_down(self):
        if not self.mouse_down_animation_active and self.mouse_down_animation:
            self.mouse_down_animation_active = True

    def on_hover(self):
        if not self.on_hover_animation_active and self.on_hover_animation:
            self.on_hover_animation_active = True

    def on_hover_off(self):
        self.mouse_down_animation_active = False

    def update(self, time=1):
        if self.mouse_down_animation_active:
            if self.mouse_down_animation.done:
                self.mouse_down_animation.done = False
                self.mouse_down_animation.reset()
                #self.mouse_down_animation_active = False

            self.mouse_down_animation.update(time)

        elif self.on_hover_animation_active:
            if self.on_hover_animation.done:
                self.on_hover_animation.done = False
                self.on_hover_animation.reset()
                self.on_hover_animation_active = False

            self.on_hover_animation.update(time)


    def draw(self):
        if self.mouse_down_animation_active:
            self.mouse_down_animation.draw(screen, self.x, self.y)
        elif self.on_hover_animation_active:
            self.on_hover_animation.draw(screen, self.x, self.y)
        else:
            screen.blit(self.surface, (self.x, self.y))

        screen.blit(self.text_label, (self.text_x, self.text_y))


    def mouse_over_button(self, mouse_pos):
        return Utils.point_in_rect(mouse_pos, (self.x, self.y, self.w, self.h))

    def get_text_draw_coords(self):
        if self.font_size == "big":
            text_w = font.size(self.text)[0]
            text_h = font.size(self.text)[1]
        elif self.font_size == "small":
            text_w = font_small.size(self.text)[0]
            text_h = font_small.size(self.text)[1]

        self.text_x = self.x + 0.5 * self.w - 0.5 * text_w
        self.text_y = self.y + 0.5 * self.h - 0.5 * text_h