Skip to content

Commit

Permalink
[generation] temporarily fixed plugin parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaKro committed Aug 9, 2024
1 parent e61ea38 commit e5abe78
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/fpm/graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import glob
import re

import numpy as np

Expand All @@ -14,10 +15,16 @@
def build_graph_from_directory(inputs: tuple):
# Build the graph by reading all composable models in the input folder
g = rdflib.Graph()

def natural_sort_key(s):
"""Split the string into a list of substrings, with numbers as numbers."""
return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', s)]

for input_folder in inputs:
input_models = glob.glob(os.path.join(input_folder, "*.json"))
print("Found {} models in {}".format(len(input_models), input_folder))
print("Adding to the graph...")
input_models = sorted(input_models, key=natural_sort_key)
for file_path in input_models:
print("\t{}".format(file_path))
g.parse(file_path, format="json-ld")
Expand Down Expand Up @@ -60,7 +67,6 @@ def get_floorplan_model_name(g):


def traverse_to_world_origin(g, frame):

# Go through the geometric relation predicates
pred_filter = traversal.filter_by_predicates([GEOM["with-respect-to"], GEOM["of"]])
# Algorithm to traverse the graph
Expand Down Expand Up @@ -92,7 +98,6 @@ def traverse_to_world_origin(g, frame):


def get_transformation_matrix_wrt_frame(g, root, target):

# Configure the traversal algorithm
filter = [GEOM["with-respect-to"], GEOM["of"]]
pred_filter = traversal.filter_by_predicates(filter)
Expand Down
23 changes: 19 additions & 4 deletions src/fpm/templates/gazebo/world.sdf.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,34 @@
<static>true</static>
<pose>-0.0 0.0 0.0 0.0 0.0 0.0</pose>
</include>
{% for instance in model.instances %}
{%- for instance in model.instances %}
{# Just here to add a newline before each include for readability#}
<include>
<name>{{instance.instance_name}}</name>
<uri>model://{{instance.name}}</uri>
<static>{{instance.static}}</static>
<pose>{{instance.pose}}</pose>
{% for start_joint_state in instance.start_joint_states %}
{%- for start_joint_state in instance.start_joint_states %}
{%- if instance.plugin_type == 'initial' %}
<plugin name="initial_plugin" filename="libinitial_plugin.so">
<joint>{{start_joint_state.joint}}</joint>
<position>{{start_joint_state.position}}</position>
</plugin>
{% endfor %}
{%- elif instance.plugin_type == 'dynamic' %}
<plugin name="dynamic_joint_pluginss" filename="libdynamic_joint_plugin.so">
<joint>{{start_joint_state.joint}}</joint>
<uri>{{start_joint_state.uri}}</uri>
</plugin>
{%- elif instance.plugin_type == 'adversarial' %}
<plugin name="adversarial_joint_plugin" filename="libadversarial_joint_plugin.so">
<joint>{{start_joint_state.joint}}</joint>
<x>{{start_joint_state.position_before}}</x>
<near>{{start_joint_state.distance_to_trigger}}</near>
<y>{{start_joint_state.position_after}}</y>
</plugin>
{%- endif %}
{%- endfor %}
</include>
{% endfor %}
{%- endfor %}
</world>
</sdf>
8 changes: 7 additions & 1 deletion src/fpm/transformations/objects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

from rdflib import RDF
from rdflib import RDF, URIRef

from fpm.transformations.sdf import (
get_sdf_geometry,
Expand Down Expand Up @@ -149,6 +149,11 @@ def get_object_instance(g, instance):
T = get_transformation_matrix_wrt_frame(g, frame, world_frame)
pose_coordinates = get_sdf_pose_from_transformation_matrix(T)

plugin_type = g.value(instance, OBJ["plugin"])
if plugin_type:
plugin_uri = URIRef(plugin_type)
plugin_type = plugin_uri.split("#")[-1]

state = g.value(instance, ST["start-state"])
start_joint_states = []

Expand All @@ -173,6 +178,7 @@ def get_object_instance(g, instance):
"static": "false",
"name": prefixed(g, of_obj)[5:],
"instance_name": prefixed(g, instance)[5:],
"plugin_type": plugin_type,
"start_joint_states": start_joint_states,
}

Expand Down
3 changes: 2 additions & 1 deletion src/fpm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def load_template(template_name, template_folder=None):
loader = PackageLoader("fpm")
else:
loader = FileSystemLoader(template_folder)
env = Environment(loader=loader)
env = Environment(loader=loader,
lstrip_blocks=True)
return env.get_template(template_name)


Expand Down

0 comments on commit e5abe78

Please sign in to comment.