之前调试ros和carla的一起运行把carla给搞崩了,昨天才安装好.真的是麻烦,环境搭配是大问题.
好久没有看carla的example,继续学习它的automatic_control.py,现在大部分可以明白了.
这个源码里面核心的部分就是关于agent的应用,它需要在while循环里面不断的更新agent的信息,并且要然后关于路径规划的部分只有set_destination()函数,我觉得按目前的要求,也只用掌握这个函数就可以了,剩下的部分就是解决传感器和数据采集的卡顿问题了.今天根据automatic_control.py实现了自己的一个小小的想法,让vehicle不断的运动,从一个点运动到另一个点,等到了点后自动更新下一个点,这样一直循环.下面是最后呈现的源码.bug很多,最重要的是中断程序carla就直接崩溃卡死了,不知道为什么,需要再研究学习下.
"""
本片文档主要练习在automatic里面学习的一些代码,
主要实现让vehicle设置一个路径点,
然后到达该路径点后自动寻找并前往下一个路径点,
主要运用的函数是
set_destination()函数
agent.done()
步骤:
1.先实现车辆从一个点到另一个点.(实现)
2.再实现到达另一个点后,自动随机选择下一个点(实现)
3.最后在加上lidar采集数据并保存在一个文件夹里面
反思:
对于同步频率的考虑不足,
对于agent的运行学习不足
对于最后退出的考虑不足
现在退出simulator后会直接卡死,不知道是什么原因
可能是没有使用try-except结构
"""
import random
import carla
from agents.navigation.behavior_agent import BehaviorAgent
from agents.navigation.roaming_agent import RoamingAgent
from agents.navigation.basic_agent import BasicAgent
def show_point(world, point_location):
world.debug.draw_string(point_location, 'X', draw_shadow=False,
color=carla.Color(r=0, g=255, b=0), life_time=50,
persistent_lines=True)
def main():
try:
client = carla.Client('localhost', 2000)
client.set_timeout(2.0)
world = client.get_world()
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = 0.05
world.apply_settings(settings)
blueprint_library = world.get_blueprint_library()
bp = blueprint_library.find('vehicle.tesla.cybertruck')
spawn_points = world.get_map().get_spawn_points()
show_point(world, spawn_points[0].location)
show_point(world, spawn_points[1].location)
vehicle_bp = world.try_spawn_actor(bp, spawn_points[0])
world.tick()
# 对于判断是否到达终点,一开始还不知道有agent.done()这个函数,
# 最开始的设想是利用vehicle.get_location()获得的坐标与
# 设定的destnation坐标进行比对然后发现它们居然z轴的坐标不一样,害我卡了好久
# 然后就设定了x与y分别对比,发现也不能输出,然后在仿真界面才发现居然只是到达了点附近
# 并没有准确的停在该点上...
# 最后询问了师兄才知道agent里面有一个API,agent.done()专门解决这个问题.
# 最后使用这个API实现了开头的任务.感觉还是得把agent相关的源码阅读一下.
# test_location = vehicle_bp.get_location()
# print(test_location.x, test_location.y)
# print(spawn_points[0].location)
# if ((test_location.x == spawn_points[0].location.x) and
# (test_location.y == spawn_points[0].location.y)):
# print(1111)
# random.shuffle(spawn_points)
agent = BasicAgent(vehicle_bp )
agent.set_destination((spawn_points[1].location.x,
spawn_points[1].location.y,
spawn_points[1].location.z))
while True:
agent.update_information(vehicle_bp)
world.tick()
if (agent.done()):
print("到达目标点")
random.shuffle(spawn_points)
show_point(world, spawn_points[1].location)
print("设立新目标点:%s", str(spawn_points[1].location))
show_point(world, spawn_points[1].location)
agent.set_destination((spawn_points[1].location.x,
spawn_points[1].location.y,
spawn_points[1].location.z))
control = agent.run_step(debug=True)
vehicle_bp.apply_control(control)
finally:
world.destroy()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
finally:
print('\ndone.')