Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

traci.simulation.executeMove() removes the ego car when reaching to a junction #15787

Open
Ahmad1441 opened this issue Nov 26, 2024 · 3 comments
Labels

Comments

@Ahmad1441
Copy link

Ahmad1441 commented Nov 26, 2024

Following with our talk on 15572
I am running a co-simulation of Unity and SUMO where I controlled a car (called it as ego car) in sumo from Unity.
I need to use traci.simulation.executeMove() as SUMO-GUI one step ahead of TRACI as shown in below image:

image

I am using a simple code like below where we collect UnityEgoCarDataCollection() from Unity and we take control of ego car in Sumo then advance one simulation step then we call traci.simulation.executeMove() for putting Traci and SUMO GUI in the same time (removing one step delay of Traci), then call sumoDataCollection() which collects the trajectory data of SUMo at the end of simulation step to pass to Unity to visualize the movement.

  `while traci.simulation.getMinExpectedNumber() > 0:  

    UnityEgoCarDataCollection()  # Collect data from Unity before advancing simulation 

    traci.simulationStep()  # Advance simulation

    traci.simulation.executeMove()

    vehicle_data = sumoDataCollection() # Collect data and speed of SUMO

    vehicle_data_json = json.dumps(vehicle_data, indent=4) 

    socket.send_string(vehicle_data_json)`         # Send the JSON data to unity

This works perfectly fine however, when ego car reaches to a junction, SUMO removes ego car and simulation stops. See:

2024-11-26.10-15-31.mp4

For your information, this is my UnityEgoCarDataCollection(), i tried different keepRoute=1,2,3,4 paramters, but it does not work.

`def UnityDataCollection():
    """
    Receives data from Unity via ZMQ, processes it, and updates the position of 'f_0.0' in SUMO.
    """
    try:
    message = socket.recv()
    unity_data_wrapper = json.loads(message)
    unity_data = unity_data_wrapper.get("array", [])

    for vehicle in unity_data:
        if vehicle["vehicle_id"] == "f_0.0":
            x = float(vehicle["position"][0])
            y = float(vehicle["position"][1])
            angle = float(vehicle["angle"])
            # Convert position to edge ID and lane index
            road_data = traci.simulation.convertRoad(x, y)
            edge_id = road_data[0]
            lane_id = road_data[1]
            traci.vehicle.moveToXY(
                vehicle["vehicle_id"],
                "",
                0,
                x,
                y,
                angle,
                keepRoute=2
            )
except Exception as e:
    print(f"Error processing Unity data: {e}")`

I would appreciate any help as this co-simulation is for an opensource project which helps many researchers job.

@m-kro m-kro added the c:traci label Nov 26, 2024
@namdre
Copy link
Contributor

namdre commented Nov 26, 2024

most likely, this happens because the car is on it's final route edge and would disappear in the next step. As a workaround, modify the route of the car so that it always has more edges to drive.

@Ahmad1441
Copy link
Author

Ahmad1441 commented Nov 26, 2024

Thanks Jakob for your response. I tried your solution and the issue remains the same. I saved 30 sec trajectory of ego car in a txt file and use MoveToXY in traci to guide the ego car follow the trajectories over time. So you dont need unity.

  1. Please download zip file
  2. Please run Sumo2UnityPY.py.
  3. Result: SUMO will remove ego car when it reaches to first junction at second 3.4.

Now, remove or comment out line 126 (executemove()) in below image and run, you will notice ego car will continue its route after junctions till end

image

SUMOdata.zip

@Ahmad1441
Copy link
Author

I think I found the solution if we put keepRoute=1 instead of 2, it worked. Thanks for taking your time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants