carla-simulator / carla

Open-source simulator for autonomous driving research.
http://carla.org
MIT License
11.5k stars 3.72k forks source link

Traffic light control #8216

Open suleymanttr opened 2 months ago

suleymanttr commented 2 months ago

Thanks for contributing to CARLA!

If you are reporting an issue, please use the following outline:

CARLA version: 0.9.13 Platform/OS:ubuntu 20.04 Problem you have experienced: I have problem with traffic lights in Carla.I want to customize its initial state and phase durations. Although I set the initial states and the phase durations(such as set_yellow_time...) it does not act how I set.Light stuck on my initial state. What you expected to happen: I want it to run just like real life: red>yellow>green>yellow>red . It stuck on the given phase. Steps to reproduce: Other information (documentation you consulted, workarounds you tried):

Below is the code to reproduce:

def set_traffic_light_timing(world, active_traffic_light_ids):
    """
    Set initial timing for traffic lights 13, 11, and 20. Only called once.
    """
    all_traffic_lights = world.get_actors().filter('traffic.traffic_light')
    print("All traffic Lights")
    for traffic_light in all_traffic_lights:
        print(traffic_light.id)
        if traffic_light.id in active_traffic_light_ids:
            print('Ifeteyim')
            traffic_light.freeze(False)      # Unfreeze them to allow phase change
            world.tick()
            # Set the timing for controlled traffic lights only once
            traffic_light.set_state(carla.TrafficLightState.Yellow)  # Set initial state to Red
            traffic_light.set_green_time(10)  # Set green phase duration
            traffic_light.set_red_time(5)    # Set red phase duration
            traffic_light.set_yellow_time(3) # Set yellow phase duration
            print("Freeze State:", traffic_light.is_frozen())
    print("Timing set for traffic lights 13, 11, and 20.")

def deactivate_and_freeze_traffic_lights(world, active_traffic_light_ids):
    """
    Freezes all traffic lights except the ones in active_traffic_light_ids.
    Ensures that only active lights are functioning.
    """
    all_traffic_lights = world.get_actors().filter('traffic.traffic_light')

    for traffic_light in all_traffic_lights:
        if traffic_light.id not in active_traffic_light_ids:
            traffic_light.freeze(True)  # Freeze the traffic light to prevent phase change
            traffic_light.set_state(carla.TrafficLightState.Off)  # Optionally turn off
        else:
            # Ensure active traffic lights stay unfrozen and can change phases
            traffic_light.freeze(False)

def main():
    """
    Main function to set up CARLA simulation and control traffic lights.
    Keeps the simulation running and ensures that only traffic lights 13, 11, and 20 are active.
    Deactivated lights are frozen to prevent further changes.
    """
    # Connect to CARLA server
    client = carla.Client('localhost', 2000)
    client.set_timeout(10.0)
        # Get the world object
    world = client.get_world()

    # Synchron: NOTE: THere is problem ask IRaam
    # settings = world.get_settings()
    # settings.synchronous_mode = True
    # world.apply_settings(settings)

    # Define the traffic light IDs you want to control (13, 11, and 20)
    active_traffic_light_ids = [13, 11, 20]
    # Freeze all traffic lights except 13, 11, and 20
    # deactivate_and_freeze_traffic_lights(world, active_traffic_light_ids)
    # Set the initial timing for traffic lights (this is only done once)
    set_traffic_light_timing(world, active_traffic_light_ids)
    try:
        while True:
            # Ensure non-active traffic lights remain frozen
            deactivate_and_freeze_traffic_lights(world, active_traffic_light_ids)
            # world.tick()
            ########
            # all_traffic_lights = world.get_actors().filter('traffic.traffic_light')
            # for traffic_light in all_traffic_lights:
            #     if traffic_light.id in active_traffic_light_ids:
            #         print(traffic_light.id)
            #         print("State:",traffic_light.get_state())
            ##########
            # Sleep for 1 second between checks to avoid overloading the system
            time.sleep(0.5)
    except KeyboardInterrupt:
        print("Simulation stopped by user.")

if __name__ == '__main__':
    main()
lingyixia commented 1 month ago

It is weird that freeze method seems “Stops all the traffic lights in the scene at their current state” but not the current traffic light as write in https://carla.readthedocs.io/en/latest/python_api/#carlatrafficlightstate.