1. 程式人生 > >VREP中的力觸覺設備接口(CHAI3D)

VREP中的力觸覺設備接口(CHAI3D)

新的 mdi 版本 情況 3.2 針對 傳遞 融合 original

  力反饋技術是一種新型的人機交互技術,它允許用戶借助力反饋設備觸碰、操縱計算機生成的虛擬環境中的物體,並感知物體的運動和相應的力反饋信息,實現人機力覺交互。雖然傳統的鼠標、鍵盤、觸摸屏等交互手段可以滿足用戶與環境中物體交互的需求,但是缺乏力覺交互信息的反饋。力反饋技術結合其他的虛擬現實技術,使用戶在交互過程中不僅能夠通過視、聽覺通道獲取信息,還能夠通過觸覺通道感受模擬現實世界力覺交互的“觸感”。因此,力反饋技術的引入,使交互體驗更加自然、真實。

  力反饋系統的實現過程中涉及到以下關鍵問題: 力反饋計算模型設計、碰撞檢測、視覺與力覺的同步渲染,相應地產生了針對解決上述問題的一系列算法和接口。

  1. 設計力反饋計算模型

  力反饋計算模型用於計算虛擬環境中物體間交互時產生的力,並由力反饋設備傳遞給用戶。目前,力反饋算法主要采用基於物理建模方法的有限元模型和彈簧—質點模型。其中,有限元模型將連續體離散為多個單元,分別對每個單元進行模擬, 之後連接各單元模擬整個連續體,最後通過解方程組計算質點的受力;彈簧—質點模型首先將物體質量離散到各個質點,並通過彈簧連接柔性物體離散後的質點,依據胡克定律計算質點之間的力,從而模擬整個物體的受力。有限元模型能夠較好地滿足交互的真實性需求,但是運算量大、求解過程復雜,難以滿足交互的實時性需求;而彈簧—質點模型具有建模過程簡單、計算量較小、交互實時性較好的特點。

  2. 實現碰撞檢測

  碰撞檢測用於實時檢測虛擬環境中物體間的碰撞情況,為之後圖形信息的繪制及力反饋信息的計算奠定基礎。當前的碰撞檢測算法主要采用包圍盒技術, 首先對物體的包圍盒進行相交檢測( 模糊碰撞檢測), 當包圍盒相交時其包圍的物體才有可能相交。然後進行物體間的精細碰撞檢測,當包圍盒不相交時,其包圍的物體一定不相交,結束物體間的碰撞檢測。

  3. 增強力覺真實感

  視覺與力覺的同步渲染用於保持較低刷新頻率的視覺信息(30fps ~ 60fps) 和較高刷新頻率的力覺信息(1000fps) 的一致性。目前,主流的力反饋接口OpenHaptics、CHAI3D等都支持視覺與力覺融合的應用開發。其中,OpenHaptics是SenseAble 公司針對Phantom系列力反饋設備研發的開發工具包,包括QuickHaptics API、Haptic Device API、Haptic Library API 三個不同層次的接口庫,能夠滿足不同用戶的設計需求;CHAI3D(Computer Haptics and Active Interface)是由斯坦福大學人工智能實驗室研發的支持多種力反饋設備

開源開發工具包,它具有較好的可擴展性,允許用戶在已有視覺、力覺渲染算法的基礎上根據自己的需要設計新的渲染算法,也可以通過添加驅動的方法支持新的力反饋設備。

技術分享

  VREP中集成了CHAI3D插件,用戶可以在Lua腳本中調用其接口編寫基於力反饋設備的仿真程序。在VREP的模型瀏覽器(Model browser/other/Interface to haptic device.ttm)中有一個力反饋設備接口模型,如下圖所示:

技術分享

  將其拖入場景中進行仿真,但出現了CHAI3D初始化失敗的錯誤:

