carla-simulator / carla

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

How to set the instantaneous speed of a vehicle? #8400

Open AOOOOOA opened 1 week ago

AOOOOOA commented 1 week ago

CARLA version: 0.9.14 Platform/OS: Ubuntu 18.04

I'm working on a task to reproduce the state of a vehicle, where I need to restore the vehicle's speed to a non-zero value at a specific position. The Python API only provides the function _set_target_velocity(), which requires an acceleration process to reach the target speed, rather than setting it directly. I noticed that an older version of CARLA had an API called set_velocity()_ that could directly set the vehicle's velocity to the desired value in the next frame. However, this function appears to be deprecated in the latest version. How can I achieve this functionality now?

PabloVD commented 1 week ago

You can try to use the enable_constant_velocity method in the carla.Actor class, that may work for your task.

Alternatively, in case you are trying to reproduce the results from a past simulation, you could use the recorder and replayer, although depending on your needs, this may not be flexible enough.

PatrickPromitzer commented 1 week ago

Hi, You can add an impulse to the vehicle, which gives them a push.

def push_vehicle(self, vehicle_actor, speed: float):

    actor_transform: carla.Transform = vehicle_actor.get_transform()
    impulse_strength = 1000.0 * speed
    push_factor = []
    for yaw in [actor_transform.rotation.yaw + 90, actor_transform.rotation.yaw]:
        while yaw > 180 or yaw < -180:
            if yaw > 180:
                yaw -= 360
            elif yaw < -180:
                yaw += 360

        if yaw > 90:
            push_factor.append(1 - (((1 / 90) * yaw) - 1))
        elif yaw < -90:
            push_factor.append(-1 - (((1 / 90) * yaw) + 1))
        else:
            push_factor.append((1 / 90) * yaw)
    actor_transform.rotation.roll = 0.0
    actor_transform.rotation.pitch = 0.0
    actor_transform.location.z += 0.1
    vehicle_actor.set_transform(actor_transform)
    print("adding force")
    vehicle_actor.add_impulse(carla.Vector3D(x=impulse_strength * push_factor[0],
                                             y=impulse_strength * push_factor[1],
                                             z=0.0))

I didn't test if the speed value is a specific type, but it should be good enough to give your vehicle a push.