Beispiel #1
0
def main(_):
    """
    Test communication between Robot and your PC.
    Connect PC to Arduino via serial port and send motor
    commands according to the keypresses
    """
    params_file = FLAGS.params_file
    params = parse_options(params_file)

    frontend = RobotFrontend(params)

    keyboard_actions = KeyboardActions()
    keyboard_actions.start()

    try:
        while True:
            action = keyboard_actions.get_action()
            status = frontend.make_action(action)
            time.sleep(0.2)

    except KeyboardInterrupt:
        print("Closing")
    except Exception as error:
        print("ERROR")
        print(error)
    finally:
        print("Exiting")
        keyboard_actions.close()
        frontend.make_action(0)
Beispiel #2
0
def main(_):
    '''
    Connect to IP cam and print results
    '''
    params_file = FLAGS.params_file
    params = parse_options(params_file)

    unity_sim = UnitySimulation(params)

    robot_actions = {}
    for aruco in params["ai_robots"]["robots"]:
        print(f'Aruco marker: {aruco["aruco_code"]}')
        robot_actions[aruco["aruco_code"]] = 0

    try:
        while True:
            frame = unity_sim.frame()
            cv2.imshow('Unity screen capture', frame)
            cv2.waitKey(1)

            for key in robot_actions.keys():
                robot_actions[key] = random.randint(0, 5)

            response = unity_sim.make_actions(robot_actions)
            print(f'Response: {"OK" if response == 1 else "ERROR"}', end='\r')

            time.sleep(0.1)
    except KeyboardInterrupt:
        print("Keyboard Interruption")
    finally:
        for key in robot_actions.keys():
            robot_actions[key] = 0
        response = unity_sim.make_actions(robot_actions)
        print("Exiting")
def main(_):
    """
    Test communication between Robot and your PC.
    Connect PC to Arduino via serial port and send motor
    commands according to the keypresses
    """
    params_file = FLAGS.params_file
    params = parse_options(params_file)
    frontend = RobotFrontend(params)

    action = FLAGS.action
    duration = FLAGS.duration

    try:
        status = frontend.make_action(action)
        time.sleep(duration / 1000)
        status = frontend.make_action(0)
    except KeyboardInterrupt:
        print("Closing")
    except Exception as error:
        print("ERROR")
        print(error)
    finally:
        print("Exiting")
        frontend.make_action(0)
def main(_):
    '''
    Connect to IP cam and print results
    '''
    params_file = FLAGS.params_file
    params = parse_options(params_file)

    brain_server = UnityBrainServer(params)

    robot_observations_dict = {
        2: {
            'lower_obs': [0] * 279,
            'upper_obs': [0] * 279
        },
        3: {
            'lower_obs': [0] * 279,
            'upper_obs': [0] * 279
        }
    }
    try:
        while True:
            response = brain_server.get_actions(robot_observations_dict)
            print(f'Got action:{response}')

            time.sleep(0.5)
    except KeyboardInterrupt:
        print("Exiting")
def setup_variables(mode):
    if mode == PROD or mode == TEST:
        params = parse_options("params-prod.yaml")
    else:
        params = parse_options("params-simu.yaml")

    if 'simulation' in params:
        width = params["simulation"]["capture_width"]
        height = params["simulation"]["capture_height"]
    elif 'ai_video_streamer' in params:
        width = params["ai_video_streamer"]["capture_width"]
        height = params["ai_video_streamer"]["capture_height"]

    image_size = (width, height, 3)
    arr_size = width * height * 3

    shared_array = Array(c_uint8, arr_size)
    shared_image = np.frombuffer(
        shared_array.get_obj(),
        dtype=np.uint8)
    shared_image = np.reshape(shared_image, image_size)

    shared_state = Value(c_bool, True)

    process_manager = Manager()
    shared_data = process_manager.dict({
            "actualDuration": -1,
            "actualDurationFPS": -1,
            "totalDuration": -1,
            "totalDurationFPS": -1,
            "imageCaptureDuration": -1,
            "obsCreationDuration": -1,
            "brainDuration": -1,
            "frontendDuration": -1,
            "status": "Initialized",
            "lowerObs": [],
            "upperObs": [],
            "angles": []
        })

    return shared_image, shared_array, shared_state, shared_data
                      'Got unexpected exception in "GStreamerVideoSink"'
                      f'Message: {error}\n=====\n')
                raise Exception(
                    "\n=====\nCan't get image. Maybe the incoming image "
                    f"size is different than {self._width}x{self._height} "
                    "which was expected\n=====\n")

        return Gst.FlowReturn.OK


