importsocketfromcompas.geometryimportFramefromcompas.geometryimportmatrix_from_framefromcompas.robotsimportConfigurationfromcompas_fab.backends.exceptionsimportBackendError__all__=['VrepError',]DEFAULT_OP_MODE=0x010000# defined in vrepConst.simx_opmode_blocking, but redefined here to prevent loading the remoteApi library# --------------------------------------------------------------------------# MAPPINGS# The following mapping functions are only internal to make sure# all transformations from and to V-REP are consistent# --------------------------------------------------------------------------defvrep_pose_to_frame(pose,scale):returnFrame.from_list(floats_from_vrep(pose,scale))defframe_to_vrep_pose(frame,scale):# COMPAS FAB uses meters, just like V-REP,# so in general, scale should always be 1pose=matrix_from_frame(frame)pose[0][3]=pose[0][3]/scalepose[1][3]=pose[1][3]/scalepose[2][3]=pose[2][3]/scalereturnpose[0]+pose[1]+pose[2]+pose[3]defconfig_from_vrep(list_of_floats,scale):# COMPAS FAB uses radians and meters, just like V-REP,# so in general, scale should always be 1radians=list_of_floats[3:]prismatic_values=map(lambdav:v*scale,list_of_floats[0:3])returnConfiguration.from_prismatic_and_revolute_values(prismatic_values,radians)defconfig_to_vrep(config,scale):# COMPAS FAB uses radians and meters, just like V-REP,# so in general, scale should always be 1values=list(map(lambdav:v/scale,config.prismatic_values))values.extend(config.revolute_values)returnvaluesdeffloats_to_vrep(list_of_floats,scale):return[v/scaleforvinlist_of_floats]deffloats_from_vrep(list_of_floats,scale):return[v*scaleforvinlist_of_floats]defassert_robot(robot):ifnotrobot:raiseValueError('No instance of robot found')ifnotrobot.model:raiseValueError('The robot instance has no model information attached')if'index'notinrobot.model.attr:raiseValueError('Robot model needs to define an index as part of the model.attr dictionary')# --------------------------------------------------------------------------# NETWORKING HELPERS# A couple of simple networking helpers for host name resolution# --------------------------------------------------------------------------defis_ipv4_address(addr):try:socket.inet_aton(addr)returnTrueexceptsocket.error:returnFalsedefresolve_host(host):ifis_ipv4_address(host):returnhostelse:returnsocket.gethostbyname(host)
[docs]classVrepError(BackendError):"""Wraps an exception that occurred inside the simulation engine."""def__init__(self,message,error_code):super(VrepError,self).__init__('Error code: '+str(error_code)+'; '+message)self.error_code=error_code