技術分享

  解決方法是可以下載一個32位版本的VREP,然後重新編譯生成CHAI3D插件的動態鏈接庫。下載CHAI3D 3.2後解壓,在modules\V-REP目錄下打開相應版本的VS解決方案文件(e.g. CHAI3D-V-REP-VS2013.sln if using VisualStudio 2013),然後生成Release版本下的32位動態鏈接庫,並將VREP安裝目錄中的替換掉。

  In summary:

  • Compile the V-REP module in RELEASE mode in 32-bit configuration.
  • Copy the file v_repExtCHAI3D.dll in the base directory of V-REP.
  • If you are using the Phantom haptic device, please also copy the file hdPhantom32.dll in the base directory of V-REP. Copy any of the other files if you are using some other devices.


  The CHAI3D scene can be controlled from V-REP through a set of LUA functions that allow the user to perform the following tasks:

  • connect to haptic devices(Haptic devices can be connected to the CHAI3D scene by calling the simExtCHAI3D_start() LUA function)
  • copy V-REP objects to the CHAI3D scene so that they can be "felt" with the haptic device
  • keep the position and orientation of the CHAI3D objects in sync with their V-REP counterparts while the simulation is running
  • constrain the behavior of the haptic device (e.g. to emulate a 2D force-feedback joystick by constraining the haptic device in a plane)
  • change the characteristics of the haptic device constraints to reflect changes in the simulation

  為了能讓設備輸出交互力,在VREP場景中創建好的物體還需要通過simExtCHAI3D_addShape()函數將其添加到CHAI3D的世界中去。添加到CHAI3D場景中後,連接好的力反饋設備就能“感受到”這個物體。如果物體在VREP場景中發生了變化,比如移動或旋轉,那麽CHAI3D場景中對應的物體也要通過simExtCHAI3D_updateShape()函數進行更新。Any object contained in the V-REP scene can be added to the CHAI3D world by using the simExtCHAI3D_addShape() LUA function. Once an object is added to the CHAI3D scene, it can be "felt" and explored haptically using any haptic device connected to the scene. If the object is moving in the V-REP simulation, it can be kept in sync on the CHAI3D side by using the simExtCHAI3D_updateShape() LUA function.

  接下來在VREP中通過CHAI3D插件提供的Lua函數創建一個簡單的靜態場景。小球代表力反饋設備末端的位置,立方體代表場景中的墻(或需要接觸交互的其它物體),當移動末端讓小球碰撞到立方體時設備能輸出一定的力讓用戶感受到。如下圖所示當小球貼著立方體正面移動時,用戶能感受到“墻”的存在,且越靠向墻,受到的反作用越大。當小球移開墻面時,力又變為零。狀態欄中輸出了小球位置、輸出力大小以及操作桿按鈕狀態(按下按鈕變為1):

技術分享

  腳本代碼如下:

addCuboid=function(siz,pos,orient)
    if CHAI3DPluginInitialized then
        local cuboidHandle=simCreatePureShape(0,4+16,siz,1)
        simSetObjectParent(cuboidHandle,modelHandle,true)
        simSetObjectPosition(cuboidHandle,modelHandle,pos)
        simSetObjectOrientation(cuboidHandle,modelHandle,orient)
        return addMesh(cuboidHandle,true), cuboidHandle
    end
    return -1, -1
end


