建立车的本体

1 点击 add ->primitive shape->cuboid(长方体)
2 设置长宽高0.2,0.3,0.1(x-0.2,y-0.3,z-0.1)
3 本体上升0.1:

translation->z:0.1

建立车轮1

1 点击 add ->primitive shape->cylinder(圆柱)
2 设置直径0.05,高度0.05(x-0.05,z-0.05)
3 车轮转正:相对于世界x转90度

rotation->x->90
4 将车轮移到正确位置:相对于世界坐标(x-0.13,y-0.1)

position->

使转动关节1和车轮1位置重叠

1 按住shift 选中关节1和车轮1
2 position->应用

position->

修改轮子透明度

1 选中轮子,点击左边栏中的搜索标志,点击adjust color->opacity(透明度)选中设置0.5

旋转转动关节到正确位置


1 rotation->ownframe->y->90

设置轮子名字rfw(右前轮)关节名字rfm(右前关节)车名字car

rfw(右前轮)装在rfm(右前关节)上:拖一下就可以

rfm(右前关节)装在车(car)上

给关节使能电动

1 点击转动关节1->最下方 show dynamic->motor enabled勾选
2 设置转动关节不可见:点击转动关节1->common->camera visibility laye->去掉勾选

复制四份车轮1,转动关节1

1 按住shift 选中rfm和rfw,右键edit->copy,点击小车附近右键edit->paste

分别命名lfw(左前轮),lfm(左前关节),lrw(左后轮),lrm(左后关节)rrw,rrm

调整其余四个车轮位置,并装在车上

1左前关节(lfm):position->x加负号
2左后关节(lrm): position->x加负号y加负号
3右后关节(rrm): position->y加负号

添加路线

add->path->circle type

修改路线的形状

1 选中path,点击左栏的曲线
2 使路线平滑:勾选path is flat
3 修改路线形状:点击左侧的path point1,点击菜单栏第七个上下移动的图标点击position 鼠标拉着点向外,依次类推
4 修改路线颜色(黑:因为用传感器)选中path,点击左栏的曲线
line size:5
adjust line color->第一个
5 路线形状使能:show path shaping dialog->enable
6 路线类型设置为平面:type->horizontal

创建传感器

add->vision sensor->orthographic type

1 修改属性:勾选use local lights(使用当前光源)
2 传感器探测像素值:resolution x/y: 1,1
3 传感器探测距离:far clipping plane:0.2(米)
4 传感器位置:点击菜单栏第七个上下移动的图标点击position
相对世界:x-0.02,y-0.16,z-0.1
5传感范围位置移动:点击菜单栏第八个旋转的图标点击rotation
相对自己:x-180
6命名为rightsensor

复制两个传感器

1命名为leftsensor命名为middlesensor
2修改middlesensor位置:相对世界:x-0
3修改leftsensor位置:相对世界:x-加负号

添加视图

add->float view(可以显示传感器数据)(三个)
1选中leftsensor,在float view里右键->view->第一个;其他两个同理
2传感器都拖到小车上

给小车创建集合

1按住shift鼠标框住所有部件后,按住shift不动,按住ctrl点击框选区域,在点一下car
2点击左侧搜索图表:common->选中object is model base

把小车移到路线上

1点击小车,点击菜单栏第七个上下移动的图标,鼠标拖动小车

编写程序

1创建脚本:点击左侧栏小本本->点击insert new script->选择child script( threaded)(子脚本线程)
2脚本关联到车上:associate object->car
3代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

//主程序
function sysCall_threadmain()
-- Put some initialization code here
//初始化
//获取左右前关节的句柄
rghtmotor=sim.getObjectHandle("rightfrontmotor")
leftmotor=sim.getObjectHandle("leftfrontmotor")
//获取三个传感器的句柄
lefts=sim.getObjectHandle("leftsensor")
middles=sim.getObjectHandle("middlesensor")
rights=sim.getObjectHandle("rightsensor")
//设置速度范围
speedrange={-1,-6}
//设置最大响应时间
backwardtime=-1

--creat custom ui
xml=
[[
<ui title="speed" closeable="true" resizeable="true">
<hslider minimum="0" maximum="100" on-change="speedrange_callback" id="1"/>
</ui>
]]
//创建ui
ui=simUI.create(xml)

speed=(speedrange[1]+speedrange[2])*0.5
//编写ui中的函数
function speedrange_callback(ui,id,newVal)
speed=speedrange[1]+(speedrange[2]-speedrange[1])*newVal/100
end

-- Put your main loop here, e.g.:
//仿真的内容
while sim.getSimulationState()~=sim.simulation_advancing_abouttostop do
//初始传感器
sensorreadingleft=false
sensorreadingmiddle=false
sensorreadingright=false
//读传感器的返回结果和数据
resultl,datal=sim.readVisionSensor(lefts)
resultm,datam=sim.readVisionSensor(middles)
resultr,datar=sim.readVisionSensor(rights)

//判断传感器是否读到数据,读到就收取数据
if resultl>=0 then
sensorreadingleft=(datal[11]<0.2)
end
if resultm>=0 then
sensorreadingmiddle=(datam[11]<0.2)
end
if resultr>=0 then
sensorreadingright=(datar[11]<0.2)
end
//定义左右关节速度(初始相等)
leftv=speed
rightv=speed
//如果左传感器读到值,左关节速度赋值(小一点就能转弯了)
if sensorreadingleft==true then
leftv=0.03*speed
end
//如果右传感器读到值,右关节速度赋值(小一点就能转弯了)
if sensorreadingright==true then
rightv=0.03*speed
end
//如果左右传感器读到值,直接进行下个周期(保持原方式行进)
if sensorreadingright==true and sensorreadingleft==true then
backwardtime=sim.getSimulationTime()+2
end

//如果运行的回传时间仍在响应时间内(未超时)设置左右关节速度
if backwardtime<sim.getSimulationTime() then
sim.setJointTargetVelocity(rghtmotor,rightv)
sim.setJointTargetVelocity(leftmotor,leftv)
end
//如果运行的回传时间超过响应时间内(超时)小车后退
if backwardtime>sim.getSimulationTime() then
sim.setJointTargetVelocity(rghtmotor,-speed)
sim.setJointTargetVelocity(leftmotor,-speed/10)
end
//切换脚本
sim.switchThread() -- resume in next simulation step
end
end
//清除工作
function sysCall_cleanup()
-- Put some clean-up code here
end

源码链接

链接:https://pan.baidu.com/s/1LriziMKsp0-iWGk8t47YzA?pwd=33yf
提取码:33yf