Skip to content

Commit

Permalink
Enable conversion from URDF content (#158)
Browse files Browse the repository at this point in the history
* enable conversion from urdf content

* fix + new test

* use stdin

* prepare 1.15

* Update README.md

Co-authored-by: Olivier Michel <[email protected]>

* current directory for realtive paths

* Apply suggestions from code review

Co-authored-by: Olivier Michel <[email protected]>

Co-authored-by: Olivier Michel <[email protected]>
  • Loading branch information
BenjaminHug8 and omichel authored Feb 14, 2022
1 parent 2d62081 commit 0ce4d72
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 172 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
matrix:
python: [3.5, 3.6, 3.7, 3.8]
runs-on: ubuntu-latest
env:
CI: 1
steps:
- uses: actions/checkout@v2
with:
Expand Down
45 changes: 35 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@ python -m urdf2webots.importer --input=someRobot.urdf [--output=outputFile] [--n

The script accepts the following arguments:
- **-h, --help**: Show the help message and exit.
- **--input=INFILE**: Specifies the urdf file to convert.
- **--output=OUTFILE**: If set, specifies the path and, if ending in ".proto", name of the resulting PROTO file. The filename minus the .proto extension will be the robot name (for PROTO conversion only).
- **--input=INPUT**: Specifies the URDF file to convert.
- **--output=OUTPUT**: If set, specifies the path and, if ending in ".proto", name of the resulting PROTO file. The filename minus the .proto extension will be the robot name (for PROTO conversion only).
- **--robot-name**: Specify the name of the robot and generate a Robot node string instead of a PROTO file (has to be unique).
- **--normal**: If set, the normals are exported if present in the URDF definition.
- **--box-collision**: If set, the bounding objects are approximated using boxes.
- **--tool-slot=LinkName**: Specify the link that you want to add a tool slot to (exact link name from urdf, for PROTO conversion only).
- **--tool-slot=LinkName**: Specify the link that you want to add a tool slot to (exact link name from URDF, for PROTO conversion only).
- **--translation="0 0 0"**: Set the translation field of the PROTO file or Webots Robot node string.
- **--rotation="0 0 1 0"**: Set the rotation field of the PROTO file or Webots Robot node string.
- **--init-pos=JointPositions**: Set the initial positions of your robot joints. Example: `--init-pos="[1.2, 0.5, -1.5]"` would set the first 3 joints of your robot to the specified values, and leave the rest with their default value.
- **--link-to-def**: Creates a DEF with the link name for each solid to be able to access it using getFromProtoDef(defName) (for PROTO conversion only).
- **--joint-to-def**: Creates a DEF with the joint name for each joint to be able to access it using getFromProtoDef(defName) (for PROTO conversion only).

In case the **--input** option is missing, the script will read the URDF content from `stdin`.
In that case, you can pipe the content of your URDF file into the script: `cat my_robot.urdf | urdf2proto.py`.
Relative paths present in your URDF file will be treated relatively to the current directory from which the script is called.

> Previously the **--static-base** argument was supported in order to set the base link to be static (disabled physics). It has been removed as there is a better way to do it by adding the following to your URDF file (assuming **base_link** is the root link of your robot):
>
>```
Expand All @@ -61,12 +65,12 @@ The script accepts the following arguments:

#### Arguments

The command line arguments available from the terminal are also available from the python interface, but some have different names:
The command line arguments available from the terminal are also available from the Python interface, but some have different names:

| Terminal | Python |
|----------|-------------|
| --input | inFile |
| --output | outFile |
| --input | input |
| --output | output |
| --robot-name | robotName |
| --normal | normal |
| --box-collision | boxCollision |
Expand All @@ -77,25 +81,46 @@ The command line arguments available from the terminal are also available from t
| --link-to-def | linkToDef |
| --joint-to-def | jointToDef |

In Python, you can convert a URDF file by passing its path as an argument to the `convertUrdfFile()` function or directly by passing its content as an argument to the `convertUrdfContent()` function.

#### Convert into Webots PROTO files

```
from urdf2webots.importer import convert2urdf
convert2urdf('MY_PATH/MY_URDF.urdf')
from urdf2webots.importer import convertUrdfFile
convertUrdfFile(input = 'MY_PATH/MY_URDF.urdf')
```

or

```
import pathlib
from urdf2webots.importer import convertUrdfContent
robot_description = pathlib.Path('MY_PATH/MY_URDF.urdf').read_text()
convertUrdfContent(input = robot_description)
```

#### Convert into Webots Robot node strings

```
from urdf2webots.importer import convert2urdf
convert2urdf('MY_PATH/MY_URDF.urdf', isProto=False)
from urdf2webots.importer import convertUrdfFile
convertUrdfFile(input = 'MY_PATH/MY_URDF.urdf', robotName="myRobot")
```

or

```
import pathlib
from urdf2webots.importer import convertUrdfContent
robot_description = pathlib.Path('MY_PATH/MY_URDF.urdf').read_text()
convertUrdfContent(input = robot_description, robotName="myRobot")
```

### In-Depth Tutorial
Check out [this tutorial](./docs/tutorial.md) for a more in-depth, step by step instruction, on how to:
- Generate a URDF file from a ROS repository.
- Convert your URDF file to a Webots PROTO file.
- Load your converted model into Webots and make final adjustments.
- Convert your URDF file to a Webots Robot string and import it.


## Notes
Expand Down
13 changes: 11 additions & 2 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ You can achieve the exact same result as above by converting the URDF to a Webot
First import the library:

```
from urdf2webots.importer import convert2urdf
from urdf2webots.importer import convertUrdfFile
```

Then you will need to get the node where you will insert your robot. The most suitable place is at the last position in the scene tree.
Expand All @@ -135,6 +135,15 @@ children_field = root_node.getField('children')
Finally you can convert your URDF file and add its corresponding robot to the simulation (be sure to set `robotName`, otherwise the tool will convert the URDF into a PROTO file):

```
robot_string = convert2urdf(inFile=model.urdf, robotName="MyRobotName")
robot_string = convertUrdfFile(input="model.urdf", robotName="MyRobotName")
children_field.importMFNodeFromString(-1, robot_string)
```

Note that you can also convert directly the content of your URDF file instead with:

```
import pathlib
from urdf2webots.importer import convertUrdfContent
robot_description = pathlib.Path("model.urdf").read_text()
convertUrdfContent(input=robot_description, robotName="myRobot")
```
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='urdf2webots',
version='1.0.14',
version='1.0.15',
author='Cyberbotics',
author_email='[email protected]',
description='A converter between URDF and PROTO files.',
Expand Down
63 changes: 49 additions & 14 deletions tests/test_export.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"""Test module of the urdf2webots script."""
import io
import os
import pathlib
import shutil
import sys
import unittest
import shutil
from urdf2webots.importer import convert2urdf

from urdf2webots.importer import convertUrdfContent, convertUrdfFile

testDirectory = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
sourceDirectory = os.path.join(testDirectory, 'sources')
resultDirectory = os.path.join(testDirectory, 'results')
expectedDirectory = os.path.join(testDirectory, 'expected')
urdf2webotsPath = os.path.abspath(os.path.join(testDirectory, '..', 'urdf2webots/importer.py'))

human_file_path = os.path.join(sourceDirectory, 'gait2392_simbody/urdf/human.urdf')

modelPathsProto = [
{
'input': os.path.join(sourceDirectory, 'motoman/motoman_sia20d_support/urdf/sia20d.urdf'),
Expand All @@ -19,7 +24,7 @@
'arguments': '--multi-file --tool-slot=tool0 --rotation="1 0 0 0" --init-pos="[0.1, -0.1, 0.2]"'
},
{
'input': os.path.join(sourceDirectory, 'gait2392_simbody/urdf/human.urdf'),
'input': human_file_path,
'output': os.path.join(resultDirectory, 'Human.proto'),
'expected': [os.path.join(expectedDirectory, 'Human.proto')],
'arguments': ''
Expand All @@ -32,29 +37,38 @@
}
]

modelContentProto = [
{
'input': pathlib.Path(human_file_path).read_text(),
'output': os.path.join(resultDirectory, 'Human.proto'),
'expected': [os.path.join(expectedDirectory, 'Human.proto')],
'arguments': ''
}
]

modelPathsRobotString = [
{
'input': os.path.join(sourceDirectory, 'kuka_lbr_iiwa_support/urdf/model.urdf'),
'output': os.path.join(resultDirectory, 'KukaLbrIiwa14R820.txt'),
'expected': [os.path.join(expectedDirectory, 'KukaLbrIiwa14R820.txt')],
'robotName': 'kuka',
'translation': '0 0 2',
'rotation': '1 0 0 -1.5708',
'rotation': '1 0 0 -1.5708'
}
]


def fileCompare(file1, file2):
def fileCompare(file1, file2, checkPaths=True):
"""Compare content of two files."""
with open(file1) as f1, open(file2) as f2:
for i, (line1, line2) in enumerate(zip(f1, f2)):
if line1.startswith('# Extracted from:') and line2.startswith('# Extracted from:'):
if line1.startswith('# Extracted from') and line2.startswith('# Extracted from'):
# This line may differ.
continue
elif line1.startswith('#VRML_SIM') and line2.startswith('#VRML_SIM'):
# This line may differ according to Webots version used
continue
elif 'CI' not in os.environ and '/home/runner/work/' in line2:
elif not checkPaths or ('CI' not in os.environ and '/home/runner/work/' in line2):
# When testing locally, the paths may differ.
continue
elif line1 != line2:
Expand All @@ -71,10 +85,11 @@ class TestScript(unittest.TestCase):
def setUp(self):
"""Cleanup results directory."""
shutil.rmtree(resultDirectory, ignore_errors=True)
os.makedirs(resultDirectory)

def test_script_produces_the_correct_result(self):
"""Test that urdf2webots produces an expected result."""
print("Proto files tests...")
def testInputFileOutputProto(self):
"""Test that urdf2webots produces an expected PROTO file using URDF file as input."""
print('Start tests with input "URDF file" and output "PROTO file"...')
for paths in modelPathsProto:
command = ('%s %s --input=%s --output=%s %s' %
(sys.executable, urdf2webotsPath, paths['input'], paths['output'], paths['arguments']))
Expand All @@ -84,11 +99,31 @@ def test_script_produces_the_correct_result(self):
self.assertTrue(fileCompare(expected.replace('expected', 'results'), expected),
msg='Expected result mismatch when exporting "%s"' % paths['input'])

print("Robot node strings tests...")
def testInputContentOutputProto(self):
"""Test that urdf2webots produces an expected PROTO file using URDF content as input."""
print('Start tests with input "URDF content" and output "PROTO file"...')
for paths in modelContentProto:
convertUrdfContent(input=paths['input'], output=paths['output'])
for expected in paths['expected']:
self.assertTrue(fileCompare(expected.replace('expected', 'results'), expected, checkPaths=False),
msg='Expected result mismatch when exporting input to "%s"' % paths['output'])

def testInputContentStdinOutputProto(self):
"""Test that urdf2webots produces an expected PROTO file using URDF content in the stdin buffer as input."""
print('Start tests with input "URDF content" and output "PROTO file"...')
for paths in modelContentProto:
sys.stdin = io.StringIO(paths['input'])
convertUrdfFile(output=paths['output'])
for expected in paths['expected']:
self.assertTrue(fileCompare(expected.replace('expected', 'results'), expected, checkPaths=False),
msg='Expected result mismatch when exporting input to "%s"' % paths['output'])

def testInputFileOutputRobotString(self):
"""Test that urdf2webots produces an expected Robot node string using URDF file as input."""
print('Start tests with input "URDF file" and output "Robot node strings"...')
for paths in modelPathsRobotString:
robot_string = convert2urdf(inFile=paths['input'], robotName=paths['robotName'],
initTranslation=paths['translation'], initRotation=paths['rotation'])
f = open(paths['output'], "a")
robot_string = convertUrdfFile(input=paths['input'], robotName=paths['robotName'], initTranslation=paths['translation'], initRotation=paths['rotation'])
f = open(paths['output'], 'w')
f.write(robot_string)
f.close()
for expected in paths['expected']:
Expand Down
Loading

0 comments on commit 0ce4d72

Please sign in to comment.