赞
踩
今天主要学习了example里面的tutorial.py的源码,越发觉得这才应该是最开始学习的代码,大部分代码块都有注释,理解起来十分容易。
- #!/usr/bin/env python
-
- # Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
- # Barcelona (UAB).
- #
- # This work is licensed under the terms of the MIT license.
- # For a copy, see <https://opensource.org/licenses/MIT>.
-
- import glob
- import os
- import sys
-
- 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
-
- import random
- import time
-
-
- def main():
- actor_list = []
-
- # In this tutorial script, we are going to add a vehicle to the simulation
- # and let it drive in autopilot. We will also create a camera attached to
- # that vehicle, and save all the images generated by the camera to disk.
-
- try:
- # First of all, we need to create the client that will send the requests
- # to the simulator. Here we'll assume the simulator is accepting
- # requests in the localhost at port 2000.
- client = carla.Client('localhost', 2000)
- client.set_timeout(2.0)
-
- # Once we have a client we can retrieve the world that is currently
- # running.
- world = client.get_world()
-
- # The world contains the list blueprints that we can use for adding new
- # actors into the simulation.
- blueprint_library = world.get_blueprint_library()
-
- # Now let's filter all the blueprints of type 'vehicle' and choose one
- # at random.
- bp = random.choice(blueprint_library.filter('vehicle'))
-
- # A blueprint contains the list of attributes that define a vehicle's
- # instance, we can read them and modify some of them. For instance,
- # let's randomize its color.
- if bp.has_attribute('color'):
- color = random.choice(bp.get_attribute('color').recommended_values)
- bp.set_attribute('color', color)
-
- # Now we need to give an initial transform to the vehicle. We choose a
- # random transform from the list of recommended spawn points of the map.
- transform = random.choice(world.get_map().get_spawn_points())
-
- # So let's tell the world to spawn the vehicle.
- vehicle = world.spawn_actor(bp, transform)
-
- # It is important to note that the actors we create won't be destroyed
- # unless we call their "destroy" function. If we fail to call "destroy"
- # they will stay in the simulation even after we quit the Python script.
- # For that reason, we are storing all the actors we create so we can
- # destroy them afterwards.
- # 这里是为了帮助销毁vehicles的时候方便,特地设置了一个list。
- actor_list.append(vehicle)
- print('created %s' % vehicle.type_id)
-
- # Let's put the vehicle to drive around.
- vehicle.set_autopilot(True)
-
- # Let's add now a "depth" camera attached to the vehicle. Note that the
- # transform we give here is now relative to the vehicle.
- camera_bp = blueprint_library.find('sensor.camera.depth')
- cam_bp = blueprint_library.find('sensor.camera.rgb')
-
- camera_transform = carla.Transform(carla.Location(z=20))
- cam01_transform = carla.Transform(carla.Location(x=10, y=10, z=20))
-
- # camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle)
- camera = world.spawn_actor(camera_bp, camera_transform)
- cam01 = world.spawn_actor(cam_bp, cam01_transform)
- actor_list.append(camera)
- actor_list.append(cam01)
-
- print('created %s' % camera.type_id)
- print('created %s' % cam01.type_id)
-
- # Now we register the function that will be called each time the sensor
- # receives an image. In this example we are saving the image to disk
- # converting the pixels to gray-scale.
- # cc = carla.ColorConverter.LogarithmicDepth
- # camera.listen(lambda image: image.save_to_disk('_out/%06d.png' % image.frame, cc))
- cam01.listen(lambda image: image.save_to_disk('_out/%06d.png' % image.frame))
- # camera.listen(lambda image: image.save_to_disk('_out/%06d.png' % image.frame))
-
-
- # Oh wait, I don't like the location we gave to the vehicle, I'm going
- # to move it a bit forward.
- location = vehicle.get_location()
- location.x += 40
- vehicle.set_location(location)
- print('moved vehicle to %s' % location)
-
- # But the city now is probably quite empty, let's add a few more
- # vehicles.
- # 创造一列车10辆
- transform.location += carla.Location(x=40, y=-3.2)
- transform.rotation.yaw = -180.0
- # for _ in range(0, 10):
- # transform.location.x += 8.0
-
- # bp = random.choice(blueprint_library.filter('vehicle'))
-
- # # This time we are using try_spawn_actor. If the spot is already
- # # occupied by another object, the function will return None.
- # npc = world.try_spawn_actor(bp, transform)
- # if npc is not None:
- # actor_list.append(npc)
- # npc.set_autopilot(True)
- # print('created %s' % npc.type_id)
-
- time.sleep(500)
-
- finally:
-
- print('destroying actors')
- camera.destroy()
- client.apply_batch([carla.command.DestroyActor(x) for x in actor_list])
- print('done.')
-
-
- if __name__ == '__main__':
-
- main()
然后我看到它有一部分里面有存储sensor采集到的数据的部分,我就想起了昨天的问题,准备借鉴这部分的内容在验证一下:
- """
- 学习tutorial.py时,看到了类似功能的代码,然后检验了一下我昨天的设想。
- 主要内容是:
- 生成一个camera.depth、一个camera.rgb、一个lidar然后分别存储进actor_list[],最后保存传感器采集
- 的数据,然后返回matlab显示。采集的数量目前来看是通过time.sleep()函数来控制的。
- """
-
- import glob
- import os
- import sys
-
- 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
- import random
- import time
-
-
- def main():
- actor_list = []
-
- try:
- client = carla.Client('localhost', 2000)
- client.set_timeout(2.0)
-
- world = client.get_world()
-
- blueprint_library = world.get_blueprint_library()
-
- bp = random.choice(blueprint_library.filter('vehicle'))
-
- if bp.has_attribute('color'):
- color = random.choice(bp.get_attribute('color').recommended_values)
- bp.set_attribute('color', color)
-
- transform = random.choice(world.get_map().get_spawn_points())
-
- # 在蓝图库中找到三个传感器
- camera_bp = blueprint_library.find('sensor.camera.depth')
- cam_bp = blueprint_library.find('sensor.camera.rgb')
- lidar_bp = blueprint_library.find('sensor.lidar.ray_cast')
- # 设置三个传感器的生成的位置
- camera_transform = carla.Transform(carla.Location(z=20))
- cam01_transform = carla.Transform(carla.Location(x=10, y=10, z=20))
- lidar01_transform = carla.Transform(carla.Location(x=20, y=10, z=1))
- # 这个location的位置选的不太好,居然一个点都没有采到,不知道是什么问题,现在验证一下。利用draw_string函数:
- # world.debug.draw_string(lidar01_transform.location, 'O', draw_shadow=False,
- # color=carla.Color(r=0, g=255, b=0), life_time=500,
- # persistent_lines=True)
- # 发现了是z设置的问题,一开始设置的太高了,设置了20
-
- # 生成三个传感器,独立于vehicle
- camera = world.spawn_actor(camera_bp, camera_transform)
- cam01 = world.spawn_actor(cam_bp, cam01_transform)
- lidar01 = world.spawn_actor(lidar_bp, lidar01_transform)
-
- #在actor_list表中添加三个传感器
- actor_list.append(camera)
- actor_list.append(cam01)
- actor_list.append(lidar01)
-
- # 终端输出三个传感器的名字id
- print('created %s' % camera.type_id)
- print('created %s' % cam01.type_id)
- print('created %s' % lidar01.type_id)
-
- # Now we register the function that will be called each time the sensor
- # receives an image. In this example we are saving the image to disk
- # converting the pixels to gray-scale.
- # cc = carla.ColorConverter.LogarithmicDepth
- # camera.listen(lambda image: image.save_to_disk('_out/%06d.png' % image.frame, cc))
- # cam01.listen(lambda image: image.save_to_disk('/media/hhh/75c0c2a9-f565-4a05-b2a5-5599a918e2f0/hhh/carlaLearning/PythonAPI/learning_document/%06d.png' % image.frame))
- # 解决了昨天的疑问,应该是昨天存储路径的问题,就是os.path的问题,我的设想从一开始就没有问题。
-
- # camera.listen(lambda image: image.save_to_disk('_out/%06d.png' % image.frame))
- # lidar01.listen(lambda image: image.save_to_disk('/media/hhh/75c0c2a9-f565-4a05-b2a5-5599a918e2f0/hhh/carlaLearning/PythonAPI/learning_document/%06d.ply' % image.frame))
-
- # But the city now is probably quite empty, let's add a few more
- # vehicles.
- # 创造一列车,10辆
- # 但我发现每次的都是随机生成的,猜测是carla随机生成
- # 后来仔细看来代码,transform.location += carla.Location(x=40, y=-3.2),这里的transform应该
- # 是用了前面的random选择,这里的carla.Location应该是x=40,y=-3.2,z=0然后加到前面的transform上面去
- transform.location += carla.Location(x=40, y=-3.2)
- transform.rotation.yaw = -180.0
- print(transform)
- for _ in range(0, 10):
- transform.location.x += 8.0
- print(transform)
- bp = random.choice(blueprint_library.filter('vehicle'))
-
- # This time we are using try_spawn_actor. If the spot is already
- # occupied by another object, the function will return None.
- # 这个函数挺有意思的,大概封装了碰撞检测函数?
- npc = world.try_spawn_actor(bp, transform)
- if npc is not None:
- actor_list.append(npc)
- npc.set_autopilot(True)
- print('created %s' % npc.type_id)
-
- #这个sleep设置了try部分运行的时间。
- time.sleep(500)
-
- finally:
-
- print('destroying actors')
- camera.destroy()
- # 利用指令来删除actor_list里面的vehicles
- client.apply_batch([carla.command.DestroyActor(x) for x in actor_list])
- print('done.')
-
-
- if __name__ == '__main__':
-
- main()
最后显示是可以采集的,也可以正常存储。说明项目要求的路侧放置lidar基本可以实现。下周周报有东西可以写了。准备这段时间尝试在路侧放置一个lidar,然后控制一辆车从路口经过,然后分析lidar采集的数据,看能不能清晰的实现。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。