Beispiel #1
0
class Robot:

    def __init__(self, scene, robot_id):
        self.robot_id = robot_id
        self.done = False
        self.signal = None

        self.turtle = RawTurtle(scene.canvas)
        self.scene = scene

        self.scr = self.turtle.getscreen()
        self.scr.setworldcoordinates(0, scene.height, scene.width, 0)

        self.turtle.penup()
        self.reset()

    def reset(self):
        positions = [((15,15), 45), 
                    ((self.scene.width-15, 15), 135),
                    ((15, self.scene.height-15), 315),
                    ((self.scene.height-15, self.scene.width-15), 135)]

        self.turtle.speed(0)
        self.turtle.setpos(positions[self.robot_id][0])
        print positions[self.robot_id]
        self.turtle.left(positions[self.robot_id][1])
        self.turtle.forward(20)
        self.turtle.speed("normal")

    def process(self):
        ##sets variables for checking collision
        turtle_x = self.turtle.xcor()
        turtle_y = self.turtle.ycor()
        t_heading = self.turtle.heading()
        ##variables used to check right
        xr = turtle_x + 15*math.cos(math.radians(self.turtle.heading()+30))
        yr = turtle_y + 15*math.sin(math.radians(self.turtle.heading()+30))
        ##variables used to check left
        xl = turtle_x + 15*math.cos(math.radians(self.turtle.heading()-30))
        yl = turtle_y + 15*math.sin(math.radians(self.turtle.heading()-30))
        ##turns the robot at window boundries
        st_orient = [0, 90, 180, 270]
        bounce_d = 20
        if turtle_x - bounce_d < 0:
            self.turtle.setheading(180-t_heading)
        if turtle_x + bounce_d > self.scene.width:
            self.turtle.setheading(180-t_heading)
        if turtle_y - bounce_d < 0:
            self.turtle.setheading(360-t_heading)
        if turtle_y + bounce_d > self.scene.height:
            self.turtle.setheading(360-t_heading)
        ##check collisions
        left = self.scene.canvas.find_overlapping(xl-1,yl-1,xl+1,yl+1)
        right = self.scene.canvas.find_overlapping(xr-1,yr-1,xr+1,yr+1)
        if self.goal_id in left + right:
            self.done = True
        elif left:
            ##turn away
            self.turtle.left(10)
        elif right:
            ##turn away
            self.turtle.right(10)
        else:
            ##else move forward
            self.turtle.forward(5)
        ##if goal not reached:
        if self.signal:
            if self.signal == "STOP":
                self.signal = None
                return
        elif not self.done:
            self.scene.master.after(20, self.process)
