Demo entry 6324482

hi

   

Submitted by anonymous on Nov 19, 2016 at 00:02
Language: Python. Code size: 13.2 kB.

func main(threshold, #threshold determining whether two positions are nearly the same
          groundTreshold, #threshold for determining whether object is on the ground
          ballPositiveTrainingImages, # rest of the arguments are training images
          ballNegativeTrainingImages,
          playerPositiveTrainingImages,
          playerNegativeTrainingImages,
          polePositiveTrainingImages,
          poleNegativeTrainingImages,
          robotShovelPositiveTrainingImages,
          robotShovelNegativeTrainingImages):

    # train CNN
    cnn = trainCNN(
             ballPositiveTrainingImages,
             ballNegativeTrainingImages,
             playerPositiveTrainingImages,
             playerNegativeTrainingImages,
             polePositiveTrainingImages,
             poleNegativeTrainingImages,
             robotShovelPositiveTrainingImages,
             robotShovelNegativeTrainingImages)
    
    svm = trainsvm(fenceBoundaryPositiveTrainingImage, fenceBoundaryNegativeTrainingImages,
                   courtBoundaryPositiveTrainingImage, courtBoundaryNegativeTrainingImages)

    courtBoundary = [];
    fenceBoundary = [];
    poles = [];

    # take stereo images of entire scene (in 5 meter squared area intervals)
    # and store detected objects in variables
    while not all directions tried:
        pos = getNext5MeterSquaredArea();
        faceDirection(pos.x, pos.y, pos.z)
        
        stereo = getStereo();

        # detect court boundaries, fence boundaries, tennis net's poles,
        # and robot's shovel.
        # get the world coordinates of their bounding boxes.
        # check if the object has already been stored, if not:
        # append it to the list of objects
        courtBoundaryDetect = detectCourtBoundary(svm, stereo);
        if courtBoundaryDetect:
            worldCoord = worldCoordinates(courtBoundaryDetect, K, r, t, stereo);
            if isDistinctObject(worldCoord, courtBoundary):
                countBoundary.append(worldCoord);
        fenceBoundaryDetect = detectFenceBoundary(svm, stereo);
        if fenceBoundaryDetect:
            worldCoord = worldCoordinates(fenceBoundaryDetect, K, r, t, stereo);
            if isDistinctObject(worldCoord, fenceBoundary):
                fenceBoundary.append(worldCoord);
        polesDetect = detectPoles(cnn, stereo);
        if polesDetect:
            worldCoord = worldCoordinates(polesDetect, K, r, t, stereo);
            if isDistinctObject(worldCoord, poles):
                poles.append(worldCoord);
        shovelDetect = detectShovel(cnn, stereo);
        if !shovel & shovelDetect:
            shovel = centerOfMass(shovelDetect, K, r, t, stereo);
            

    # detect ball while game has not ended
    while game has not ended:
        prevBall = ball or NIL if ball has not been initialized;
        ball = NIL;

        # take stereo images and detect ball
        # for all faceDirections until ball found
        # or until all directions have been tried
        getNext5MeterSquaredArea('restart');
        do:
            pos = getNext5MeterSquaredArea();
            faceDirection(pos.x, pos.y, pos.z);

            stereo = getStereo();
            ball = detectBall(cnn, stereo);

            if ball:
                # check if ball is on ground, stationary, and within the
                # court fence. If so, fetch the ball and deliver to the
                # server.
                ball = centreOfMass(courtBoundaryDetect, K, r, t, stereo);
                if ! (distance(ball, prevBall) < threshold &
                ball.y < groundTreshold &
                isWithinBounds(fenceBoundary, ball)):
                    # for each sub-path along path to ball,
                    # and check for obstacles. If there are obstacles,
                    # recompute robot's trajectory
                    ball = NIL;
                
        while (!ball and not all directions tried);  

        # ball was not detected, restart loop
        if !ball:
            continue;

        # Detect obstacles: pole, fence, and non-server player if present in the scene,
        # and if there are obstacles, recalculate trajectory 
        # so that collisions are avoided.
        # determine whether a player is the server based on the side
        # of the court they are on.
        [player1, player2] = detectPlayers(cnn, stereo);

        if player1:
            player1Center = centerOfMass(player1, K, r, t, stereo)
        if player2:
            player2Center = centerOfMass(player2, K, r, t, stereo)

        serverPlayer = center of mass of server player if exist otherwise, NIL
        nonServerPlayer = bounding box of non server player if exist otherwise, NIL

        obstacles = detectObstacles(poles, fenceBoundary,
                                                    nonServerPlayer, stereo, x, y, z, r, t);

        paths = computeShortestPath(poles, fenceBoundary, nonServerPlayer, 
            serverPlayer.x - 30 centimeters, 0, serverPlayer.z - 30 centimeters);
    
        for path in paths:
            faceDirection(path.x, path.y, path.z);
            stereo = getStereo();

            # detect obstacles
            [player1, player2] = detectPlayers(cnn, stereo);

            if player1:
                player1Center = centerOfMass(player1, K, r, t, stereo)
            if player2:
                player2Center = centerOfMass(player2, K, r, t, stereo)

            serverPlayer = center of mass of server player if exist otherwise, NIL
            nonServerPlayer = bounding box of non server player if exist otherwise, NIL

            obstacles = detectObstacles(poles, fenceBoundary,
                                                        nonServerPlayer, stereo, x, y, z, r, t);
                                            
            # if there are obstacles recompute robot trajectory
            if obstacles:
                paths = computeShortestPath(obstacles, ball.x, 0, ball.z);
            else: # continue moving along path as there are no obstacles
                moveToLocation(path.x, path.y, path.z);

        # bring ball to player
        deliverToPlayer(cnn, K, r, t, shovel, poles, fenceBoundary);

    # game has ended, victory dance
    pos = currentRobotLocation(r, t):
    victoryDance(pos.x, pos.y, pos.z);

func isWithinBounds(boundingBox, obj):
    # return true iff obj is within boundaries of boundingBox
    
func centreOfMass(boundingBox, K, r, t, stereo):
    # Compute centre of mass in world coordinates of boundingBox
    # Use fundamental matrix, K, r, and t to do so.

func positionOutsideCourtBoundaries():
    # Compute a position outside court boundaries
    # so that robot can move there an not interfere with game.

function getStereo():
    # Take stereo image of current scene.

func deliverToPlayer(cnn, K, r, t, shovel, poles, fenceBoundary)
    # deliver ball to player
    prevPlayer = serverPlayer or NIL if player has not been initialized;
    serverPlayer = NIL;
    
    # take stereo images and detect ball
    # for various faceDirections until ball found
    # or until all directions have been tried
    getNext5MeterSquaredArea('restart');
    while !serverPlayer:
        pos = getNext5MeterSquaredArea();
        faceDirection(pos.x, pos.y, pos.z);

        stereo = getStereo();

        [player1, player2] = detectPlayers(cnn, stereo):
        if player1:
            player1Center = centerOfMass(player1, K, r, t, stereo);
        if player2:
            player2Center = centerOfMass(player2, K, r, t, stereo);

        If either player1 or player2 is the server player,
        (check by comparing its world coordinates to courtBoundaries,
        to see if the player is on the serving side of the court):
            serverPlayer = center of mass of server player
            nonServerPlayer = bounding box of non server player

    # ignore the y coordinate since robot doesn't fly.
    # go to a destination of -30 centimeters of the player position because don't want to collide with player. 
    obstacles = detectObstacles(poles, fenceBoundary,
            nonServerPlayer, stereo, x, y, z, r, t);
    paths = computeShortestPath(poles, fenceBoundary, nonServerPlayer,
                                serverPlayer.x - 30  centimeters, 0, serverPlayer.z - 30 centimeters);

    # for each sub-path along path to server player,
    # check for obstacles and whether the server player
    # has moved
    for path in paths:
        stereo = getStereo();
                
        [player1, player2] = detectPlayers(cnn, stereo):

        if player1:
            player1Center = centerOfMass(player1, K, r, t, stereo);
        if player2:
            player2Center = centerOfMass(player2, K, r, t, stereo);

        # determine whether the non-server player collides with the path,
        # and if so, recalculate trajectory so that collision can be avoided.
        serverPlayer = center of mass of server player
        nonServerPlayer = bounding box of non server player

        obstacles = detectObstacles(poles, fenceBoundary,
            nonServerPlayer, stereo, x, y, z, r, t);
                
        # if server player has moved substantially or if there are obstacles, recompute
        # robot trajectory
        if obstacles or distance(prevServerPlayer, serverPlayer) > 100 centimeters:
            paths = computeShortestPath(obstacles, serverPlayer.x - 30  centimeters, 0,
                                        serverPlayer.z - 30 centimeters);
         else: # continue moving
            moveToLocation(path.x, path.y, path.z);

    faceDirection(shovel.x, shovel.y, shovel.z);

    # player has not yet grabbed the ball
    do:
      ball = detectBall(cnn, stereo);
    while ball;

    # move to a position outside of court boundaries so robot does not interfere with game
    pos = positionOutsideCourtBoundaries():
    faceDirection(pos.x, pos.y, pos.z);
    obstacles = detectObstacles(poles, fenceBoundary,
                                                    nonServerPlayer, stereo, x, y, z, r, t);
    paths = computeShortestPath(obstacles, pos.x, pos.y, pos.z);
    for path in paths:
        stereo = getStereo();
                
        [player1, player2] = detectPlayers(cnn, stereo):

        if player1:
            player1Center = centerOfMass(player1, K, r, t, stereo);
        if player2:
            player2Center = centerOfMass(player2, K, r, t, stereo);

        # determine whether the non-server player collides with the path,
        # and if so, recalculate trajectory so that collision can be avoided.
        serverPlayer = center of mass of server player
        nonServerPlayer = bounding box of non server player

        obstacles = detectObstacles(poles, fenceBoundary,
            nonServerPlayer, stereo, x, y, z, r, t);
                
        # if server player has moved substantially or if there are obstacles, recompute
        # robot trajectory
        if obstacles or distance(prevServerPlayer, serverPlayer) > 100 centimeters:
            paths = computeShortestPath(obstacles, x, 0, z);
         else: # continue moving
            moveToLocation(path.x, path.y, path.z);


