今天主要学习了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 systry: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:passimport carlaimport random
import timedef 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 += 40vehicle.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 systry: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:passimport carla
import random
import timedef 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# 生成三个传感器,独立于vehiclecamera = 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)# 终端输出三个传感器的名字idprint('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.0print(transform)for _ in range(0, 10):transform.location.x += 8.0print(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里面的vehiclesclient.apply_batch([carla.command.DestroyActor(x) for x in actor_list])print('done.')if __name__ == '__main__':main()

最后显示是可以采集的,也可以正常存储。说明项目要求的路侧放置lidar基本可以实现。下周周报有东西可以写了。准备这段时间尝试在路侧放置一个lidar,然后控制一辆车从路口经过,然后分析lidar采集的数据,看能不能清晰的实现。

carla学习笔记(五)相关推荐

  1. python函数是一段具有特定功能的语句组_Python学习笔记(五)函数和代码复用

    本文将为您描述Python学习笔记(五)函数和代码复用,具体完成步骤: 函数能提高应用的模块性,和代码的重复利用率.在很多高级语言中,都可以使用函数实现多种功能.在之前的学习中,相信你已经知道Pyth ...

  2. Ethernet/IP 学习笔记五

    Ethernet/IP 学习笔记五 Accessing data within a device using a non-time critical message (an explicit mess ...

  3. StackExchange.Redis学习笔记(五) 发布和订阅

    StackExchange.Redis学习笔记(五) 发布和订阅 原文:StackExchange.Redis学习笔记(五) 发布和订阅 Redis命令中的Pub/Sub Redis在 2.0之后的版 ...

  4. 吴恩达《机器学习》学习笔记五——逻辑回归

    吴恩达<机器学习>学习笔记五--逻辑回归 一. 分类(classification) 1.定义 2.阈值 二. 逻辑(logistic)回归假设函数 1.假设的表达式 2.假设表达式的意义 ...

  5. 好程序员教程分析Vue学习笔记五

    好程序员教程分析Vue学习笔记五,上次我们学习了Vue的组件,这次我们来学习一下路由的使用.在Vue中,所谓的路由其实跟其他的框架中的路由的概念差不多,即指跳转的路径. 注意:在Vue中,要使用路由, ...

  6. 【AngularJs学习笔记五】AngularJS从构建项目开始

    为什么80%的码农都做不了架构师?>>>    #0 系列目录# AngularJs学习笔记 [AngularJs学习笔记一]Bower解决js的依赖管理 [AngularJs学习笔 ...

  7. ROS学习笔记五:理解ROS topics

    ROS学习笔记五:理解ROS topics 本节主要介绍ROS topics并且使用rostopic和rqt_plot命令行工具. 例子展示 roscore 首先运行roscore系列服务,这是使用R ...

  8. Spring Boot 框架学习笔记(五)( SpringSecurity安全框架 )

    Spring Boot 框架学习笔记(五) SpringSecurity安全框架 概述 作用 开发示例: 1. 新建项目 2. 引入依赖 3. 编写`SecurityConfig`类,实现认证,授权, ...

  9. Java学习笔记(五):一张图总结完JVM8基础概念

    Java学习笔记(五):一张图总结完JVM8基础概念 引文 最近在学习JVM的相关内容,好不容易把基础概念全部都学了一遍,却发现知识网络是零零散散的.迫不得已,只好再来一次总的归纳总结.为了更好的理解 ...

最新文章

  1. 硬货 | 一文解读完五篇重磅ACL2017 NLP论文
  2. 域控制器活动目录之备份与恢复
  3. unity3d小小白之导入素材、添加天空盒
  4. 《高性能JavaScript》第七章 Ajax
  5. C语言中兴面试编程题,中兴一套笔试题及部分答案
  6. Tencent JDK 国产化CPU架构支持分享
  7. What day is that day? 模拟
  8. 漫画:为什么生僻字计算机上打不出来,或者打出来也无法显示呢?
  9. ubuntu安装最新版apktool(最新版)反编译工具
  10. SQL数据库学习之路(八)
  11. java batik 字体文件_用 Apache batik 1.10 把svg代码转成png图片,文字丢失???
  12. 记录下准备蓝桥杯的过程吧
  13. selenium录制百度3D地图
  14. 第7章第10节:分割式布局:纵向居中分割版面 [PowerPoint精美幻灯片实战教程]
  15. YOLOv5源码逐行超详细注释与解读(3)——训练部分train.py
  16. matplotlib中导入中文字体
  17. 表单reset重置按钮的作用并非是清空表单
  18. 小半斤拔凉 支付Java 相关参考
  19. [转]在今天这个金钱的社会,如果肆意的由这样的人横行霸道; 何来的“百善孝为先”??!...
  20. python实现北京租房信息计算

热门文章

  1. 任正非 采访 安卓 鸿蒙,法媒专访任正非透露“鸿蒙”系统“很可能”快过安卓...
  2. FCoin“暴雷”投资人该怎么办?律师这样建议
  3. J: Participate in E-sports [大数牛顿迭代判断是否是平方数]
  4. Java 统计接口消耗时间
  5. 验证CUDA和CUDNN是否安装成功的方法
  6. 【windows版本】 db2数据库安装与使用
  7. 买外链有没有影响?会导致网站降权吗?玉米社
  8. NHWC BGR -> NCHW RGB
  9. netsh端口映射与端口转发
  10. Java JNI初体验