268 lines
10 KiB
Python
268 lines
10 KiB
Python
#!/usr/bin/env python3
|
|
"""Sinh world mo phong co DOC: resources/worlds/slope_test.world + hai model.
|
|
|
|
Bo cuc (nhin tu tren xuong, x sang phai, y len tren; met):
|
|
|
|
y=33.5 +--------------------------------------------------+
|
|
| SANH TREN (san cao H = 1.5 m) |
|
|
y=25.5 +---------+---------+---------+---------+----------+
|
|
| 5 do | 10 do | 15 do | 20 do | 30 do |
|
|
| | | | | | lan rong 2.4 m,
|
|
| doc | doc | doc | doc | doc | ngan boi tuong
|
|
| dai | | | | ngan | 0.2 m
|
|
y=8 +---------+---------+---------+---------+----------+
|
|
| SANH DUOI (z = 0) |
|
|
y=0 +--------------------------------------------------+
|
|
x=0 x=12.8
|
|
|
|
Moi lan: doc bat dau o y = 8 (z = 0) va len toi H, roi di phang o H toi sanh
|
|
tren. Nam doc CUNG noi hai mat bang, nen di len lan nay xuong lan kia la mot
|
|
vong khep kin -- dung thu SLAM can thu: khep vong ca x, y LAN z.
|
|
|
|
Do dai doc = H / tan(goc): 17.1 m (5 do), 8.5 (10), 5.6 (15), 4.1 (20),
|
|
2.6 m (30 do).
|
|
|
|
Model `vslam_terrain_slope_test` la DIA HINH: bo mo phong coi mat cua no la
|
|
SAN robot di len, khong phai vat can (xem libs/vslam_sim/include/vslam/
|
|
gazebo_world.h, `kTerrainModelPrefix`). Mesh co toa do texture (vt) theo met,
|
|
de giai doan render GPU dan texture ma khong phai sinh lai.
|
|
|
|
Do dac co texture lay tu ~/gazebo_models/aws_hospital_fuel; thieu thi bo doc
|
|
world bo qua (khong loi), chi mat vai vat cho camera bam.
|
|
|
|
Chay tu goc repo:
|
|
|
|
python3 tools/gen_slope_world.py
|
|
export GAZEBO_MODEL_PATH=$PWD/resources/models
|
|
./build/app/vslam_node --sim --world resources/worlds/slope_test.world \\
|
|
--x 6.4 --y 4 --yaw 1.5708 --token t
|
|
"""
|
|
import argparse
|
|
import math
|
|
import os
|
|
|
|
H = 1.5 # chenh hai mat bang, m
|
|
ANGLES = [5, 10, 15, 20, 30] # do, tu trai sang phai
|
|
LANE_W = 2.4 # be rong long lan
|
|
WALL_T = 0.2 # be day tuong
|
|
HALL_D = 8.0 # chieu sau moi sanh
|
|
LANE_LEN = 17.5 # chieu dai lan (>= doc 5 do dai nhat)
|
|
WALL_H = H + 2.5 # tuong cao toi day, du che ca tang tren
|
|
|
|
XI = len(ANGLES) * LANE_W + (len(ANGLES) - 1) * WALL_T # be rong long nha
|
|
Y_LANE0 = HALL_D
|
|
Y_LANE1 = HALL_D + LANE_LEN
|
|
Y_TOP = Y_LANE1 + HALL_D
|
|
|
|
TERRAIN = 'vslam_terrain_slope_test'
|
|
WALLS = 'vslam_slope_test_walls'
|
|
|
|
# Do dac co texture: (model, x, y, z, yaw). Chi dat sat tuong dau/cuoi va o
|
|
# KHE GIUA cac duong vao lan (tam lan x = 1.2, 3.8, 6.4, 9.0, 11.6, moi ben
|
|
# 0.6 m) -- dat chan duong vao lan nao la lan do khong chay thu duoc: lop an
|
|
# toan dung xe truoc khi toi doc (da mac: xe lan o (12.0, 6.6) chan lan 30 do).
|
|
FURNITURE = [
|
|
('MetalCabinet', 0.6, 1.0, 0.0, 0.0),
|
|
('StorageRack', 3.0, 0.6, 0.0, 0.0),
|
|
('IVStand', 5.1, 1.0, 0.0, 0.0),
|
|
('PatientWheelChair', 7.7, 1.0, 0.0, 0.0),
|
|
('BedTable', 9.5, 0.8, 0.0, 0.0),
|
|
('VendingMachine', 12.1, 1.2, 0.0, -math.pi / 2),
|
|
('MetalCabinet', 0.6, 32.6, H, 0.0),
|
|
('IVStand', 5.1, 32.8, H, 0.0),
|
|
('StorageRack', 6.4, 32.9, H, math.pi),
|
|
('BedTable', 11.8, 32.6, H, 0.0),
|
|
]
|
|
|
|
|
|
class Obj:
|
|
"""Mesh OBJ: Z-up, met. Mat huong LEN khi dinh di nguoc kim dong ho tu tren."""
|
|
|
|
def __init__(self, header):
|
|
self.lines = ['# ' + l for l in header.splitlines()]
|
|
self.nv = 0
|
|
|
|
def quad(self, p, uv=None):
|
|
"""p: 4 dinh (x, y, z) theo thu tu vong. uv mac dinh = (x, y) met."""
|
|
base = self.nv
|
|
for i, (x, y, z) in enumerate(p):
|
|
self.lines.append('v %.4f %.4f %.4f' % (x, y, z))
|
|
u, w = uv[i] if uv else (x, y)
|
|
self.lines.append('vt %.4f %.4f' % (u, w))
|
|
self.nv += 4
|
|
a, b, c, d = (base + k + 1 for k in range(4))
|
|
self.lines.append('f %d/%d %d/%d %d/%d' % (a, a, b, b, c, c))
|
|
self.lines.append('f %d/%d %d/%d %d/%d' % (a, a, c, c, d, d))
|
|
|
|
def box(self, x0, y0, z0, x1, y1, z1):
|
|
"""Hop kin, mat huong RA ngoai. uv cua mat dung theo (doc tuong, z)."""
|
|
self.quad([(x0, y0, z1), (x1, y0, z1), (x1, y1, z1), (x0, y1, z1)]) # tren
|
|
self.quad([(x0, y1, z0), (x1, y1, z0), (x1, y0, z0), (x0, y0, z0)]) # day
|
|
self.quad([(x0, y0, z0), (x1, y0, z0), (x1, y0, z1), (x0, y0, z1)],
|
|
[(x0, z0), (x1, z0), (x1, z1), (x0, z1)]) # -y
|
|
self.quad([(x1, y1, z0), (x0, y1, z0), (x0, y1, z1), (x1, y1, z1)],
|
|
[(x1, z0), (x0, z0), (x0, z1), (x1, z1)]) # +y
|
|
self.quad([(x0, y1, z0), (x0, y0, z0), (x0, y0, z1), (x0, y1, z1)],
|
|
[(y1, z0), (y0, z0), (y0, z1), (y1, z1)]) # -x
|
|
self.quad([(x1, y0, z0), (x1, y1, z0), (x1, y1, z1), (x1, y0, z1)],
|
|
[(y0, z0), (y1, z0), (y1, z1), (y0, z1)]) # +x
|
|
|
|
def text(self):
|
|
return '\n'.join(self.lines) + '\n'
|
|
|
|
|
|
def lane_x(i):
|
|
x0 = i * (LANE_W + WALL_T)
|
|
return x0, x0 + LANE_W
|
|
|
|
|
|
def terrain_mesh():
|
|
m = Obj('Dia hinh world doc: san duoi z=0, san tren z=%.2f, %d lan doc.\n'
|
|
'Z-UP, met. Sinh boi tools/gen_slope_world.py -- dung sua tay.'
|
|
% (H, len(ANGLES)))
|
|
m.quad([(0, 0, 0), (XI, 0, 0), (XI, Y_LANE0, 0), (0, Y_LANE0, 0)])
|
|
m.quad([(0, Y_LANE1, H), (XI, Y_LANE1, H), (XI, Y_TOP, H), (0, Y_TOP, H)])
|
|
for i, deg in enumerate(ANGLES):
|
|
x0, x1 = lane_x(i)
|
|
y_top = Y_LANE0 + H / math.tan(math.radians(deg))
|
|
m.quad([(x0, Y_LANE0, 0), (x1, Y_LANE0, 0), (x1, y_top, H), (x0, y_top, H)])
|
|
if y_top < Y_LANE1 - 1e-6:
|
|
m.quad([(x0, y_top, H), (x1, y_top, H), (x1, Y_LANE1, H), (x0, Y_LANE1, H)])
|
|
return m.text()
|
|
|
|
|
|
def walls_mesh():
|
|
m = Obj('Tuong va cot cua world doc. Z-UP, met.\n'
|
|
'Sinh boi tools/gen_slope_world.py -- dung sua tay.')
|
|
t = WALL_T
|
|
# Vong ngoai.
|
|
m.box(-t, -t, 0, 0, Y_TOP + t, WALL_H)
|
|
m.box(XI, -t, 0, XI + t, Y_TOP + t, WALL_H)
|
|
m.box(-t, -t, 0, XI + t, 0, WALL_H)
|
|
m.box(-t, Y_TOP, 0, XI + t, Y_TOP + t, WALL_H)
|
|
# Tuong ngan lan.
|
|
for i in range(len(ANGLES) - 1):
|
|
_, x1 = lane_x(i)
|
|
m.box(x1, Y_LANE0, 0, x1 + t, Y_LANE1, WALL_H)
|
|
# Vai cot va mau tuong de LiDAR co cho bam trong hai sanh rong. Dat o KHE
|
|
# giua cac duong vao/ra lan (xem FURNITURE), khong chan lan nao.
|
|
m.box(2.3, 3.8, 0, 2.7, 4.2, WALL_H)
|
|
m.box(10.1, 3.8, 0, 10.5, 4.2, WALL_H)
|
|
m.box(4.6, 5.0, 0, 5.6, 5.2, WALL_H)
|
|
m.box(4.9, 29.3, H, 5.3, 29.7, WALL_H)
|
|
m.box(7.5, 29.3, H, 7.9, 29.7, WALL_H)
|
|
m.box(9.7, 30.5, H, 10.9, 30.7, WALL_H)
|
|
return m.text()
|
|
|
|
|
|
SDF = '''<?xml version="1.0" ?>
|
|
<sdf version="1.6">
|
|
<model name="{name}">
|
|
<link name="link">
|
|
<visual name="visual">
|
|
<geometry>
|
|
<mesh>
|
|
<uri>model://{name}/meshes/{mesh}</uri>
|
|
</mesh>
|
|
</geometry>
|
|
</visual>
|
|
<collision name="collision">
|
|
<geometry>
|
|
<mesh>
|
|
<uri>model://{name}/meshes/{mesh}</uri>
|
|
</mesh>
|
|
</geometry>
|
|
</collision>
|
|
</link>
|
|
<static>1</static>
|
|
</model>
|
|
</sdf>
|
|
'''
|
|
|
|
CONFIG = '''<?xml version="1.0" ?>
|
|
<model>
|
|
<name>{name}</name>
|
|
<version>1.0</version>
|
|
<sdf version="1.6">model.sdf</sdf>
|
|
<author>
|
|
<name>vslam_cpp</name>
|
|
<email></email>
|
|
</author>
|
|
<description>
|
|
{desc}
|
|
Sinh boi tools/gen_slope_world.py.
|
|
</description>
|
|
</model>
|
|
'''
|
|
|
|
|
|
def include(name, uri, x, y, z, yaw):
|
|
return (' <model name="%s">\n'
|
|
' <include>\n'
|
|
' <uri>model://%s</uri>\n'
|
|
' <static>true</static>\n'
|
|
' <pose>%.3f %.3f %.3f 0 0 %.4f</pose>\n'
|
|
' </include>\n'
|
|
' </model>\n' % (name, uri, x, y, z, yaw))
|
|
|
|
|
|
def world_text():
|
|
parts = ['<?xml version="1.0" ?>\n',
|
|
'<!-- World co DOC cho mo phong dia hinh 3D. Sinh boi\n'
|
|
' tools/gen_slope_world.py -- dung sua tay.\n'
|
|
' Xuat phat goi y: --x 6.4 --y 4 --yaw 1.5708 (sanh duoi, nhin\n'
|
|
' vao lan 15 do). Lan doc theo x: ',
|
|
', '.join('%d do o x = %.1f' % (d, sum(lane_x(i)) / 2)
|
|
for i, d in enumerate(ANGLES)),
|
|
'. -->\n',
|
|
'<sdf version="1.6">\n <world name="slope_test">\n',
|
|
' <include><uri>model://ground_plane</uri></include>\n',
|
|
' <include><uri>model://sun</uri></include>\n',
|
|
include(TERRAIN + '_1', TERRAIN, 0, 0, 0, 0),
|
|
include(WALLS + '_1', WALLS, 0, 0, 0, 0)]
|
|
count = {}
|
|
for name, x, y, z, yaw in FURNITURE:
|
|
count[name] = count.get(name, 0) + 1
|
|
parts.append(include('%s_%d' % (name, count[name]), name, x, y, z, yaw))
|
|
parts.append(' </world>\n</sdf>\n')
|
|
return ''.join(parts)
|
|
|
|
|
|
def write(path, text):
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
with open(path, 'w') as f:
|
|
f.write(text)
|
|
print('ghi', path)
|
|
|
|
|
|
def main():
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
|
ap.add_argument('--root', default=os.path.dirname(here),
|
|
help='goc repo (mac dinh: thu muc cha cua tools/)')
|
|
a = ap.parse_args()
|
|
models = os.path.join(a.root, 'resources', 'models')
|
|
|
|
write(os.path.join(models, TERRAIN, 'meshes', 'terrain.obj'), terrain_mesh())
|
|
write(os.path.join(models, TERRAIN, 'model.sdf'),
|
|
SDF.format(name=TERRAIN, mesh='terrain.obj'))
|
|
write(os.path.join(models, TERRAIN, 'model.config'),
|
|
CONFIG.format(name=TERRAIN, desc='San, chieu nghi va 5 lan doc (dia hinh).'))
|
|
write(os.path.join(models, WALLS, 'meshes', 'walls.obj'), walls_mesh())
|
|
write(os.path.join(models, WALLS, 'model.sdf'),
|
|
SDF.format(name=WALLS, mesh='walls.obj'))
|
|
write(os.path.join(models, WALLS, 'model.config'),
|
|
CONFIG.format(name=WALLS, desc='Tuong bao, tuong ngan lan va cot.'))
|
|
write(os.path.join(a.root, 'resources', 'worlds', 'slope_test.world'), world_text())
|
|
|
|
for i, deg in enumerate(ANGLES):
|
|
x0, x1 = lane_x(i)
|
|
print('lan %2d do: x %.1f-%.1f, doc dai %.2f m, chan doc y = %.1f, '
|
|
'tuong gia o chan doc cach %.2f m' %
|
|
(deg, x0, x1, H / math.tan(math.radians(deg)), Y_LANE0,
|
|
0.2165 / math.tan(math.radians(deg))))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|