1. 程式人生 > >1.2 vrep例程之建立模型(build a clean model)

1.2 vrep例程之建立模型(build a clean model)

文章目錄

前言

本文所涉及的主題(見標題)是最重要的部分,沒有之一,原因如下:

in order to have a nice looking, fast displaying, fast simulating and stable simulation model

建立可見的形狀

  • 首先我們只考慮模型的可見部分,而動態部分(如關節,感測器)後續再處理。

the dynamic aspect (its undelying even more simplified/optimized model), joints, sensors, etc. will be handled at a later stage

  • 我們可以建立pure形狀或regular形狀(軟體自帶),前者為了動力學互動被優化過,且動力學選項預設enabled,至於primitive形狀只是簡單的網格,不一定包含足夠的細節和幾何上準確度
  • 我們也可以從外部應用匯入mesh模型,即CAD資料,匯入資料時,CAD資料不要過於複雜,影響模擬速度

we have the option to create pure shapes, or regular shapes. Pure shape will be optimized for dynamic interaction, and also directly be dynamically enabled (i.e. fall, collide, but this can be disabled at a later stage). Primitive shapes will be simple meshes, which might not contain enough details or geometric accuracy for our application. Our other option in that case would be to import a mesh from an external application.

模型預處理

為了不讓CAD資料過於複雜,進行以下處理:


  1. 移除小孔,螺絲,物體內部成分(我們只需要外殼)

try to remove all the holes, screws, the inside of objects, etc. from your original model data.
If you have the original model data represented as parametric surfaces/objects, then it is most of the time a simple matter of selecting the items and deleting them (e.g. in Solidworks).


  1. 限制精確度

The second important step is to export the original data with a limited precision


  1. 分步匯入物體,先大物體(精確度設定為低),後小物體(精確度較高)

簡化mesh

  • 注意到匯入的模型一般為單個mesh(a single mesh),要對其分割,但其初始方向(orientation)通常是錯誤的,但暫時先不改變,理由見下文:

best is to keep the orientation as it is, until the whole model was built, since, if at a later stage we want to import other items that are related to that same robot, they will automatically have the correct position/orientation relative to the original mesh.

簡化方法

  1. 自動mesh劃分(附英語原文):
  • 允許通過共同的邊緣,為那些沒有連線在一起的部分產生一個新的形狀,並不總是奏效但值得嘗試,因為僅作用於選定的mesh elements總比作用於所有elements要好,具體實現方式:[Menu bar --> Edit --> Grouping/Merging --> Divide selected shapes]
  • 有時會劃分過度,我們這時需要來進行合併邏輯上屬於一起(如同一連桿的一部分,有相同的視覺屬性)的部分為單個形狀(grouping)

allows to generate a new shape for all elements that are not linked together via a common edge. This does not always work for the selected mesh, but is always worth a try, since working on mesh elements gives us more control than if we had to work on all elements at the same time.
Sometimes, a mesh will be divided more than expected. In that case, simply merge elements that logically belong together (i.e. that will have the same visual attributes and that are part of the same link) back into one single shape ([Menu bar --> Edit -> Grouping/Merging --> Merge selected shapes]).

  1. 提取凸包:將mesh轉換為凸包來簡化
  2. 對mesh進行decimate(十中取一):減少mesh裡包含的三角形(mesh由三角形構成)數量,理解為降取樣
  3. 移除mesh內部成分:該功能基於視覺感測器,根據所選擇的的設定可能取得更好或更差的結果,具體實現方式: [Menu bar --> Edit --> Extract inside of selected shape]

效果圖

在這裡插入圖片描述

  • 上圖分別對應Convex hull, decimated mesh, and extracted inside
  • 上述方法中自動mesh劃分總是被推薦去嘗試,其他取決於匯入的mesh幾何性質

應用

先對mesh進行decimate兩次,然後extract the inside of the shape,OK,最終得到的是整個機器人模型的a single shape

劃分為連桿

  • 前面的步驟使我們獲得了a single shape for the whole robot,下面還要分為不同的link(連桿)

  • 自動mesh劃分(前文已提及):inspect the shape and generate a new shape for all elements that are not linked together via a common edge

  • 手動劃分(絕大部分情況):進入 triangle edit mode,選擇三角形,然後 Extract shape,提出後刪除之前選中的三角形

含空洞模型的劃分(拓展)

接著考慮到有時用凸包視覺效果會更好,如下圖
在這裡插入圖片描述

孔洞影響簡化模型的操作,所以手動編輯,提取邏輯上屬於一個凸實體的部分,具體如下:

  1. 首先提取三個近似凸形成分(不同顏色標註了),此時忽略兩個孔洞的三角形,最終得到三個形狀(shapes)
    在這裡插入圖片描述
  2. 其中兩個形狀進一步改善,即:去除那些空洞的三角形,然後為三個形狀各自提取凸包,再合併(merging)

在這裡插入圖片描述

統一各部分屬性

  • 在shape properties中,我們可以選擇是否顯示形狀邊緣或者在邊緣顯示時指定要考慮的角度,一個示例就是shading angle,指明形狀要顯示哪個面。

  • 下一步,我們要合併邏輯上屬於一體(一個連桿的不同部分)的成分,使得它們視覺上呈現一樣的效果,方法就是先搞掂一個形狀,然後選中它,再Ctrl+選中其他要統一為一體的所有形狀,apply to selection,就OK啦!

  • 接著,在完成視覺屬性的統一後,再合併相同部分,具體的: [Menu bar --> Edit --> Grouping/merging -> Group selected shapes]

  • 當建立形狀時,預設設定其參考座標系位置為形狀的幾何中心,而姿態則是遵循使得形狀的bounding box即包圍盒越小越好,但我們可以重新設定形狀的參考座標系:[Menu bar --> Edit --> Reorient bounding box --> with reference frame of world]

建立關節

若我們只有D-H引數,則我們可以通過工具模型(模型瀏覽器窗口裡)建立。

we can build our joints via the tool model located in Models/tools/Denavit-Hartenberg joint creator.ttm, in the model browser.

大部分情況下我們要自己從mesh中提取形狀,可以從原始CAD資料模型提取,而不是簡化後的資料模型
首先subdivide原始的mesh,如果不行的話,則手動進入三角形編輯模式,然後找revolute形狀,作為要建立關節所在位置和姿態的參考,此時移除其他不需要的形狀,便於操作,如下圖為一機器人基座,其用於設定第一個關節的位姿
在這裡插入圖片描述Robot base: normal and triangle edit mode visualization

用tool button來調整視角,如fit-to-view按鈕,加入vertex edit mode,選擇所有屬於上圓盤的頂點,然後進入triangle edit mode,如圖
在這裡插入圖片描述

然後選擇extract cylinder,然後基於選擇的形狀,產生一個cylinder形狀,然後新增旋轉關節並選中,接著Ctrl+選中上述提取出的cylinder形狀,之後分別使得兩者位置和姿態對應(分別使用位置和姿態調整按鈕)。有時,我們還需要將關節環繞其自己的參考座標系旋轉90度或180度,來獲得正確的狀態或旋轉方向,旋轉操作可通過 rotation tab 進行。同樣的方式,你可以讓關節沿著其軸線移動,最終效果如下:
在這裡插入圖片描述
對模型所有的關節建立如上述所示進行,記住關節預設在layer 2,此時,我們完成了模型的大體定義和層次樹,但還需要新增動力學性質。

新增動力學(❤)

所謂有動力學屬性,即一個形狀滿足以下要求:

  1. dynamic or static:
    • 具有動力學屬性(也稱為非靜態)的形狀能被外部力或力矩影響或fall,而沒有動力學屬性的形狀要麼原地不動,要麼跟隨父節點在場景中的移動
  2. respondable or non-respondable:
    • 即該形狀能對其他respondable形狀引起碰撞反應。此時,若它們均是dynamic(enabled),則在運動中互相影響。而非respondable的狀態在與其他形狀碰撞時不計算碰撞反應。

綜上所述,respondable形狀應該儘可能簡單,便於計算的穩定性和迅速性,物理引擎能對以下五種型別形狀進行模擬:


Pure shapes: a pure shape will be stable and handled very efficiently by the physics engine. The draw-back is that pure shapes are limited in geometry: mostly cuboids, cylinders and spheres. If possible, use those for items that are in contact with other items for a longer time (e.g. the feet of a humanoid robot, the base of a serial manipulator, the fingers of a gripper, etc.). Pure shapes can be created with [Menu bar --> Add --> Primitive shape].

  1. 純形狀:模擬效率很高,很穩定,缺點在於侷限於以下幾何體:圓柱,球體和立方體,如果有可能的話,當需要長時間接觸時,用這些形狀構成的物體來來與其他物體接觸,如人形機器人足部,串聯機器人基座,抓手手指等

Pure compound shapes: a pure compound shape is a grouping of several pure shapes. It performs almost as well as pure shapes and shares similar properties. Pure compound shapes can be generated by grouping several pure shapes [Menu bar --> Edit --> Grouping/Merging --> Group selected shapes].

  1. 純複合形狀:純形狀組合而成,效能表現與純形狀差不多,可通過grouping幾個純形狀獲得

Convex shapes: a convex shape will be a little bit less stable and take a little bit more computation time when handled by the physics engine. It allows for a more general geometry (only requirement: it need to be convex) than pure shapes. If possible, use convex shapes for items that are sporadically in contact with other items (e.g. the various links of a robot). Convex shapes can be generated with [Menu bar --> Add --> Convex hull of selection] or with [Menu bar --> Edit --> Morph selection into convex shapes].

  1. 凸形狀:相比於純形狀,稍微不穩定,計算成本稍微上升,但幾何要求降低了,更一般了(唯一要求就是是凸形的),若是sporadic接觸,用凸形狀構成的物體來進行(如機器人的不同連桿)

Compound convex shapes, or convex decomposed shapes: a convex decomposed shape is a grouping of several convex shapes. It performs almost as well as convex shapes and shares similar properties. Convex decomposed shapes can be generated by grouping several convex shapes [Menu bar --> Edit --> Grouping/Merging --> Group selected shapes], with [Menu bar --> Add --> Convex decomposition of selection…], or with [Menu bar --> Edit --> Morph selection into its convex decomposition…].

  1. 複合凸形狀,也稱為分解後的凸形狀:凸形狀組合而成,效能表現同凸形狀差不多,可通過grouping幾個凸形狀

Random shapes: a random shape is a shape that is not convex nor pure. It generally has poor performance (calculation speed and stability). Avoid using random shapes as much as possible.

  1. 隨機形狀是一種即非純形狀也非凸形狀的形狀,效能表現較差,儘量避免使用

綜上所述,優先順序如下:純形狀,純複合形狀,凸形狀,凸複合形狀和隨機形狀。
對於機器人,我們將基座作為純圓柱形狀,而其他連桿作為凸或凸複合形狀

按道理說,我們也可以用動力學屬性已經enabled的形狀作為機器人的可見部分,但那樣看起來不太好,所以我們為該手冊第一部分建立的可見形狀建立動力學屬性enabled的對應部分,但設定為隱藏(不用現成的形狀,自己建立形狀【第一部分已完成】,然後建立對應的動力學模型,只供物理引擎使用),而可見的機器人部分只用於視覺效果以及最小距離模組,接近感測器的探測等


處理基座

