VREP中的Regular API中有一些矩陣操作的函數,不過有時候還是不能滿足計算需求,這時就需要在VREP中使用其它科學計算庫(或者用Python/MATLAB之類的外部程序控制)。

在這里下載Lua Matrix,它是一個用純Lua編寫的矩陣運算庫。解壓后將lua文件夾中的matrix.lua和complex.lua復制到VREP安裝目錄下的lua文件夾中:
下面是一個簡單的測試,對矩陣進行一系列運算並驗證。點擊VREP仿真按鈕,沒有出現錯誤。
if (sim_call_type==sim_childscriptcall_initialization) then local matrix = require "matrix" local mtx, m1,m2,m3,m4,m5, ms,ms1,ms2,ms3,ms4 -- normal matrix mtx = matrix {{1,2},{3,4}} assert( tostring(mtx) == "1\t2\n3\t4" ) -- vector mtx = matrix {1,2,3} assert( tostring(mtx) == "1\n2\n3" ) -- matrix with size 2x2 mtx = matrix (2,2) assert( tostring(mtx) == "0\t0\n0\t0" ) -- matrix with size 2x2 and default value 1/3 mtx = matrix (2,2,1/3) assert( tostring(mtx) == "0.33333333333333\t0.33333333333333\n0.33333333333333\t0.33333333333333" ) -- identity matrix with size 2x2 mtx = matrix (2,"I") assert( tostring(mtx) == "1\t0\n0\t1" ) -- matrix.add; number m1 = matrix{{8,4,1},{6,8,3}} m2 = matrix{{-8,1,3},{5,2,1}} assert(m1 + m2 == matrix{{0,5,4},{11,10,4}}, "error") -- matrix.sub; number m1 = matrix{{8,4,1},{6,8,3}} m2 = matrix{{-8,1,3},{5,2,1}} assert(m1 - m2 == matrix{{16,3,-2},{1,6,2}}) -- matrix.mul; number m1 = matrix{{8,4,1},{6,8,3}} m2 = matrix{{3,1},{2,5},{7,4}} assert(m1 * m2 == matrix{{39,32},{55,58}}) -- matrix.div; number m1 = matrix {{1,2},{3,4}} m2 = matrix {{4,5},{6,7}} assert( m1*m2^-1 == m1/m2 ) -- matrix.divnum; number, same complex m2 = matrix {{4,5},{6,7}} assert( m2/2 == matrix{{2,2.5},{3,3.5}} ) mtx = matrix {{3,5,1},{2,4,5},{1,2,2}} assert( 2 / mtx == matrix{{4,16,-42},{-2,-10,26},{0,2,-4}} ) -- matrix.det; number mtx = matrix {{1,4,3,2},{2,1,-1,-1},{-3,2,2,-2},{-1,-5,-4,1}} assert( mtx:det() == 78 ) -- matrix.invert; number mtx = matrix{{3,5,1},{2,4,5},{1,2,2}} local mtxinv = matrix{{2,8,-21},{-1,-5,13},{0,1,-2}} assert( mtx^-1 == mtxinv ) -- matrix.transpose mtx = matrix{{3,5,1},{2,4,5},{1,2,2}} assert(mtx^'T' == matrix{{3,2,1},{5,4,2},{1,5,2}}) end if (sim_call_type==sim_childscriptcall_actuation) then end if (sim_call_type==sim_childscriptcall_sensing) then end if (sim_call_type==sim_childscriptcall_cleanup) then end
Lua 模塊與包
模塊類似於一個封裝庫,從 Lua 5.1 開始,Lua 加入了標准的模塊管理機制,可以把一些公用的代碼放在一個文件里,以 API 接口的形式在其他地方調用,有利於代碼的重用和降低代碼耦合度。
Lua 的模塊是由變量、函數等已知元素組成的 table,因此創建一個模塊很簡單,就是創建一個 table,然后把需要導出的常量、函數放入其中,最后返回這個 table 就行。以下為創建自定義模塊 module.lua,文件代碼格式如下:
-- 文件名為 module.lua -- 定義一個名為 module 的模塊 module = {} -- 定義一個常量 module.constant = "這是一個常量" -- 定義一個函數 function module.func1() io.write("這是一個公有函數!\n") end local function func2() print("這是一個私有函數!") end function module.func3() func2() end return module
由上可知,模塊的結構就是一個 table 的結構,因此可以像操作調用 table 里的元素那樣來操作調用模塊里的常量或函數。
上面的 func2 聲明為程序塊的局部變量,即表示一個私有函數,因此是不能從外部訪問模塊里的這個私有函數,必須通過模塊里的公有函數來調用.
require 函數
Lua提供了一個名為require的函數用來加載模塊。要加載一個模塊,只需要簡單地調用就可以了。例如:
require("<模塊名>")
或者
require "<模塊名>"
執行 require 后會返回一個由模塊常量或函數組成的 table,並且還會定義一個包含該 table 的全局變量。
-- test_module.lua 文件 -- module 模塊為上文提到到 module.lua require("module") print(module.constant) module.func3()
以上代碼執行結果為:
這是一個常量
這是一個私有函數!
或者給加載的模塊定義一個別名變量,方便調用:
-- test_module2.lua 文件 -- module 模塊為上文提到到 module.lua -- 別名變量 m local m = require("module") print(m.constant) m.func3()
以上代碼執行結果為:
這是一個常量
這是一個私有函數!
參考:
