V-rep 逆运动学(IK)7-fkAndIkResolutionForParallelMechanisms 示例重建
1.建立模型
(1)打开示例3的模型
V-rep 逆运动学(IK) 3-twoIkGroupsWithEachOneIkElement-resolutionOrderIsNotRelevant示例重建
这里所有的圆柱都是直径0.02,高0.1的
(2)建立第三个关节
把所有之前关节的角度设置成0°,方便调整关节的位置
-
复制关节j2_1,粘贴为j2_3,沿y轴方向平移0.1
-
复制关节j2_2,粘贴为j2_4,沿y轴方向平移0.1
-
将关节j2_3和j2_4分别放置到l2_1和l2_2下(方便调整位置)
-
调整第一组连杆和第二组连杆的颜色
(3)建立第三个连杆
- 新建一个scene
- 建立三个高为0.1,直径为0.02的圆柱,并更改颜色
- 把三个连杆组合成一个物体
- 把合并后的Cylinder3复制到示例7的场景里并移动合适距离
(4)调整base_2的位置
(5)建立正确的父级关系,删除tip_2
(6)重命名,链接重命名后的closureTip与target
(7)重新调整关节的角度至图示状态
fk_motor1:-60
j3:-60
fk_motor2:-60
2.建立ik组和元素
3.添加脚本文件(non-threaded)
注意:自带文件里的新建UI的方式自己在重构场景时会报错,可以采取下述代码中的方式构建cuotom UI
<ui title="'.."Motor Angle"..'
设置了滑条窗口的名称,此处设置为Motor Angle
因为本示例有两个滑条可以调两个电机的角度,因此建立两行<hslider minimum="0" maximum="1000" id="1"/> <label text="Motor1" style="* {margin-left: 300px;}"/>
hslider 表明这是个滑条
minimum 是滑条最小值
maximum 是滑条最大值
text 显示了滑条对应的名称
id的值不能重复!
function sysCall_init()
-- Create the custom UI:
xml = '<ui title="'.."Motor Angle"..' speed" closeable="false" resizeable="false" activate="false">'..[[
<hslider minimum="0" maximum="1000" id="1"/>
<label text="Motor1" style="* {margin-left: 300px;}"/>
<hslider minimum="0" maximum="1000" id="2"/>
<label text="Motor2" style="* {margin-left: 300px;}"/>
</ui>
]]
fkui=simUI.create(xml)
ikGroup = sim.getIkGroupHandle('ik')
tipDummy = sim.getObjectHandle('tip')
motorJoint1 = sim.getObjectHandle('fk_motor1')
motorJoint2 = sim.getObjectHandle('fk_motor2')
-- First set the motor joint into ik mode:
sim.setJointMode(motorJoint1,sim.jointmode_ik,0)
sim.setJointMode(motorJoint2,sim.jointmode_ik,0)
-- Make sure the IK element that brings the tip onto the target is disabled:
sim.setIkElementProperties(ikGroup,tipDummy,0)
-- Now close the mechanism (if it was open):
sim.handleIkGroup(ikGroup)
-- Read the current position of the motor joint:
local angle=sim.getJointPosition(motorJoint1)
simUI.setSliderValue(fkui,1,1000*(angle*180/math.pi+160)/320)
angle=sim.getJointPosition(motorJoint2)
simUI.setSliderValue(fkui,2,1000*(angle*180/math.pi+160)/320)
end
function sysCall_actuation()
-- First set the motor joint into ik mode:
sim.setJointMode(motorJoint1,sim.jointmode_ik,0)
sim.setJointMode(motorJoint2,sim.jointmode_ik,0)
-- Set the desired joint angle:
local angle=simUI.getSliderValue(fkui,1)
sim.setJointPosition(motorJoint1,math.pi*(-160+320*angle/1000)/180)
local angle2=simUI.getSliderValue(fkui,2)
sim.setJointPosition(motorJoint2,math.pi*(-160+320*angle2/1000)/180)
-- Make sure the IK element that brings the tip onto the target is enabled:
sim.setIkElementProperties(ikGroup,tipDummy,sim.ik_x_constraint+sim.ik_y_constraint)
-- Compute:
sim.handleIkGroup(ikGroup)
end
4.运行结果
启动后末端直接运动到目标位置,滑动滑动条可以直接更改两个电机的角度