對於機器人,我們將基座作為純圓柱形狀,而其他連桿作為凸或凸複合形狀
依照上文所述,操作:選中機器人,若機器人是複合形狀,則首先要ungroup,然後將individual shapes合併出所要操作的目標形狀(如基座),進入三角形編輯模式,選中組成電源線的三角形形狀,erase them,然後選取該形狀的所有三角形,提取純圓柱,最終得到純圓柱所代表的基座

在這裡插入圖片描述


處理連桿

下面處理連桿(作為凸形狀或凸複合形狀):
選中連桿,然後從中產生凸形狀Menu bar --> Add --> Convex hull of selection,若覺得丟失很多原形狀的細節,你可以從composing elements來手動提取所有的凸包,然後合併,或者使用自動的方法Menu bar --> Add --> Convex decomposition of selection

you could manually extract several convex hulls from its composing elements, then group all the convex hulls with [Menu bar --> Edit --> Grouping/Merging --> Group selected shapes]. If that appears to be problematic or time consuming, then you can automatically extract a convex decomposed shape with [Menu bar --> Add --> Convex decomposition of selection…]:

在這裡插入圖片描述
Original shape, and convex shape pendant


在這裡插入圖片描述

Original shape, and convex decomposed shape pendant

接著對於其餘連桿,重複上述操作,完成後將每個動態模型attach到對應的visible部分

效果圖

在這裡插入圖片描述
考慮到我們要讓動態模型只為物理引擎所用,而不是其他calculation module,所以uncheck所有的object special properties

  • 接著我們仍然需要將動態模型設定為dynamic和respondable,在shape dynamics properties中設定,首先enable基座的respondable屬性,以及enable前四個local respondable mask標誌位,disable後四個local respondable mask標誌位(為了讓連續的respondable連桿不相互碰撞),然後對於第一個連桿(與基座相連),仍然enable respondable屬性,但這次*disable前四個local respondable mask標誌位,enable後四個local respondable mask標誌位,重複上述過程,使得掩碼交替。理由如下:

once the model will be defined, consecutive dynamic shapes of the robot will not generate any collision response when interacting with each other. Try to always end up with a construction where the dynamic base of the robot, and the dynamic last link of the robot have only the first 4 Local respondable mask flags enabled, so that we can attach the robot to a mobile platform, or attach a gripper to the last dynamic link of the robot without dynamic collision interferences.


最後,我們需要將動態模型的dynamic屬性也enable下,在shape dynamics properties中設定,然後設定mass和inertia Tensor properties,可手動或自動設定:點選 Compute mass & inertia properties for selected convex shapes。
同時,注意到基座的特殊性,當機器人單獨使用時,我們希望其實非動態的(static),以避免機械臂運動時基座fall,但若將機械臂與小車繫結,我們又希望基座能dynamic(非靜態),可通過enabling the Set to dynamic if gets parent item, then disabling the Body is dynamic item達到目的。
此時運動模擬時,除了基座(通過上述設定)其他的動態shape都會fall,當然,用於視覺效果的visual shapes也會跟著fall。

定義模型

拖拽方式獲得如下層次結構(關節加入層次樹)
在這裡插入圖片描述
將機器人模型基座重新命名,畢竟基座代表整個模型(作為超父節點),此時,選中層次樹的超父節點,即基座(robot節點),enable基座的 Object is model base以及 enable Object/model can transfer or accept DNA,從而產生包圍盒(包圍機器人模型),但這個包圍盒太大了,是因為其還包含了不可見部分(如 joint,感測器),所以要為所有關節enable the Don’t show as inside model selection item,同理,對於其他不可見部分同樣處理,最終效果
在這裡插入圖片描述

  • 接著為了不讓機器人模型被意外修改,選中機器人模型中的所有可見object,然後enable Select base of model instead,達到選擇機器人模型任一部分,卻是選擇機器人base的目的(一體化),此時若想確實選中特定部分,要在層次樹中選擇。
  • 接著我們要把機器人模型放在正確的預設位姿上(put the robot into a correct default position/orientation),首先儲存當前scene為參考系,接著選中模型並修正位姿(一種做法是使模型,即base,位於X=0,Y=0位置)