isDistinctObject(objects, obj):
    # Return true iff obj is not already in objects list,
    # by determining whether its location is the same or close to,
    # based on a threshold, to those in objects.
    
computeShortestPath(obstacles, x, y, z);
    # Compute the shortest path from current robot location
    # to x, y, z.
    # The returned path will be a list of positions
    # in 10 distance cm increments that will eventually
    # reach position (x, y, z), while avoiding
    # the list of obstacles in variable obstacles.

getNext5MeterSquaredArea(reset='no'): #default value of reset is "no"
    # Returns the centre of mass of the next unvisited
    # 5 meter square area.
    # so that robot can scan the whole scene.
    # if reset == 'restart', the all visited areas will be
    # reset to unvisited, and first area will start
    # from current robot position. Other areas returned
    # will just be the next closest unvisited area.
    
worldCoordinates(boundingBox, K, r, t, stereo):
    # Get world coordinates of the boundingBox using
    # fundamental matrix, K, r, t.

currentRobotLocation(r, t):
    # Get the location of robot based on r and t.

detectObstacles(poles, fenceBoundary, nonServerPlayer, stereo, x, y, z, r, t):
    # Detect and return a list of obstacles (i.e. the net poles, players, fence)
    # between shortest path between current robot location to (x,y,z).
    obstacles = [];
    currPos = currentRobotoLocation(r, t);
    
    Append to obstacles the bounding box of any net poles that
    collide with the shortest path to destination (x, y, z) by
    checking whether the boudning box of the netpole collide
    with the robot bounding box along the shortest path.
    
    Append to obstacles the bounding box of any areas of the fence
    that will collides with the robot along the shortest path.
    
    Append to obstacles the bounding box of the non server player
    if the non server player collides with the robot.

    return obstacles;
    

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).