-- copy V-REP objects to the CHAI3D scene so that they can be "felt" with the haptic device
addMesh=function(shapeHandle,whenRemovedEraseMesh)
    if CHAI3DPluginInitialized then
        if not objCont then
            objCont={}
        end
        objCont[#objCont+1]=shapeHandle
        local vertices,indices=simGetShapeMesh(shapeHandle)
        local pos=simGetObjectPosition(shapeHandle,modelHandle)
        local orient=simGetObjectOrientation(shapeHandle,modelHandle)
        local hapticObjId=simExtCHAI3D_addShape(vertices,indices,pos,orient,1000)
        objCont[#objCont+1]=hapticObjId
        objCont[#objCont+1]=whenRemovedEraseMesh
        return hapticObjId
    end
    return -1
end


removeAllObjects=function()
    if CHAI3DPluginInitialized then
        if objCont then
            for i=1,(#objCont)/3,1 do
                local obj=objCont[3*(i-1)+1]
                if objCont[3*(i-1)+3] then
                    simRemoveObject(obj)
                end
                simExtCHAI3D_removeObject(objCont[3*(i-1)+2])
            end
        end
        objCont=nil
    end
end



if (sim_call_type==sim_childscriptcall_initialization) then

    modelHandle=simGetObjectAssociatedWithScript(sim_handle_self)

    -- Check if the plugin is loaded:
    moduleName=0
    moduleVersion=0
    index=0
    pluginNotFound=true
    while moduleName do
        moduleName,moduleVersion=simGetModuleName(index)
        if (moduleName==CHAI3D) then
            pluginNotFound=false
        end
        index=index+1
    end

    if (pluginNotFound) then
        simDisplayDialog(Error,CHAI3D plugin was not found, or was not correctly initialized (v_repExtCHAI3D.dll).,sim_dlgstyle_ok,false,nil,{0.8,0,0,0,0,0},{0.5,0,0,1,1,1})
    else

        -- Start the device:
        local toolRadius=0.005 -- the radius of the tool
        local workspaceRadius=0.2 -- the workspace radius
        if simExtCHAI3D_start(0,toolRadius,workspaceRadius)~=1 then
            simDisplayDialog(Error,Device failed to initialize.,sim_dlgstyle_ok,false,nil,{0.8,0,0,0,0,0},{0.5,0,0,1,1,1})
        else
            CHAI3DPluginInitialized=true

            toolSphereHandle=simCreatePureShape(1,4+16,{toolRadius*2,toolRadius*2,toolRadius*2},1)
            simSetObjectParent(toolSphereHandle,modelHandle,true)
            simSetShapeColor(toolSphereHandle,‘‘,0,{1,1,1})
         
            -- Create a haptic cuboid:
            local cuboidSize={0.1,0.1,0.1}
            local cuboidPosition={0.01,-0.01,0.05}
            local cuboidOrientation={0,0,0}
            cuboidId,cuboidHandle=addCuboid(cuboidSize,cuboidPosition,cuboidOrientation)

--[[
            -- Create a haptic mesh from an existing V-REP shape:
            meshHandle=simGetObjectHandle(‘mesh‘)
            local removeOriginalShapeWhenDone=false
            meshId=addMesh(meshHandle,removeOriginalShapeWhenDone)
--]]

        end
    end
end


if (sim_call_type==sim_childscriptcall_actuation) then

end


if (sim_call_type==sim_childscriptcall_sensing) then
    if CHAI3DPluginInitialized then

        -- Read the position of the cursor:
        local p=simExtCHAI3D_readPosition(0)

        -- Reflect the position of the cursor in V-REP:
        simSetObjectPosition(toolSphereHandle,modelHandle,p)

        -- Read the the interaction force:
        local f=simExtCHAI3D_readForce(0)
        
        -- Read the buttons of the device:
        local b = simExtCHAI3D_readButtons(0)

        local info = string.format("Position:%.2f,%.2f,%.2f Force:%.2f,%.2f,%.2f Button State:%d",p[1],p[2],p[3],f[1],f[2],f[3],b)  
        simAddStatusbarMessage(info)
    end
end


if (sim_call_type==sim_childscriptcall_cleanup) then
    if CHAI3DPluginInitialized then
        -- Remove all Haptic objects (objects and forces):
        removeAllObjects()

        -- Remove all objects in the haptic scene:
        simExtCHAI3D_reset()
    end
end

  可以看出使用CHAI3D就能很方便的讓力反饋設備感知場景中的環境,輸出力。如果不使用CHAI3D,則需要自行獲取VREP中物理引擎計算的接觸力信息,然後轉換成合適的值發送給設備去渲染接觸力。

參考:

CHAI3D plugin for V-REP Documentation

Haptic Data Retrieve—VREP Forum

VREP Modules—CHAI3D Forum

CHAI3D Current Release (Version 3.2)

VREP中的力觸覺設備接口(CHAI3D)