-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from ll7/38-add-an-example-for-recording-and-p…
…layback-of-a-simulation
- Loading branch information
Showing
4 changed files
with
126 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Simulate a random policy with a map defined in SVG format and view the recording.""" | ||
import os | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
from loguru import logger | ||
|
||
from robot_sf.nav.svg_map_parser import SvgMapConverter | ||
from robot_sf.nav.map_config import MapDefinition, MapDefinitionPool | ||
from robot_sf.gym_env.env_config import SimulationSettings, EnvSettings | ||
from robot_sf.gym_env.robot_env import RobotEnv | ||
from robot_sf.sensor.sensor_fusion import OBS_RAYS, OBS_DRIVE_STATE | ||
from robot_sf.robot.differential_drive import DifferentialDriveSettings | ||
from robot_sf.render.playback_recording import load_states_and_visualize | ||
|
||
|
||
|
||
|
||
logger.info("Simulate a random policy with a map defined in SVG format.") | ||
|
||
def test_simulation(map_definition: MapDefinition): | ||
"""Test the simulation with a random policy.""" | ||
|
||
logger.info("Creating the environment.") | ||
env_config = EnvSettings( | ||
map_pool=MapDefinitionPool(map_defs={"my_map": map_definition}) | ||
) | ||
env = RobotEnv(env_config, debug=True, recording_enabled=True) # Activate recording | ||
|
||
env.reset() | ||
|
||
logger.info("Simulating the random policy.") | ||
for _ in range(1000): | ||
action = env.action_space.sample() | ||
env.step(action) | ||
env.render() | ||
|
||
env.reset() # Save the recording | ||
env.exit() | ||
|
||
def convert_map(svg_file: str): | ||
"""Create MapDefinition from svg file.""" | ||
|
||
logger.info("Converting SVG map to MapDefinition object.") | ||
logger.info(f"SVG file: {svg_file}") | ||
|
||
converter = SvgMapConverter(svg_file) | ||
return converter.map_definition | ||
|
||
def get_file(): | ||
"""Get the latest recorded file.""" | ||
|
||
filename = max( | ||
os.listdir('recordings'), key=lambda x: os.path.getctime(os.path.join('recordings', x))) | ||
return Path('recordings', filename) | ||
|
||
|
||
|
||
def main(): | ||
"""Simulate a random policy with a map defined in SVG format and view the recording.""" | ||
|
||
# Create example recording | ||
map_def = convert_map("maps/svg_maps/02_simple_maps.svg") | ||
test_simulation(map_def) | ||
|
||
# Load the states from the file and view the recording | ||
load_states_and_visualize(get_file()) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters