Ejemplo n.º 1
0
    def run(self_queue: mp.Queue, arr, periscope: Periscope,
            plane_loc: MirrorLocation, model):
        down = periscope.mirror_down.triangle
        up = periscope.mirror_up.triangle
        while True:
            periscope.target = self_queue.get()

            angles = model.predict(
                numpy.array([[
                    periscope.target.location.y, periscope.target.location.z
                ]]))
            periscope.mirror_down.triangle = down.rotate_plane(
                angles[0][0], Angle.YAW)
            periscope.mirror_down.triangle = periscope.mirror_down.triangle.rotate_plane(
                angles[0][1], Angle.PITCH)
            periscope.mirror_down.triangle = periscope.mirror_down.triangle.rotate_plane(
                angles[0][2], Angle.ROLL)

            periscope.mirror_up.triangle = up.rotate_plane(
                angles[0][3], Angle.YAW)
            periscope.mirror_up.triangle = periscope.mirror_up.triangle.rotate_plane(
                angles[0][4], Angle.PITCH)
            periscope.mirror_up.triangle = periscope.mirror_up.triangle.rotate_plane(
                angles[0][5], Angle.ROLL)

            self_plane = periscope.mirror_down.triangle
            if plane_loc == MirrorLocation.UP:
                self_plane = periscope.mirror_up.triangle

            arr[0] = self_plane.point_b.x
            arr[1] = self_plane.point_b.y
            arr[2] = self_plane.point_b.z
            arr[3] = self_plane.point_c.x
            arr[4] = self_plane.point_c.y
            arr[5] = self_plane.point_c.z
Ejemplo n.º 2
0
    def __init__(self, input_model: str = '2d'):
        self.log_list = []
        pygame.init()
        self.input_model = input_model
        config = parse(input_model)

        self.periscope: Periscope = Periscope(config)
        p_target = self.periscope.ray_to_aim().intersect_plane(
            Triangle(Point3d(0.2, 0.5, 0.2),
                     Point3d(0.2, 0.4, 0.1),
                     Point3d(0.2, 0.3, 0.5)
                     ))
        tee = Target(p_target, config["target_radius"])
        self.periscope.set_target(tee)

        self.renderer = Renderer(self.periscope)

        # Shared memory
        self.down_plane_points = mp.Array('d', 6)
        self.up_plane_points = mp.Array('d', 6)
        self.__init_share_memory()

        self.up_plane_queue = mp.Queue()
        self.down_plane_queue = mp.Queue()

        self.up_plane_process: mp.Process = mp.Process(
            target=DirectAlgorithm.plane_direct_process,
            args=(self.up_plane_queue, self.up_plane_points, self.periscope, MirrorLocation.UP))
        self.down_plane_process: mp.Process = mp.Process(
            target=DirectAlgorithm.plane_direct_process,
            args=(self.down_plane_queue, self.down_plane_points, self.periscope, MirrorLocation.DOWN))
Ejemplo n.º 3
0
    def __init__(self, conf: str):
        self.conf = conf
        config = parse(conf)
        self.periscope: Periscope = Periscope(config)

        p_target = self.periscope.ray_to_aim().intersect_plane(
            Triangle(Point3d(0.2, 0.5, 0.2), Point3d(0.2, 0.4, 0.1),
                     Point3d(0.2, 0.3, 0.5)))
        tee = Target(p_target, 0.02)
        self.periscope.set_target(tee)

        plane_up = self.periscope.mirror_up.triangle
        plane_down = self.periscope.mirror_down.triangle
        self.down_plane = Triangle(plane_down.point_a, plane_down.point_b,
                                   plane_down.point_c)
        self.up_plane = Triangle(plane_up.point_a, plane_up.point_b,
                                 plane_up.point_c)

        self.base_length = {
            'up': Vector(plane_up.point_c, plane_up.point_b).length(),
            'down': Vector(plane_down.point_c, plane_down.point_b).length()
        }
        self.height_length = {
            'up':
            Vector(plane_up.point_a,
                   (plane_up.point_b + plane_up.point_c) / 2).length(),
            'down':
            Vector(plane_down.point_a,
                   (plane_down.point_b + plane_down.point_c) / 2).length()
        }

        self.side_length = {
            'up': Vector(plane_up.point_a, plane_up.point_b).length(),
            'down': Vector(plane_down.point_a, plane_down.point_b).length()
        }
Ejemplo n.º 4
0
    def plane_direct_process_2(self_queue: mp.Queue, arr, periscope: Periscope,
                               plane_loc: MirrorLocation):
        iteration = 0

        while True:
            print('down')
            periscope.target = self_queue.get()
            process_name = mp.process.current_process().name
            str_process = 'process: ' + process_name + ' got: ' + periscope.target.get_description(
            )

            with open(f'{process_name}_down_process.txt', 'a') as f:
                print(str_process, file=f)

            diff = PeriscopeApplication.final_ray_target_diff(
                periscope.laser, periscope.mirror_down.triangle,
                periscope.mirror_up.triangle, periscope.target.location)

            first_loc_plane = MirrorLocation.UP
            second_loc_plane = MirrorLocation.DOWN
            if iteration % 2 == 0:
                first_loc_plane = MirrorLocation.DOWN
                second_loc_plane = MirrorLocation.UP

            step = 0
            while diff > periscope.target.radius / 2 and step < 10:
                PeriscopeApplication.correct_one_plane(periscope,
                                                       first_loc_plane,
                                                       Angle.ROLL, step)
                PeriscopeApplication.correct_one_plane(periscope,
                                                       first_loc_plane,
                                                       Angle.PITCH, step)

                PeriscopeApplication.correct_one_plane(periscope,
                                                       second_loc_plane,
                                                       Angle.ROLL, step)
                PeriscopeApplication.correct_one_plane(periscope,
                                                       second_loc_plane,
                                                       Angle.PITCH, step)

                diff = PeriscopeApplication.final_ray_target_diff(
                    periscope.laser, periscope.mirror_down.triangle,
                    periscope.mirror_up.triangle, periscope.target.location)
                step += 1

            self_plane = periscope.mirror_down.triangle
            if plane_loc == MirrorLocation.UP:
                self_plane = periscope.mirror_up.triangle

            arr[0] = self_plane.point_b.x
            arr[1] = self_plane.point_b.y
            arr[2] = self_plane.point_b.z
            arr[3] = self_plane.point_c.x
            arr[4] = self_plane.point_c.y
            arr[5] = self_plane.point_c.z
            iteration += 1
Ejemplo n.º 5
0
    def plane_direct_process(self_queue: mp.Queue, arr, periscope: Periscope,
                             plane_loc: MirrorLocation):
        iteration = 0

        while True:
            periscope.target = self_queue.get()

            diff = DirectAlgorithm.final_ray_target_diff(
                periscope.laser, periscope.mirror_down.triangle,
                periscope.mirror_up.triangle, periscope.target.location)

            first_loc_plane = MirrorLocation.UP
            second_loc_plane = MirrorLocation.DOWN
            if iteration % 2 == 0:
                first_loc_plane = MirrorLocation.DOWN
                second_loc_plane = MirrorLocation.UP

            step = 0
            while diff > periscope.target.radius / 2 and step < 10:
                DirectAlgorithm.correct_one_plane(periscope, first_loc_plane,
                                                  Angle.ROLL, step)
                DirectAlgorithm.correct_one_plane(periscope, first_loc_plane,
                                                  Angle.PITCH, step)

                DirectAlgorithm.correct_one_plane(periscope, second_loc_plane,
                                                  Angle.ROLL, step)
                DirectAlgorithm.correct_one_plane(periscope, second_loc_plane,
                                                  Angle.PITCH, step)

                diff = DirectAlgorithm.final_ray_target_diff(
                    periscope.laser, periscope.mirror_down.triangle,
                    periscope.mirror_up.triangle, periscope.target.location)
                step += 1

            self_plane = periscope.mirror_down.triangle
            if plane_loc == MirrorLocation.UP:
                self_plane = periscope.mirror_up.triangle

            arr[0] = self_plane.point_b.x
            arr[1] = self_plane.point_b.y
            arr[2] = self_plane.point_b.z
            arr[3] = self_plane.point_c.x
            arr[4] = self_plane.point_c.y
            arr[5] = self_plane.point_c.z
            iteration += 1
Ejemplo n.º 6
0
    def run(self_queue: mp.Queue, arr, periscope: Periscope,
            plane_loc: MirrorLocation, input_model: str):
        model = load_model(
            str(get_project_root()) + '\\src\\neuralnet\\' + input_model +
            '_model.h5')
        down = periscope.mirror_down.triangle
        up = periscope.mirror_up.triangle
        while True:
            periscope.target = self_queue.get()

            angles = model.predict(
                numpy.array([[
                    periscope.target.location.y, periscope.target.location.z
                ]]))
            periscope.mirror_down.triangle = down.rotate_plane(
                angles[0][0], Angle.YAW)
            periscope.mirror_down.triangle = periscope.mirror_down.triangle.rotate_plane(
                angles[0][1], Angle.PITCH)
            periscope.mirror_down.triangle = periscope.mirror_down.triangle.rotate_plane(
                angles[0][2], Angle.ROLL)

            periscope.mirror_up.triangle = up.rotate_plane(
                angles[0][3], Angle.YAW)
            periscope.mirror_up.triangle = periscope.mirror_up.triangle.rotate_plane(
                angles[0][4], Angle.PITCH)
            periscope.mirror_up.triangle = periscope.mirror_up.triangle.rotate_plane(
                angles[0][5], Angle.ROLL)

            self_plane = periscope.mirror_down.triangle
            if plane_loc == MirrorLocation.UP:
                self_plane = periscope.mirror_up.triangle

            arr[0] = self_plane.point_b.x
            arr[1] = self_plane.point_b.y
            arr[2] = self_plane.point_b.z
            arr[3] = self_plane.point_c.x
            arr[4] = self_plane.point_c.y
            arr[5] = self_plane.point_c.z
Ejemplo n.º 7
0
def run():
    pygame.init()
    config = parser.parse('2d')
    periscope = Periscope(config)
    renderer = Renderer(periscope)

    p_target = periscope.ray_to_aim().intersect_plane(
        Triangle(Point3d(0.2, 0.5, 0.2), Point3d(0.2, 0.4, 0.1),
                 Point3d(0.2, 0.3, 0.5)))
    tee = Target(p_target, config["target_radius"])
    periscope.set_target(tee)
    iteration = 0
    while True:
        for i in pygame.event.get():
            if i.type == pygame.QUIT:
                exit()
            elif i.type == pygame.KEYDOWN:
                if i.key == pygame.K_UP:
                    tee.location.y += 0.01
                elif i.key == pygame.K_DOWN:
                    tee.location.y -= 0.01
                elif i.key == pygame.K_RIGHT:
                    tee.location.x += 0.01
                elif i.key == pygame.K_LEFT:
                    tee.location.x -= 0.01
                elif i.key == pygame.K_KP2:
                    tee.location.z -= 0.01
                elif i.key == pygame.K_KP1:
                    tee.location.z += 0.01
                DirectAlgorithm.correct_planes(periscope, iteration)
                iteration += 1

        mirror_down = periscope.mirror_down
        mirror_up = periscope.mirror_up
        p1_intersect = periscope.laser.intersect_plane(mirror_down.triangle)
        p2_intersect = periscope.laser.reflect_plane(
            mirror_down.triangle).intersect_plane(mirror_up.triangle)
        p_aim = periscope.ray_to_aim().intersect_plane(
            Triangle(Point3d(tee.location.x, 0.5, 0.2),
                     Point3d(tee.location.x, 0.4, 0.1),
                     Point3d(tee.location.x, 0.3, 0.5)))

        renderer.render(p1_intersect, p2_intersect, tee, p_aim)
        pygame.time.delay(10)
Ejemplo n.º 8
0
    def __init__(
        self,
        input_model: str = '2d',
        algorithm: SolveAlgorithm = SolveAlgorithm.DIRECT,
        mode: str = '',
    ):
        self.log_list = []
        pygame.init()
        self.input_model = input_model
        self.mode = mode
        config = parse(input_model)

        self.periscope: Periscope = Periscope(config)
        p_target = self.periscope.ray_to_aim().intersect_plane(
            Triangle(Point3d(0.4, 0.7, 0.2), Point3d(0.5, 0.4, 0.1),
                     Point3d(0.2, 0.6, 0.5)))
        p_target = Point3d(0.62, 0.66, 0)
        tee = Target(p_target, config["target_radius"])
        self.periscope.set_target(tee)

        self.renderer = Renderer(self.periscope)

        # Shared memory
        self.down_plane_points = mp.Array('d', 6)
        self.up_plane_points = mp.Array('d', 6)
        self.plane_3_points = mp.Array('d', 6)
        self.__init_share_memory()

        self.up_plane_queue = mp.Queue()
        self.down_plane_queue = mp.Queue()
        self.plane_3_queue = mp.Queue()

        if algorithm == SolveAlgorithm.DIRECT:
            self.up_plane_process: mp.Process = mp.Process(
                target=DirectAlgorithm.plane_direct_process,
                args=(self.up_plane_queue, self.up_plane_points,
                      self.periscope, MirrorLocation.UP))
            self.down_plane_process: mp.Process = mp.Process(
                target=DirectAlgorithm.plane_direct_process,
                args=(self.down_plane_queue, self.down_plane_points,
                      self.periscope, MirrorLocation.DOWN))
            self.plane_3_process: mp.Process = mp.Process(
                target=DirectAlgorithm.plane_direct_process,
                args=(self.plane_3_queue, self.plane_3_points, self.periscope,
                      MirrorLocation.THIRD))
        else:  # algorithm == SolveAlgorithm.NEURAL_NET:
            from keras.models import load_model
            from src.algorithms.net import NeuralNetAlgorithm
            model = load_model(
                str(get_project_root()) + '\\src\\neuralnet\\' +
                self.input_model + '_model.h5')
            self.up_plane_process: mp.Process = mp.Process(
                target=NeuralNetAlgorithm.run,
                args=(self.up_plane_queue, self.up_plane_points,
                      self.periscope, MirrorLocation.UP, model))
            self.down_plane_process: mp.Process = mp.Process(
                target=NeuralNetAlgorithm.run,
                args=(self.down_plane_queue, self.down_plane_points,
                      self.periscope, MirrorLocation.DOWN, model))
            self.plane_3_process: mp.Process = mp.Process(
                target=NeuralNetAlgorithm.run,
                args=(self.plane_3_queue, self.plane_3_points, self.periscope,
                      MirrorLocation.THIRD, model))
Ejemplo n.º 9
0
    def plane_direct_process(self_queue: mp.Queue, arr, periscope: Periscope,
                             plane_loc: MirrorLocation,
                             target_locations_x: mp.Array,
                             target_locations_y: mp.Array,
                             dist_queue: mp.Queue, lose_prob,
                             plane_2_queue: mp.Queue):
        iteration = 0
        Rn = 0

        while True:
            print('up')
            periscope.target = self_queue.get()
            process_name = mp.process.current_process().name
            print_str = process_name + ': принят pkt' + periscope.target.get_description(
            )

            if Rn == PeriscopeApplication.find_index(periscope.target,
                                                     target_locations_x,
                                                     target_locations_y):
                print_str += ' доставлен '
                target = Target(init_coords=Point3d(target_locations_x[Rn],
                                                    target_locations_y[Rn]),
                                radius=0.02)
                PeriscopeApplication._send(dist_queue,
                                           target=target,
                                           lose_prob=lose_prob)
                print_str += ', send ' + target.get_description()
                plane_2_queue.put(target)
                Rn += 1

                diff = PeriscopeApplication.final_ray_target_diff(
                    periscope.laser, periscope.mirror_down.triangle,
                    periscope.mirror_up.triangle, periscope.target.location)

                first_loc_plane = MirrorLocation.UP
                second_loc_plane = MirrorLocation.DOWN
                if iteration % 2 == 0:
                    first_loc_plane = MirrorLocation.DOWN
                    second_loc_plane = MirrorLocation.UP

                step = 0
                while diff > periscope.target.radius / 2 and step < 10:
                    PeriscopeApplication.correct_one_plane(
                        periscope, first_loc_plane, Angle.ROLL, step)
                    PeriscopeApplication.correct_one_plane(
                        periscope, first_loc_plane, Angle.PITCH, step)

                    PeriscopeApplication.correct_one_plane(
                        periscope, second_loc_plane, Angle.ROLL, step)
                    PeriscopeApplication.correct_one_plane(
                        periscope, second_loc_plane, Angle.PITCH, step)

                    diff = PeriscopeApplication.final_ray_target_diff(
                        periscope.laser, periscope.mirror_down.triangle,
                        periscope.mirror_up.triangle,
                        periscope.target.location)
                    step += 1

                self_plane = periscope.mirror_down.triangle
                if plane_loc == MirrorLocation.UP:
                    self_plane = periscope.mirror_up.triangle

                arr[0] = self_plane.point_b.x
                arr[1] = self_plane.point_b.y
                arr[2] = self_plane.point_b.z
                arr[3] = self_plane.point_c.x
                arr[4] = self_plane.point_c.y
                arr[5] = self_plane.point_c.z
                iteration += 1

            elif Rn < PeriscopeApplication.find_index(
                    periscope.target, target_locations_x, target_locations_y):
                target = Target(init_coords=Point3d(
                    target_locations_x[Rn - 1], target_locations_y[Rn - 1]),
                                radius=0.02)
                PeriscopeApplication._send(dist_queue,
                                           target=target,
                                           lose_prob=lose_prob)
                print_str += ' send ' + target.get_description()
            else:
                print_str += ' сброшен '
                PeriscopeApplication._send(dist_queue, periscope.target,
                                           lose_prob)
                print_str += ' send ' + periscope.target.get_description()

            with open(f'{process_name}_up_process.txt', 'a') as f:
                print(print_str, file=f)
Ejemplo n.º 10
0
    def __init__(self,
                 input_model: str = '2d',
                 algorithm: SolveAlgorithm = SolveAlgorithm.DIRECT,
                 window_size=3,
                 lose_prob=0.2,
                 transfer_number=13):
        self.log_list = []
        self.count_win = 0
        self.count_all = 0
        self.sleep_time = 1
        pygame.init()
        self.input_model = input_model
        config = parse(input_model)

        self.periscope: Periscope = Periscope(config)
        p_target = self.periscope.ray_to_aim().intersect_plane(
            Triangle(Point3d(0.2, 0.5, 0.2), Point3d(0.2, 0.4, 0.1),
                     Point3d(0.2, 0.3, 0.5)))

        p_target_prev = self.periscope.ray_to_aim().intersect_plane(
            Triangle(Point3d(0.2, 0.5, 0.2), Point3d(0.2, 0.4, 0.1),
                     Point3d(0.2, 0.3, 0.5)))
        tee = Target(p_target, config["target_radius"])
        tee_prev = Target(p_target_prev, config["prev_target_radius"])

        self.periscope.set_target(tee)
        self.periscope.set_prev_target(tee_prev)

        self.renderer = Renderer(self.periscope)

        self._sender_queue = mp.Queue()  # for target coord
        # self._receiver_queue = mp.Queue()  # for planes

        self.pack_number = 0  # number or package that caught for session (used in number mode)

        self.target_locations_x = mp.Array('d', transfer_number * window_size)
        self.target_locations_y = mp.Array('d', transfer_number * window_size)
        self.ii = 0
        self.target_locations_x[self.ii] = self.periscope.target.location.x
        self.target_locations_y[self.ii] = self.periscope.target.location.y
        self.ii += 1

        # Shared memory
        self.down_plane_points = mp.Array('d', 6)
        self.up_plane_points = mp.Array('d', 6)
        self.__init_share_memory()

        self.up_plane_queue = mp.Queue()
        self.down_plane_queue = mp.Queue()

        self._sender_process = mp.Process(
            target=self._gbn_sender_number,
            args=(self._sender_queue, self.up_plane_queue, window_size,
                  lose_prob, transfer_number))

        self.up_plane_process: mp.Process = mp.Process(
            target=PeriscopeApplication.plane_direct_process,
            args=(self.up_plane_queue, self.up_plane_points, self.periscope,
                  MirrorLocation.UP, self.target_locations_x,
                  self.target_locations_y, self._sender_queue, lose_prob,
                  self.down_plane_queue))
        self.down_plane_process: mp.Process = mp.Process(
            target=PeriscopeApplication.plane_direct_process_2,
            args=(self.down_plane_queue, self.down_plane_points,
                  self.periscope, MirrorLocation.DOWN))