Ejemplo n.º 1
0
class Scene:
    
    #initializes the root of the sceneGraph, empty dictionariies for the Naos and a socket that connects to the server    
    def __init__(self, socket = None):
        if (socket == None):
            self.__socket = Sock("localhost", 3200, None, None)
            self.__socket.start()
        else:
            self.__socket = socket
        self.__root = tree_node.Tree_Node(0)
        self.__naos_left = {}
        self.__naos_right = {}
        self.__idcount = 1
        self.__nodes = [self.__root]
        self.__ball_node = None
        
    #creates and maintains the scenegraph in a loop. Not to be used by the agent, just on stand alone usage (eg. for analyzing)   
    def start(self):
        msg = self.__socket.receive()
        data = parser.parse_sexp(msg)
        #print(data)
        self.createScene(data)
        while True:
            msg = self.__socket.receive()
            data = parser.parse_sexp(msg)
            self.updateScene(data)
            #print(data)
        
    #should be called by the agent before it calls get_position.
    #receives one scene graph message and either creates a new scene graph or updates an existing one.
    def run_cycle(self, data = None):
        if (data == None):
            msg = self.__socket.receive()
            data = parser.parse_sexp(msg)
        header = data[1]
        if (header[0] == "RSG"):
            self.__root = tree_node.Tree_Node(0)
            self.__naos_left = {}
            self.__naos_right = {}
            self.__idcount = 1
            self.__nodes = [self.__root]
            self.__ball_node = None
            self.create_scene(data)
        if (header[0] == "RDS"):
            #print("monitor:" + str(data[0][0][1]))
            self.update_scene(data)
    
    #creates a completely new sceneGraph. Should be called if the server sends (RSG 0 1)
    def create_scene(self, msg):
        header = msg[1]
        graph = msg[2]
        if (header[0] != "RSG"):
            print("Error: Message doesn't contain a complete scene graph")
            return
        if (header[1] != 0 | header[2] != 1):
            print("Wrong scene graph version")
            return
        self.seek_children(self.__root, graph)
        return
    
    # seeks the children of a node and appends them to it. msg should contain all the child-nodes like:
    # [ [nd, [nd]], [nd] , [nd, [nd, [nd]]] ]  (showing just the structure, information is missing)
    def seek_children(self, node, msg):
        for element in msg:
            #print("Rufe fuer " + str(node.getId()) + " einen " + str(element[1]) + " Knoten")
            node.append(self.create_node(element))
        return
    
    # decides which node to create reading the given message. msg should look like:
    # [nd, TRF, [SLT, nx, ny, nz, 0, ox, oy, oz, 0, ax, ay, az, 0, Px, Py, Pz, 1] ]
    def create_node(self, msg):
        if(msg[0] != "nd"):
            print("Error: Message doesn't contain a node")
            return None
        if(msg[1] == "TRF"):
            node = self.create_trans_node(msg[2])
            if (len(msg) > 3):
                #print(str(node.getId()) + " hat noch Kinderknoten...")
                self.seek_children(node, msg[3:])
        if(msg[1] == "Light"):
            node = self.create_light_node(msg[2:])
        if(msg[1] == "SMN"):
            node = self.create_smn_node(msg[2:])
        if(msg[1] == "StaticMesh"):
            node = self.create_static_mesh_node(msg[2:])
            
        return node
        
            
    # creates a transformation node. msg should be a list containing the transformation matrix like:
    # [SLT, nx, ny, nz, 0, ox, oy, oz, 0, ax, ay, az, 0, Px, Py, Pz, 1]
    def create_trans_node(self,msg):
        if(msg[0] != "SLT"):
            print("Error: Not a transformation node")
            return None
        matrix = numpy.array( ((msg[1],msg[5],msg[9],msg[13]),
                               (msg[2],msg[6],msg[10],msg[14]),
                               (msg[3],msg[7],msg[11],msg[15]),
                               (msg[4],msg[8],msg[12],msg[16])) )
        node = trans_node.Trans_Node(self.__idcount, matrix)
        self.__nodes.append(node)
        self.__idcount += 1
        return node
     
    # creates a light node. msg should be a list containing lists full of information concerning the node like:
    # [ [setDiffuse, x, y, z, w], [setAmbient, x, y, z, w], [setSpecular, x, y, z, w] ]        
    def create_light_node(self,msg):
        if((msg[0][0] != "setDiffuse") | (msg[1][0] != "setAmbient") | (msg[2][0] != "setSpecular")):
            print("Error: Not a light node")
            return None
        diffuse = numpy.array([msg[0][1],msg[0][2],msg[0][3],msg[0][4]])
        ambient = numpy.array([msg[1][1],msg[1][2],msg[1][3],msg[1][4]])
        specular = numpy.array([msg[2][1],msg[2][2],msg[2][3],msg[2][4]])
        node = light_node.Light_Node(self.__idcount,diffuse,ambient,specular)
        self.__nodes.append(node)
        self.__idcount += 1
        return node
    
    # creates a smn node. msg should be a list containing lists full of information concerning the node like:
    # [ [load, StdUnitBox], [sSc, 1, 31, ], [sMat, matGrey] ]
    def create_smn_node(self,msg):
        transparent = None
        visible = None
        for element in msg:
            if (element[0] == "load"):
                load = element[1:]
            if (element[0] == "sSc"):
                sSc = element [1:]
            if (element[0] == "sMat"):
                sMat = element[1]
            if (element[0] == "setTransparent"):
                transparent = 1
            if (element[0] == "setVisible"):
                visible = element[1]
        node = smn_node.Smn_Node(self.__idcount, load, sSc, visible, transparent, sMat)
        self.__nodes.append(node)
        self.__idcount += 1
        return node
    
    # creates a static mesh node. msg should be a list containing lists full of information concerning the node like:
    # [ [load, models/rlowerarm.obj], [sSc, 0.05, 0.05, 0.05], [resetMaterials, matLeft, naowhite] ]
    def create_static_mesh_node(self,msg):
        transparent = None
        visible = None
        for element in msg:
            if (element[0] == "load"):
                load = element[1]
            if (element[0] == "sSc"):
                sSc = element [1:]
            if (element[0] == "resetMaterials"):
                reset = element[1:]
            if (element[0] == "setTransparent"):
                transparent = 1
            if (element[0] == "setVisible"):
                visible = element[1]
        node = stat_mesh_node.Stat_Mesh_Node(self.__idcount, load, sSc, visible, transparent, reset)
        self.__nodes.append(node)
        self.__idcount += 1
        if (load == "models/naobody.obj"):
            if(reset[1] == "matLeft"):
                self.__naos_left[reset[0]] = node
            if(reset[1] == "matRight"):
                self.__naos_right[reset[0]] = node
        if(load == "models/soccerball.obj"):
            self.__ball_node = node
        return node
    
    # updates the sceneGraph. Should be called if the server sends (RDS 0 1)
    def update_scene(self, msg):
        header = msg[1]
        graph = msg[2]
        if (header[0] != "RDS"):
            print("Error: Message doesn't a contain partial scene graph")
            return
        if (header[1] != 0 | header[2] != 1):
            print("Wrong scene graph version")
            return
        idcount = self.__idcount
        self.__idcount = 0
        self.update_children(graph)
        self.__idcount += 1
         
        return
    
    # iterates through a list of nodes and updates them if necessary
    def update_children(self, msg):
        for element in msg:
            if (element[0] == "nd"):
                self.__idcount += 1
                if(len(element) > 1):
                    if((element[1] == "StaticMesh") | (element[1] == "SMN")):
                        self.update_children(element[2:])
                    else:
                        self.update_children(element[1:])
            if (element[0] == "SLT"):
                matrix = numpy.array( ((element[1],element[5],element[9],element[13]),
                                       (element[2],element[6],element[10],element[14]),
                                       (element[3],element[7],element[11],element[15]),
                                       (element[4],element[8],element[12],element[16])) )
                self.__nodes[self.__idcount].set_matrix(matrix)
    
    # returns a matrix containing the position and orientation of the nao with id naoID of one team.
    # team needs to be either "left" or "right"
    # NaoID needs to be the number of the nao (the one that is printed on his back)
    # calculates the position by multiplying the transformation matrixes from the root down to the nao
    def get_position(self, team, naoID):
        key = "matNum" + str(naoID)
        if(team == "left"):
            if (self.__naos_left.has_key(key)):
                nao = self.__naos_left[key]
            else:
                return None
        elif(team == "right"):
            if (self.__naos_right.has_key(key)):
                nao = self.__naos_right[key]
            else:
                return None
        else:
            return None
        result = self.calc_position(nao)
        return result
    
    
    # returns a list containing the position and orientation of the nao with id naoID of one team.
    # team needs to be either "left" or "right"
    # NaoID needs to be the number of the nao (the one that is printed on his back)
    def get_position_xy(self, team, naoID):
        result = self.get_position(team, naoID)
        if(result != None):
            return [result[0][3],result[1][3]]
        else:
            return None
    
    # returns the xy-coordinates of the current position of the ball on the pitch    
    def get_ball_position(self):
        if (self.__ball_node):
            result = self.calc_position(self.__ball_node)
        else:
            return None
        return [result[0][3],result[1][3]]
        
        
    # IS CALLED BY get_position methods, YOU DON'T NEED to call it on your own!
    # Use get_position(team, player_id) or get_position_xy(team, player_id) to get a nao's position
    # and get_ball_position() to get the ball's position instead
    # calculates the position of a mobile entity by multiplying the transformation matrixes from the root down to the given node   
    def calc_position(self, node):
        parent = node.get_parent()
        matrices = []
        while (parent != self.__root):
            matrices.append(parent.get_matrix())
            parent = parent.get_parent()
        result = matrices.pop()
        while (len(matrices) > 0):
            result = numpy.dot(result, matrices.pop())
        return result
    
    
    # returns the dictionary containing the nao id : node id pairs of the left team
    def get_naos_left(self):
        return self.__naos_left
    
    # returns the dictionary containing the nao id : node id pairs of the right team
    def get_naos_right(self):
        return self.__naos_right

    # find node with id
    def find_node(self, node_id):
        #for node in self.__nodes:
        #    if node.get_id() == node_id:
        if(self.__nodes[node_id]):
                return self.__nodes[node_id]
        print 'Node not found'
    
    # returns the socket that is used to receive data
    def get_socket(self):
        return self.__socket
