B0-based remote API modus operandi
基于B0的远程API操作方法
The BØ-based remote API should not be mixed-up with the c remote API (or simply remote API), which is an older version of the remote API that is less flexible, and more difficult to extend.
基于BØ的远程API不应与早期的远程API(或简单的远程API)混淆,后者是远程API的旧版本,灵活性较差,更难扩展。
A B0-based remote API function is called in a similar fashion as a regular API function, however with one major difference:
基于B0的远程API函数的调用方式与常规API函数类似,但有一个主要区别:
Most B0-based remote API functions require one additional argument: the topic or communication channel to use for executing the function call. The topic can be the return value of one of following 5 functions:
大多数基于B0的远程API函数需要一个附加参数:用于执行函数调用的主题或通信通道。主题可以是以下5个函数之一的返回值:
simxServiceCall: this topic allows to execute the function in a blocking mode, i.e. the command will travel to the server (i.e. CoppeliaSim), execute there, and return a response to the client. Use this topic only when fetching command responses from the server as a one-time operation (e.g. simxGetObjectHandle would typically use a service call to execute).
本主题允许在阻塞模式下执行功能,即命令将传输到服务器(即CoppeliaSim),在那里执行,并向客户端返回响应。仅当作为一次性操作从服务器获取命令响应时才使用此主题(例如,simxGetObjectHandle通常使用这个服务去执行)。
simxDefaultPublisher: this topic allows to execute the function in a non-blocking mode, i.e. the function is sent to the server (i.e. CoppeliaSim), and control returns immediately to the client (i.e. the client will not wait for a reply from the server). Use this topic only when sending commands to the server, where you do not expect/need a response from it (e.g. simxSetJointPosition would typically use the default publisher to execute).
本主题允许在非阻塞模式下执行函数,即函数被发送到服务器(即CoppeliaSim),控制立即返回到客户端(即客户端不会等待服务器的回复)。仅当向服务器发送命令时才使用此主题,因为您不希望/不需要服务器的响应(例如,simxSetJointPosition通常使用默认发布服务器来执行)。
simxDefaultSubscriber: this topic informs the server to continuously execute the function, and continuously stream the response to the client. The client will be receiving the response inside of a callback function. Use this topic only when you want to receive the response from the same command continuously executed on the server side. (e.g. simxGetJointForce would typically use the default subscriber to execute). Defined callback functions are called via the simxSpinOnce function (when a response is available in the input buffer).
本主题通知服务器继续执行该函数,并将响应流式传输到客户端。客户端将在回调函数中接收响应。仅当您希望从服务器端连续执行的同一命令接收响应时,才使用此主题。(例如,simxGetJointForce通常使用默认订阅服务器来执行)。定义的回调函数通过simxSpinOnce函数调用(当输入缓冲区中有响应可用时)。
simxCreatePublisher: this is very similar to simxDefaultPublisher, with the difference, that a dedicated publisher topic is created, i.e. a dedicated publishing channel is created. It can be useful to assign specific functions/commands to a dedicated publisher, specially with heavy data (e.g. simxSetVisionSensorImage would typically use a dedicated publisher to execute).
这与simxDefaultPublisher非常相似,不同之处在于创建了一个专用的发布者主题,即创建了一个专用的发布频道。将特定功能/命令分配给专用发布服务器非常有用,特别是在数据量大的情况下(例如,simxSetVisionSensorImage通常会使用专用发布服务器来执行)。
simxCreateSubscriber: this is very similar to simxDefaultSubscriber, with the difference, that a dedicated subscriber topic is created, i.e. a dedicated subscriber channel is created. It can be useful to assign specific functions/commands to a dedicated subscriber, specially with heavy data (e.g. simxGetVisionSensorImage would typically use a dedicated subscriber to execute).
这与simxDefaultSubscriber非常相似,不同之处在于创建了一个专用订户主题,即创建了一个专用订户频道。将特定功能/命令分配给专用订阅者可能很有用,特别是在数据量大的情况下(例如,simxGetVisionSensorImage通常会使用专用订阅者来执行)。
默认情况下,基于B0的远程API客户端和服务器(即CoppeliaSim)将异步运行。但是,可以让客户机单独触发每个模拟步骤,以实现同步操作。以下是同步模式的Python示例:
import b0RemoteApi
import timewith b0RemoteApi.RemoteApiClient('b0RemoteApi_pythonClient','b0RemoteApi') as client: doNextStep=Truedef simulationStepStarted(msg): # simxDefaultSubscriber 的回调函数simTime=msg[1][b'simulationTime'];print('Simulation step started. Simulation time: ',simTime)def simulationStepDone(msg): # simxDefaultSubscriber 的回调函数simTime=msg[1][b'simulationTime'];print('Simulation step done. Simulation time: ',simTime);global doNextStepdoNextStep=Trueclient.simxSynchronous(True)client.simxGetSimulationStepStarted(client.simxDefaultSubscriber(simulationStepStarted)); # 检查当前模拟步骤是否已完成执行client.simxGetSimulationStepDone(client.simxDefaultSubscriber(simulationStepDone));client.simxStartSimulation(client.simxDefaultPublisher())startTime=time.time()while time.time()<startTime+5: if doNextStep:doNextStep=Falseclient.simxSynchronousTrigger() # 如果先前启用了CoppeliaSim同步模式,则触发下一个模拟步骤client.simxSpinOnce() # 调用有消息等待的订阅者的所有回调,然后返回client.simxStopSimulation(client.simxDefaultPublisher())