You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
The text was updated successfully, but these errors were encountered:
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.
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.
Please download zip file
Please run Sumo2UnityPY.py.
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
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:
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.
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.
I would appreciate any help as this co-simulation is for an opensource project which helps many researchers job.
The text was updated successfully, but these errors were encountered: