Пример #1
0
    def set_joint_position(self,
                           position: float,
                           allow_force_mode=True) -> None:
        """Sets the intrinsic position of the joint.

        :param positions: A list of positions of the joints (angular or linear
            values depending on the joint type).
        :param allow_force_mode: If True, then the position can be set even
            when the joint mode is in Force mode. It will disable dynamics,
            move the joint, and then re-enable dynamics.
        """
        if not allow_force_mode:
            vrep.simSetJointPosition(self._handle, position)
            return

        is_model = self.is_model()
        if not is_model:
            self.set_model(True)

        prior = vrep.simGetModelProperty(self.get_handle())
        p = prior | vrep.sim_modelproperty_not_dynamic
        # Disable the dynamics
        vrep.simSetModelProperty(self._handle, p)

        vrep.simSetJointPosition(self._handle, position)
        self.set_joint_target_position(position)
        vrep.simExtStep(True)  # Have to step once for changes to take effect

        # Re-enable the dynamics
        vrep.simSetModelProperty(self._handle, prior)
        self.set_model(is_model)
Пример #2
0
    def step(self) -> None:
        """Execute the next simulation step.

        If the physics simulation is not running, then this will only update
        the UI.
        """
        with self._step_lock:
            vrep.simExtStep()
Пример #3
0
    def step_ui(self) -> None:
        """Update the UI.

        This will not execute the next simulation step, even if the physics
        simulation is running.
        This is only applicable when PyRep was launched without a responsive UI.
        """
        with self._step_lock:
            vrep.simExtStep(False)
Пример #4
0
 def _run_responsive_ui_thread(self) -> None:
     while True:
         if not self.running:
             with self._step_lock:
                 if self._shutting_down or vrep.simExtGetExitRequest():
                     break
                 vrep.simExtStep(False)
         time.sleep(0.01)
     # If the exit request was from the UI, then call shutdown, otherwise
     # shutdown caused this thread to terminate.
     if not self._shutting_down:
         self.shutdown()
Пример #5
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)
        cwd = os.getcwd()
        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()
        os.chdir(cwd)  # Go back to the previous cwd
Пример #6
0
    def set_joint_positions(self,
                            positions: List[float],
                            allow_force_mode=True) -> None:
        """Sets the intrinsic position of the joints.

        See :py:meth:`Joint.set_joint_position` for more information.

        :param positions: A list of positions of the joints (angular or linear
            values depending on the joint type).
        :param allow_force_mode: If True, then the position can be set even
            when the joint mode is in Force mode. It will disable dynamics,
            move the joint, and then re-enable dynamics.
        """
        self._assert_len(positions)

        if not allow_force_mode:
            [
                j.set_joint_position(p, allow_force_mode)
                for j, p in zip(self.joints, positions)
            ]
            return

        is_model = self.is_model()
        if not is_model:
            self.set_model(True)

        prior = vrep.simGetModelProperty(self.get_handle())
        p = prior | vrep.sim_modelproperty_not_dynamic
        # Disable the dynamics
        vrep.simSetModelProperty(self._handle, p)

        [
            j.set_joint_position(p, allow_force_mode)
            for j, p in zip(self.joints, positions)
        ]
        [
            j.set_joint_target_position(p)
            for j, p in zip(self.joints, positions)
        ]
        vrep.simExtStep(True)  # Have to step once for changes to take effect

        # Re-enable the dynamics
        vrep.simSetModelProperty(self._handle, prior)
        self.set_model(is_model)