Ejemplo n.º 1
0
 def start(self) -> None:
     """Starts the physics simulation if it is not already running.
     """
     if self._ui_thread is None:
         raise PyRepError('V-REP has not been launched. Call launch first.')
     if not self.running:
         vrep.simStartSimulation()
         self.running = True
Ejemplo n.º 2
0
    def launch(self,
               scene_file="",
               headless=False,
               responsive_ui=False,
               blocking=False,
               write_coppeliasim_stdout_to_file=False) -> None:
        """Launches CoppeliaSim.

        Launches the UI thread, waits until the UI thread has finished, this
        results in the current thread becoming the simulation thread.

        :param scene_file: The scene file to load. Empty string for empty scene.
        :param headless: Run CoppeliaSim in simulation mode.
        :param responsive_ui: If True, then a separate thread will be created to
            asynchronously step the UI of CoppeliaSim. Note, that will reduce
            the responsiveness of the simulation thread.
        :param blocking: Causes CoppeliaSim to launch as if running the default
            c++ client application. This is causes the function to block.
            For most users, this will be set to False.
        :param write_coppeliasim_stdout_to_file: Causes CoppeliaSim to write the stdout to files in /tmp, rather than
            the terminal stdout as the python script is run. This helps reduce screen clutter, particularly if using
            multiple PyRep instances with multiprocessing, for example.
        """
        abs_scene_file = os.path.abspath(scene_file)
        if len(scene_file) > 0 and not os.path.isfile(abs_scene_file):
            raise PyRepError('Scene file does not exist: %s' % scene_file)
        cwd = os.getcwd()
        self._ui_thread = threading.Thread(
            target=self._run_ui_thread,
            args=(abs_scene_file, headless, write_coppeliasim_stdout_to_file))
        self._ui_thread.daemon = True
        self._ui_thread.start()

        while not sim.simExtCanInitSimThread():
            time.sleep(0.1)

        sim.simExtSimThreadInit()
        time.sleep(0.2)  # Stops CoppeliaSim crashing if restarted too quickly.

        if blocking:
            while not sim.simExtGetExitRequest():
                sim.simExtStep()
            self.shutdown()
        elif responsive_ui:
            self._responsive_ui_thread = threading.Thread(
                target=self._run_responsive_ui_thread)
            self._responsive_ui_thread.daemon = True
            try:
                self._responsive_ui_thread.start()
            except (KeyboardInterrupt, SystemExit):
                if not self._shutting_down:
                    self.shutdown()
                sys.exit()
            self.step()
        else:
            self.step()
        os.chdir(cwd)  # Go back to the previous cwd
Ejemplo n.º 3
0
Archivo: pyrep.py Proyecto: wkoa/PyRep
    def launch(self,
               scene_file: str = "",
               headless: bool = False,
               responsive_ui: bool = False,
               blocking: bool = False,
               verbosity: Verbosity = Verbosity.NONE) -> None:
        """Launches CoppeliaSim.

        Launches the UI thread, waits until the UI thread has finished, this
        results in the current thread becoming the simulation thread.

        :param scene_file: The scene file to load. Empty string for empty scene.
        :param headless: Run CoppeliaSim in simulation mode.
        :param responsive_ui: If True, then a separate thread will be created to
            asynchronously step the UI of CoppeliaSim. Note, that will reduce
            the responsiveness of the simulation thread.
        :param blocking: Causes CoppeliaSim to launch as if running the default
            c++ client application. This is causes the function to block.
            For most users, this will be set to False.
        :param verbosity: The verbosity level for CoppeliaSim.
            Usually Verbosity.NONE or Verbosity.LOAD_INFOS.
        """
        abs_scene_file = os.path.abspath(scene_file)
        if len(scene_file) > 0 and not os.path.isfile(abs_scene_file):
            raise PyRepError('Scene file does not exist: %s' % scene_file)
        cwd = os.getcwd()
        self._ui_thread = threading.Thread(target=self._run_ui_thread,
                                           args=(abs_scene_file, headless,
                                                 verbosity))
        self._ui_thread.daemon = True
        self._ui_thread.start()

        while not sim.simExtCanInitSimThread():
            time.sleep(0.1)

        sim.simExtSimThreadInit()
        time.sleep(0.2)  # Stops CoppeliaSim crashing if restarted too quickly.

        if blocking:
            while not sim.simExtGetExitRequest():
                sim.simExtStep()
            self.shutdown()
        elif responsive_ui:
            self._responsive_ui_thread = threading.Thread(
                target=self._run_responsive_ui_thread)
            self._responsive_ui_thread.daemon = True
            try:
                self._responsive_ui_thread.start()
            except (KeyboardInterrupt, SystemExit):
                if not self._shutting_down:
                    self.shutdown()
                sys.exit()
            self.step()
        else:
            self.step()
        os.chdir(cwd)  # Go back to the previous cwd
