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

Update URDF parsing logic and improve R/W operations on files #100

Merged
merged 5 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/black.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: black
uses: psf/black@stable
with:
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/pypi-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ jobs:
python-version:
- 3.8
os:
- ubuntu-20.04
- ubuntu-22.04

steps:
- uses: actions/checkout@master
- run: git fetch --prune --unshallow

- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -36,19 +36,19 @@ jobs:
run: python -m build --sdist --wheel --outdir dist/ .

- name: Archive artifacts
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: dist
path: dist

deploy_test_PyPI:
name: 📦 Deploy to TestPyPI
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
needs: [build_wheels]
if: github.ref == 'refs/heads/main'
steps:
- name: Download artifacts
uses: actions/download-artifact@v2
uses: actions/download-artifact@v4
with:
name: dist
path: dist
Expand All @@ -61,12 +61,12 @@ jobs:

deploy_PyPI:
name: 📦 Deploy to PyPI
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
needs: [build_wheels]
if: startsWith(github.ref, 'refs/tags')
steps:
- name: Download artifacts
uses: actions/download-artifact@v2
uses: actions/download-artifact@v4
with:
name: dist
path: dist
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
python-version: [3.11]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand Down
73 changes: 35 additions & 38 deletions examples/mpc-ik.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/github/ami-iit/adam/blob/main/examples/mpc-ik.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MmkmgG_gGWVi"
},
"source": [
"## Install Mujoco, adam, and mediapy.\n",
"\n",
"Download also mujoco-menagerie for the panda model and the urdf needed from adam.\n",
"Set some stuff for the visualization."
],
"metadata": {
"id": "MmkmgG_gGWVi"
}
]
},
{
"cell_type": "code",
Expand All @@ -47,12 +47,12 @@
},
{
"cell_type": "markdown",
"source": [
"## Import packages"
],
"metadata": {
"id": "Dz4cHPy2Gtmq"
}
},
"source": [
"## Import packages"
]
},
{
"cell_type": "code",
Expand All @@ -66,20 +66,17 @@
"import mediapy as media\n",
"from adam.casadi import KinDynComputations\n",
"import numpy as np\n",
"import casadi as cs\n",
"import distutils.util\n",
"import os\n",
"import subprocess"
"import casadi as cs"
]
},
{
"cell_type": "markdown",
"source": [
"## Import the panda scene in mujoco"
],
"metadata": {
"id": "2zw4FO-IGxdR"
}
},
"source": [
"## Import the panda scene in mujoco"
]
},
{
"cell_type": "code",
Expand All @@ -95,17 +92,17 @@
},
{
"cell_type": "markdown",
"metadata": {
"id": "CZMO7PsmKUB6"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just selecting a few lines, but I'm considering the whole file. I was wondering: which is the automatic formatter you used? I will put it in the dependencies ;)
I would remove this file from this PR for now (just to enhance the clarity of this PR) and maybe add it in a new PR that just formats the files!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using black[jupyter] and isort for formatting.

Formatting removed in 79f6d67 with rebase

},
"source": [
"## Import urdf in adam\n",
"\n",
"Set the commanded joint list and impor the urdf in adam.\n",
"\n",
"For now I have to use a separate urdf for adam.\n",
"An importer for a mujoco model could be an idea for the future!"
],
"metadata": {
"id": "CZMO7PsmKUB6"
}
]
},
{
"cell_type": "code",
Expand All @@ -122,12 +119,12 @@
},
{
"cell_type": "markdown",
"source": [
"## A wrapper interface with mujoco"
],
"metadata": {
"id": "g5LX5kQAKwaM"
}
},
"source": [
"## A wrapper interface with mujoco"
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -173,15 +170,15 @@
},
{
"cell_type": "markdown",
"metadata": {
"id": "wuY9hqdlD3Vo"
},
"source": [
"# Model Inverse Kinematics as an MPC\n",
"\n",
"An MPC is maybe not the best way to solve an IK problem.\n",
"I just want to show how to use the casadi interface."
],
"metadata": {
"id": "wuY9hqdlD3Vo"
}
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -239,6 +236,9 @@
},
{
"cell_type": "markdown",
"metadata": {
"id": "Hf-Uq8PWFy6v"
},
"source": [
"# Simulation loop\n",
"\n",
Expand All @@ -247,10 +247,7 @@
"\n",
"On the notebook it is a bit slow.\n",
"To run it real time set OMP_NUM_THREADS=1 on your laptop!"
],
"metadata": {
"id": "Hf-Uq8PWFy6v"
}
]
},
{
"cell_type": "code",
Expand Down Expand Up @@ -314,6 +311,10 @@
}
],
"metadata": {
"colab": {
"include_colab_link": true,
"provenance": []
},
"kernelspec": {
"display_name": "adam_env",
"language": "python",
Expand All @@ -330,10 +331,6 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
Expand Down
37 changes: 22 additions & 15 deletions src/adam/model/std_factories/std_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,43 @@ def urdf_remove_sensors_tags(xml_string):
return modified_xml_string


def get_xml_string(path: str):
isPath = False
def get_xml_string(path: str | pathlib.Path):

isPath = isinstance(path, pathlib.Path)
isUrdf = False

# Extract the maximum path length for the current OS
try:
from ctypes.wintypes import MAX_PATH
except ValueError:
MAX_PATH = os.pathconf("/", "PC_PATH_MAX")

# Checking if it is a path or an urdf
if type(path) is not (pathlib.Path):
if os.path.exists(path):
if not isPath:
if len(path) <= MAX_PATH and os.path.exists(path):
path = pathlib.Path(path)
isPath = True
else:
root = ET.fromstring(path)
robot_el = None

for elem in root.iter():
if elem.tag == "robot":
xml_string = path
isUrdf = True
elif path.exists():
isPath = True
break

if isPath:
if not path.is_file():
raise FileExistsError(path)

with path.open() as xml_file:
xml_string = xml_file.read()

if not (isPath) and not (isUrdf):
if not isPath and not isUrdf:
raise ValueError(
f"Invalid urdf string: {path}. It is neither a path nor a urdf string"
)

if isPath:
if not (path.exists()):
raise FileExistsError(path)
path = pathlib.Path(path)
with open(path, "r") as xml_file:
xml_string = xml_file.read()
xml_file.close()
return xml_string


Expand Down
19 changes: 11 additions & 8 deletions tests/parametric/test_CasADi_computations_parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
Repo.clone_from(git_url, temp_dir.name)
model_path = temp_dir.name + "/models/stickBot/model.urdf"

## Ack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
robot_file_read = open(model_path, "r")
robot_urdf_string = robot_file_read.read()
robot_urdf_string = robot_urdf_string.replace("<?xml", "")
robot_urdf_string = robot_urdf_string.replace("version='1.0'", "")
robot_urdf_string = robot_urdf_string.replace("encoding='UTF-8'?>", "")
robot_file_write = open(model_path, "w")
robot_file_write.write(robot_urdf_string)
## Hack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
with open(model_path, "r", encoding="utf-8") as robot_file:
robot_urdf_string = (
robot_file.read()
.replace("<?xml", "")
.replace("version='1.0'", "")
.replace("encoding='UTF-8'?>", "")
)

with open(model_path, "w") as robot_file:
robot_file.write(robot_urdf_string)

joints_name_list = [
"torso_pitch",
Expand Down
19 changes: 11 additions & 8 deletions tests/parametric/test_Jax_computations_parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
Repo.clone_from(git_url, temp_dir.name)
model_path = temp_dir.name + "/models/stickBot/model.urdf"

## Ack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
robot_file_read = open(model_path, "r")
robot_urdf_string = robot_file_read.read()
robot_urdf_string = robot_urdf_string.replace("<?xml", "")
robot_urdf_string = robot_urdf_string.replace("version='1.0'", "")
robot_urdf_string = robot_urdf_string.replace("encoding='UTF-8'?>", "")
robot_file_write = open(model_path, "w")
robot_file_write.write(robot_urdf_string)
## Hack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
with open(model_path, "r", encoding="utf-8") as robot_file:
robot_urdf_string = (
robot_file.read()
.replace("<?xml", "")
.replace("version='1.0'", "")
.replace("encoding='UTF-8'?>", "")
)

with open(model_path, "w") as robot_file:
robot_file.write(robot_urdf_string)

joints_name_list = [
"torso_pitch",
Expand Down
19 changes: 11 additions & 8 deletions tests/parametric/test_NumPy_computations_parametric.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
Repo.clone_from(git_url, temp_dir.name)
model_path = temp_dir.name + "/models/stickBot/model.urdf"

## Ack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
robot_file_read = open(model_path, "r")
robot_urdf_string = robot_file_read.read()
robot_urdf_string = robot_urdf_string.replace("<?xml", "")
robot_urdf_string = robot_urdf_string.replace("version='1.0'", "")
robot_urdf_string = robot_urdf_string.replace("encoding='UTF-8'?>", "")
robot_file_write = open(model_path, "w")
robot_file_write.write(robot_urdf_string)
## Hack to remove the encoding urdf, see https://github.com/icub-tech-iit/ergocub-gazebo-simulations/issues/49
with open(model_path, "r", encoding="utf-8") as robot_file:
robot_urdf_string = (
robot_file.read()
.replace("<?xml", "")
.replace("version='1.0'", "")
.replace("encoding='UTF-8'?>", "")
)

with open(model_path, "w") as robot_file:
robot_file.write(robot_urdf_string)

joints_name_list = [
"torso_pitch",
Expand Down
Loading
Loading