8000 Use data files from data package · Russ76/robotics-toolbox-python@604cb56 · GitHub
[go: up one dir, main page]

Skip to content

Commit 604cb56

Browse files
committed
Use data files from data package
1 parent 411ad82 commit 604cb56

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

roboticstoolbox/robot/DHRobot.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
from collections import namedtuple
7+
from roboticstoolbox.tools.data import path_to_datafile
78

89
import numpy as np
910
from roboticstoolbox.robot.Robot import Robot # DHLink
@@ -62,6 +63,7 @@ class DHRobot(Robot):
6263
def __init__(
6364
self,
6465
L,
66+
meshdir=None,
6567
**kwargs):
6668

6769
# Verify L
@@ -97,6 +99,16 @@ def __init__(
9799
if not all([link.mdh == self.mdh for link in self.links]):
98100
raise ValueError('Robot has mixed D&H link conventions')
99101

102+
# load meshes if required
103+
if meshdir is not None:
104+
self.meshdir = path_to_datafile(meshdir)
105+
self.basemesh = self.meshdir / "link0.stl"
106+
for j, link in enumerate(self._links, start=1):
107+
link.mesh = self.meshdir / "link{:d}.stl".format(j)
108+
else:
109+
self.basemesh = None
110+
111+
100112
# rne parameters
101113
self._rne_ob = None
102114

roboticstoolbox/robot/ERobot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from roboticstoolbox.tools import URDF
2323
from roboticstoolbox.robot.Robot import Robot
2424
from roboticstoolbox.robot.Gripper import Gripper
25+
from roboticstoolbox.tools.data import path_to_datafile
26+
2527
from pathlib import PurePath, PurePosixPath
2628
from ansitable import ANSITable, Column
2729
from spatialmath import SpatialAcceleration, SpatialVelocity, \
@@ -391,10 +393,10 @@ def urdf_to_ets_args(self, file_path, tld=None):
391393
"""
392394

393395
# get the path to the class that defines the robot
394-
classpath = sys.modules[self.__module__].__file__
396+
base_path = path_to_datafile('xacro')
395397
# print("*** urdf_to_ets_args: ", classpath)
396398
# add on relative path to get to the URDF or xacro file
397-
base_path = PurePath(classpath).parent.parent / 'URDF' / 'xacro'
399+
# base_path = PurePath(classpath).parent.parent / 'URDF' / 'xacro'
398400
file_path = base_path / PurePosixPath(file_path)
399401
name, ext = splitext(file_path)
400402

roboticstoolbox/robot/Robot.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ def __init__(
5151
base=None,
5252
tool=None,
5353
gravity=None,
54-
meshdir=None,
5554
keywords=(),
5655
symbolic=False):
5756

@@ -61,7 +60,6 @@ def __init__(
6160
self.symbolic = symbolic
6261
self.base = base
6362
self.tool = tool
64-
self.basemesh = None
6563
self._reach = None
6664

6765
if keywords is not None and not isinstance(keywords, (tuple, list)):
@@ -94,14 +92,6 @@ def __init__(
9492

9593
self._dynchanged = True
9694

97-
# this probably should go down to DHRobot
98-
if meshdir is not None:
99-
classpath = sys.modules[self.__module__].__file__
100-
self.meshdir = PurePath(classpath).parent / PurePosixPath(meshdir)
101-
self.basemesh = self.meshdir / "link0.stl"
102-
for j, link in enumerate(self._links, start=1):
103-
link.mesh = self.meshdir / "link{:d}.stl".format(j)
104-
10595
# URDF Parser Attempt
10696
# # Search mesh dir for meshes
10797
# if urdfdir is not None:

roboticstoolbox/tools/data.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from pathlib import Path
22
import sys
3+
import importlib
4+
5+
36

47
def loadmat(filename):
58
"""
@@ -50,15 +53,15 @@ def loaddata(filename, handler, **kwargs):
5053
path = path_to_datafile(filename)
5154
return handler(path, **kwargs)
5255

53-
def path_to_datafile(filename):
56+
def path_to_datafile(filename, local=False):
5457
"""
5558
Get absolute path to datafile
5659
5760
:param filename: pathname of datafile
5861
:type filename: str
5962
:raises FileNotFoundError: File does not exist
6063
:return: Absolute path
61-
:rtype: str
64+
:rtype: Path
6265
6366
If ``filename`` contains no path specification eg. ``map1.mat`` it will
6467
first attempt to locate the file within the ``roboticstoolbox/data``
@@ -76,26 +79,29 @@ def path_to_datafile(filename):
7679

7780
filename = Path(filename)
7881

79-
if filename.parent == Path():
80-
# just a filename, no path, assume it is in roboticstoolbox/data
82+
if local:
83+
# check if file is in user's local filesystem
8184

82-
p = Path(__file__).resolve().parent.parent / 'data' / filename
85+
p = filename.expanduser()
86+
p = p.resolve()
8387
if p.exists():
84-
return str(p.resolve())
88+
return str(p)
8589

86-
p = filename.expanduser()
87-
p = p.resolve()
88-
if not p.exists():
89-
raise FileNotFoundError(f"File '{p}' does not exist")
90-
return str(p)
90+
# assume it is in rtbdata
91+
92+
rtbdata = importlib.import_module("rtbdata")
93+
root = Path(rtbdata.__path__[0])
94+
95+
path = root / filename
96+
if path.exists():
97+
return path.resolve()
98+
else:
99+
raise ValueError(f"file {filename} not found locally or in rtbdata")
91100

92101
if __name__ == "__main__":
93102

94103
a = loadmat("map1.mat")
95104
print(a)
96-
a = loadmat("roboticstoolbox/data/map1.mat")
97-
print(a)
98-
a = loadmat("roboticstoolbox/data/../data/map1.mat")
99-
print(a)
100-
a = loadmat("~/code/robotics-toolbox-python/roboticstoolbox/data/map1.mat")
105+
a = loadmat("data/map1.mat")
101106
print(a)
107+

0 commit comments

Comments
 (0)
0