def target_velocity(self, velocity: Union[int, float]): if self.target_velocity != velocity: client().simxSetJointTargetVelocity( self.handle, velocity, client().simxDefaultPublisher()) self._target_velocity = velocity
def _change_motor_state(self, enabled: int): client().simxSetObjectIntParameter( self.handle, sim.sim_jointintparam_motor_enabled, enabled, client().simxDefaultPublisher(), )
def _change_control_loop_state(self, enabled: int): client().simxSetObjectIntParameter( self.handle, sim.sim_jointintparam_ctrl_enabled, enabled, client().simxDefaultPublisher(), )
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
def parent(self, parent_handle): client().simxSetObjectParent( self.handle, parent_handle, True, False, client().simxServiceCall(), )
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
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_}])' )
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())
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())
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)
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(), )
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 }
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)}')
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 }
def length(self): return client().simxGetModelLength(self.handle, client().simxServiceCall())
def target_position(self, position: Union[int, float]): if self.target_position != position: client().simxSetJointTargetPosition( self.handle, position, client().simxDefaultPublisher()) self._target_velocity = position
def height(self): return client().simxGetModelHeight(self.handle, client().simxServiceCall())
def mass(self): c = client() return sum( c.simxExecuteScriptString(f'sim.getShapeMassAndInertia({shape})', c.simxServiceCall()) for shape in self.shapes)
def __init__(self, pos_dummy_name: str): c = client() self.obj_name = pos_dummy_name self.handle = c.simxGetObjectHandle(pos_dummy_name, c.simxServiceCall())
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())
def width(self): return client().simxGetModelWidth(self.handle, client().simxServiceCall())