carla-simulator / carla

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

How to get velocity using carla-python API in carla-sumo co-simulation #7143

Open ahsan155 opened 6 months ago

ahsan155 commented 6 months ago

CARLA version: 0.9.15 Platform/OS: linux

I am running carla-sumo co-simulation. I am using the script 'spawn_npc_sumo.py' which is located in Co-Simulation/sumo to spawn vehicles in simulation. Inside this script, I tried to collect vehicles velocity inside the synchronization loop. The issue is that vehicles velocity keeps returning 0 even though vehicle is running. I know there is a way to collect velocity using sumo/traci. But I would like to collect velocity information using carla functionality. The synchronization loop:

while True: start = time.time() synchronization.tick()

        # Updates vehicle routes
        for vehicle_id in traci.vehicle.getIDList():
            route = traci.vehicle.getRoute(vehicle_id)
            index = traci.vehicle.getRouteIndex(vehicle_id)
            vclass = traci.vehicle.getVehicleClass(vehicle_id)

            if index == (len(route) - 1):
                current_edge = sumo_net.getEdge(route[index])
                available_edges = list(current_edge.getAllowedOutgoing(vclass).keys())
                if available_edges:
                    next_edge = random.choice(available_edges)

                    new_route = [current_edge.getID(), next_edge.getID()]
                    traci.vehicle.setRoute(vehicle_id, new_route)

        for vehicle in world.get_actors().filter('vehicle.*'):
            velocity = vehicle.get_velocity()
            print(f"Vehicle {vehicle.id} velocity: {velocity.x}, {velocity.y}, {velocity.z}")

        end = time.time()
        elapsed = end - start
        if elapsed < args.step_length:
            time.sleep(args.step_length - elapsed)

        second+=1

except KeyboardInterrupt:
    logging.info('Cancelled by user.')

finally:
    synchronization.close()

    if os.path.exists(tmpdir):
        shutil.rmtree(tmpdir)

So, how can I get non zero velocity using carla code?

Steps to reproduce: Just add the below code inside synchronization loop of the script spawn_npc_sumo.py

'for vehicle in world.get_actors().filter('vehicle.*'): velocity = vehicle.get_velocity() print(f"Vehicle {vehicle.id} velocity: {velocity.x}, {velocity.y}, {velocity.z}")'

csonthomihaly commented 6 months ago

I think, the co-simulation works without dynamic information exchange. I was just using the other script called python3 run_synchronization.py examples/Town04.sumocfg --sumo-gui which is not the same as the one you mentioned, but im sure it works the same way. In this case SUMO is responsible for the dynamic traffic modelling and it is just moves phisycally-static objects in CARLA. Therefore in that side you are not able to get the speed easily. I think there is 2 possible solutions: