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

Saving a Specific Tick State in Carla Simulation #7511

Closed
yagol2020 opened this issue Apr 28, 2024 · 0 comments
Closed

Saving a Specific Tick State in Carla Simulation #7511

yagol2020 opened this issue Apr 28, 2024 · 0 comments

Comments

@yagol2020
Copy link

Hello,

I am currently working on a project where I need to save (of fork) a specific tick (timestamp) state during a simulation for later analysis or to restore the simulation to that exact point.

CARLA version: 0.9.13
Platform/OS: Linux, Ubuntu 20.04
Problem you have experienced: Try to fork a Carla.World, and restore it after complete a simulation. I try to use fork_world = copy.deepcopy(world) but not work.
What you expected to happen: Fork a Carla world, and can restore it later.
Steps to reproduce: Here is my demo code.

import carla
import time
import random
import loguru
import copy

client = carla.Client("localhost", 2000)
client.set_timeout(10.0)
fork_world = None
fork_spectator_transform = None
for i in range(5):
    client.load_world("Town01")
    loguru.logger.info(f"The {i}-th simulation")
    world = client.get_world()
    blueprint_library = world.get_blueprint_library()
    transform = random.choice(world.get_map().get_spawn_points())
    vehicle_bp = random.choice(blueprint_library.filter("vehicle.*"))
    vehicle = world.spawn_actor(vehicle_bp, transform)
    # create a spectator
    spectator = world.get_spectator()
    spectator_transform = carla.Transform(
        transform.location + carla.Location(z=50), carla.Rotation(pitch=-90)
    )
    spectator.set_transform(spectator_transform)
    if i == 0: # the simulation i interested
        fork_world = copy.deepcopy(world) # fail: RuntimeError: Pickling of "carla.libcarla.World" instances is not enabled (http://www.boost.org/libs/python/doc/v2/pickle.html)
        # fork_world = world    # fail: no vehicle in the fork_world
        fork_spectator_transform= spectator_transform
        time.sleep(5)
    else:
        vehicle.destroy()
    time.sleep(2)


# restore the simulation point
world = fork_world
spectator= world.get_spectator()
spectator.set_transform(fork_spectator_transform)

Other information (documentation you consulted, workarounds you tried):
I attempted to use copy.deepcopy of the Carla.World object to save the state of the simulation at a particular tick. However, there are some error in the code.

Traceback (most recent call last):
  File "/home/k/Downloads/scenario_runner-0.9.13/fork_world.py", line 26, in <module>
    fork_world = copy.deepcopy(world)
  File "/home/k/anaconda3/envs/carla_run/lib/python3.7/copy.py", line 169, in deepcopy
    rv = reductor(4)
RuntimeError: Pickling of "carla.libcarla.World" instances is not enabled (http://www.boost.org/libs/python/doc/v2/pickle.html)

While fork_world = world can run success, the vehicle that spawn in origin simulation do not show in fork world.

My guess is that the simulation environment cannot be forked because Carla PythonAPI is a C++ interface. Is there any similar API or documentation support?

I appreciate any assistance you can provide.

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

No branches or pull requests

1 participant