Skip to content

Commit

Permalink
Add compressed urdf file's md5sum and ues it to check downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
iory committed Dec 10, 2024
1 parent 1100376 commit 2ed9ff3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
21 changes: 13 additions & 8 deletions ros/kxr_models/node_scripts/urdf_model_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def __init__(self):
full_namespace[:last_slash_pos] if last_slash_pos != 0 else ""
)

def set_urdf_hash(self, md5sum, urdf_data):
rospy.set_param(self.clean_namespace + "/urdf_hash", md5sum)
def set_urdf_hash(self, urdf_md5sum, compressed_md5sum, urdf_data):
rospy.set_param(self.clean_namespace + "/urdf_hash", urdf_md5sum)
rospy.set_param(self.clean_namespace + "/compressed_urdf_hash", compressed_md5sum)
rospy.set_param(self.clean_namespace + "/robot_description_viz", urdf_data)

def run(self):
Expand All @@ -43,13 +44,15 @@ def run(self):
temp_file.write(urdf)
urdf_path = temp_file.name

md5sum = checksum_md5(urdf_path)
urdf_md5sum = checksum_md5(urdf_path)
compressed_urdf_path = os.path.join(
kxr_models_path, "models", "urdf", f"{md5sum}.tar.gz"
kxr_models_path, "models", "urdf", f"{urdf_md5sum}.tar.gz"
)

if os.path.exists(compressed_urdf_path):
self._update_robot_description(urdf_path, md5sum, kxr_models_path)
compressed_md5sum = checksum_md5(compressed_urdf_path)
self._update_robot_description(urdf_path, urdf_md5sum,
compressed_md5sum, kxr_models_path)
continue

lock_path = compressed_urdf_path + ".lock"
Expand All @@ -60,18 +63,20 @@ def run(self):
compress=True,
)
rospy.loginfo(f"Compressed URDF model saved to {compressed_urdf_path}")
self._update_robot_description(urdf_path, md5sum, kxr_models_path)
compressed_md5sum = checksum_md5(compressed_urdf_path)
self._update_robot_description(urdf_path, urdf_md5sum,
compressed_md5sum, kxr_models_path)

os.remove(urdf_path)

def _update_robot_description(self, urdf_path, md5sum, kxr_models_path):
def _update_robot_description(self, urdf_path, md5sum, compressed_md5sum, kxr_models_path):
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
tree = ET.parse(urdf_path, parser)
root = tree.getroot()
robot_name = root.get("name", "default_robot")
with open(urdf_path) as urdf_file:
urdf_data = urdf_file.read()
self.set_urdf_hash(md5sum, urdf_data)
self.set_urdf_hash(md5sum, compressed_md5sum, urdf_data)
rospy.loginfo(f"Updated robot description for {robot_name}")


Expand Down
23 changes: 18 additions & 5 deletions ros/kxr_models/python/kxr_models/download_urdf.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from distutils.version import StrictVersion
import os.path as osp

import gdown
import pkg_resources
import rospkg
import rospy

from kxr_models.ros import get_namespace

gdown_version = pkg_resources.get_distribution("gdown").version


def download_urdf_mesh_files(namespace=None):
if namespace is None:
Expand All @@ -14,11 +18,15 @@ def download_urdf_mesh_files(namespace=None):
server_ip = rospy.get_param(namespace + "/model_server_ip", "localhost")

urdf_hash = rospy.get_param(namespace + "/urdf_hash", None)
compressed_urdf_hash = rospy.get_param(
namespace + "/compressed_urdf_hash", None)
rate = rospy.Rate(1)
while not rospy.is_shutdown() and urdf_hash is None:
while not rospy.is_shutdown() and urdf_hash is None and compressed_urdf_hash is None:
rate.sleep()
rospy.loginfo("Waiting rosparam {} set".format(namespace + "/urdf_hash"))
rospy.loginfo("Waiting rosparam {} and {} set".format(
namespace + "/urdf_hash", namespace + "/compressed_urdf_hash"))
urdf_hash = rospy.get_param(namespace + "/urdf_hash", None)
compressed_urdf_hash = rospy.get_param(namespace + "/compressed_urdf_hash", None)

server_url = f"http://{server_ip}:{port}/urdf/{urdf_hash}.tar.gz"

Expand All @@ -31,7 +39,12 @@ def download_urdf_mesh_files(namespace=None):
while not osp.exists(compressed_urdf_path):
rospy.loginfo(f"Waiting {compressed_urdf_path} from server")
rospy.sleep(1.0)
gdown.cached_download(url=server_url,
hash=f'md5:{urdf_hash}',
path=compressed_urdf_path)
if StrictVersion(gdown_version) < StrictVersion("5.1.0"):
gdown.cached_download(url=server_url,
md5=f'{compressed_urdf_hash}',
path=compressed_urdf_path)
else:
gdown.cached_download(url=server_url,
hash=f'md5:{compressed_urdf_hash}',
path=compressed_urdf_path)
gdown.extractall(osp.join(kxr_models_path, "models", "urdf", f"{urdf_hash}.tar.gz"))

0 comments on commit 2ed9ff3

Please sign in to comment.