carla中车辆的加减速如果只更改油门参数throttle的数值vehicle.apply_control(carla.VehicleControl(throttle=0.4+incre, steer=0.0))
是不会实现车辆的加速和减速的。
本文使用了一个很简单的循环来实现车辆的加减速,具体思路就是每隔一段距离更改车辆的throttle数值,一直加速到车辆达到thorttle的最大值1.0。代码如下:
...
incre_distance = 6
incre = 0
vehicle2.apply_control(carla.VehicleControl(throttle=0.3, steer=0.0))
while True:
x_v2 = vehicle2.get_location().x
#y_v2 = vehicle2.get_location().y
if (abs(x_v2 - initial) / incre_distance) < 1:
vehicle2.apply_control(carla.VehicleControl(throttle=0.4+incre, steer=0.0))
if 0.4 + incre <= 1:
incre += 0.05
else:
incre += 0
incre_distance += 6
上文的程序实现了让车每行驶6米提高throttle数值0.05。减速过程的实现可以是让车每行驶6米减少throttle的数值0.05,思路是相似的。
实现了车辆的加减速就方便我们做更多的场景了。
具体的37类危险场景模式见博客:危险场景分类(NHTSA 37 Pre-Crash Scenarios)
本文实现了其中的五个模式(对应思维导图中的rear-end部分),如下图
给出代码片段中的循环语句部分:
...
while True:
#waypoint_v2 = mp.get_waypoint(vehicle2.get_location())
#yaw_v2 = waypoint_v2.transform.rotation.yaw
x_v1 = vehicle1.get_location().x
x_v2 = vehicle2.get_location().x
#print(x_v1, x_v2)
y_v1 = vehicle1.get_location().y
y_v2 = vehicle2.get_location().y
#print(abs(x_v1 - x_v2))
#print(abs(y_v1 - y_v2))
#The lag vehicle turn right:
if abs(x_v1 - x_v2) < 17:
if abs(x_v1 - x_v2) >= 15:
vehicle1.apply_control(carla.VehicleControl(throttle=0.1, steer=0.0))
else:
vehicle1.apply_control(carla.VehicleControl(throttle=throttle_RotR, steer=steerR))
#The lag vehicle stops by force while turning:
if abs(y_v1 - y_v2) < 5.3:
vehicle1.apply_control(carla.VehicleControl(throttle=0.0, steer=0.0, brake=1.0, hand_brake=True))
if abs(x_v1 - x_v2) > 150:
break
...
while True:
#Get the location of 2 vehicles:
x_v1 = vehicle1.get_location().x
x_v2 = vehicle2.get_location().x
y_v1 = vehicle1.get_location().y
y_v2 = vehicle2.get_location().y
if abs(x_v2 - x_v1) > 12:#Drive without warning
vehicle1.apply_control(carla.VehicleControl(throttle=0.7, steer=0.0))
elif abs(x_v2 - x_v1) <= 12:#Drive within dangerous zone
while True:
y_v1 = vehicle1.get_location().y
y_v2 = vehicle2.get_location().y
vehicle1.apply_control(carla.VehicleControl(throttle=0.2, steer=-0.5))#turn left
if abs(y_v1 - y_v2) > 2:#if the rotation angle is too big, change the direction of steer
vehicle1.apply_control(carla.VehicleControl(throttle=0.5, steer=0.4))
break
while True:
x_v1 = vehicle1.get_location().x
x_v2 = vehicle2.get_location().x
print(x_v1 - x_v2)
#print(x_v2)
if abs(x_v1 - x_v2) < 7.1:
#The number in this if statement depends on the safe distance set before.
#print(abs(x_v1 - x_v2))
vehicle1.apply_control(carla.VehicleControl(throttle=0.7, steer=0.0))
if abs(x_v1 - x_v2) > 25:
break
break
throttle_start = 0.1
dist_incre = 15
throttle_incre = 0
vehicle2.apply_control(carla.VehicleControl(throttle=throttle_start, steer=0.0))
while True:
#waypoint_v2 = mp.get_waypoint(vehicle2.get_location())
#yaw_v2 = waypoint_v2.transform.rotation.yaw
x_v1 = vehicle1.get_location().x
x_v2 = vehicle2.get_location().x
y_v1 = vehicle1.get_location().y
y_v2 = vehicle2.get_location().y
#if abs(x_v2 - x_v1) > 12:#Drive without warning
#Control the vehicle:
vehicle1.apply_control(carla.VehicleControl(throttle=0.8, steer=0.0))
#vehicle2.apply_control(carla.VehicleControl(throttle=0.4, steer=0.0, brake=1.0))
#vehicle1.apply_control(carla.VehicleControl(throttle=0.6, steer=0.0))
#The lead vehicle accelerates:
if (abs(x_v2 - initial_x2) / dist_incre) < 1:
vehicle2.apply_control(carla.VehicleControl(throttle=throttle_start+throttle_incre, steer=0.0))
if 0.4 + throttle_incre <= 1:
throttle_incre += 0.05
print(throttle_incre)
else:
throttle_incre += 0
dist_incre += 15
if abs(x_v2 - x_v1) < 10:
#print(abs(x_v2 - x_v1))
#if abs(x_v2 - x_v1) < :
vehicle1.apply_control(carla.VehicleControl(throttle=0, steer=0.0, brake=1.0))
break
...
throttle_start = 0.5
....
....
throttle_incre -= 0.05