Ejemplo n.º 4
0
 def stop(self) -> None:
     """Stops the physics simulation if it is running.
     """
     if self._ui_thread is None:
         raise PyRepError('V-REP has not been launched. Call launch first.')
     if self.running:
         vrep.simStopSimulation()
         self.running = False
         # Need this so the UI updates
         [self.step() for _ in range(5)]
Ejemplo n.º 5
0
 def __init__(self, name_or_handle: Union[str, int]):
     raise PyRepError(
         'Currently there is an error in CoppeliaSim with distance objects. '
         'As soon as CoppeliaSim resolves this issue, this error will be '
         'removed.')
     if isinstance(name_or_handle, int):
         self._handle = name_or_handle
     else:
         self._handle = sim.simGetDistanceHandle(name_or_handle)
     # The entity needs to be set as explicit handling for reads to work.
     if not sim.simGetExplicitHandling(self._handle):
         sim.simSetExplicitHandling(self._handle, 1)
Ejemplo n.º 6
0
    def __init__(self):
        self.running = False
        self._process = None
        self._robot_to_count = {}
        self.connected = False

        self._ui_thread = None
        self._responsive_ui_thread = None

        self._init_thread_id = None
        self._shutting_down = False

        self._handles_to_objects = {}

        if 'COPPELIASIM_ROOT' not in os.environ:
            raise PyRepError(
                'COPPELIASIM_ROOT not defined. See installation instructions.')
        self._vrep_root = os.environ['COPPELIASIM_ROOT']
        if not os.path.exists(self._vrep_root):
            raise PyRepError('COPPELIASIM_ROOT was not a correct path. '
                             'See installation instructions')
Ejemplo n.º 7
0
    def launch(self,
               scene_file="",
               headless=False,
               responsive_ui=False,
               blocking=False) -> None:
        """Launches V-REP.

        Launches the UI thread, waits until the UI thread has finished, this
        results in the current thread becoming the simulation thread.

        :param scene_file: The scene file to load. Empty string for empty scene.
        :param headless: Run V-REP in simulation mode.
        :param responsive_ui: If True, then a separate thread will be created to
            asynchronously step the UI of V-REP. Note, that will reduce
            the responsiveness of the simulation thread.
        :param blocking: Causes V-REP to launch as if running the default c++
            client application. This is causes the function to block. For most
            users, this will be set to False.
        """
        if len(scene_file) > 0 and not os.path.isfile(
                os.path.abspath(scene_file)):
            raise PyRepError('Scene file does not exist: %s' % scene_file)
        self._ui_thread = threading.Thread(target=self._run_ui_thread,
                                           args=(scene_file, headless))
        self._ui_thread.daemon = True
        self._ui_thread.start()

        while not vrep.simExtCanInitSimThread():
            time.sleep(0.1)

        vrep.simExtSimThreadInit()
        time.sleep(0.2)  # Stops V-REP crashing if it is restarted too quickly.

        if blocking:
            while not vrep.simExtGetExitRequest():
                vrep.simExtStep()
            self.shutdown()
        elif responsive_ui:
            self._responsive_ui_thread = threading.Thread(
                target=self._run_responsive_ui_thread)
            self._responsive_ui_thread.daemon = True
            try:
                self._responsive_ui_thread.start()
            except (KeyboardInterrupt, SystemExit):
                if not self._shutting_down:
                    self.shutdown()
                sys.exit()
            self.step()
        else:
            self.step()
Ejemplo n.º 8
0
    def read(self) -> Union[float, None]:
        """Reads the distance of a registered distance object.

        :raises: PyRepError if no objects could be measured.

        :return: The smallest distance between the 2 entities or None if no
            measurement could be made.
        """
        num_measurements = sim.simHandleDistance(self._handle)
        if num_measurements == 0:
            raise PyRepError(
                'Could not make a measurement. Are both entities associated '
                'with the distance object marked as measurable?')
        return (None if num_measurements == 0 else sim.simReadDistance(
            self._handle))
Ejemplo n.º 9
0
 def shutdown(self) -> None:
     """Shuts down the V-REP simulation.
     """
     if self._ui_thread is None:
         raise PyRepError('V-REP has not been launched. Call launch first.')
     if self._ui_thread is not None:
         self._shutting_down = True
         self.stop()
         self.step_ui()
         vrep.simExtPostExitRequest()
         vrep.simExtSimThreadDestroy()
         self._ui_thread.join()
         if self._responsive_ui_thread is not None:
             self._responsive_ui_thread.join()
         # V-REP crashes if new instance opened too quickly after shutdown.
         # TODO: A small sleep stops this for now.
         time.sleep(0.1)
     self._ui_thread = None
     self._shutting_down = False
Ejemplo n.º 10
0
 def _check_signal(self, value: int, type_name: str) -> None:
     if value == 0:
         raise PyRepError('Signal %s of type %s does not exist.' % (
             self._name, type_name))