def main(demo_mode, real_engine, setter=None): # used to randomly color the vehicles random.seed(1) start_sumo("cfg/freeway.sumo.cfg", False) plexe = Plexe() traci.addStepListener(plexe) step = 0 state = GOING_TO_POSITION while running(demo_mode, step, 6000): # when reaching 60 seconds, reset the simulation when in demo_mode if demo_mode and step == 6000: start_sumo("cfg/freeway.sumo.cfg", True) step = 0 state = GOING_TO_POSITION random.seed(1) traci.simulationStep() if step == 0: # create vehicles and track the joiner topology = add_vehicles(plexe, N_VEHICLES, real_engine) traci.gui.trackVehicle("View #0", JOINER) traci.gui.setZoom("View #0", 20000) if step % 10 == 1: # simulate vehicle communication every 100 ms communicate(plexe, topology) if step == 100: # at 1 second, let the joiner get closer to the platoon topology = get_in_position(plexe, JOINER, FRONT_JOIN, topology) if state == GOING_TO_POSITION and step > 0: # when the distance of the joiner is small enough, let the others # open a gap to let the joiner enter the platoon if get_distance(plexe, JOINER, FRONT_JOIN) < JOIN_DISTANCE + 1: state = OPENING_GAP topology = open_gap(plexe, BEHIND_JOIN, JOINER, topology, N_VEHICLES) if state == OPENING_GAP: # when the gap is large enough, complete the maneuver if get_distance(plexe, BEHIND_JOIN, FRONT_JOIN) > \ 2 * JOIN_DISTANCE + 2: state = COMPLETED plexe.set_fixed_lane(JOINER, 0, safe=False) plexe.set_active_controller(JOINER, CACC) plexe.set_path_cacc_parameters(JOINER, distance=DISTANCE) plexe.set_active_controller(BEHIND_JOIN, CACC) plexe.set_path_cacc_parameters(BEHIND_JOIN, distance=DISTANCE) topology = reset_leader(BEHIND_JOIN, topology, N_VEHICLES) 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 main(demo_mode, real_engine, setter=None): # used to randomly color the vehicles random.seed(1) start_sumo("cfg/freeway.sumo.cfg", False) step = 0 state = GOING_TO_POSITION while running(demo_mode, step, 6000): # when reaching 60 seconds, reset the simulation when in demo_mode if demo_mode and step == 6000: start_sumo("cfg/freeway.sumo.cfg", True) step = 0 state = GOING_TO_POSITION random.seed(1) traci.simulationStep() if step == 0: # create vehicles and track the joiner topology = add_vehicles(N_VEHICLES, real_engine) traci.gui.trackVehicle("View #0", JOINER) traci.gui.setZoom("View #0", 20000) if step % 10 == 1: # simulate vehicle communication every 100 ms communicate(topology) if step == 100: # at 1 second, let the joiner get closer to the platoon topology = get_in_position(JOINER, FRONT_JOIN, topology) if state == GOING_TO_POSITION: # when the distance of the joiner is small enough, let the others # open a gap to let the joiner enter the platoon if get_distance(JOINER, FRONT_JOIN) < JOIN_DISTANCE + 1: state = OPENING_GAP topology = open_gap(BEHIND_JOIN, JOINER, topology, N_VEHICLES) if state == OPENING_GAP: # when the gap is large enough, complete the maneuver if get_distance(BEHIND_JOIN, FRONT_JOIN) > 2 * JOIN_DISTANCE + 2: state = COMPLETED change_lane(JOINER, 0) set_par(JOINER, cc.PAR_ACTIVE_CONTROLLER, cc.CACC) set_par(JOINER, cc.PAR_CACC_SPACING, DISTANCE) set_par(BEHIND_JOIN, cc.PAR_ACTIVE_CONTROLLER, cc.CACC) set_par(BEHIND_JOIN, cc.PAR_CACC_SPACING, DISTANCE) topology = reset_leader(BEHIND_JOIN, topology, N_VEHICLES) 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 != "": (g, rpm) = cc.unpack(get_par(tracked_id, cc.PAR_ENGINE_DATA)) data = get_par(tracked_id, cc.PAR_SPEED_AND_ACCELERATION) (v, a, u, x, y, t) = cc.unpack(data) setter(rpm, g, v, a) step += 1 traci.close()
def main(demo_mode, real_engine, setter=None): # used to randomly color the vehicles random.seed(1) start_sumo("cfg/freeway.sumo.cfg", False) plexe = Plexe() traci.addStepListener(plexe) step = 0 topology = dict() min_dist = 1e6 while running(demo_mode, step, 1500): # when reaching 60 seconds, reset the simulation when in demo_mode if demo_mode and step == 1500: print("Min dist: %f" % min_dist) start_sumo("cfg/freeway.sumo.cfg", True) step = 0 random.seed(1) traci.simulationStep() if step == 0: # create vehicles and track the braking vehicle topology = add_vehicles(plexe, 8, 1, real_engine) traci.gui.trackVehicle("View #0", BRAKING_VEHICLE) traci.gui.setZoom("View #0", 20000) if step % 10 == 1: # simulate vehicle communication every 100 ms communicate(plexe, topology) 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) if step == 500: plexe.set_fixed_acceleration(BRAKING_VEHICLE, True, -6) if step > 1: radar = plexe.get_radar_data("v.0.1") if radar[RADAR_DISTANCE] < min_dist: min_dist = radar[RADAR_DISTANCE] step += 1 traci.close()
def main(demo_mode, real_engine, setter=None): # used to randomly color the vehicles random.seed(1) start_sumo("cfg/jiangxinzhou.sumo.cfg", False) plexe = Plexe() traci.addStepListener(plexe) step = 0 redlight_stop = 0 topology_0 = dict() platoons_num = 1 car_num = [26] leader = [0] state_2_flag = [0] redlight_stop = [0] judge_flag = [0] special_stop_flag = [0] color = [(255, 255, 255, 255), (255, 255, 0, 255), (0, 255, 255, 255), (255, 0, 255, 255), (255, 255, 255, 255), (255, 255, 0, 255), (0, 255, 255, 255), (255, 0, 255, 255), (255, 255, 255, 255), (255, 255, 0, 255), (0, 255, 255, 255), (255, 0, 255, 255)] special_judge_flag = [0] while running(demo_mode, step, 500000): # when reaching 60 seconds, reset the simulation when in demo_mode if demo_mode and step == 500000: start_sumo("cfg/jiangxinzhou.sumo.cfg", True) step = 0 random.seed(1) traci.simulationStep() if step == 0: # create vehicles and track the braking vehicle distance_0 = 5 speed_0 = 25 car_num_0 = car_num[0] platoons_num_0 = 1 position_0 = 1000 topology_0 = add_vehicles(plexe, car_num_0, platoons_num_0, "vtypeauto", "platoon_route_0", position_0, speed_0, distance_0, real_engine) traci.gui.trackVehicle("View #0", "v.0.0") # traci.vehicle.highlight("v.0.0", color=(255,255,0,255), size=-1, alphaMax=-1, duration=-1, type=0) # traci.gui.trackVehicle("View #1", "v.0.1") traci.gui.setZoom("View #0", 700) # traci.gui.setOffset("View #0",5078.44,6332.91) # acf = traci.gui.getZoom(viewID='View #0') # print (acf) if step % 10 == 1: # simulate vehicle communication every 100 ms communicate(plexe, topology_0) # print (topology_0) if step > 10: # print(platoons_num) for i in range(platoons_num): # print(i) num_temp = int(leader[i]) leader_id = "v.0.%d" % num_temp # for size_cycle in range (0,100): traci.vehicle.highlight(leader_id, color[i], size=50, alphaMax=-1, duration=-1, type=0) # print (leader_id) nextTLS, current_phase, left_distance, absolute_time = getnext_trafficlightandphace( leader_id) if ((left_distance >= RANGE - 1) and (left_distance < RANGE)): judge_flag[i] = 1 else: judge_flag[i] = 0 if left_distance <= 0.5: state_2_flag[i] = 0 special_stop_flag[i] = 0 # print (left_distance,state_2_flag[i],special_stop_flag[i]) leader_data = plexe.get_vehicle_data(leader_id) if (nextTLS != "none" and judge_flag[i] == 1): time_left = absolute_time - traci.simulation.getTime() pass_num = int( math.floor((leader_data.speed * time_left - RANGE) / (LENGTH + DISTANCE))) state = judge_state(plexe, car_num[i], pass_num) # if i==0: # print (pass_num,state) if ((state == 0 and current_phase == 'g') or (current_phase == 'r')) and special_stop_flag[i] == 0: # plexe.set_cc_desired_speed(leader_id, 0) # plexe.set_active_controller(leader_id, 3) decel = leader_data.speed**2 / (2 * (left_distance - 10)) plexe.set_fixed_acceleration("v.0.%d" % int(leader[i]), True, -decel) redlight_stop[i] = 1 # ALL_PASS = 1 # elif state == 1 and current_phase == 'g': # continue # PART_PASS = 2 elif state == 2 and current_phase == 'g' and state_2_flag[ i] == 0: new_platoon_leader = "v.0.%d" % (int(leader[i]) + pass_num) for j in range((int(leader[i]) + pass_num) + 1, (int(leader[i]) + car_num[i])): # temporarily change the leader topology_0["v.0.%d" % j]["leader"] = new_platoon_leader traci.vehicle.setColor("v.0.%d" % j, color[platoons_num]) # the front vehicle if the vehicle opening the gap is the joiner plexe.set_active_controller(new_platoon_leader, ACC) traci.vehicle.setColor(new_platoon_leader, color[platoons_num]) plexe.set_path_cacc_parameters(new_platoon_leader, distance=100) topology_0[new_platoon_leader] = {} leader.append(int(leader[i]) + pass_num) car_num.append(car_num[i] - pass_num) platoons_num = platoons_num + 1 car_num[i] = pass_num redlight_stop.append(1) state_2_flag[i] = 1 state_2_flag.append(0) judge_flag.append(0) special_judge_flag[i] = 1 special_judge_flag.append(0) # must stop after separated special_stop_flag.append(1) leader_data_temp = plexe.get_vehicle_data( "v.0.%d" % int(leader[platoons_num - 1])) nextTLS, current_phase, left_distance, absolute_time = getnext_trafficlightandphace( "v.0.%d" % int(leader[platoons_num - 1])) decel = leader_data_temp.speed**2 / ( 2 * (left_distance - 10)) plexe.set_fixed_acceleration( "v.0.%d" % int(leader[platoons_num - 1]), True, -decel) # plexe.set_cc_desired_speed("v.0.%d" % int(leader[i+1]), 0) # plexe.set_active_controller("v.0.%d" % int(leader[platoons_num-1]), FAKED_CACC) # traci.gui.trackVehicle("View #0", "v.0.%d" % int(leader[i+1])) # if i>=1: # print(i, leader_id, leader, car_num,special_stop_flag,state_2_flag) # print (redlight_stop,current_phase) if redlight_stop[i] == 1 and current_phase != 'r' and int( leader_data.speed) <= 1: plexe.set_fixed_acceleration(leader_id, False, 0) plexe.set_cc_desired_speed(leader_id, 25) plexe.set_active_controller(leader_id, ACC) plexe.set_path_cacc_parameters(leader_id, distance=DISTANCE) redlight_stop[i] = 0 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()
if demo_mode and step == 6000: start_sumo("cfg/freeway.sumo.cfg", True) step = 0 state = GOING_TO_POSITION random.seed(1) traci.simulationStep() if step == 0: # create vehicles and track the joiner topology = add_vehicles(plexe, N_VEHICLES, real_engine) traci.gui.trackVehicle("View #0", JOINER) traci.gui.setZoom("View #0", 20000) if step % 10 == 1: # simulate vehicle communication every 100 ms communicate(plexe, topology) # 程序执行 100 步 (0.01s * 100 = 1s) 后,令单个车辆驶近目标位置 if step == 100: # at 1 second, let the joiner get closer to the platoon topology = get_in_position(plexe, JOINER, FRONT_JOIN, topology) # 当车辆达到指定位置,令后车留出足够的空间,以便汇入。 if state == GOING_TO_POSITION and step > 0: # when the distance of the joiner is small enough, let the others # open a gap to let the joiner enter the platoon if get_distance(plexe, JOINER, FRONT_JOIN) < JOIN_DISTANCE + 1: state = OPENING_GAP topology = open_gap(plexe, BEHIND_JOIN, JOINER, topology, N_VEHICLES) # 当空间足够大时,完成汇入
def main(real_engine, setter=None, demo_mode=False): global genStep # start_sumo("cfg/freeway.sumo.cfg", False) step = 0 batch_num = 0 source_edges = ['source0', 'source1', 'source2', 'source3'] removed_vehs = [] all_arrives = [] all_arrived = [] edge_filter, vtype_filter = validate_params(edge_filter=PLAT_EDGES, vtype_filter=["vtypeauto"]) pstate = INSERT while running(demo_mode, step, 2200): if demo_mode and step == 2200: start_sumo("cfg/freeway.sumo.cfg", False) step = 0 print("step is : {}".format(step)) print("Current time is :{}".format(traci.simulation.getCurrentTime())) print("pstate is : {}".format(pstate)) if pstate == INSERT: add_vehicles(N_VEHICLES_GEN, batch_num, list_of_leaders, fromEdge=source_edges[0], platoon_len=24, real_engine=False) batch_num = batch_num + 3 add_vehicles(N_VEHICLES_GEN, batch_num, list_of_leaders, fromEdge=source_edges[1], platoon_len=24, real_engine=False) batch_num = batch_num + 3 add_vehicles(N_VEHICLES_GEN, batch_num, list_of_leaders, fromEdge=source_edges[2], platoon_len=24, real_engine=False) batch_num = batch_num + 3 add_vehicles(N_VEHICLES_GEN, batch_num, list_of_leaders, fromEdge=source_edges[3], platoon_len=24, real_engine=False) batch_num = batch_num + 3 traci.gui.setZoom("View #0", 4500) all_arrived = arrived_vehz(all_arrives) all_arrives = all_arrived traci.simulationStep() pstate = PLATOONING genStep = step if pstate == INSERT2: inserted = add_vehiclez(N_VEHICLES_GEN, batch_num, list_of_leaders, priorityswitches, fromEdge=source_edges[0], platoon_len=24, real_engine=False) batch_num = batch_num + inserted inserted = add_vehiclez(N_VEHICLES_GEN, batch_num, list_of_leaders, priorityswitches, fromEdge=source_edges[1], platoon_len=24, real_engine=False) batch_num = batch_num + inserted inserted = add_vehiclez(N_VEHICLES_GEN, batch_num, list_of_leaders, priorityswitches, fromEdge=source_edges[2], platoon_len=24, real_engine=False) batch_num = batch_num + inserted inserted = add_vehiclez(N_VEHICLES_GEN, batch_num, list_of_leaders, priorityswitches, fromEdge=source_edges[3], platoon_len=24, real_engine=False) batch_num = batch_num + inserted traci.gui.setZoom("View #0", 4500) all_arrived = arrived_vehz(all_arrives) all_arrives = all_arrived traci.simulationStep() topology = {} pstate = PLATOONING genStep = step if pstate == PLATOONING and step >= genStep + 110: switches = [] for lane in range(1, 4): upstreamcheck = upstream_check(batch_num, lane, all_arrived) downstreamcheck = downstream_check(batch_num, lane, all_arrived) switches.append(upstreamcheck) switches.append(downstreamcheck) priorityswitches = switching_logic(switches) if 'GREEN' in priorityswitches: pstate = INSERT2 if step > genStep + 1 and pstate == PLATOONING: flag_planes = [] lanes = lane_gen() for lane in lanes: if not traci.lane.getLastStepVehicleIDs(lane): continue lane_vehicles = [ veh for veh in traci.lane.getLastStepVehicleIDs(lane)[::-1] if veh not in removed_vehs ] planes = sorted_planes(lane_vehicles, lane) for plane in planes: if plane.near_flag(): flag_planes.append(plane) teleported_vehicles = traci.simulation.getEndingTeleportIDList( ) for vehicle in teleported_vehicles: try: traci.vehicle.remove(vehicle, REMOVE_PARKING) except: print(f"Veh {vehicle} has been removed already") else: print(f"Veh {vehicle} has been removed") removed_vehs.append(vehicle) topology = plane.topo_contsructor(removed_vehs) topology = plane.pla_speed_spacing(topology) communicate(topology) all_arrived = arrived_vehz(all_arrives) all_arrives = all_arrived for plane in flag_planes: flag_n_poi_index = plane.look_for_flags(pois, step) if flag_n_poi_index[0] == True: plane.move_to_next_best_lane( step, flag_n_poi_index) plane.set_arrived_free() traci.simulationStep() remove_parked(removed_vehs) teleported_vehicles = traci.simulation.getEndingTeleportIDList() for vehicle in teleported_vehicles: try: traci.vehicle.remove(vehicle, REMOVE_PARKING) except: print(f"Veh {vehicle} has been removed already") else: print(f"Veh {vehicle} has been removed") removed_vehs.append(vehicle) step += 1 traci.close()
def main(real_engine, setter=None, demo_mode= False): start_sumo("cfg/freeway.sumo.cfg", False) step = 0 batch_num = 0 veh_of_interest = "v.40" source_edges = ['source0', 'source1', 'source2', 'source3'] edge_filter, vtype_filter = validate_params( edge_filter=PLAT_EDGES, vtype_filter=["vtypeauto"]) pstate = NON while running(demo_mode, step, 4132): if demo_mode and step == 4132: start_sumo("cfg/freeway.sumo.cfg", False) step = 0 flags_at_pois = {} if pstate == NON: lanes = lane_gen() add_vehicles(N_VEHICLES_GEN, batch_num, fromEdge = source_edges[0], platoon_len = 24, real_engine = False) batch_num = batch_num + 3 ### Start from here add_vehicles(N_VEHICLES_GEN, batch_num, fromEdge = source_edges[1], platoon_len = 24, real_engine = False) batch_num = batch_num + 3 add_vehicles(N_VEHICLES_GEN, batch_num, fromEdge = source_edges[2], platoon_len = 24, real_engine = False) batch_num = batch_num + 3 add_vehicles(N_VEHICLES_GEN, batch_num, fromEdge = source_edges[3], platoon_len = 24, real_engine = False) batch_num = batch_num + 3 traci.gui.setZoom("View #0", 4500) # f = open("/Users/mac/src/Simulations/mixedtraffic/gen_times","w") # f.write("simulation step is {}".format(traci.simulation.getCurrentTime())) # f.close() #traci.gui.setZoom("View #1", 4500) topology = {} flags_at_pois = {} teleported_vehicles =[] vstate = IDLE pstate = PLATOONING genStep = step print("Gen Step is : {}".format(genStep)) print("pstate at gen is : {}".format(pstate)) if pstate == PLATOONING and step == genStep + 1: veh_of_interest = traci.lane.getLastStepVehicleIDs('source0_3')[::-1][0] print("veh of interest is: {}".format(veh_of_interest)) if pstate == PLATOONING: traci.simulationStep() if step > genStep + 10 and pstate == PLATOONING: print("veh of int is:{}".format(veh_of_interest)) if veh_of_interest in traci.edge.getLastStepVehicleIDs("p12"): print("veh of interest at set location {}!!!!!!!!!!!".format(traci.vehicle.getLaneID(veh_of_interest))) pstate = NON if step <= genStep + 14: set_lc_mode() print("LC Mode Set to FIX_LC") list_of_leaders =[] for lane in lanes: if traci.lane.getLastStepVehicleIDs(lane)==[]: continue lane_vehicles = traci.lane.getLastStepVehicleIDs(lane)[::-1] teleported_vehicles = traci.simulation.getEndingTeleportIDList() print("end teleported {}".format(teleported_vehicles)) teleported_vehicles = [vehicle for vehicle in teleported_vehicles if vehicle not in removed_vehicles] print("Teleported {}".format(teleported_vehicles)) removed_vehicles = [] for vehicle in teleported_vehicles: try: traci.vehicle.remove(vehicle, REMOVE_PARKING) except: print("vehicle already removed") else: removed_vehicles.append(vehicle) print("vehicle {} has been removed".format(vehicle)) #teleported_vehicles = [vehicle for vehicle in end_teleport_vehicles if vehicle in teleported_vehicles and vehicle not in removed_vehicles] if lane_vehicles != []: planes = sorted_planes(lane_vehicles, lane, removed_vehicles) if planes != []: for plane in planes: topology = plane.topo_contsructor() topology = plane.pla_speed_spacing(topology, states) #print("Topology at platSpeedSpacing is : {}".format(topology)) communicate(topology) set_arrived_free(ARR_EDGES) for plane in planes: #print("states are {}".format(states)) if traci.vehicle.getRouteID(plane.plane_leader()).split("_") == '0': continue if plane.near_flag(): leader = plane.plane_leader() print("Veh {} looking for flag".format(leader)) flag_data = look_for_flags(leader,pois,flags_at_pois, step) if flag_data != None: if flag_data[0] == True and plane.safe_to_cl(): print("Veh {} has found a flag, changing lanes now".format(leader)) plane.move_to_next_best_lane(step) flags_at_pois[leader]['state'].append('completed') flags_at_pois[leader]['poitype'].append(flag_data[1]) elif flag_data[0] == True and plane.safe_to_cl() == False: print(plane.safe_to_cl()) #pdb.set_trace() obstruction = plane.plane_obstructed(states, obstructors) if obstruction == True: plane.obst_overtaken(obstructors, states) planers.remove_obstructors() #pdb.set_trace() traci.simulationStep() print("step is : {}".format(step)) print("Current time is :{}".format(traci.simulation.getCurrentTime())) print("pstate is : {}".format(pstate)) step += 1 traci.close()