40623130 陳鉅忠 的個人網站

Task-2 << Previous Next >> Vrep手足球-All_Version

Vrep手足球-lua

測試方向鍵於手足球的人偶自由度&code設定 :

RJ1 : 為人偶前後踢球的軸代稱 \ RJ2 : 為控制人偶左右的桿子的軸代稱

樹狀圖為gif中所表示

下方為Lua code --->用於Vrep3.6.1版本中

function sysCall_init()  
    right_left_handle= sim.getObjectHandle('RJ1') 
    up_down_handle= sim.getObjectHandle('RJ2') 
    MaxVel=5 
    right_left_velocity=0 
    up_down_velocity=0 
    dVel=0.5; 
    sim.setJointTargetVelocity(right_left_handle,right_left_velocity) 
    sim.setJointTargetVelocity(up_down_handle,up_down_velocity) 
end 
 
function sysCall_actuation()  
    message,auxiliaryData=sim.getSimulatorMessage() 
    while message~=-1 do 
        if (message==sim.message_keypress) then 
            if (auxiliaryData[1]==32) then 
                -- right key 
                right_left_velocity=0 
                up_down_velocity=0 
                sim.setJointForce(right_left_handle, 0) 
                sim.setJointForce(up_down_handle, 0) 
                break 
            else 
                sim.setJointForce(right_left_handle, 10000) 
                sim.setJointForce(up_down_handle, 10000) 
            end 
            if (auxiliaryData[1]==2007) then 
                -- left key 
                right_left_velocity=right_left_velocity+dVel*1.3 
            end 
            if (auxiliaryData[1]==2008) then 
                -- right key 
                right_left_velocity=right_left_velocity-dVel*1.3 
            end 
            if (auxiliaryData[1]==2009) then 
                -- up key 
                up_down_velocity=up_down_velocity+dVel/10 
            end 
            if (auxiliaryData[1]==2010) then 
                -- down key 
                up_down_velocity=up_down_velocity-dVel/10 
            end 
        end 
        message,auxiliaryData=sim.getSimulatorMessage()
    end
    if right_left_velocity>MaxVel then
        right_left_velocity=-5
    end
    if right_left_velocity<-MaxVel then
        right_left_velocity=5
    end
    if up_down_velocity>MaxVel then
        up_down_velocity=0
    end
    if up_down_velocity<-MaxVel then
        up_down_velocity=0
    end
    sim.setJointTargetVelocity(right_left_handle,right_left_velocity)
    sim.setJointTargetVelocity(up_down_handle,up_down_velocity)
end

改寫code=python to lua並簡化

優點:

python : 可執行運算值較大的編譯

lua : Vrep內部沿用 , 延遲現象較少

缺點:

python : 會有爆ping問題導致延遲

lua : 太多運算時直接停止

unction sysCall_init()
    KickBallV = 10
    R_KickBallVel = (math.pi/180)*KickBallV
    B_KickBallVel = -(math.pi/180)*KickBallV
    Sphere_handle=sim.getObjectHandle('Sphere')
    BRod_handle=sim.getObjectHandle('BRod')
    BRev_handle=sim.getObjectHandle('BRev')
    BMo_handle=sim.getObjectHandle('BMo')
    --
    RRod_handle=sim.getObjectHandle('RRod')
    RRev_handle=sim.getObjectHandle('RRev')
    RMo_handle=sim.getObjectHandle('RMo')
end
function sysCall_actuation()
    position_BR=sim.getObjectPosition(BRod_handle,-1)
    position_S=sim.getObjectPosition(Sphere_handle,-1)
    X =position_S[1] - position_BR[1]
    Y =position_S[2] - position_BR[2]
    if 1 then
        if X <= 0.02 and Y <= 0 then
            sim.setJointTargetVelocity(BRev_handle,B_KickBallVel)
            sim.setJointTargetVelocity(BRev_handle,-1)
        elseif X > 0.02 and Y <= 0 then
            sim.setJointTargetVelocity(BRev_handle,B_KickBallVel)
            sim.setJointTargetVelocity(BRev_handle,1)
        elseif X <= 0.02 and Y > 0 then           
            sim.setJointTargetVelocity(BRev_handle,R_KickBallVel)
            sim.setJointTargetVelocity(BRev_handle,-1)       
        elseif X > 0.02 and Y > 0 then           
            sim.setJointTargetVelocity(BRev_handle,R_KickBallVel)    
            sim.setJointTargetVelocity(BRev_handle,1)
        end
        YYYYY = Y*5
        sim.setJointTargetVelocity(BMo_handle,YYYYY)
    end
---
    position_RR=sim.getObjectPosition(RRod_handle,-1)
    X =position_S[1] - position_RR[1]
    Y =position_S[2] - position_RR[2]
    if 1 then
        if X <= 0.02 and Y <= 0 then
            sim.setJointTargetVelocity(RRev_handle,B_KickBallVel)
            sim.setJointTargetVelocity(RRev_handle,-1)
        elseif X > 0.02 and Y <= 0 then
            sim.setJointTargetVelocity(RRev_handle,B_KickBallVel)
            sim.setJointTargetVelocity(RRev_handle,1)
        elseif X <= 0.02 and Y > 0 then           
            sim.setJointTargetVelocity(RRev_handle,R_KickBallVel)
            sim.setJointTargetVelocity(RRev_handle,-1)       
        elseif X > 0.02 and Y > 0 then           
            sim.setJointTargetVelocity(RRev_handle,R_KickBallVel)    
            sim.setJointTargetVelocity(RRev_handle,1)
        end
        YYYYY = Y*5
        sim.setJointTargetVelocity(RMo_handle,YYYYY)
    end
end
--by 40623130--

ttt檔

原本要編寫切換鍵但於vrep中用io.clock等時間等待輸入雙命令時會出現運算值過大導致停止問題 , 所以先寫電腦打擊與跟蹤球

原python code出處(by 40623128):https://mdekmol.github.io/cd2019a-task1-2019cda_t1_g3_1/content/%E6%89%8B%E8%B6%B3%E7%90%83%E6%89%8B%E6%8E%A7%E8%88%87%E9%9B%BB%E8%85%A6%E5%B0%8D%E6%89%93.html


Task-2 << Previous Next >> Vrep手足球-All_Version