Bootstrap

CoppeliaSim(原Vrep)中实现多关节机械臂的正运动学仿真【CoppeliaSim内嵌脚本lua语言实现】

  • lua实现

本章在动力学建模完成的前提下进行

传送门【CoppeliaSim】(原V-rep)模型文件导入及动力学建模_魚香肉丝盖饭的博客-CSDN博客_vrep导入stl

1)创建脚本

image-20211129122626251

​ 因为用到的RML库函数只能在线程模式下运行,所以添加脚本时要选择线程脚本。

2)代码实现

function sysCall_threadmain()
    
    -- 获取必要的句柄handle(以下四行代码是机械臂标准获取句柄方式)
    jointHandles={}
    for i = 1,4,1 do
        jointHandles[i] = sim.getObjectHandle('RRRR_J'..i)
    end


    -- 设置机械臂运动属性
    -- 属性根据实际状态去设置
    local accel = 0.5
    local jerk  = 0.5
    local vel   = 0.5
    local currentVel   = {0,0,0,0}
    local currentAccel = {0,0,0,0}
    local maxVel       = {vel,vel,vel,1.5*vel}
    local maxAccel     = {accel,accel,accel,accel}
    local maxJerk      = {jerk,jerk,jerk,jerk}
    local targetVel    = {0,0,0,0}
     
    
    
    -- 主循环
    while sim.getSimulationState()~=sim.simulation_advancing_abouttostop do
        -- 先运行到工作状态
        Tarpos = {80*math.pi/180,80*math.pi/180,80*math.pi/180,80*math.pi/180}
        -- 工作姿态
        sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,
            maxVel,maxAccel,maxJerk,Tarpos,targetVel)
        -- 因为只能在线程模式下运行,所以添加脚本时要选择线程脚本
        Tarpos = {0,0,0,0} -- 初始姿态
        sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,
            maxVel,maxAccel,maxJerk,Tarpos,targetVel)
        Tarpos ={-80*math.pi/180,-80*math.pi/180,
            -80*math.pi/180,-80*math.pi/180} -- 工作姿态
        sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,
            maxVel,maxAccel,maxJerk,Tarpos,targetVel)
        Tarpos = {0,0,0,0} -- 初始姿态
        sim.rmlMoveToJointPositions(jointHandles,-1,currentVel,currentAccel,
                maxVel,maxAccel,maxJerk,Tarpos,targetVel)
        -- 以上是四个关节同时运动

        -- 以下是四个关节依次运动

        Tarpos = {80*math.pi/180,80*math.pi/180,80*math.pi/180,80*math.pi/180}
        for i =1,4,1 do
            sim.rmlMoveToJointPositions({jointHandles[i]},-1,{currentVel[i]},{currentAccel[i]},
            {maxVel[i]},{maxAccel[i]},{maxJerk[i]},{Tarpos[i]},{targetVel[i]})
        end
        Tarpos = {0,0,0,0}
        for i =1,4,1 do
            sim.rmlMoveToJointPositions({jointHandles[i]},-1,{currentVel[i]},{currentAccel[i]},
            {maxVel[i]},{maxAccel[i]},{maxJerk[i]},{Tarpos[i]},{targetVel[i]})
        end

        Tarpos ={-80*math.pi/180,-80*math.pi/180,
            -80*math.pi/180,-80*math.pi/180}
        for i =1,4,1 do
            sim.rmlMoveToJointPositions({jointHandles[i]},-1,{currentVel[i]},{currentAccel[i]},
            {maxVel[i]},{maxAccel[i]},{maxJerk[i]},{Tarpos[i]},{targetVel[i]})
        end

        Tarpos = {0,0,0,0} -- 初始姿态
        for i =1,4,1 do
            sim.rmlMoveToJointPositions({jointHandles[i]},-1,{currentVel[i]},{currentAccel[i]},
            {maxVel[i]},{maxAccel[i]},{maxJerk[i]},{Tarpos[i]},{targetVel[i]})
        end
    end
end

function sysCall_cleanup()
    -- Put some clean-up code here
end

3)以上代码的运行结果

coppeliaSim正运动学

4)代码中用到函数的help文档

  • sim.getSimulationState

image-20211129205712395

​ 此函数的返回值:

sim.simulation_stopped  -- 仿真停止
sim.simulation_paused   -- 仿真暂停
sim.simulation_advancing_firstafterstop
sim.simulation_advancing_running
sim.simulation_advancing_lastbeforepause
sim.simulation_advancing_firstafterpause
sim.simulation_advancing_abouttostop
sim.simulation_advancing_lastbeforestop
  • sim.getObjectHandle

image-20211129210700340

  • sim.rmlMoveToJointPositions

image-20211129205605959

;