刚开始接触V-REP,这个软件好像很牛×的样子,先翻译一篇官网教程压压惊(╮(╯▽╰)╭)!
详细的英文教程和教程成品可以在V-REP安装文件里面找,我只是大自然的搬运工(╭(╯ε╰)╮)!
做好的成品是这样的!
1. 添加bubbleRob
① [Menu bar --> Add --> Primitive shape --> Sphere],在弹出的对话框中为Sphere指定大小为0.2。
另外这个添加的球体Sphere是dynamic和respondable的,这意味着Sphere具有运动学属性并且可以和其他物体发生相互作用,这种属性在object properties里面也可以看到。
② 为了让Sphere能够为calculation modules(这个在后面传感器计算物体之间的距离会用到)所用,需要使能Sphere的Collidable、Measurable、Renderable和Detectable属性,这个在object properties里面设置。Moreover,可以在Shape里面设置Sphere的外观属性比如颜色。
③ 将Sphere沿Z方向举高0.02,方法如下:
选中Sphere->点击position dialog->点击translation->赋值Along Z为0.02->点击Translation selection(确保Relative to对应的是World)
④ 双击scene hierarchy里面的Sphere,重命名为bubbleRob。
2. 为bubbleRob添加proximity sensor
① 属性设置
[Menu bar --> Add --> Proximity sensor --> Cone type],为新添加的Proximity_sensor指定绝对位置(0.1,0,0.12),绝对姿态角(0,90,90)。
按下两图设置其他相关属性。
② 双击Proximity_sensor,重命名为bubbleRob_sensingNose(注意大小写哦,这命名是和后面的代码是对应的呢!)。
③ 选中bubbleRob_sensingNose,按住Ctrl再选中bubbleRob,然后点击[Menu bar --> Edit --> Make last selected object parent]
或者拖曳bubbleRob_sensingNose到bubbleRob上。
上述步骤结束后的界面应该是这样的:
3. 为bubbleRob添加rightWheel和leftWheel
① 创建new scene
click [Menu bar --> File --> New scene],当你创建一个特定的object时,使用多个场景是可以带来一些方便的。
② 创建bubbleRob_rightWheel(注意大小写哦)
click [Menu bar --> Add --> Primitive shape --> Cylinder],在弹出的对话框中为Cylinder设置大小(0.08,0.08,0.02);
设置Cylinder的绝对位置(0.05,0.1,0.04),绝对欧拉角(-90,0,0);
如同bubbleRob一样使能Cylinder的Collidable、Measurable、Renderable和Detectable属性;
重命名Cylinder为bubbleRob_rightWheel;
③ 创建bubbleRob_leftWheel(注意区分大小写哦)
选中bubbleRob_rightWheel,然后复制粘贴,将bubbleRob_rightWheel0的绝对位置设置为(0.05,-0.1,0.04),同时重命名为bubbleRob_leftWheel。
④ 选中bubbleRob_leftWheel和bubbleRob_rightWheel,复制粘贴到bubbleRob所在的scene。
4. 为bubbleRob添加rightMotor和leftMotor
① 添加rightMotor(注意大小写哦)
click [Menu bar --> Add --> Joint --> Revolute] ;
重命名Revolute_joint为bubbleRob_leftMotor;
选中bubbleRob_leftMotor,按Ctrl选中bubbleRob_leftWheel,在位置设置界面点击Apply to selection,将
bubbleRob_leftMotor置于bubbleRob_leftWheel的中心;
选中bubbleRob_leftMotor,按Ctrl选中bubbleRob_leftWheel,在姿态角设置界面点击Apply to selection,使
bubbleRob_leftMotor和bubbleRob_leftWheel的指向相同;
② 指定leftMotor属性
使能Motor enabled和Lock motor when target velocity is zero。
③ attach the bubbleRob_leftWheel to the bubbleRob_leftMotor。
④ attach the bubbleRob_leftMotor to the bubbleRob。
⑤ 类似添加leftMotor的方法添加rightMotor。
上述步骤结束后的界面应该是这样的:
5. 为bubbleRob添加slider(or caster)
当我们start simulation时,会发现bubbleRob是会倾倒的,这是因为它还缺少一个前轮。
① 创建slider
创建一个new scene,添加一个大小为0.05的sphere;
重命名为bubbleRob_slider;
使能Collidable、Measurable、Renderable和Detectable属性;
并且按图设置如下属性;
② 创建force sensor(将slider和bubbleRob链接)
click [Menu bar --> Add --> Force sensor];
重命名force_sensor为bubbleRob_connection;
沿z方向举高0.05;
③ attach the slider to the force sensor。
④ 将bubbleRob_connection和bubbleRob_slider拷贝到bubbleRob所在的scene。
⑤ 将bubbleRob_connection沿x绝对方向移动-0.07。
⑥ attach the bubbleRob_connection to the bubbleRob。
6. 使bubbleRob具有稳定性
如果start simulation,会发现bubbleRob_slider相对于bubbleRob有微小的振动,这是bubbleRob_slider和bubbleRob相互作用的结果,为此按下图设置参数:
之后振动便不会发生了,但是整个bubble有缓慢的移动,为此将two wheels和slider的质量和惯性矩都放大八倍,一次放大两倍,所以一共放大三次。
7. 创建bubbleRob collection
click [Menu bar --> Tools --> Collections];
点击Add new collection;
选中新添加的collection,再选中Scene hierarchy里面的bubbleRob,点击Collections对话框的Add按钮;
重命名collection为bubbleRob_collection后关闭Collections对话框;
8. 创建calculation module
click [Menu bar --> Tools --> Calculation module properties];
在弹出的对话框中选中distance calculation对话框,点击Add new distance object,选[collection] bubbleRob_collection 和 all other measurable objects in the scene作为参数;
重命名Distance为bubbleRob_distance;
9. 创建graph
click [Menu bar --> Add --> Graph];
重命名为bubbleRob_graph;
attach the graph to bubbleRob;
设置绝对坐标为(0,0,0.005);
uncheck Display XYZ-planes;
按图添加参数;
uncheck for bubbleRob_x_pos、bubbleRob_y_pos和bubbleRob_z_pos;
按下图设置参数:
attach the graph to bubbleRob。
10. 添加obstacles
① 创建大小为(0.1,0.1,0.2)的Cylinder;
② uncheck Body is dynamic;
③ 使能Collidable、Measurable、Renderable和Detectable属性;
④ 选中cylinder,同时click the object translation toolbar button,这样就可以使cylinder随着鼠标移动了;
⑤ 在bubbleRob附近放置多个这样的cylinder(你可以选择拷贝)作为obstacles;
11. 完成bubbleRob model模型构建
① 选中bubbleRob,在object properties里面check Object is model base和 Object/model can transfer or accept DNA;
② 选中两个motors、graph和已有的一个proximity sensor,在object properties的common界面勾选ignored by model bounding box,然后点击Apple to selection;
③ 选中两个motors和仅有的一个force sensor,同样的在②的对话框中,uncheck camera visibility layer 2,并且check camera
visibility layer 10,然后记得Apple to selection,这可以将这三个objects隐藏起来;
④ 选中两个wheels、一个graph、一个slider、一个proximity sensor,然后再object properties的common界面勾选 Select base of
model instead,这样当我们试图选中其中的任何一个object的时候,选中的都会是bubbleRob,这样可以保护模型;
12. 添加vision sensor
① click [Menu bar --> Add --> Vision sensor --> Perspective type];
② 为新添加的Vision_sensor执行11的② ④步骤;
③ 在Vision_sensor的object properties的Vision Sensor界面,设置Far clipping plane为1,设置Resolution x and Resolution y为256;
④ 点击Show filter dialog,在filter component 一栏选择Edge detection on work image,然后点击Add filter,然后将新添加的filter置于第二的位置,最后双击新添加的filter,设置Threshold为0.2;
⑤ 在模型视图窗口右键->Add->Floating view->在新出现的窗口右击->View->Associate view with selected vision sensor(确保此时
Vision_sensor处于选中的状态);
⑥ attach the Vision_sensor to the bubbleRob_sensingNose,并且设置相对于 bubbleRob_sensingNose的位置和姿态角为(0,0,0)和(0,0,0);
13. 最后一步:添加代码
选中bubbleRob,然后click [Menu bar --> Add --> Associated child script --> Non threaded];
点击bubbleRob旁边出现的一个脚本图标,进入脚本编辑,将下面的代码粘贴复制进去;
function speedChange_callback(ui,id,newVal)
speed=minMaxSpeed[1]+(minMaxSpeed[2]-minMaxSpeed[1])*newVal/100
end
function sysCall_init()
-- This is executed exactly once, the first time this script is executed
bubbleRobBase=sim.getObjectAssociatedWithScript(sim.handle_self) -- this is bubbleRob's handle
leftMotor=sim.getObjectHandle("bubbleRob_leftMotor") -- Handle of the left motor
rightMotor=sim.getObjectHandle("bubbleRob_rightMotor") -- Handle of the right motor
noseSensor=sim.getObjectHandle("bubbleRob_sensingNose") -- Handle of the proximity sensor
minMaxSpeed={50*math.pi/180,300*math.pi/180} -- Min and max speeds for each motor
backUntilTime=-1 -- Tells whether bubbleRob is in forward or backward mode
-- Create the custom UI:
xml = '<ui title="'..sim.getObjectName(bubbleRobBase)..' speed" closeable="false" resizeable="false" activate="false">'..[[
<hslider minimum="0" maximum="100" onchange="speedChange_callback" id="1"/>
<label text="" style="* {margin-left: 300px;}"/>
</ui>
]]
ui=simUI.create(xml)
speed=(minMaxSpeed[1]+minMaxSpeed[2])*0.5
simUI.setSliderValue(ui,1,100*(speed-minMaxSpeed[1])/(minMaxSpeed[2]-minMaxSpeed[1]))
end
function sysCall_actuation()
result=sim.readProximitySensor(noseSensor) -- Read the proximity sensor
-- If we detected something, we set the backward mode:
if (result>0) then backUntilTime=sim.getSimulationTime()+4 end
if (backUntilTime<sim.getSimulationTime()) then
-- When in forward mode, we simply move forward at the desired speed
sim.setJointTargetVelocity(leftMotor,speed)
sim.setJointTargetVelocity(rightMotor,speed)
else
-- When in backward mode, we simply backup in a curve at reduced speed
sim.setJointTargetVelocity(leftMotor,-speed/2)
sim.setJointTargetVelocity(rightMotor,-speed/8)
end
end
function sysCall_cleanup()
simUI.destroy(ui)
end
然后设置两个motors的速度,就可以start simulation了!
上成品图:
下一篇写代码部分的,哈哈哈哈哈(☆_☆)!