# Main for testing video connection. Run with below command executed in
# project's root folder. Make sure 'params-prod.yaml' file has
# 'ai_video_streamer' group and correct parameters set.
# python -m reallife_camera_source.gstreamer_video_sink
if __name__ == '__main__':
    from utils.utils import parse_options
    # Create the video object
    # Add port= if is necessary to use a different one
    params = parse_options("params-prod.yaml")
    video = GStreamerVideoSink(params)

    while True:
        # Wait for the next frame
        if not video.frame_available():
            continue

        frame = video.frame()
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    def _start_game_loop(self, mode, shared_array, shared_state, shared_data):
        try:
            self._mode = mode
            self._shared_data = shared_data
            self._shared_array = shared_array
            if mode == PROD or mode == TEST:
                params = parse_options("params-prod.yaml")
            else:
                params = parse_options("params-simu.yaml")

            self._robot_arucos = []
            for robot in params["ai_robots"]["robots"]:
                self._robot_arucos.append(int(robot['aruco_code']))

            if 'simulation' in params:
                width = params['simulation']['capture_width']
                height = params['simulation']['capture_height']
            elif 'ai_video_streamer' in params:
                width = params['ai_video_streamer']['capture_width']
                height = params['ai_video_streamer']['capture_height']
            image_size = (width, height, 3)
            arr_size = width * height * 3

            self._step_time = 1 / params['decision_rate']
            self._image_source, self._frontend = \
                self._get_image_source_and_frontend(mode, params)

            self._image_processer = ImageProcesser(params)
            if mode == PROD or mode == SIMU:
                brain_server = UnityBrainServer(params)

            shared_image = np.frombuffer(shared_array.get_obj(),
                                         dtype=np.uint8)
            self._shared_image = np.reshape(shared_image, image_size)

            while True:
                self._log_time(log_start=True)
                with shared_state.get_lock():
                    if shared_state.value is False:
                        break

                # 1) Get image
                if self._image_source.frame_available() is None:
                    print("==== IMAGE IS NONE")
                if not self._image_source.frame_available():
                    self._shared_data['status'] = 'No image'
                    print("\n\n=========== No image\n\n")
                    time.sleep(0.1)
                    continue
                image = self._image_source.frame()
                self._log_time(log_name='imageCaptureDuration')

                # 2) Get observations from image
                # The _ and __ variables are placeholders for the second
                # robot and it's observations
                robot_observations_dict = \
                    self._image_processer.image_to_observations(image=image)
                self._log_time(log_name='obsCreationDuration')

                # 2.1) We didn't get observations
                if not robot_observations_dict:
                    self._shared_data['status'] = 'No observations'
                    # print("\n\n=========== No observations\n\n")
                    self._stop_robots()
                    self._end_routine()
                    continue
                # 2.2) We got observations
                for aruco_id in robot_observations_dict.keys():
                    self._shared_data[f'robot_{aruco_id}_lower_obs'] = \
                        robot_observations_dict[aruco_id]['lower_obs']
                    self._shared_data[f'robot_{aruco_id}_upper_obs'] = \
                        robot_observations_dict[aruco_id]['lower_obs']
                    self._shared_data['angles'] = self._image_processer.angles

                if mode == PROD or mode == SIMU:
                    # 3a) Get action from brain with the observations
                    actions = brain_server.get_actions(robot_observations_dict)
                    self._log_time(log_name='brainDuration')

                    print(f"Got actions: {actions}")
                    # 4) Send the action to frontend
                    actions = dict(
                        filter(lambda act: act[0] in self._robot_arucos,
                               actions.items()))
                    print(f"Got filtered: {actions}")
                    _ = self._frontend.make_actions(actions)
                    self._shared_data['status'] = 'Playing game'
                    self._log_time(log_name='frontendDuration')
                else:
                    # 3b) Just log status, don't connect to
                    # brain server nor frontend
                    self._shared_data['status'] = 'Running in test mode'

                self._end_routine()

        except KeyboardInterrupt:
            print("\nGame: Keyboard Interrupt")

        except Exception as error:
            traceback.print_exc()
            shared_state.value = False
            self._image_source.stop()
            print('\n=====\nGot unexpected exception in "_start_game" in '
                  f'Game-class. Message: {error}\n=====\n')

        finally:
            self._stop_robots()
            self._image_source.stop()
            print("Game: Game stopped")