import carla
import random
import time
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
# 获取地图中的起点A和终点B
start_location = carla.Location(x=100, y=200, z=1.0)
end_location = carla.Location(x=130, y=200, z=1.0)
# 在起点A生成一个新车辆
blueprint_library = world.get_blueprint_library()
vehicle_bp = random.choice(blueprint_library.filter('vehicle.*.*'))
spawn_transform = carla.Transform(carla.Location(start_location), carla.Rotation(yaw=0.0))
vehicle = world.try_spawn_actor(vehicle_bp, spawn_transform)
# 设置车辆控制命令
vehicle_control = carla.VehicleControl(throttle=1.0, steer=0.0)
vehicle_control.target_location = end_location
# 将车辆移动到终点B
while True:
vehicle.apply_control(vehicle_control)
location = vehicle.get_location()
distance = location.distance(end_location)
if distance < 2.0:
break
time.sleep(0.1)
# 销毁车辆并关闭CARLA客户端
vehicle.destroy()
client.apply_batch([carla.command.DestroyActor(vehicle)])
client.disconnect()
CARLA 从A点移动到B点
最新推荐文章于 2024-11-12 23:15:59 发布