projectchrono / chrono

High-performance C++ library for multiphysics and multibody dynamics simulations
http://projectchrono.org
BSD 3-Clause "New" or "Revised" License
2.24k stars 469 forks source link

[Carla] Worst realistic with Chrono #447

Closed ClementLeBihan closed 1 year ago

ClementLeBihan commented 1 year ago

Hi,

Carla vehicle dynamic seems not very realistic (https://github.com/carla-simulator/carla/issues/3256). I've tryied carla with chrono on a very simple scenario (throttle set to 0.3 during few seconds, then throttle to 0 until velocity < 2m.s.

I compared both scenario with and without enable_chrono_physics.

Without Chrono : without_chrono

With Chrono : with_chrono

Why using chrono is even worth on vehicle acceleration ?

Best, Clément

The code is bellow :

import carla
from math import sqrt

# initialise carla with our default map
client = carla.Client("localhost", 2000)
client.set_timeout(5)
world = client.load_world("Town06")

settings = world.get_settings()
settings.fixed_delta_seconds = 0.001
settings.synchronous_mode = True
settings.no_rendering_mode = False
world.apply_settings(settings)
blueprints = world.get_blueprint_library()

print(world.get_map().get_spawn_points()[1])

# spawn our favourite car
default_car = blueprints.find("vehicle.tesla.model3")
#position = carla.Transform(carla.Location(x=350, y=12.5, z=3), carla.Rotation(yaw=180))
position = carla.Transform(carla.Location(x=600, y=-13, z=0.300000), carla.Rotation(yaw=180))
vehicle = world.spawn_actor(default_car, position)

for i in range(20):
    try:
        world.tick()
    except:
        print("Skipped tick")

weather = carla.WeatherParameters(
    cloudiness=80.0,
    precipitation=30.0,
    sun_altitude_angle=70.0)

world.set_weather(weather)

# Set the base path
base_path = "/home/carla/carla/Build/chrono-install/share/chrono/data/vehicle/"

# Set the template files

vehicle_json = "sedan/vehicle/Sedan_Vehicle.json"
powertrain_json = "sedan/powertrain/Sedan_SimpleMapPowertrain.json"
tire_json = "sedan/tire/Sedan_TMeasyTire.json"

# Enable Chrono physics
print(f"Enable Chrono physics")
vehicle.enable_chrono_physics(5000, 0.002, vehicle_json, powertrain_json, tire_json, base_path)

world.get_spectator().set_transform(position)

#exit()

# full throttle in the beginning
#vehicle.apply_control(carla.VehicleControl(throttle=1.0))
print("Full throttle")
for _ in range(20):
    try:
        world.tick()
    except:
        print("Skipped tick")

# use norm of velocity and accel vectors (maybe try to disregard z?)
def norm(vector):
    return sqrt(vector.x**2 + vector.y**2 + vector.z**2)

# log all vehicle data in lists
accel_log = []
velocity_log = []
throttle_log = []
brake_log = []
z_log = []

# accelerate up to 20m/s and then apply throttle=0.82 to stay there until set location
throttle_changed = False
throttle = 0.3
while vehicle.get_location().x > 550:
    try:
        world.tick()
    except:
        print("Skipped tick")
    a = vehicle.get_acceleration()
    a_x = 0 if a.x == 0 else -norm(a)*a.x/abs(a.x)

    v = vehicle.get_velocity()
    v_x = 0 if v.x == 0 else -norm(v)*v.x/abs(v.x)
    if not throttle_changed and v_x > 20: # 20 40
        throttle = 0.82 # 0.48 0.77
        print(f"Apply throttle={throttle} and hold speed")
        throttle_changed = True

    vehicle.apply_control(carla.VehicleControl(throttle=throttle))

    accel_log.append(a_x)
    print(vehicle.get_location().x, v_x, a_x)
    velocity_log.append(v_x)
    throttle_log.append(throttle)
    brake_log.append(0.0)
    z_log.append(a.z)

# now change throttle and brake to see the impact on the velocity
throttle = 0.0
brake = 0.0

print(f"Apply throttle={throttle}, brake={brake}")
vehicle.apply_control(carla.VehicleControl(throttle=throttle, brake=brake))

# break the loop at some coordinate or when the velocity is low enough
while v_x > 2:
    try:
        world.tick()
    except:
        print("Skipped tick")
    a = vehicle.get_acceleration()
    a_x = 0 if a.x == 0 else -norm(a)*a.x/abs(a.x)

    v = vehicle.get_velocity()
    v_x = 0 if v.x == 0 else -norm(v)*v.x/abs(v.x)
    if v_x < 2:
        print("Vehicle stopped")
        break

    accel_log.append(a_x)
    print(vehicle.get_location().x, v_x, a_x)
    throttle_log.append(throttle)
    brake_log.append(brake)
    velocity_log.append(v_x)
    z_log.append(a.z)
else:
    print("Vehicle has driven outside the test area")

import matplotlib.pyplot as plt
import matplotlib 
matplotlib.use('TkAgg')

# first plot the velocities, accels, throttle and brake values by tick number (implicit indexing)
plt.plot(velocity_log, label="v")
plt.plot(accel_log, label="a")
#plt.plot(z_log, label="z cooridnate")
# throttle and brake are scaled *10 to make them readable
plt.plot([10*t for t in throttle_log], label="throttle*10")
plt.plot([10*b for b in brake_log], label="brake*10")
plt.legend()

plt.show()
rserban commented 1 year ago

You will need to discuss this with the Carla developers. While I was aware they implemented an interface to Chrono vehicle dynamics, I do not know how they did that and so I cannot gauge on its correctness or comment on results.