Carla——5个危险场景模式以及车辆的加减速

文章目录

  • 1. 车辆的加(减)速
  • 2. 37类场景中的5个具体场景模式

本文承接上一篇: 一个简单场景的搭建

1. 车辆的加(减)速

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,思路是相似的。

实现了车辆的加减速就方便我们做更多的场景了。

2. 37类场景中的5个具体场景模式

具体的37类危险场景模式见博客:危险场景分类(NHTSA 37 Pre-Crash Scenarios)

本文实现了其中的五个模式(对应思维导图中的rear-end部分),如下图
Carla——5个危险场景模式以及车辆的加减速_第1张图片
给出代码片段中的循环语句部分:

  1. Following vehicle making a maneuver
...
	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

  1. Lead Vehicle Stopped:
    参考博客:Carla-搭建一个简单危险场景中的代码
  2. Lead Vehicle at Lower Constant Speed
...
	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
  1. Lead Vehicle Accelerating
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
  1. Lead Vehicle Decelerating
    这里只需要把throttle的数值改成从大到小递减即可。
...
throttle_start = 0.5
....
....
throttle_incre -= 0.05

你可能感兴趣的:(智能汽车)