First, we save current scene as a reference (e.g. if at a later stage we need to import CAD data that have the same orientation at the curent robot). Then we select the model and modify its position/orientation appropriately. It is considered good practice to position the model (i.e. its base object) at X=0 and Y=0.


Robot model in default configuration
Robot model in default configuration

  • 此時執行模擬,機器人立刻collapse,是由於沒有設定關節控制引數,之前我們新增關節時只是讓關節處於力/力矩模式,而沒有enable motor or controller,現在我們想要用PID控制器控制每個關節,所以在joint dynamic properties,我們點選 Motor enabled 並調整 the maximum torque,接著我們點選Control loop enabled 並選擇 Position control (PID),這回執行模擬,機器人能保持位置

  • 模擬時,我們能通過 Dynamic content visualization & verification toolbar button(選擇模擬引擎旁邊的按鈕)來驗證場景的動態內容,選中則只顯示物理引擎考慮的內容(即動態模型),這樣做的好處在於便於debug,更快發現動態模型表現不如預期的原因,注意到模擬過程中,動力學enable的object名字右邊有球狀物:

dynamically enabled objects should display a ball-bounding icon on the right-hand side of their name

在這裡插入圖片描述

具有動力學形狀的連線

  • 接著我們要將一gripper放在機器人上或者將機器人放在移動平臺上,一般兩個動力學enable的形狀要想 rigidly attached to each other(互相連線)有兩種方法:

  • 通過組合方式:選擇兩個形狀,然後 [Menu bar --> Edit --> Grouping/Merging --> Group selected shapes].

  • 通過力感測器相連:a force torque sensor can also act as a rigid link between two separate dynamically enabled shapes

採用第二種方法,建立力感測器,將該感測器移動到機器人末端(tip)上,然後將其與連桿繫結(拖拽),調整大小和外觀(設定為紅色,表示可選擇的連線點)

在這裡插入圖片描述

  • 接著建立一個gripper(模型庫裡找),選中它,然後Ctrl+選中力感測器,點選 Assembling/disassembling toolbar button,則gripper goes into place
    在這裡插入圖片描述

機械臂與移動平臺的連線

The gripper knew how to attach itself because it was appropriately configured during its model definition. We now also need to properly configure the robot model, so that it will know how to attach itself to a mobile base for instance. We select the robot model, then click Assembling in the object common properties. Set an empty string for ‘Parent’ match values, then click Set matrix. This will memorize the current base object’s local transformation matrix, and use it to position/orient itself relative to the mobile robot’s attachment point.

大意是:抓手能自動連線,是因為在抓手模型定義時已設定,所以為了機器人與移動平臺自動連線,我們同樣要設定機器人模型:在object common properties中點選Assembling,然後Set an empty string for ‘Parent’ match values, then click Set matrix


驗證是否成功:

To verify that we did things right, we drag the model Models/robots/mobile/KUKA Omnirob.ttm into the scene. Then we select our robot model, then control-click one of the attachment points on the mobile platform, then click the Assembling/disassembling toolbar button. Our robot should correctly place itself on top of the mobile robot:


現在我們可以給機器人加些東西,比如感測器等,當然為了控制機器人行為,我們可以內嵌指令碼,此時要知道怎麼在內嵌指令碼中獲取object的控制代碼,除此之外,我們還可以通過a plugin, a remote API client, a ROS node, a BlueZero node, an add-on來與模型互動。

加入模型庫

搞掂模型後,可以摺疊模型層次樹,最後只有基座,選中它,然後 [Menu bar --> File --> Save model as…],那麼我們以後就可以在模型庫(瀏覽器)中使用自定義的機器人模型了。

小Tips:

  • 上述操作均可新建一個scene,保持原scene的完整性,搞掂後再複製回來