Carla入门 (四)- 车辆跟随视角

本文介绍了如何在CARLA模拟器中使用Python和Pygame创建自驾车(Ego_vehicle)并配置旁观者相机(SpectatorCamera),实现车辆的自动驾驶和实时图像显示。作者展示了如何在游戏循环中处理控制、键入事件以及相机的切换和销毁。
摘要由CSDN通过智能技术生成

一、Ego_vehicle

通过blueprint获取所需的vehicle,在地图上获取随即生成点生成,并将其设置为自动驾驶模式。

import carla
import random
import pygame
import numpy as np

client = carla.Client('localhost', 2000)
world = client.get_world()

blueprint_library = world.get_blueprint_library()
vehicle_bp = = blueprint_library.filter('*vehicle*')
spawn_points = world.get_map().get_spawn_points()
ego_vehicle = world.spawn_actor(random.choice(vehicle_bp), random.choice(spawn_points))
ego_vehicle.set_autopilot(True)

二、Spectator Camera

  1. 获取camera蓝图
  2. 生成camera并绑定ego_vehicle
  3. 设置camera监听模式
camera_trans =carla.Transform(carla.Location(x=-5,z=3),carla.Rotation(pitch=-20))
camera_bp = world.get_blueprint_library().find('sensor.camera.rgb')
camera = world.spawn_actor(camera_bp, camera_trans, attach_to=ego_vehicle)

camera.listen(lambda image : pygame_callback(image,renderObject))

三、设置Pygame

class RenderObject(object):
    def __init__(self,width, height):
        init_img = np.random.randint(0,255,(height,width,3), dtype='uint8')
        self.surface = pygame.surfarray.make_surface(init_img.swapaxes(0,1))

def pygame_callback(data, obj):
    img = np.reshape(np.copy(data.raw_data),(data.height, data.width, 4))
    img = img[:, :, :3]
    img = img[:, :, ::-1]
    obj.surface = pygame.surfarray.make_surface(img.swapaxes(0,1))

image_w = camera_bp.get_attribute("image_size_x").as_int()
image_h = camera_bp.get_attribute("image_size_y").as_int()

renderObject = RenderObject(image_w, image_h)

pygame.init()
gameDisplay = pygame.display.set_mode((image_w,image_h),pygame.HWSURFACE | pygame.DOUBLEBUF)
gameDisplay.fill((0, 0, 0))
gameDisplay.blit(renderObject.surface, (0,0))
pygame.display.flip()

# Game loop
crashed = False

while not crashed:
    # Advance the simulation time
    world.tick()
    # Update the display
    gameDisplay.blit(renderObject.surface, (0,0))
    pygame.display.flip()
    # Process the current control state
    # controlObject.process_control()
    # Collect key press events
    for event in pygame.event.get():
        # If the window is closed, break the while loop
        if event.type == pygame.QUIT:
            crashed = True

        # Parse effect of key press event on control state
        # controlObject.parse_control(event)
        if event.type == pygame.KEYUP:
            # TAB key switches vehicle
            if event.key == pygame.K_TAB:
                ego_vehicle.set_autopilot(True)
                ego_vehicle = random.choice(vehicles)
                # Ensure vehicle is still alive (might have been destroyed)
                if ego_vehicle.is_alive:
                    # Stop and remove the camera
                    camera.stop()
                    camera.destroy()

                    # Spawn new camera and attach to new vehicle
                    # controlObject = ControlObject(ego_vehicle)
                    camera = world.spawn_actor(camera_bp, camera_init_trans, attach_to=ego_vehicle)
                    camera.listen(lambda image: pygame_callback(image, renderObject))

                    # Update PyGame window
                    gameDisplay.fill((0,0,0))               
                    gameDisplay.blit(renderObject.surface, (0,0))
                    pygame.display.flip()

# Stop camera and quit PyGame after exiting game loop
camera.stop()
pygame.quit()
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值