def add_vehicles(plexe: Plexe, names: list, gaps: list, has_slipstream_device: list): # The example uses only a vehicle type, called "car" # (see cfg/freeway.rou.xml), which has a length of 4.971 m. length = 4.971 # To avoid interferences, vehicles must be added from the first to the last. for i in range(len(names)): add_platooning_vehicle(plexe, names[i], sum(gaps[i:]) + (len(names) - i) * length, 0, CRUISING_SPEED, GAP, False, vtype='car') traci.vehicle.setSpeedMode(names[i], 0) plexe.set_fixed_lane(names[i], 0, safe=False) plexe.use_controller_acceleration(names[i], False) if i == 0: plexe.set_active_controller(names[i], ACC) else: plexe.set_active_controller(names[i], CACC) plexe.enable_auto_feed(names[i], True, names[0], names[i - 1]) if has_slipstream_device[i]: traci.vehicle.setParameter(names[i], 'has.slipstream.device', 'true')
def add_vehicles(plexe, n, real_engine=False): """ Adds a platoon of n vehicles to the simulation, plus an additional one farther away that wants to join the platoon :param plexe: API instance :param n: number of vehicles of the platoon :param real_engine: set to true to use the realistic engine model, false to use a first order lag model :return: returns the topology of the platoon, i.e., a dictionary which indicates, for each vehicle, who is its leader and who is its front vehicle. The topology can the be used by the data exchange logic to automatically fetch data from leading and front vehicle to feed the CACC """ # add a platoon of n vehicles topology = {} for i in range(n): vid = "v.%d" % i add_platooning_vehicle(plexe, vid, (n - i + 1) * (DISTANCE + LENGTH) + 50, 0, SPEED, DISTANCE, real_engine) plexe.set_fixed_lane(vid, 0, safe=False) traci.vehicle.setSpeedMode(vid, 0) if i == 0: plexe.set_active_controller(vid, ACC) else: plexe.set_active_controller(vid, CACC) if i > 0: topology[vid] = {"front": "v.%d" % (i - 1), "leader": LEADER} # add a vehicle that wants to join the platoon vid = "v.%d" % n add_platooning_vehicle(plexe, vid, 10, 1, SPEED, DISTANCE, real_engine) plexe.set_fixed_lane(vid, 1, safe=False) traci.vehicle.setSpeedMode(vid, 0) plexe.set_active_controller(vid, ACC) plexe.set_path_cacc_parameters(vid, distance=JOIN_DISTANCE) return topology
def add_vehicles(plexe, n, n_platoons, real_engine=False): """ Adds a set of platoons of n vehicles each to the simulation :param plexe: API instance :param n: number of vehicles of the platoon :param n_platoons: number of platoons :param real_engine: set to true to use the realistic engine model, false to use a first order lag model :return: returns the topology of the platoon, i.e., a dictionary which indicates, for each vehicle, who is its leader and who is its front vehicle. The topology can the be used by the data exchange logic to automatically fetch data from leading and front vehicle to feed the CACC """ # add a platoon of n vehicles # topology 中以 dictionary 的形式存储车辆之间的通信拓扑结构,包括每辆车的前车和头车编号 topology = {} p_length = n * LENGTH + (n - 1) * DISTANCE # p_length is the whole length of the platooning formation for p in range(n_platoons): # n_platoons = number of platoons = 0 (only 1 platoon in this case) for i in range(n): vid = "v.%d.%d" % (p, i) # p is always zero, i will change according to the number of vehicles in the platoon. # vid = vehicle id # 调用 utils module 中的函数,将车辆编排成 platoon 的形式 add_platooning_vehicle(plexe, vid, (n-p+1) * (p_length + PLATOON_DISTANCE) + (n-i+1) * (DISTANCE+LENGTH), 0, SPEED, DISTANCE, real_engine) # 将车辆保持在 lane 0 ,并忽略安全距离限制 plexe.set_fixed_lane(vid, 0, False) # 设置车辆速度模式。车辆的速度有 5 个影响因素 # https://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#speed_mode_.280xb3.29 # 1\. safe speed # 2\. max accel # 3\. max speed # 4\. right of way at intersections # 5\. brake hard to avoid passing red light # 如果全都不考虑,则设置为 [0, 0, 0, 0, 0] = 0, 此时,车速完全由 traci 控制 # 如果全都考虑,则设置为 [1, 1, 1, 1, 1] = 31 (dec) # 如果只考虑 safe speed,则设置为 [0, 0, 0, 0, 1] = 1 traci.vehicle.setSpeedMode(vid, 0) # 在 platoon 中头车采用 adaptive cruise control 的控制方式 # 后边的跟车采用 cooperative adative cruise control 的控制方式 plexe.use_controller_acceleration(vid, False) if i == 0: plexe.set_active_controller(vid, ACC) else: plexe.set_active_controller(vid, CACC) if i > 0: topology[vid] = {"front": "v.%d.%d" % (p, i - 1), "leader": "v.%d.0" % p} # this is to update the latest key and values of the topology dic else: topology[vid] = {} # this is to clear the dic to a completely blank dic if i = 0 return topology
def add_vehicles(plexe, n, n_platoons, vtype, route, position, speed, distance, real_engine=False): """ Adds a set of platoons of n vehicles each to the simulation :param plexe: API instance :param n: number of vehicles of the platoon :param n_platoons: number of platoons :param vtype: the type of car in rou.xml :param route: the route in rou.xml :param position: the position of begining to start :param real_engine: set to true to use the realistic engine model, false to use a first order lag model :return: returns the topology of the platoon, i.e., a dictionary which indicates, for each vehicle, who is its leader and who is its front vehicle. The topology can the be used by the data exchange logic to automatically fetch data from leading and front vehicle to feed the CACC """ # add a platoon of n vehicles topology = {} p_length = n * LENGTH + (n - 1) * distance for p in range(n_platoons): for i in range(n): vid = "v.%d.%d" % (p, i) add_platooning_vehicle( plexe, vid, position + (n_platoons - p - 1) * (p_length + PLATOON_DISTANCE) + (n - i - 1) * (distance + LENGTH) + 1, 0, speed, distance, vtype, route, real_engine) plexe.set_fixed_lane(vid, 0, False) traci.vehicle.setSpeedMode(vid, 0) plexe.use_controller_acceleration(vid, False) if i == 0: plexe.set_active_controller(vid, ACC) else: plexe.set_active_controller(vid, CACC) if i > 0: topology[vid] = { "front": "v.%d.%d" % (p, i - 1), "leader": "v.%d.0" % p } else: topology[vid] = {} # RGBA traci.vehicle.setColor(vid, (255, 0, 255, 255)) plexe.set_path_cacc_parameters(vid, distance=DISTANCE) return topology
def main(demo_mode, real_engine, setter=None): start_sumo("cfg/freeway.sumo.cfg", False) plexe = Plexe() traci.addStepListener(plexe) step = 0 state_left = None state_right = None random.seed(1) while running(demo_mode, step, 6000): if demo_mode and step == 6000: start_sumo("cfg/freeway.sumo.cfg", True) step = 0 random.seed(1) traci.simulationStep() if step == 1: add_platooning_vehicle(plexe, "p0", 150, 0, 25, 5, real_engine) add_vehicle(plexe, "v0", 140, 1, 25, "passenger") add_vehicle(plexe, "v1", 250, 0, 20, "passenger2") traci.gui.trackVehicle("View #0", "p0") traci.gui.setZoom("View #0", 50000) plexe.set_active_controller("p0", ACC) plexe.set_cc_desired_speed("p0", 25) traci.vehicle.setSpeedMode("p0", 0) if step > 1: state = traci.vehicle.getLaneChangeState("p0", 1)[0] if state_left != state: state_left = state str_status = get_status(state) print("Step %d, vehicle p0. Lane change status (LEFT ): %s" % (step, str_status)) state = traci.vehicle.getLaneChangeState("p0", -1)[0] if state_right != state: state_right = state str_status = get_status(state) print("Step %d, vehicle p0. Lane change status (RIGHT): %s" % (step, str_status)) if real_engine and setter is not None: # if we are running with the dashboard, update its values tracked_id = traci.gui.getTrackedVehicle("View #0") if tracked_id != "": ed = plexe.get_engine_data(tracked_id) vd = plexe.get_vehicle_data(tracked_id) setter(ed[RPM], ed[GEAR], vd.speed, vd.acceleration) step += 1 traci.close()
def add_vehicles(plexe): """ Adds the vehicles to the simulation """ for i in range(len(VEHICLES)): vid = VEHICLES[i] add_platooning_vehicle(plexe, vid, 10, i, 0, 5) plexe.set_fixed_lane(vid, i, False) plexe.set_active_controller(vid, ACC) plexe.set_engine_model(vid, ENGINE_MODEL_REALISTIC) plexe.set_vehicles_file(vid, "vehicles.xml") plexe.set_vehicle_model(vid, vid) plexe.set_fixed_acceleration(vid, True, 0) traci.vehicle.setSpeedMode(vid, 0)
def add_vehicles(plexe, n, n_platoons, real_engine=False): """ Adds a set of platoons of n vehicles each to the simulation :param plexe: API instance :param n: number of vehicles of the platoon :param n_platoons: number of platoons :param real_engine: set to true to use the realistic engine model, false to use a first order lag model :return: returns the topology of the platoon, i.e., a dictionary which indicates, for each vehicle, who is its leader and who is its front vehicle. The topology can the be used by the data exchange logic to automatically fetch data from leading and front vehicle to feed the CACC """ # add a platoon of n vehicles topology = {} p_length = n * LENGTH + (n - 1) * DISTANCE for p in range(n_platoons): for i in range(n): vid = "v.%d.%d" % (p, i) add_platooning_vehicle( plexe, vid, (n - p + 1) * (p_length + PLATOON_DISTANCE) + (n - i + 1) * (DISTANCE + LENGTH), 0, SPEED, DISTANCE, real_engine) plexe.set_fixed_lane(vid, 0, False) traci.vehicle.setSpeedMode(vid, 0) plexe.use_controller_acceleration(vid, False) if i == 0: plexe.set_active_controller(vid, ACC) else: plexe.set_active_controller(vid, CACC) if i > 0: topology[vid] = { "front": "v.%d.%d" % (p, i - 1), "leader": "v.%d.0" % p } else: topology[vid] = {} return topology
def add_vehicles(plexe, n, n_platoons, real_engine=False): """ Adds a set of platoons of n vehicles each to the simulation :param plexe: API instance :param n: number of vehicles of the platoon :param n_platoons: number of platoons :param real_engine: set to true to use the realistic engine model, false to use a first order lag model :return: returns the topology of the platoon, i.e., a dictionary which indicates, for each vehicle, who is its leader and who is its front vehicle. The topology can the be used by the data exchange logic to automatically fetch data from leading and front vehicle to feed the CACC """ # add a platoon of n vehicles topology = {} p_length = n * LENGTH + (n - 1) * DISTANCE # p_length = (8*4)+(8-1)*5 = 67 for p in range(n_platoons): # p = 0 for i in range(n): # n = 7 vid = "v.%d.%d" % (p, i) # Differences with the brakedemo.py is the value = 200 in the 3rd param (position) in add_platooning vehicle function # (n-p+1) * (p_length + PLATOON_DISTANCE) = 8 * (67 + 51.5) not equal 200 # Value 200 is probably something related to the network file of the car position on the road add_platooning_vehicle(plexe, vid, 200 + (n-i+1) * (DISTANCE+LENGTH), 0, SPEED, DISTANCE, real_engine) plexe.set_fixed_lane(vid, 0, False) traci.vehicle.setSpeedMode(vid, 0) plexe.use_controller_acceleration(vid, False) # if we want to try without cacc, we can just set for the else part to acc. if i == 0: # leader is ACC plexe.set_active_controller(vid, ACC) else: plexe.set_active_controller(vid, CACC) if i > 0: # follower is CACC topology[vid] = {"front": "v.%d.%d" % (p, i - 1), "leader": "v.%d.0" % p} else: topology[vid] = {} return topology
def add_vehicles(plexe, n, real_engine=False): """ Adds a platoon of n vehicles to the simulation, plus an additional one farther away that wants to join the platoon :param plexe: API instance :param n: number of vehicles of the platoon :param real_engine: set to true to use the realistic engine model, false to use a first order lag model """ # add a platoon of n vehicles for i in range(n): vid = "v.%d" % i add_platooning_vehicle(plexe, vid, (n - i + 1) * (DISTANCE + LENGTH) + 50, 0, SPEED, DISTANCE, real_engine) plexe.set_fixed_lane(vid, 0, safe=False) traci.vehicle.setSpeedMode(vid, 0) plexe.use_controller_acceleration(vid, False) if i == 0: plexe.set_active_controller(vid, ACC) else: plexe.set_active_controller(vid, CACC) if i > 0: plexe.enable_auto_feed(vid, True, LEADER, "v.%d" % (i - 1))