Ejemplo n.º 2
0
class IrcClient:

    sock = None
    console = NonBlockConsole()
    host = ''
    port = -1
    username = ''
    password = ''
    channel = ''

#

    def __init__(self, host, port, username, password):
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.sock = Sock(host = host, port = port)

    def get_response(self):
        return self.sock.receive()

    def join_chann(self, chan):
        if self.channel != '':
            self.leave_chann();
        self.sock.deliver("JOIN #{1}\r\n".format(self.username, chan))
        self.channel = chan

    def privmsg(self, msg):
        self.sock.deliver('PRIVMSG #{} :{}\r\n'.format(self.channel, msg))

    def take_input(self):
        values = self.console.get_commands()
        for key in values:
            if key == "quit":
                return 1
            if key == "send password":
                self.send_password()
            if key == "send username":
                self.send_username()
            if key == "send":
                self.sock.deliver(values[key][0])
            if key == "msg":
                self.privmsg(values[key][0])
            if key == "join":
                self.join_chann(values[key][0])
            if key == "pong":
                self.send_pong(values[key][0])
            if key == "enable membership":
                self.enable_membership()

    def shutdown(self):
        self.leave_chann()
        self.sock.end_con()

#

    def send_pong(self, msg):
        self.sock.deliver('PONG :{}\r\n'.format(msg))

    def send_username(self):
        self.sock.deliver('NICK {}\r\n'.format(self.username))

    def send_password(self):
        self.sock.deliver('PASS {}\r\n'.format(self.password))

    def enable_membership(self):
        self.sock.deliver('CAP REQ :twitch.tv/membership\r\n')

    def leave_chann(self):
        self.sock.deliver('PART #{}\r\n'.format(self.channel))