Exemplo n.º 1
0
    def target_velocity(self, velocity: Union[int, float]):

        if self.target_velocity != velocity:
            client().simxSetJointTargetVelocity(
                self.handle, velocity,
                client().simxDefaultPublisher())
            self._target_velocity = velocity
Exemplo n.º 2
0
 def _change_motor_state(self, enabled: int):
     client().simxSetObjectIntParameter(
         self.handle,
         sim.sim_jointintparam_motor_enabled,
         enabled,
         client().simxDefaultPublisher(),
     )
Exemplo n.º 3
0
 def _change_control_loop_state(self, enabled: int):
     client().simxSetObjectIntParameter(
         self.handle,
         sim.sim_jointintparam_ctrl_enabled,
         enabled,
         client().simxDefaultPublisher(),
     )
Exemplo n.º 4
0
 def target_position(self, radian_angle: Union[int, float]):
     if self.target_position != radian_angle:
         client().simxSetJointTargetPosition(
             self.handle,
             radian_angle,
             client().simxDefaultPublisher(),
         )
         self._target_position = radian_angle
Exemplo n.º 5
0
 def parent(self, parent_handle):
     client().simxSetObjectParent(
         self.handle,
         parent_handle,
         True,
         False,
         client().simxServiceCall(),
     )
Exemplo n.º 6
0
 def name(self, name):
     while True:
         i = 0
         try:
             client().simxSetObjectName(self.handle, f'robot_{name}')
             break
         except RuntimeError:
             name += f'#{i}'
             i += 1
             if i > 10:
                 raise
Exemplo n.º 7
0
 def check_mass(self):
     mass = self.mass
     min_, max_ = client().simxCallScriptFunction(
         'getMassLimit@restrictions_funcs',
         sim.sim_scripttype_customizationscript,
         [],
         client().simxServiceCall(),
     )
     if mass < min_ or mass > max_:
         raise ValueError(
             f'Робот не прошел проверку массы (масса: {mass}, пределы: [{min_}, {max_}])'
         )
Exemplo n.º 8
0
    def __init__(self, joint_name: str):
        """
        Обертка над объектом joint для упрощения базовых операций.

        :param joint_name: имя joint'а (прямо из CoppeliaSim)
        """
        super(Joint, self).__init__(joint_name)

        self._target_velocity = client().simxGetJointTargetVelocity(
            self.handle,
            client().simxServiceCall())
        self._target_position = client().simxGetJointTargetPosition(
            self.handle,
            client().simxServiceCall())
Exemplo n.º 9
0
def loaded_robot(
    model_path: str,
    robot_name: str,
    team_name: str,
    ignore_restrictions: bool = False,
):
    c = client()

    positions = (
        Position('player_1_pos'),
        Position('player_2_pos'),
    )

    for position in positions:
        if not position.busy:
            target_position = position
            break
    else:
        raise RuntimeError('No available positions')

    robot = Robot(model_path)

    try:
        if not ignore_restrictions:
            robot.check_size()
            robot.check_mass()

        robot.move_to_start_position(target_position)
        robot.name = robot_name
        robot.parent = target_position.handle

        yield
    finally:
        c.simxRemoveObjects([robot.handle], 1, c.simxDefaultPublisher())
Exemplo n.º 10
0
    def __init__(self, model_path: str):
        with open(model_path, 'rb') as file:
            buffer = file.read()
        self.handle = client().simxLoadModelFromBuffer(
            buffer,
            client().simxServiceCall())

        self.shapes = client().simxGetObjectsInTree(
            self.handle,
            sim.sim_object_shape_type,
            0,
            client().simxServiceCall(),
        )

        # init objects tree
        RobotObjectsTree(self.handle)
Exemplo n.º 11
0
 def move_to_start_position(self, position: Position):
     c = client()
     c.simxCallScriptFunction(
         f'move_to_start_position@positions',
         sim.sim_scripttype_customizationscript,
         [self.handle, position.obj_name],
         c.simxServiceCall(),
     )
Exemplo n.º 12
0
 def children(self):
     c = client()
     handles = c.simxGetObjectsInTree(
         self.handle,
         sim.sim_handle_all,
         # do not include base itself
         1,
         c.simxServiceCall(),
     )
     return {
         c.simxGetObjectName(handle, False,
                             c.simxServiceCall()).decode('utf-8'): handle
         for handle in handles
     }
Exemplo n.º 13
0
    def check_size(self):
        proper_bbox_size = client().simxCallScriptFunction(
            'checkSize@restrictions_funcs',
            sim.sim_scripttype_customizationscript,
            [self.length, self.width, self.height],
            client().simxServiceCall(),
        )

        if not proper_bbox_size:
            limits = client().simxCallScriptFunction(
                'getSizeLimits@restrictions_funcs',
                sim.sim_scripttype_customizationscript,
                [],
                client().simxServiceCall(),
            )

            raise ValueError(
                'Робот не прошел проверку размеров.\n'
                f'Размер робота: {{\n'
                f'\tlength: {self.length}, \n'
                f'\twidth: {self.width}, \n'
                f'\theight: {self.height}\n'
                f'}}\n'
                f'Границы размера: {json.dumps(limits, indent=4)}')
Exemplo n.º 14
0
    def __init__(self, robot_base_handle: int = None):
        assert robot_base_handle is not None

        c = client()
        handles = c.simxGetObjectsInTree(
            robot_base_handle,
            sim.sim_handle_all,
            # include base
            0,
            c.simxServiceCall(),
        )
        self.objects = {
            c.simxGetObjectName(handle, False, c.simxServiceCall()).decode('utf-8'): handle
            for handle in handles
        }
Exemplo n.º 15
0
 def length(self):
     return client().simxGetModelLength(self.handle,
                                        client().simxServiceCall())
Exemplo n.º 16
0
 def target_position(self, position: Union[int, float]):
     if self.target_position != position:
         client().simxSetJointTargetPosition(
             self.handle, position,
             client().simxDefaultPublisher())
         self._target_velocity = position
Exemplo n.º 17
0
 def height(self):
     return client().simxGetModelHeight(self.handle,
                                        client().simxServiceCall())
Exemplo n.º 18
0
 def mass(self):
     c = client()
     return sum(
         c.simxExecuteScriptString(f'sim.getShapeMassAndInertia({shape})',
                                   c.simxServiceCall())
         for shape in self.shapes)
Exemplo n.º 19
0
 def __init__(self, pos_dummy_name: str):
     c = client()
     self.obj_name = pos_dummy_name
     self.handle = c.simxGetObjectHandle(pos_dummy_name,
                                         c.simxServiceCall())
Exemplo n.º 20
0
 def copy_object_orientation(self, object_handle):
     c = client()
     obj_orientation = c.simxGetObjectOrientation(object_handle, -1,
                                                  c.simxServiceCall())
     c.simxSetObjectOrientation(self.handle, -1, obj_orientation,
                                c.simxServiceCall())
Exemplo n.º 21
0
 def width(self):
     return client().simxGetModelWidth(self.handle,
                                       client().simxServiceCall())