function sysCall_init()[将项目和脚本链接]
bubbleRobBase=sim.getObjectAssociatedWithScript(sim.handle_self)[得到电机的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
[设置速度区间,180/3.14=57,以后直接用这个转换一下,注意这里转化为弧度]
minMaxSpeed={50*math.pi/180,300*math.pi/180}-- Min and max speeds for each motor
[切换前进后退的标志位]
backUntilTime=-1[创建UI--这里没明白]
xml ='<ui title="'..sim.getObjectName(bubbleRobBase)..' speed" closeable="false" resizeable="false" activate="false">'..[[<hslider minimum="0" maximum="100" on-change="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
2. sysCall_actuation() 动作函数
检测与物体的距离
正常工作在前进状态
更换右后退状态
function sysCall_actuation()[检测距离传感器, 快碰到return1,else is 0;一般输出为0]
result=sim.readProximitySensor(noseSensor)[如果碰到物体,设置倒退模式(大可更换其他方法)]if(result>0)
then backUntilTime=sim.getSimulationTime()+4
end
[前进模式]if(backUntilTime<sim.getSimulationTime()) then
sim.setJointTargetVelocity(leftMotor,speed)
sim.setJointTargetVelocity(rightMotor,speed)else[后退模式]
sim.setJointTargetVelocity(leftMotor,-speed/2)
sim.setJointTargetVelocity(rightMotor,-speed/8)
end
end
3.function sysCall_cleanup() 清除
停止运行时执行一次
一般用来清除窗口
function sysCall_cleanup()
simUI.destroy(ui)
end
4.子函数
改变速度
只在UI中用了一下不知道干了啥
function speedChange_callback(ui,id,newVal)
speed=minMaxSpeed[1]+(minMaxSpeed[2]-minMaxSpeed[1])*newVal/100
end