前面已经介绍过如何在CARLA中释放自车。如何释放障碍物也是另一个非常重要的【技能】。接下来以释放障碍物车为例,
介绍如何优雅的布置属于自己的场景!
import glob
import os
import sys
import random
import time
try:
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
actor_list = []
try:
client = carla.Client("127.0.0.1", 2000)
client.set_timeout(20.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
bp = blueprint_library.filter("ambulance")[0]
spawn_point = carla.Transform(carla.Location(x=-15, y=0, z=2.0), carla.Rotation(pitch=0, yaw=180, roll=0))
vehicle = world.spawn_actor(bp, spawn_point)
vehicle.apply_control(carla.VehicleControl(throttle=0.0, steer=0.0))
actor_list.append(vehicle)
time.sleep(5)
finally:
for actor in actor_list:
actor.destory()
print('All Cleaned up!')
废话不多说,直接上最简单粗暴的例子。例子中是在设定的点中释放一辆救护车到CARLA环境中。
spawn_point = carla.Transform(carla.Location(x=-15, y=0, z=2.0),
carla.Rotation(pitch=0, yaw=180, roll=0))
其中的核心就是上面这句,设定释放位置x、y、z和pitch、yaw、roll
直接运行python文件
成功在CARLA中添加了障碍物车辆。虽然简单的代码实现了简单的操作,但是存在许多不方便。
1、释放的障碍物车辆无法取消,必须关闭整个CARLA才能消失
2、每次都要通过修改python来更改释放的位置与姿态,并且对于姿态与位置都没办法直接通过鼠标指定
import glob
import os
import sys
import random
import time
try:
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
npc_pos=[[5,-2,2,180],[-18,-2,2,180]]
actor_list = []
# Connect to the client and retrieve the world object
client = carla.Client("127.0.0.1", 2000)
world = client.get_world()
settings = world.get_settings()
settings.synchronous_mode = True # Enables synchronous mode
settings.fixed_delta_seconds = 0.05
world.apply_settings(settings)
#client.set_timeout(20.0)
def main():
try:
blueprint_library = world.get_blueprint_library()
bp = blueprint_library.filter("ambulance")[0]
print(bp)
spawn_point = carla.Transform(carla.Location(x=-15, y=0, z=2.0), carla.Rotation(pitch=0, yaw=180, roll=0))
vehicle = world.spawn_actor(bp, spawn_point)
time.sleep(1)
while True:
time.sleep(1)
finally:
print('\ndestroying %d vehicles' % len(actor_list))
client.apply_batch([carla.command.DestroyActor(x) for x in actor_list])
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
print('\ndestroying %d vehicles' % len(actor_list))
client.apply_batch([carla.command.DestroyActor(x) for x in actor_list])
只需要稍微更改下最基础的代码,在vehicle = world.spawn_actor(bp, spawn_point)之后增加一个死循环,让脚本不会在释放障碍物后退出。
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
print('\ndestroying %d vehicles' % len(actor_list))
client.apply_batch([carla.command.DestroyActor(x) for x in actor_list])
最后通过__main__运行脚本,释放障碍物后如果发现不合适直接键盘按下Ctrl+C后清空缓存,实现障碍物消失。
接下来就要解决,如何通过鼠标释放障碍物。这部分代码比较复杂,就整理下具体实现的思路。可以参考前面【自车释放】的做法,利用rviz实现通过鼠标拖放,释放障碍物。
【自车释放】使用的是【2D Pose Estimate】还有一个【2D Nav Goal】
点击【2D Nav Goal】便可以在rviz中释放一个位置与方向,并且发布一个话题move_base_simple。
需要作的就是把释放障碍物的代码,编写成一个节点。启动carla的同时连同rviz一起启动,在节点中订阅move_base_simple,获取x\y\z\roll\pitch\yaw
结合在地图中释放自车的方式,实现在任意地图位置释放障碍物车。
欢迎关注
古德曼那汽车工业