【动画】python脚本通过 b0RemoteApi 与 Coppeliasim 通讯

4f1a0e87c891f9aee074308f81b295cd.png

仿真场景

操作演示

部分源码

Coppeliasim部分脚本:

--panda脚本  执行运动函数,被python脚本调用
function executeMovement(id)
    movementToExecute[#movementToExecute+1]=id
end


--运动字典 赋值操作。被python脚本调用
function movementDataFunction(movData)
    allMovementData[movData.id]=movData
end
--主循环
function coroutineStart()
    while true do
        if #movementToExecute>0 then --运动序列非零
            local id=table.remove(movementToExecute,1)  --取出一条运动的索引
            local movementData=allMovementData[id] --根据索引取出运动数据
            allMovementData[id]=nil   --将运动数据数组  对应索引数据置位nil
            if movementData.type=='mov' then --mov类型运动数据
                local currentConfig={}  
                for i=1,#jointHandles,1 do
                    currentConfig[i]=sim.getJointPosition(jointHandles[i])--获取关节当前位置数组
                end
        --执行运动。  更新新的位置,当前速度,当前加速度
                newPos,currentVel,currentAccel=sim.moveToConfig(-1,currentConfig,currentVel,currentAccel,movementData.maxVel,movementData.maxAccel,maxJerk,movementData.targetConfig,movementData.targetVel,movCallback,jointHandles)
            end
            if movementData.type=='pts' then  --ptp类型运动数据
                executePtpMovement(jointHandles,movementData)   --执行ptp运动
            end
            sim.setStringSignal(stringSignalName,id)  --字符串信号: 当前已执行的运动索引
        else
            sim.switchThread() -- in a coroutine will execute a yield instead
        end
    end
end
-- 执行ptp运动
function executePtpMovement(handles,data)
    --以插值方式应用关节配置:
    local lb=sim.setThreadAutomaticSwitch(false) --禁用线程自动切换
    local path={}   --插值的路径点数组
    for i=1,#data.times,1 do     --遍历运动数据的 times数组,取出路径点
        path[(i-1)*7+1]=data.j1[i]  
        path[(i-1)*7+2]=data.j2[i]
        path[(i-1)*7+3]=data.j3[i]
        path[(i-1)*7+4]=data.j4[i]
        path[(i-1)*7+5]=data.j5[i]
        path[(i-1)*7+6]=data.j6[i]
        path[(i-1)*7+7]=data.j7[i]
    end
    local startTime=sim.getSimulationTime()  --获取当前仿真时间
    local t=0 --已运动时间
    while t<data.times[#data.times] do  --t小于最后一个时间点
        local conf=sim.getPathInterpolatedConfig(path,data.times,t)  --取出插值点:轴配置
        applyJointTargetPositions(handles,conf)  --应用关节目标位置 
        sim.switchThread()
        t=sim.getSimulationTime()-startTime  --更新t
    end
    local conf=sim.getPathInterpolatedConfig(path,data.times,data.times[#data.times]) --取出最后一个路径点 轴配置
    applyJointTargetPositions(handles,conf) --应用关节目标位置
    sim.setThreadAutomaticSwitch(lb) --取消禁用自动切换
end

Python脚本:

import b0RemoteApi
import math


# 常数
d2r = math.pi / 180  # 转换系数:度 到 弧度


# 在 CoppeliaSim 中打开与 Panda 的连接
with b0RemoteApi.RemoteApiClient('b0RemoteApi_pythonClient', 'b0RemoteApi', 60) as client:
    client.executedMovId = 'notReady'  #执行运动的id:初始时为 notReady


    targetArm = 'Panda' #目标机械臂


    stringSignalName = targetArm + '_executedMovId'  #存储机械臂运动ID  字符串信号名


    # 等待 id=ready、movSeq1或者movSeq2 等 执行完毕
    def waitForMovementExecuted(id):
        while client.executedMovId != id:  
            client.simxSpinOnce()   


  #获取执行的运动id
    def executedMovId_callback(msg):
        if type(msg[1]) == bytes:
            msg[1] = msg[1].decode('ascii')  #python2/python3的区别
        client.executedMovId = msg[1]


    # 感知回调函数
    # 获取时间
    def timeCallback(msg):
        client.time = msg[1]


    # 获取关节1位置
    def jointAngleCallback(msg):
        jointAngle = msg[1]
        print('Joint 1: '+str(jointAngle))


    # 获取末端执行器位置 x,y,z 
    def endeffectorCallback(msg):
        position = msg[1]
        print('time: ' + str(client.time))
        print('x: ' + str(position[0]))
        print('y: ' + str(position[1]))
        print('z: ' + str(position[2]))


    # 获取关节句柄
    numberOfJoints = 7
    jointHandles = []
    # 关节的对象句柄
    for i in range(numberOfJoints):
        _, handle = client.simxGetObjectHandle('Panda_joint' + str(i + 1), client.simxServiceCall())
        jointHandles.append(handle)


    # 末端执行器的对象句柄
    _, endeffector = client.simxGetObjectHandle('Panda_gripper', client.simxServiceCall())


    # 获取仿真时间
    client.simxGetSimulationTime(client.simxDefaultSubscriber(timeCallback))


    # 启动关节1角度和末端执行器坐标的数据流
    client.simxGetJointPosition(jointHandles[0], client.simxDefaultSubscriber(jointAngleCallback))
    client.simxGetObjectPosition(endeffector, -1, client.simxDefaultSubscriber(endeffectorCallback))


    #订阅 stringSignalName 字符串信号:
    client.simxGetStringSignal(stringSignalName, client.simxDefaultSubscriber(executedMovId_callback))


    # pts 运动序列的硬编码坐标
    times = [0.000, 0.050, 0.100, 0.150, 0.200, 0.250, 0.300, 0.350, 0.400, 0.450, 0.500, 0.550, 0.600, 0.650, 0.700,
             0.750, 0.800, 0.850, 0.900, 0.950, 1.000, 1.050, 1.100, 1.150, 1.200, 1.250, 1.300, 1.350, 1.400, 1.450,
             1.500, 1.550, 1.600, 1.650, 1.700, 1.750, 1.800, 1.850, 1.900, 1.950, 2.000, 2.050, 2.100, 2.150, 2.200,
             2.250, 2.300, 2.350, 2.400, 2.450, 2.500, 2.550, 2.600, 2.650, 2.700, 2.750, 2.800, 2.850, 2.900, 2.950,
             3.000, 3.050, 3.100, 3.150, 3.200, 3.250, 3.300, 3.350, 3.400, 3.450, 3.500, 3.550, 3.600, 3.650, 3.700,
             3.750, 3.800, 3.850, 3.900, 3.950, 4.000, 4.050, 4.100, 4.150, 4.200, 4.250, 4.300, 4.350, 4.400, 4.450,
             4.500, 4.550, 4.600, 4.650, 4.700, 4.750, 4.800, 4.850, 4.900, 4.950, 5.000, 5.050, 5.100, 5.150, 5.200,
             5.250, 5.300, 5.350, 5.400, 5.450, 5.500, 5.550, 5.600, 5.650, 5.700, 5.750, 5.800, 5.850, 5.900, 5.950,
             6.000, 6.050, 6.100, 6.150, 6.200, 6.250, 6.300, 6.350]
    j1 = [0.000, 0.000, 0.002, 0.009, 0.022, 0.042, 0.068, 0.100, 0.139, 0.185, 0.237, 0.296, 0.360, 0.431, 0.506,
          0.587, 0.669, 0.753, 0.838, 0.923, 1.008, 1.091, 1.170, 1.243, 1.308, 1.365, 1.414, 1.455, 1.491, 1.519,
          1.541, 1.557, 1.566, 1.564, 1.556, 1.543, 1.524, 1.498, 1.465, 1.426, 1.380, 1.328, 1.270, 1.205, 1.136,
          1.065, 0.994, 0.922, 0.849, 0.777, 0.705, 0.632, 0.560, 0.487, 0.415, 0.342, 0.270, 0.197, 0.125, 0.053,
          -0.020, -0.092, -0.165, -0.237, -0.309, -0.382, -0.454, -0.527, -0.599, -0.671, -0.744, -0.816, -0.888,
          -0.961, -1.033, -1.106, -1.178, -1.250, -1.323, -1.394, -1.462, -1.523, -1.556, -1.595, -1.632, -1.664,
          -1.690, -1.709, -1.723, -1.729, -1.730, -1.723, -1.710, -1.691, -1.665, -1.632, -1.593, -1.548, -1.495,
          -1.437, -1.372, -1.302, -1.226, -1.146, -1.064, -0.980, -0.895, -0.810, -0.724, -0.638, -0.552, -0.469,
          -0.390, -0.318, -0.254, -0.199, -0.151, -0.110, -0.076, -0.048, -0.027, -0.012, -0.004, -0.001, -0.001,
          -0.000, -0.000, -0.000]
    j2 = [0.000, 0.000, 0.002, 0.009, 0.022, 0.042, 0.068, 0.100, 0.140, 0.185, 0.237, 0.296, 0.361, 0.431, 0.507,
          0.587, 0.670, 0.754, 0.838, 0.924, 1.009, 1.092, 1.171, 1.243, 1.308, 1.365, 1.414, 1.455, 1.491, 1.519,
          1.541, 1.557, 1.566, 1.564, 1.557, 1.544, 1.529, 1.513, 1.497, 1.481, 1.465, 1.449, 1.432, 1.416, 1.400,
          1.384, 1.367, 1.351, 1.335, 1.319, 1.303, 1.286, 1.270, 1.254, 1.238, 1.221, 1.205, 1.189, 1.173, 1.157,
          1.140, 1.124, 1.108, 1.092, 1.075, 1.059, 1.043, 1.027, 1.010, 0.994, 0.978, 0.961, 0.945, 0.929, 0.913,
          0.896, 0.880, 0.864, 0.848, 0.831, 0.815, 0.799, 0.786, 0.769, 0.749, 0.730, 0.710, 0.689, 0.669, 0.649,
          0.629, 0.609, 0.589, 0.569, 0.548, 0.528, 0.508, 0.488, 0.468, 0.448, 0.427, 0.407, 0.387, 0.367, 0.347,
          0.327, 0.306, 0.286, 0.266, 0.246, 0.226, 0.206, 0.186, 0.166, 0.146, 0.125, 0.105, 0.084, 0.064, 0.044,
          0.025, 0.012, 0.004, 0.001, 0.000, 0.000, 0.000, 0.000]
    j3 = [0.000, 0.000, -0.002, -0.009, -0.022, -0.042, -0.068, -0.100, -0.139, -0.185, -0.237, -0.296, -0.361, -0.433,
          -0.511, -0.595, -0.681, -0.767, -0.854, -0.942, -1.027, -1.107, -1.182, -1.249, -1.311, -1.365, -1.414,
          -1.455, -1.491, -1.519, -1.541, -1.557, -1.566, -1.564, -1.556, -1.543, -1.524, -1.498, -1.465, -1.426,
          -1.381, -1.328, -1.270, -1.205, -1.133, -1.055, -0.971, -0.885, -0.798, -0.711, -0.624, -0.537, -0.450,
          -0.362, -0.275, -0.188, -0.101, -0.013, 0.074, 0.161, 0.249, 0.336, 0.423, 0.510, 0.598, 0.685, 0.772, 0.859,
          0.947, 1.032, 1.112, 1.186, 1.253, 1.314, 1.369, 1.416, 1.458, 1.492, 1.521, 1.542, 1.557, 1.566, 1.564,
          1.557, 1.544, 1.524, 1.498, 1.466, 1.427, 1.383, 1.338, 1.293, 1.247, 1.201, 1.155, 1.110, 1.064, 1.018,
          0.972, 0.926, 0.881, 0.835, 0.789, 0.743, 0.697, 0.652, 0.606, 0.560, 0.514, 0.468, 0.423, 0.377, 0.331,
          0.285, 0.239, 0.194, 0.149, 0.109, 0.076, 0.048, 0.027, 0.012, 0.004, 0.002, 0.001, 0.000, 0.000, 0.000]
    j4 = [0.000, 0.000, 0.002, 0.009, 0.022, 0.042, 0.068, 0.100, 0.139, 0.185, 0.237, 0.296, 0.361, 0.433, 0.511,
          0.595, 0.681, 0.768, 0.855, 0.942, 1.027, 1.107, 1.181, 1.249, 1.310, 1.365, 1.413, 1.455, 1.490, 1.519,
          1.541, 1.556, 1.565, 1.567, 1.574, 1.587, 1.603, 1.619, 1.636, 1.653, 1.670, 1.686, 1.703, 1.720, 1.737,
          1.754, 1.771, 1.788, 1.805, 1.822, 1.839, 1.856, 1.873, 1.890, 1.907, 1.923, 1.940, 1.957, 1.974, 1.991,
          2.008, 2.025, 2.042, 2.059, 2.076, 2.093, 2.110, 2.127, 2.144, 2.161, 2.178, 2.195, 2.212, 2.229, 2.246,
          2.263, 2.280, 2.297, 2.314, 2.331, 2.344, 2.352, 2.350, 2.343, 2.330, 2.310, 2.284, 2.252, 2.212, 2.167,
          2.115, 2.056, 1.991, 1.919, 1.841, 1.760, 1.679, 1.597, 1.515, 1.433, 1.350, 1.268, 1.186, 1.104, 1.022,
          0.940, 0.858, 0.776, 0.694, 0.612, 0.530, 0.452, 0.379, 0.312, 0.252, 0.198, 0.151, 0.110, 0.076, 0.048,
          0.027, 0.012, 0.004, 0.002, 0.001, 0.000, 0.000, 0.000]
    j5 = [0.000, 0.000, 0.002, 0.009, 0.022, 0.042, 0.068, 0.100, 0.139, 0.185, 0.237, 0.296, 0.361, 0.433, 0.511,
          0.595, 0.681, 0.768, 0.855, 0.942, 1.028, 1.108, 1.182, 1.250, 1.311, 1.366, 1.414, 1.455, 1.491, 1.519,
          1.541, 1.557, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.567, 1.567, 1.567, 1.567,
          1.567, 1.567, 1.567, 1.567, 1.567, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568,
          1.568, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.570, 1.570, 1.570, 1.570,
          1.570, 1.570, 1.570, 1.570, 1.570, 1.571, 1.571, 1.568, 1.561, 1.548, 1.529, 1.503, 1.470, 1.431, 1.388,
          1.342, 1.297, 1.251, 1.205, 1.159, 1.113, 1.067, 1.021, 0.975, 0.929, 0.883, 0.837, 0.791, 0.745, 0.699,
          0.653, 0.607, 0.561, 0.516, 0.470, 0.424, 0.378, 0.332, 0.286, 0.240, 0.194, 0.149, 0.109, 0.076, 0.048,
          0.027, 0.012, 0.004, 0.002, 0.001, 0.000, 0.000, 0.000]
    j6 = [0.000, 0.000, 0.002, 0.009, 0.022, 0.042, 0.068, 0.100, 0.139, 0.185, 0.237, 0.296, 0.361, 0.433, 0.511,
          0.595, 0.681, 0.768, 0.855, 0.942, 1.027, 1.108, 1.182, 1.249, 1.311, 1.365, 1.414, 1.455, 1.491, 1.519,
          1.541, 1.557, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.567, 1.567, 1.567, 1.567,
          1.567, 1.567, 1.567, 1.567, 1.567, 1.567, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568,
          1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.570, 1.570, 1.570, 1.570, 1.570,
          1.570, 1.570, 1.570, 1.570, 1.571, 1.571, 1.571, 1.569, 1.561, 1.548, 1.529, 1.503, 1.470, 1.431, 1.388,
          1.343, 1.297, 1.251, 1.205, 1.159, 1.113, 1.067, 1.021, 0.975, 0.929, 0.883, 0.837, 0.791, 0.745, 0.699,
          0.653, 0.607, 0.561, 0.515, 0.470, 0.424, 0.378, 0.332, 0.286, 0.240, 0.194, 0.149, 0.109, 0.076, 0.048,
          0.027, 0.012, 0.004, 0.002, 0.001, 0.000, 0.000, 0.000]
    j7 = [0.000, 0.000, 0.002, 0.009, 0.022, 0.042, 0.068, 0.100, 0.139, 0.185, 0.237, 0.296, 0.361, 0.433, 0.511,
          0.595, 0.681, 0.768, 0.855, 0.942, 1.027, 1.108, 1.182, 1.249, 1.311, 1.365, 1.414, 1.455, 1.491, 1.519,
          1.541, 1.557, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.566, 1.567, 1.567, 1.567, 1.567,
          1.567, 1.567, 1.567, 1.567, 1.567, 1.567, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568, 1.568,
          1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.569, 1.570, 1.570, 1.570, 1.570, 1.570,
          1.570, 1.570, 1.570, 1.570, 1.571, 1.571, 1.571, 1.569, 1.561, 1.548, 1.529, 1.503, 1.470, 1.431, 1.388,
          1.343, 1.297, 1.251, 1.205, 1.159, 1.113, 1.067, 1.021, 0.975, 0.929, 0.883, 0.837, 0.791, 0.745, 0.699,
          0.653, 0.607, 0.561, 0.515, 0.470, 0.424, 0.378, 0.332, 0.286, 0.240, 0.194, 0.149, 0.109, 0.076, 0.048,
          0.027, 0.012, 0.004, 0.002, 0.001, 0.000, 0.000, 0.000]


    # 设置完所有数据流后,我们可以开始仿真
    client.simxStartSimulation(client.simxServiceCall())
    print('Simulation start')


    # 等到准备好
    waitForMovementExecuted('ready')
    print('los')


    #####################################################################################################
    # 驱动类型“pts”


  # 如果你想将你的关节数据生成为 np 数组,
    # 您必须通过 .tolist 方法 将数组转换为列表


    # 生成运动字典
    print('pts Movement sequence')
    movementData = {"id": "movSeq1", "type": "pts", "times": times, "j1": j1, "j2": j2, "j3": j3, "j4": j4,
                    "j5": j5, "j6": j6, "j7": j7}


    # 将运动序列发送到仿真,调用运动数据函数    运动字典 赋值给脚本变量
    client.simxCallScriptFunction('movementDataFunction@' + targetArm, 'sim.scripttype_childscript', movementData,
                                  client.simxDefaultPublisher())


    # 执行运动序列:执行id为 movSeq1 的 运动。调用【targetArm = 'Panda' 】的脚本函数executeMovement
    client.simxCallScriptFunction('executeMovement@' + targetArm, 'sim.scripttype_childscript', 'movSeq1',
                                  client.simxDefaultPublisher())


    # 等到上述动作序列1执行完毕:
    waitForMovementExecuted('movSeq1')


    ###############################################################################################
    # 执行类型 "mov"


    # 设置一些运动值:
    mVel = 60 * d2r  #低于 pi/3 的最大速度将破坏系统
    mAccel = 5 * d2r  
    maxVel = [mVel, mVel, mVel, mVel, mVel, mVel, mVel]
    maxAccel = [mAccel, mAccel, mAccel, mAccel, mAccel, mAccel, mAccel]
    initConfig = [0, 0, 0, 0, 0, 90 * d2r, 45 * d2r]  #初始关节配置


    # 第一个移动序列  目标关节配置,目标速度。
    targetConfig = [45 * d2r, 45 * d2r, 0 * d2r, -80 * d2r, 0 * d2r, 125 * d2r, 45 * d2r]
    targetVel = [0, 0, 0, 0, 0, 0, 0]


    # 生成运动字典 运动序列2:movSeq2
    movementData1 = {"id": "movSeq2", "type": "mov", "targetConfig": targetConfig, "targetVel": targetVel,
                    "maxVel": maxVel, "maxAccel": maxAccel}


    #将运动序列发送到仿真
    client.simxCallScriptFunction('movementDataFunction@' + targetArm, 'sim.scripttype_childscript', movementData1,
                                  client.simxDefaultPublisher())


    #执行移动序列
    client.simxCallScriptFunction('executeMovement@' + targetArm, 'sim.scripttype_childscript', 'movSeq2',
                                  client.simxDefaultPublisher())


    # 等到上述动作序列执行完毕:
    waitForMovementExecuted('movSeq2')


    #################################################################################################
    #结束仿真
    client.simxStopSimulation(client.simxServiceCall())

笔记

table.remove()##

·        原型:table.remove (tab_table [, pos])

·        解释:在表tab_table的pop位置删除元素,并且的必要时移动其他的元素(一般是将这个元素之后的元素向前移动),如果删除成功函数返回被删除的值,第二个参数pos,默认值为数组长度,也就是省略第二个参数时,会删除table的最后一个元素。

BØ-based remote API

基于B0的远程API允许从外部应用程序或远程硬件(例如,真实的机器人,远程计算机等)控制仿真(或仿真器本身)。基于CoppeliaSim B0的远程API由大约一百个特定函数和一个通用函数组成,可以从C ++应用程序,Python脚本,Java应用程序,Matlab程序或Lua脚本中调用。基于B0的远程API函数通过BlueZero中间件及其与CoppeliaSim的接口插件与CoppeliaSim进行交互。所有这些对用户而言都是隐藏的。远程API可以让一个或几个外部应用程序以同步或异步的方式(默认情况下为异步)与CoppeliaSim进行交互,甚至还支持对仿真器的远程控制(例如,远程加载场景,开始,暂停或停止仿真)。
“同步”一词的含义是每个仿真周期都与远程API应用程序同步运行(即,仿真器将等待来自客户端的触发信号,以在时间t + dt处开始下一个仿真周期)。阻塞/非阻塞操作与同步/异步不同。远程API还支持阻塞和非阻塞操作。基于B0的远程API函数的调用方式与常规API函数的调用方式相似,但是有一个主要区别:

大多数基于B0的远程API函数都需要一个附加参数:用于执行函数调用的话题或通信渠道。该话题可以是以下5个函数之一的返回值:

simxServiceCall 此话题允许以阻塞模式执行该功能,即命令将传递到服务器(即CoppeliaSim),在服务器上执行,然后将响应返回给客户端。仅当为了从服务器获取命令响应(一次性操作)时才使用此话题(例如simxGetObjectHandle通常使用ServiceCall来执行)。
simxDefaultPublisher 此话题允许以非阻塞模式执行该功能,即该功能被发送到服务器(即CoppeliaSim),并且控制权立即返回给客户端(即客户端将不等待服务器的答复)。仅在不希望/不需要服务器响应的服务器上发送命令时才使用此主题(例如,simxSetJointPosition通常将使用DefaultPublisher来执行)。
simxDefaultSubscriber:此话题通知服务器继续执行功能,并将响应连续流式传输到客户端。客户端将在回调函数中接收响应。仅当您希望从服务器端连续执行的同一命令接收响应时,才使用此主题。(例如simxGetJointForce通常会使用DefaultSubscriber执行)。定义的回调函数通过simxSpinOnce函数调用(当输入缓冲区中有响应时)。
simxCreatePublisher:这与simxDefaultPublisher非常相似,不同之处在于创建了专用的发布者话题,即创建了专用的发布渠道。将特定的功能/命令分配给专用的发布者可能很有用,特别是对于大量数据(例如simxSetVisionSensorImage通常会使用专用的发布者来执行)。
simxCreateSubscriber:这与simxDefaultSubscriber非常相似,不同之处在于,创建了专用的订户主题,即创建了专用的订户频道。将特定的功能/命令分配给专用订户,尤其是处理大量数据(例如,simxGetVisionSensorImage通常将使用专用订户执行),可能会很有用。

客户端(即您的应用程序):客户端上基于B0的远程API可用于许多不同的编程语言。当前支持以下语言:C ++,Python,Java,Matlab和Lua。您可以轻松地创建其他语言的绑定。服务器端(即CoppeliaSim):服务器端基于B0的远程API是通过CoppeliaSim插件和Lua脚本(lua /b0RemoteApiServer.lua)实现的。该插件应在启动时由CoppeliaSim加载:simExtBlueZero.dll,libsimExtBlueZero.dylib或libsimExtBlueZero.so。

客户端

API的所有单位均以米,千克,秒和弧度或它们的组合为单位(除非另有明确说明)。

要在 C ++应用程序 中使用基于B0的远程API功能,只需在项目中包括以下文件:

1.    programming/remoteApiBindings/b0Based/cpp/b0RemoteApi.h

2.    programming/remoteApiBindings/b0Based/cpp/b0RemoteApi.h

3.    包括路径 programming/remoteApiBindings/b0Based/cpp/msgpack-c/include

4.    包括路径 programming/blueZero/include/b0/bindings

5.    链接blueZero(例如b0.dll),并且不要忘记blueZero库本身具有依赖项(例如libzmq,boost_chrono,boost_system,boost_thread等)。

请查看programming / remoteApiBindings /b0Based / cpp / simpleTest项目文件,以及相应的基于B0的演示场景RemoteApiDemo.ttt,以了解更多详细信息。

要在Python脚本中使用基于B0的远程API功能,需要执行以下操作:

1.    为Python安装MessagePack: pip install msgpack

2.    programming/remoteApiBindings/b0Based/python/b0RemoteApi.py

3.    programming/remoteApiBindings/b0Based/python/b0.py

4.    链接blueZero(例如b0.dll),并且不要忘记blueZero库本身具有依赖项(例如libzmq,boost_chrono,boost_system,boost_thread等)。

有关其他详细信息,请参阅programming / remoteApiBindings / b0Based/ python / simpleTest.py程序,以及基于B0的演示场景RemoteApiDemo.ttt。

参考

https://coppeliarobotics.com/helpFiles/en/b0RemoteApiClientSide.htm

https://blog.csdn.net/qq_29696095/article/details/104466293

https://www.jianshu.com/p/abecc0841933


The End

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中使用b0 remote apiCoppeliaSim(以前是V-REP)交互的步骤与与V-REP交互的步骤类似。下面是具体的步骤: 1. 安装b0库:可以通过以下命令在终端中使用pip安装b0库: ``` pip install pyzmq ``` 2. 下载CoppeliaSim提供的b0 remote api文件,并将其解压缩到任意目录中。 3. 在Python脚本中导入b0库和需要使用的CoppeliaSim远程API函数。例如: ```python import time import zmq import sim context = zmq.Context() socket = context.socket(zmq.REQ) ``` 4. 连接到CoppeliaSimb0 remote api服务器。例如: ```python socket.connect("tcp://127.0.0.1:19997") ``` 这里的IP地址和端口号应该与你在CoppeliaSim中启动的远程API服务器的地址和端口号相同。 5. 调用CoppeliaSim的远程API函数。例如: ```python # 获取物体句柄 res, obj_handle = sim.simxGetObjectHandle(clientID, "object_name", sim.simx_opmode_blocking) # 设置物体位置 res = sim.simxSetObjectPosition(clientID, obj_handle, -1, [x, y, z], sim.simx_opmode_blocking) ``` 在这个例子中,我们使用了CoppeliaSim提供的simxGetObjectHandle和simxSetObjectPosition函数来获取物体句柄并设置物体位置。需要注意的是,对于每个函数调用,我们都需要传递一个clientID参数,这个参数是在连接到CoppeliaSimb0 remote api服务器时返回的。 6. 关闭与CoppeliaSim的连接。例如: ```python sim.simxFinish(clientID) ``` 完整的Python示例代码如下: ```python import time import zmq import sim # 连接到b0 remote api服务器 context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect("tcp://127.0.0.1:19997") # 开始CoppeliaSim的远程API客户端 sim.simxFinish(-1) clientID = sim.simxStart("127.0.0.1", 19997, True, True, 5000, 5) if clientID != -1: print("Connected to remote API server") # 获取物体句柄 res, obj_handle = sim.simxGetObjectHandle(clientID, "object_name", sim.simx_opmode_blocking) # 设置物体位置 res = sim.simxSetObjectPosition(clientID, obj_handle, -1, [x, y, z], sim.simx_opmode_blocking) # 关闭与CoppeliaSim的连接 sim.simxFinish(clientID) else: print("Failed to connect to remote API server") # 关闭b0 remote api连接 socket.close() context.term() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值