接着前一章的继续,导入小车后,做小车的控制;这里先说一下v-rep的uiAPI脚本,打开v-rep帮助文档,可以找到Custom UIs的介绍,包括对应的API等,如下:
然后我们可以在v-rep安装文档里找到,对应的demo文件,V-REP3\V-REP_PRO_EDU\scenes,如下:
打开后,在就可以看到有这样的一个UI:
基本的控件都包含了,我可以查看里面的script文件,v-rep的UI是用xml写的,其中截取了一部分样式如下:
xml = [[
<ui closeable="true" on-close="closeEventHandler" resizable="true">
<label text="This is a demo of the CustomUI plugin. Browse through the tabs below to explore all the widgets that can be created with the plugin." wordwrap="true" />
<tabs>
<tab title="Buttons">
<button text="Simple button" on-click="buttonClick" />
<label text="Multi valued option:" />
<group>
<label text="These are radiobutton elements. You can only select one of these." wordwrap="true" />
<label text="No item has been selected" id="1000" wordwrap="true" />
<radiobutton text="A" on-click="radiobuttonClick" id="1001" />
<radiobutton text="B" on-click="radiobuttonClick" id="1002" />
<radiobutton text="C" on-click="radiobuttonClick" id="1003" />
</group>
<..............删除了一部分>
</tab>
</tabs>
</ui>
]]
ui=simUI.create(xml)
好了,有了以上我们就可以给小车写一些代码,让小车可以控制动起来,添加如下代码:
function sysCall_init()
--系统函数下添加xml
xml = [[
<ui closeable="true" onclose="closeEventHandler" resizable="true" size="220,200">
<label text="Car control pan" wordwrap="true" />
<group>
<label text="Red wheel speed(0.1deg/s):" wordwrap="true" />
<spinbox minimum="-5000" maximum="5000" onchange = "Red_speed_set" />
<label text="Green wheel speed(0.1deg/s)" wordwrap="true" />
<spinbox minimum="-5000" maximum="5000" onchange = "Green_speed_set" />
<label text="Blue wheel speed(0.1deg/s)" wordwrap="true" />
<spinbox minimum="-5000" maximum="5000" onchange = "Blue_speed_set" />
<label text="Yellow wheel speed(0.1deg/s)" wordwrap="true" />
<spinbox minimum="-5000" maximum="5000" onchange = "Yellow_speed_set" />
<stretch />
</group>
<group>
<button text="Start move" onclick = "Start_move" />
<button text="Stop move" onclick = "Stop_move" />
<stretch />
</group>
</ui>
]]
--启动动UI
ui=simUI.create(xml)
--设置各个关节的句柄
Red_wheel=sim.getObjectHandle('joint12')
Green_wheel=sim.getObjectHandle('joint22')
Blue_wheel=sim.getObjectHandle('joint32')
Yellow_wheel=sim.getObjectHandle('joint42')
--ui=simUI.create(xml) --这句也可以启动UI
--设置转速变量
Red_vel = 0
Green_vel = 0
Blue_vel = 0
Yellow_vel = 0
--开始标志
Start_flag = false
end
--关闭窗口时候触发
function closeEventHandler(h)
sim.addStatusbarMessage('Window '..h..' is closing...')
--隐藏窗口
simUI.hide(h)
end
--添加开始函数,对应onclick
function Start_move(h)
Start_flag = true
end
--添加停止函数,对应onclick
function Stop_move(h)
Start_flag = false
end
--添加速度函数,对应onclick
function Red_speed_set(ui,id,newVal)
Red_vel = newVal
end
--添加速度函数,对应onclick
function Green_speed_set(ui,id,newVal)
Green_vel = newVal
end
--添加速度函数,对应onclick
function Blue_speed_set(ui,id,newVal)
Blue_vel = newVal
end
--添加速度函数,对应onclick
function Yellow_speed_set(ui,id,newVal)
Yellow_vel = newVal
end
--系统函数内
function sysCall_actuation()
--添加处理函数,如果启动了,设置关节的速度,每启动则设置速度为0
if Start_flag then
--sim.setJointTargetVelocity设置速度api
sim.setJointTargetVelocity(Red_wheel,Red_vel/10)
sim.setJointTargetVelocity(Green_wheel,Green_vel/10)
sim.setJointTargetVelocity(Blue_wheel,-Blue_vel/10)
sim.setJointTargetVelocity(Yellow_wheel,-Yellow_vel/10)
else
sim.setJointTargetVelocity(Red_wheel,0)
sim.setJointTargetVelocity(Green_wheel,0)
sim.setJointTargetVelocity(Blue_wheel,0)
sim.setJointTargetVelocity(Yellow_wheel,0)
end
end
function sysCall_cleanup()
-- do some clean-up here
simUI.destroy(ui) --清除ui
end
效果如下,档设置好速度后小车就开始按照设定运行:
测试时候,想查看小车运动轨迹,这时候可以添加graph来查看轨迹,先新建一个dummy和graph,然后双击graph,添加dummy的相对世界坐标的绝对位置:
将xyz数据都添加好后,电机Edit 3d curves:
将graph与dummy放在car body下面,如下结构:
再次运行后,就可看到如下,小车后面会有一条轨迹: