Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ll7/robot_sf_ll7
Browse files Browse the repository at this point in the history
  • Loading branch information
ll7 committed Aug 27, 2024
2 parents ec3f65a + 5aec628 commit c8854b1
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 81 deletions.
4 changes: 3 additions & 1 deletion examples/simulate_with_svg_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def main():
"""Simulate a random policy with a map defined in SVG format."""
logger.info("Simulating a random policy with the map.")

svg_file = "maps/svg_maps/02_simple_maps.svg"
#svg_file = "maps/svg_maps/02_simple_maps.svg"
#svg_file = "maps/svg_maps/03_mid_object.svg"
svg_file = "maps/svg_maps/04_small_mid_object.svg"

logger.info("Converting SVG map to MapDefinition object.")
logger.info(f"SVG file: {svg_file}")
Expand Down
71 changes: 71 additions & 0 deletions examples/view_recording.py
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()
129 changes: 129 additions & 0 deletions maps/svg_maps/03_mid_object.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 129 additions & 0 deletions maps/svg_maps/04_small_mid_object.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions robot_sf/gym_env/robot_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(
a dictionary as input and returns a float as reward.
- debug (bool): If True, enables debugging information such as
visualizations.
- recording_enabled (bool): If True, enables recording of the simulation
"""

# Environment configuration details
Expand Down
Loading

0 comments on commit c8854b1

Please sign in to comment.