Beispiel #2
0
class Vehicle:
    ###############################################################################################################
    def __init__(self, turtle_window, id_number):
        self.speed_params = [20, 0.2, 6]
        self.turn_parameters = [20]
        self.turtle_window = turtle_window
        self.max_location = self.turtle_window.screen_size / 2 - 10
        self.vehicle = RawTurtle(self.turtle_window.wn)
        self.vehicle.hideturtle()
        self.id_number = id_number
        self.type = random.choice(["crossed", "direct"])
        self.vehicle.shape('turtle')
        self.vehicle.turtlesize(1)
        self.vehicle.penup()
        if self.type == 'crossed':
            self.vehicle.color("red", (1, 0.85, 0.85))
        else:
            self.vehicle.color("blue", (0.85, 0.85, 1))

        self.place()
        self.vehicle.showturtle()

    def place(self):

        self.vehicle.goto(
            random.randint(-self.max_location, self.max_location),
            random.randint(-self.max_location, self.max_location))
        self.vehicle.right(random.randint(0, 360))

    ###############################################################################################################
    def move(self):
        cumulative_speed = 0
        cumulative_turn_amount = 0
        for heat_source in self.turtle_window.heat_source_list:
            input_distance = self.vehicle.distance(
                heat_source.heat_source.pos())
            input_angle = self.vehicle.heading() - self.vehicle.towards(
                heat_source.heat_source.pos())
            sin_angle = math.sin(math.radians(input_angle))
            left_sensor_distance = input_distance - sin_angle
            right_sensor_distance = input_distance + sin_angle
            left_speed, right_speed, combined_speed = self.compute_speed(
                left_sensor_distance, right_sensor_distance)
            turn_amount = self.turn_parameters[0] * (right_speed - left_speed)
            cumulative_speed += combined_speed
            cumulative_turn_amount += turn_amount

        if isinstance(cumulative_turn_amount, complex):
            cumulative_turn_amount = 0

        if cumulative_speed < 0:
            cumulative_speed = 0

        self.vehicle.right(cumulative_turn_amount)
        self.vehicle.forward(cumulative_speed)
        self.check_border_collision()

    def check_border_collision(self):
        if self.vehicle.xcor() > self.max_location:
            self.vehicle.goto(self.max_location, self.vehicle.ycor())
        if self.vehicle.xcor() < -self.max_location:
            self.vehicle.goto(-self.max_location, self.vehicle.ycor())

        if self.vehicle.ycor() > self.max_location:
            self.vehicle.goto(self.vehicle.xcor(), self.max_location)
        if self.vehicle.ycor() < -self.max_location:
            self.vehicle.goto(self.vehicle.xcor(), -self.max_location)

        if self.vehicle.ycor() <= -self.max_location:
            if 0 <= self.vehicle.heading() <= 180:
                turn_angle = 180 - self.vehicle.heading()
                self.vehicle.setheading(turn_angle)
            else:
                turn_angle = abs(360 - self.vehicle.heading())
                self.vehicle.setheading(turn_angle)

        if self.vehicle.ycor() >= self.max_location:
            if 0 <= self.vehicle.heading() <= 180:
                turn_angle = 360 - self.vehicle.heading()
                self.vehicle.setheading(turn_angle)
            else:
                turn_angle = 360 - (self.vehicle.heading() - 180)
                self.vehicle.setheading(turn_angle)

        if self.vehicle.xcor() <= -self.max_location:
            if 0 <= self.vehicle.heading() <= 90:
                turn_angle = 360 - self.vehicle.heading()
                self.vehicle.setheading(turn_angle)
            if 270 < self.vehicle.heading() <= 360:
                turn_angle = 360 - self.vehicle.heading()
                self.vehicle.setheading(turn_angle)
            if 90 < self.vehicle.heading() < 180:
                turn_angle = self.vehicle.heading() - 90
                self.vehicle.setheading(turn_angle)
            if 180 <= self.vehicle.heading() <= 360:
                turn_angle = self.vehicle.heading() + 90
                self.vehicle.setheading(turn_angle)

        if self.vehicle.xcor() >= self.max_location:
            if 0 <= self.vehicle.heading() <= 180:
                turn_angle = self.vehicle.heading() + 90
                self.vehicle.setheading(turn_angle)
            else:
                turn_angle = self.vehicle.heading() - 90
                self.vehicle.setheading(turn_angle)

    ###############################################################################################################
    def compute_speed(self, left_distance, right_distance):
        if self.type == 'crossed':
            left_speed = (
                self.speed_params[0] /
                (right_distance**self.speed_params[1])) - self.speed_params[2]
            right_speed = (
                self.speed_params[0] /
                (left_distance**self.speed_params[1])) - self.speed_params[2]
        else:
            left_speed = (
                self.speed_params[0] /
                (left_distance**self.speed_params[1])) - self.speed_params[2]
            right_speed = (
                self.speed_params[0] /
                (right_distance**self.speed_params[1])) - self.speed_params[2]
        combined_speed = (left_speed + right_speed) / 2
        return left_speed, right_speed, combined_speed
Beispiel #3
0
class Vehicle:
    def __init__(self, turtle_window, id_number):

        # comment here
        self.turtle_window = turtle_window
        self.id_number = id_number

        # comment here
        self.speed_params = [20, 0.2, 6]
        self.turn_parameters = [20]

        # comment here
        self.turtle_object = RawTurtle(self.turtle_window.wn)
        self.turtle_object.hideturtle()
        self.turtle_object.shape('turtle')
        self.turtle_object.turtlesize(1)
        self.turtle_object.penup()

        # comment here
        self.likes_food_dict = {'Sugar': random.choice([True, False])}

        # comment here
        if self.likes_food_dict['Sugar']:
            self.turtle_object.color("red", (1, 0.85, 0.85))
        else:
            self.turtle_object.color("blue", (0.85, 0.85, 1))

        # comment here
        self.place()
        self.turtle_object.showturtle()

    def place(self):
        # comment here
        self.turtle_object.goto(random.randint(-MAX_LOCATION, MAX_LOCATION),
                                random.randint(-MAX_LOCATION, MAX_LOCATION))
        self.turtle_object.right(random.randint(0, 360))

    def move(self):

        # comment here
        cumulative_speed = 0
        cumulative_turn_amount = 0

        # comment here
        for food_source in self.turtle_window.food_source_list:

            # comment here
            likes_food = self.likes_food_dict[food_source.name]

            # comment here
            input_distance = self.turtle_object.distance(
                food_source.turtle_object.pos())

            # comment here
            input_angle = self.turtle_object.heading(
            ) - self.turtle_object.towards(food_source.turtle_object.pos())

            # comment here
            sin_angle = math.sin(math.radians(input_angle))

            # comment here
            left_sensor_distance = input_distance - sin_angle
            right_sensor_distance = input_distance + sin_angle

            # comment here
            left_speed, right_speed, combined_speed = self.compute_speed(
                left_sensor_distance, right_sensor_distance, likes_food)

            # comment here
            turn_amount = self.turn_parameters[0] * (right_speed - left_speed)

            # comment here
            cumulative_speed += combined_speed
            cumulative_turn_amount += turn_amount

        # comment here
        if isinstance(cumulative_turn_amount, complex):
            cumulative_turn_amount = 0

        # comment here
        if cumulative_speed < 0:
            cumulative_speed = 0

        # comment here
        self.turtle_object.right(cumulative_turn_amount)
        self.turtle_object.forward(cumulative_speed)

        # comment here
        self.check_border_collision()

    def check_border_collision(self):
        '''
        comment here. Make one big comment for the function, but it must be more specific and detailed then
        just repeating the function name...
        '''

        if self.turtle_object.xcor() > MAX_LOCATION:
            self.turtle_object.goto(MAX_LOCATION, self.turtle_object.ycor())
        if self.turtle_object.xcor() < -MAX_LOCATION:
            self.turtle_object.goto(-MAX_LOCATION, self.turtle_object.ycor())

        if self.turtle_object.ycor() > MAX_LOCATION:
            self.turtle_object.goto(self.turtle_object.xcor(), MAX_LOCATION)
        if self.turtle_object.ycor() < -MAX_LOCATION:
            self.turtle_object.goto(self.turtle_object.xcor(), -MAX_LOCATION)

        if self.turtle_object.ycor() <= -MAX_LOCATION:
            if 0 <= self.turtle_object.heading() <= 180:
                turn_angle = 180 - self.turtle_object.heading()
                self.turtle_object.setheading(turn_angle)
            else:
                turn_angle = abs(360 - self.turtle_object.heading())
                self.turtle_object.setheading(turn_angle)

        if self.turtle_object.ycor() >= MAX_LOCATION:
            if 0 <= self.turtle_object.heading() <= 180:
                turn_angle = 360 - self.turtle_object.heading()
                self.turtle_object.setheading(turn_angle)
            else:
                turn_angle = 360 - (self.turtle_object.heading() - 180)
                self.turtle_object.setheading(turn_angle)

        if self.turtle_object.xcor() <= -MAX_LOCATION:
            if 0 <= self.turtle_object.heading() <= 90:
                turn_angle = 360 - self.turtle_object.heading()
                self.turtle_object.setheading(turn_angle)
            if 270 < self.turtle_object.heading() <= 360:
                turn_angle = 360 - self.turtle_object.heading()
                self.turtle_object.setheading(turn_angle)
            if 90 < self.turtle_object.heading() < 180:
                turn_angle = self.turtle_object.heading() - 90
                self.turtle_object.setheading(turn_angle)
            if 180 <= self.turtle_object.heading() <= 360:
                turn_angle = self.turtle_object.heading() + 90
                self.turtle_object.setheading(turn_angle)

        if self.turtle_object.xcor() >= MAX_LOCATION:
            if 0 <= self.turtle_object.heading() <= 180:
                turn_angle = self.turtle_object.heading() + 90
                self.turtle_object.setheading(turn_angle)
            else:
                turn_angle = self.turtle_object.heading() - 90
                self.turtle_object.setheading(turn_angle)

    ###############################################################################################################
    def compute_speed(self, left_distance, right_distance, likes_food):
        '''
        comment here. Make one big comment for the function, but it must be more specific and detailed then
        just repeating the function name... explain this in Braitenberg's terms
        '''

        if likes_food:
            left_speed = (
                self.speed_params[0] /
                (right_distance**self.speed_params[1])) - self.speed_params[2]
            right_speed = (
                self.speed_params[0] /
                (left_distance**self.speed_params[1])) - self.speed_params[2]
        else:
            left_speed = (
                self.speed_params[0] /
                (left_distance**self.speed_params[1])) - self.speed_params[2]
            right_speed = (
                self.speed_params[0] /
                (right_distance**self.speed_params[1])) - self.speed_params[2]
        combined_speed = (left_speed + right_speed) / 2
        return left_speed, right_speed, combined_speed
class Robot:
    def __init__(self, scene, robot_id):
    #sets variables                     
        self.robot_id = robot_id
        self.turtle = RawTurtle(scene.canvas)
        self.scene = scene
        self.scr = self.turtle.getscreen()
        self.scr.setworldcoordinates(0, scene.height, scene.width, 0)
        self.turtle.penup()
        if robot_id == 1:
            self.turtle.color("blue")
        else:
            self.turtle.color("red")
    #create turtles sprite
##        self.turtle.register_shape("ship",((-7,-2),(-6,-1),(-3,0),(-3,1),(-2,5),
##                                           (0,7),(2,5),(3,1),(3,0),(6,-1),(7,-2),
##                                           (3,-1),(3,-2),(2,-7),(-2,-7),(-3,-2),
##                                           (-3,-1),(-2,-1),(-2,-2),(-1,-6),(1,-6),
##                                           (2,-2),(2,-1),(-2,-1),(-2,0),(2,0),
##                                           (2,1),(1,4),(0,5),(-1,4),(-2,1),
##                                           (-2,-1),(-3,-1))
##                         )
##        self.turtle.shape("ship")
##        self.turtle.shapesize(2,2)
    #place robot using reset
        self.reset()

    def reset(self):
    #set start positions for robots
        positions = [((15,15), 45), 
                    ((self.scene.width-15, 15), 135),
                    ((15, self.scene.height-15), 315),
                    ((self.scene.height-15, self.scene.width-15), 135)]
    #move robot to starting possition
        self.turtle.speed(0)
        self.turtle.setpos(positions[self.robot_id][0])
        #print positions[self.robot_id]
        self.turtle.left(positions[self.robot_id][1])
        self.turtle.forward(20)
        self.turtle.speed(0)
        self.turtle.screen.bgcolor("sky blue")

    def orientate(self, landmarks, canvas, goal):
##sd = shortest distance
##x1,x2,y1,y2 = circles corners
##lx,ly = length x/y
##h = hypothinus
##ln = landmark number
        sd = 40000000
        ln = 0
        
        for ID in landmarks:
            if canvas.itemcget(ID, "fill") == "dark green":
                ln+=1
                x1,y1,x2,y2 = canvas.coords(ID)
                lx = ((x1+x2)/2) - self.turtle.xcor()
                ly = ((y1+y2)/2) - self.turtle.ycor()
                h = math.sqrt(lx*lx + ly*ly)
                if h < sd:
                    sd = h
                    stored_ID = ID
                    stored_x = lx
                    stored_y = ly
        
        if ln == 0:
            stored_ID = goal
            x1,y1,x2,y2 = canvas.coords(goal)
            lx = ((x1+x2)/2) - self.turtle.xcor()
            ly = ((y1+y2)/2) - self.turtle.ycor()
            sd = math.sqrt(lx*lx + ly*ly)
            stored_x = ((x1+x2)/2) - self.turtle.xcor()
            stored_y = ((y1+y2)/2) - self.turtle.ycor()
        
        if sd < 37:
            return stored_ID
        
        if stored_x < 0:
            if stored_y < 0:
                new_heading = 180 + math.degrees(math.atan((-stored_y)/(-stored_x)))
            else:
                new_heading = 180 - math.degrees(math.atan(stored_y/(-stored_x)))
        elif stored_y < 0:
            new_heading = 360 - math.degrees(math.atan((-stored_y)/stored_x))
        else:
            new_heading = math.degrees(math.atan(stored_y/stored_x))

        self.turtle.seth(new_heading)
        return False

    def collisions_move(self, speed, depth):
    ##breaks the recursion if the robots get to close
        if depth > 10:
            return
    ##sets variables for checking collision
        turtle_x = self.turtle.xcor()
        turtle_y = self.turtle.ycor()
        t_heading = self.turtle.heading()
    ##variables used to check right
        xr = turtle_x + 15*math.cos(math.radians(self.turtle.heading()+30))
        yr = turtle_y + 15*math.sin(math.radians(self.turtle.heading()+30))
    ##variables used to check left
        xl = turtle_x + 15*math.cos(math.radians(self.turtle.heading()-30))
        yl = turtle_y + 15*math.sin(math.radians(self.turtle.heading()-30))
    ##check for the collision
        left = self.scene.canvas.find_overlapping(xl-1,yl-1,xl+1,yl+1)
        right = self.scene.canvas.find_overlapping(xr-1,yr-1,xr+1,yr+1)
        if left:
            ##turn away
            self.turtle.left(20)
            self.collisions_move(speed, depth+1)
            #self.turtle.forward(speed/5)
        elif right:
            ##turn away
            self.turtle.right(20)
            self.collisions_move(speed, depth+1)
            #self.turtle.forward(speed/5)
        else:
            ##else move forward
            self.turtle.forward(speed)
        return