def main():
    try:
        client = carla.Client("localhost", 2000)
        client.set_timeout(10.0)
        world = client.load_world('Town05')

        # set the weather
        weather = carla.WeatherParameters(cloudiness=10.0,
                                          precipitation=0.0,
                                          sun_altitude_angle=90.0)
        world.set_weather(weather)

        # set the spectator position for demo purpose
        spectator = world.get_spectator()
        spectator.set_transform(
            carla.Transform(
                carla.Location(x=-190, y=1.29, z=75.0),
                carla.Rotation(pitch=-88.0, yaw=-1.85,
                               roll=1.595)))  # top view of intersection

        env = CARLA_ENV(world)
        time.sleep(2)  # sleep for 2 seconds, wait the initialization to finish

        traffic_light_list = get_traffic_lights(world.get_actors())

        intersection_list = create_intersections(env, 4, traffic_light_list)
        init_intersection = intersection_list[0]
        normal_intersections = intersection_list[1:]
        init_intersection.add_ego_vehicle(safety_distance=15.0,
                                          stop_choice="abrupt",
                                          vehicle_color='255,255,255')
        init_intersection.add_follow_vehicle(follow_distance=20.0,
                                             stop_choice="penetrate",
                                             penetrate_distance=2.0)
        init_intersection.add_lead_vehicle(lead_distance=20.0,
                                           stop_choice="abrupt",
                                           vehicle_color='255,255,255')
        init_intersection.add_vehicle(choice="left",
                                      stop_choice="abrupt",
                                      vehicle_color='255,255,255')
        init_intersection.add_vehicle(choice="right", command="left")

        # test edit settings
        name1 = init_intersection.add_vehicle(choice="ahead", command="left")
        name2 = init_intersection.add_vehicle(choice="ahead", command="right")

        name1 = init_intersection.edit_vehicle_settings(
            name1, choice="ahead", vehicle_color='128,128,128')
        name2 = init_intersection.edit_vehicle_settings(
            name2, choice="ahead", gap=15.0, vehicle_color='128,128,128')
        #init_intersection.edit_traffic_light("subject")
        #init_intersection.edit_traffic_light("left",red_start = 40.0,red_end = 60.0,yellow_start=30.0,yellow_end=40.0,green_start=0.0,green_end = 30.0)
        #init_intersection.edit_traffic_light("right",red_start = 0.0,red_end = 10.0,yellow_start=10.0,yellow_end=20.0,green_start=20.0,green_end = 40.0)
        #init_intersection.edit_traffic_light("ahead",red_start = 20.0,red_end = 40.0,yellow_start=10.0,yellow_end=20.0,green_start=0.0,green_end = 10.0)

        # get bounding box
        bb = init_intersection.get_vehicle_bounding_box(name1)
        print("bb.x = %f, bb.y = %f, bb.z = %f" % (bb.x, bb.y, bb.z))

        intersection_list[1].add_vehicle(choice="ahead")
        intersection_list[1].add_vehicle(choice="left", command="left")
        intersection_list[1].add_vehicle(choice="right", command="left")
        intersection_list[1].add_vehicle(choice="right", command="right")
        intersection_list[1]._shift_vehicles(-10, choice="right", index=0)
        #intersection_list[1].edit_traffic_light("left")

        intersection_list[2].add_vehicle(choice="ahead")
        intersection_list[2].add_vehicle(choice="left", command="left")
        intersection_list[2].add_vehicle(choice="right", command="left")
        intersection_list[2].add_vehicle(choice="right", command="right")
        #intersection_list[2].edit_traffic_light("right")

        intersection_list[3].add_vehicle(command="left")
        intersection_list[3].add_vehicle()
        #intersection_list[3].edit_traffic_light("ahead")

        # test import/export
        init_setting = init_intersection.export_settings()

        intersection_list[3].import_settings(init_setting)
        intersection_list[3].add_vehicle(command="left")
        intersection_list[3].add_vehicle()
        third_setting = intersection_list[3].export_settings()

        write_intersection_settings("third_intersection_setting",
                                    third_setting)
        new_third_setting = read_intersection_settings(
            'third_intersection_setting')

        intersection_list[2].import_settings(new_third_setting)

        IntersectionBackend(env, intersection_list)
    finally:
        time.sleep(10)
        env.destroy_actors()
Example #2
0
    def __init__(self):
        super(Main, self).__init__()
        # variables needed for intersections
        self.light_list_all = []
        self.car_list = []
        self.delete_car = []
        self.saved_intersections = []
        self.cur_inter = 0
        self.cur_light = 0
        self.cur_lane = 0
        self.redLightDuration = 10
        self.yellowLightDuration = 5
        self.greenLightDuration = 10
        self.gloSafetyDistance = 15
        self.ego_spawned = False
        self.cur_ID = [0, 0, 0]
        self.normal_num = -1
        self.penetrate_dis = 0

        ### Front end setup ###
        #variables create
        self.collision = True
        self.navigation_speed = 10.0
        self.safety_distance = 15
        self.ego = -1
        self.lead = -1
        self.follow = -1

        # build ui
        self.inter = intersection.Ui_Form()
        self.inter.setupUi()
        self.inter.showMaximized()
        self.TrafLit = intersection.Ui_TrafLightSet()
        self.TrafLit.setupUi()
        self.addVeh = intersection.Ui_AddVehicle()
        self.addVeh.setupUi()
        self.errMes = intersection.Ui_ErrMes()
        self.errMes.setupUi()
        self.simulation = intersection.Ui_Simulation()
        self.simulation.setupUi()
        self.pene = intersection.Ui_Penetrate()
        self.pene.setupUi()

        #add 6 widget maps in list
        widgetmaps.append(self.inter.widget3Fro)
        widgetmaps.append(self.inter.widget4Fro)
        widgetmaps.append(self.inter.widget5Fro)
        widgetmaps.append(self.inter.widget6Fro)
        widgetmaps.append(self.inter.widget7Fro)
        widgetmaps.append(self.inter.widget8Fro)

        #link buttons and input
        self.inter.RightFro.clicked.connect(self.right)
        self.inter.LeftFro.clicked.connect(self.left)
        self.inter.RightMostFro.clicked.connect(self.rightMost)
        self.inter.LeftMostFro.clicked.connect(self.leftMost)
        self.inter.spinBoxNumIntFro.valueChanged.connect(self.change_int)
        self.inter.Int1Fro.clicked.connect(lambda: self.front_inter(num=1))
        self.inter.Int2Fro.clicked.connect(lambda: self.front_inter(num=2))
        self.inter.Int3Fro.clicked.connect(lambda: self.front_inter(num=3))
        self.inter.Int4Fro.clicked.connect(lambda: self.front_inter(num=4))
        self.inter.Int5Fro.clicked.connect(lambda: self.front_inter(num=5))
        self.inter.Int6Fro.clicked.connect(lambda: self.front_inter(num=6))
        self.inter.checkBoxColFro.stateChanged.connect(self.collision_change)
        self.inter.spinBoxMaxVFro.valueChanged.connect(self.Max_speed_set)
        self.inter.spinBoxSafDisFro.valueChanged.connect(
            self.safety_distance_set)
        self.inter.backButtonInt.clicked.connect(self.inter_front)
        self.inter.TrafLightInt.clicked.connect(self.inter_Light)
        self.inter.backButtonLit.clicked.connect(self.Light_inter)
        self.inter.backButtonSpa.clicked.connect(self.spawn_inter)
        self.inter.AddVehInt.clicked.connect(self.inter_spawn)
        self.inter.set1Lit.clicked.connect(lambda: self.OpenTrafLit(num=0))
        self.inter.set2Lit.clicked.connect(lambda: self.OpenTrafLit(num=1))
        self.inter.set3Lit.clicked.connect(lambda: self.OpenTrafLit(num=2))
        self.inter.set4Lit.clicked.connect(lambda: self.OpenTrafLit(num=3))
        self.inter.AddSubSpa.clicked.connect(lambda: self.OpenAddVeh(num=0))
        self.inter.AddLeftSpa.clicked.connect(lambda: self.OpenAddVeh(num=1))
        self.inter.AddAheadSpa.clicked.connect(lambda: self.OpenAddVeh(num=2))
        self.inter.AddRightSpa.clicked.connect(lambda: self.OpenAddVeh(num=3))
        self.inter.confirmVeh.clicked.connect(self.confirm)
        self.inter.cobBoxIDInt.currentIndexChanged.connect(self.change_inter)
        self.inter.cobBoxImportInt.currentIndexChanged.connect(
            self.import_inter)
        self.inter.StartSimFro.clicked.connect(self.Front_simulation)
        self.inter.DeleteVeh.clicked.connect(self.deleteCar)
        self.inter.cobBoxStopVeh.currentIndexChanged.connect(
            self.Veh_penetrate)

        for i in range(4):
            name = "CarSub" + str(i + 1)
            tmp = self.inter.findChild(QLabel, name)
            tmp.clicked.connect(self.make_any_vehicle(x=0, y=i))

        for i in range(4):
            name = "CarLeft" + str(i + 1)
            tmp = self.inter.findChild(QLabel, name)
            tmp.clicked.connect(self.make_any_vehicle(x=1, y=i))

        for i in range(4):
            name = "CarAhead" + str(i + 1)
            tmp = self.inter.findChild(QLabel, name)
            tmp.clicked.connect(self.make_any_vehicle(x=2, y=i))

        for i in range(4):
            name = "CarRight" + str(i + 1)
            tmp = self.inter.findChild(QLabel, name)
            tmp.clicked.connect(self.make_any_vehicle(x=3, y=i))

        self.inter.backButtonVeh.clicked.connect(self.vehicle_inter)
        self.TrafLit.buttonBoxTLDia.accepted.connect(self.traffic_light_ok)
        self.addVeh.buttonBoxAdDia.accepted.connect(self.add_vehicle_ok)
        self.simulation.buttonBoxSimDia.accepted.connect(self.start_simulation)

        ### back end setup ###
        self.client = carla.Client("localhost", 2000)
        self.client.set_timeout(20.0)
        self.world = self.client.load_world('Town05')
        self.spectator = self.world.get_spectator()
        self.spectator.set_transform(
            carla.Transform(carla.Location(x=-190, y=1.29, z=75.0),
                            carla.Rotation(pitch=-88.0, yaw=-1.85,
                                           roll=1.595)))
        self.env = CARLA_ENV(self.world)
        self.traffic_light_list = get_traffic_lights(self.world.get_actors())
        self.intersection_list = create_intersections(self.env, 4,
                                                      self.traffic_light_list,
                                                      self.navigation_speed)

        #lists initialized
        for i in range(4):
            light_list_one = [[0.00, 10.00, 10.00, 15.00, 15.00, 25.00],
                              [0.00, 10.00, 10.00, 15.00, 15.00, 25.00],
                              [0.00, 10.00, 10.00, 15.00, 15.00, 25.00],
                              [0.00, 10.00, 10.00, 15.00, 15.00, 25.00]]
            self.light_list_all.append(light_list_one)
            self.saved_intersections.append(None)
            inter_list = []
            for j in range(4):
                lane_list = []
                inter_list.append(lane_list)

            self.car_list.append(inter_list)
        for i in range(4):
            inter_list = []
            for j in range(4):
                lane_list = []
                for k in range(4):
                    lane_list.append(False)
                inter_list.append(lane_list)
            self.delete_car.append(inter_list)

        self.show_win()