V-rep运动学仿真笔记(四)——固定四轮小车,UI控制转向

本文介绍了一种通过修改脚本代码实现四轮小车转弯的方法。通过设置不同轮子的速度,小车能够向前、后、左、右移动。代码中详细展示了如何使用sim.setJointTargetVelocity函数来控制各轮子的转速。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

还是接上面的固定四轮小车,这里修改了一下脚本代码,使得小车可以转弯,是一个简单应用程序:

还是在script文件编写如下,这里就不写注解了:


function sysCall_init()
  --注解可以参考前面一章
xml = [[
<ui closeable="true" onclose="closeEventHandler" resizable="true" size="220,200">
    <label text="Car control pan" wordwrap="true" />
    <group>
        <label text="Set speed(0.1deg/s):"  wordwrap="true" />
        <spinbox minimum="-5000" maximum="5000" onchange = "speed_set" />
        <stretch />
    </group>
    
    <group>
        <button text="Start move" onclick = "Start_move" />
        <button text="Stop move" onclick = "Stop_move" />
        <stretch />
    </group>   

    <group>
        <button text="Front" onclick = "Front_move" />
        <button text="Back" onclick = "Back_move" />
        <button text="Left" onclick = "Left_move" />
        <button text="Right" onclick = "Right_move" />
        <stretch />
    </group>  
</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)
    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

function Start_move(h)
    Start_flag = true
    Front_flag = true
    Back_flag = false     
end

function Stop_move(h)
    Start_flag = false   
end

function Front_move(h)
    Front_flag = true
    Back_flag = false  
    Right_flag = false  
    Left_flag = false 
end

function Back_move(h)
    Back_flag = true  
    Front_flag = false  
    Right_flag = false  
    Left_flag = false    
end

function Left_move(h)
    Left_flag = true  
    Right_flag = false 
end

function Right_move(h)
    Right_flag = true 
    Left_flag = false   
end

function speed_set(ui,id,newVal)
    Speed_vel = newVal
end

function sysCall_actuation()
    if Start_flag then
            if Front_flag then
                sim.setJointTargetVelocity(Red_wheel,Speed_vel/10)
                sim.setJointTargetVelocity(Green_wheel,Speed_vel/10)
                sim.setJointTargetVelocity(Blue_wheel,-Speed_vel/10)
                sim.setJointTargetVelocity(Yellow_wheel,-Speed_vel/10)                  
            end

            if Back_flag then
                sim.setJointTargetVelocity(Red_wheel,-Speed_vel/10)
                sim.setJointTargetVelocity(Green_wheel,-Speed_vel/10)
                sim.setJointTargetVelocity(Blue_wheel,Speed_vel/10)
                sim.setJointTargetVelocity(Yellow_wheel,Speed_vel/10)
            end

            if Left_flag then  
                sim.setJointTargetVelocity(Red_wheel,Speed_vel/10)
                sim.setJointTargetVelocity(Green_wheel,Speed_vel/5)
                sim.setJointTargetVelocity(Blue_wheel,-Speed_vel/10)
                sim.setJointTargetVelocity(Yellow_wheel,-Speed_vel/5)
            end

            if Right_flag then
                sim.setJointTargetVelocity(Red_wheel,Speed_vel/5)
                sim.setJointTargetVelocity(Green_wheel,Speed_vel/10)
                sim.setJointTargetVelocity(Blue_wheel,-Speed_vel/5)
                sim.setJointTargetVelocity(Yellow_wheel,-Speed_vel/10)
            end
    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)
end

效果如下:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值