Пример #1
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))
Пример #2
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()
        }
Пример #3
0
    def _gbn_sender(self, self_queue: mp.Queue(), dist_up_queue: mp.Queue(),
                    args: SenderArgs):
        repeat = False
        sleep(self.sleep_time)
        print_str = 'Передатчик:'

        if args.Sn < args.Sm:
            target_message = Target(init_coords=Point3d(
                x=self.target_locations_x[args.Sn],
                y=self.target_locations_y[args.Sn],
                z=0),
                                    radius=0.02)
            PeriscopeApplication._send(queue_up=dist_up_queue,
                                       target=target_message,
                                       lose_prob=args.lose_prob)
            print_str += ' послан pkt: ' + target_message.get_description()
            args.Sn += 1

        else:
            try:
                target_message_number = self_queue.get()
                print_str += ' принят ACK ' + target_message_number.get_description() + \
                             f' Sb: {args.Sb} ' + 'x = ' + format(self.target_locations_x[args.Sb], '.2f') + ' y = ' + format(
                        self.target_locations_y[args.Sb], '.2f')

                target_Sb = Target(init_coords=Point3d(
                    self.target_locations_x[args.Sb],
                    self.target_locations_y[args.Sb]),
                                   radius=0.02)
                #print(f'Target Sb: {target_Sb}')
                #print(f'Target message: {target_message_number}')

                if PeriscopeApplication.is_equals(target_message_number,
                                                  target_Sb):
                    target_Sn = Target(init_coords=Point3d(
                        self.target_locations_x[args.Sn],
                        self.target_locations_y[args.Sn],
                        z=0),
                                       radius=0.02)
                    PeriscopeApplication._send(dist_up_queue, target_Sn,
                                               args.lose_prob)
                    print_str += ' послан pkt: ' + target_Sn.get_description()
                    args.Sn, args.Sb, args.Sm = args.Sn + 1, args.Sb + 1, args.Sm + 1

                elif self.get_target_index_in_locations(
                        target_message_number) > args.Sb:
                    repeat = True
            except Empty:
                repeat = True

        if repeat:
            args.Sn = args.Sb
            print_str += ' повтор с Sn: ' + 'x = ' + str(
                self.target_locations_x[args.Sn]) + ' y = ' + str(
                    self.target_locations_y[args.Sn])

        with open('передатчик_гбн_new.txt', 'a') as f:
            print(print_str, file=f)
Пример #4
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))
Пример #5
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)
Пример #6
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))
Пример #7
0
 def is_equals(t1: Target, t2: Target):
     return t1.get_description() == t2.get_description()