Skip to content

Commit

Permalink
Updated find package implementation to be consistent with control / d…
Browse files Browse the repository at this point in the history
…escription yaml parameters
  • Loading branch information
hilary-luo committed Jan 3, 2024
1 parent a9a142b commit 115828a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 100 deletions.
70 changes: 70 additions & 0 deletions clearpath_config/common/types/package_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Software License Agreement (BSD)
#
# @author Luis Camero <lcamero@clearpathrobotics.com>
# @copyright (c) 2023, Clearpath Robotics, Inc., All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Clearpath Robotics nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from clearpath_config.common.types.file import File


class PackagePath:
PACKAGE = "package"
PATH = "path"

def __init__(
self,
package: str = None,
path: str = None,
) -> None:
self.package = package
self.path = File.clean(path, make_abs=False)

def from_dict(self, config: dict) -> None:
if self.PACKAGE in config:
self.package = config[self.PACKAGE]
if self.PATH in config:
self.path = config[self.PATH]

def to_dict(self) -> dict:
return {
self.PACKAGE: self.package,
self.PATH: self.path,
}

@property
def package(self) -> str:
return self._package

@package.setter
def package(self, value: str) -> None:
self._package = value

@property
def path(self) -> str:
return self._path

@path.setter
def path(self, value: str) -> None:
self._path = value
4 changes: 1 addition & 3 deletions clearpath_config/links/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,7 @@ def add_mesh(
mesh: Mesh = None,
# By Parameters
name: str = None,
visual: str = Mesh.VISUAL,
package: str = Mesh.PACKAGE,
visual: dict = Mesh.VISUAL,
parent: str = Accessory.PARENT,
xyz: List[float] = Accessory.XYZ,
rpy: List[float] = Accessory.RPY,
Expand All @@ -512,7 +511,6 @@ def add_mesh(
mesh = Mesh(
name=name,
visual=visual,
package=package,
parent=parent,
xyz=xyz,
rpy=rpy,
Expand Down
32 changes: 11 additions & 21 deletions clearpath_config/links/types/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,22 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os

from clearpath_config.common.types.accessory import Accessory
from clearpath_config.common.types.file import File
from clearpath_config.links.types.link import BaseLink
from clearpath_config.common.types.package_path import PackagePath
from typing import List


class Mesh(BaseLink):
LINK_TYPE = "mesh"
VISUAL = "empty.stl"
PACKAGE = ""
VISUAL = ""
# COLLISION = "empty.stl"

def __init__(
self,
name: str,
parent: str = Accessory.PARENT,
visual: str = VISUAL,
package: str = PACKAGE,
visual: dict = VISUAL,
# collision: float = COLLISION,
xyz: List[float] = Accessory.XYZ,
rpy: List[float] = Accessory.RPY,
Expand All @@ -60,8 +56,8 @@ def __init__(
offset_rpy
)

self.visual: str = Mesh.VISUAL
self.set_visual(visual, package)
self.visual: PackagePath = PackagePath(Mesh.VISUAL)
self.set_visual(visual)

def to_dict(self) -> dict:
d = super().to_dict()
Expand All @@ -71,17 +67,11 @@ def to_dict(self) -> dict:
def from_dict(self, d: dict) -> None:
super().from_dict(d)
if 'visual' in d:
if 'package' in d:
self.set_visual(d['visual'], d['package'])
else:
self.set_visual(d['visual'])
self.set_visual(d['visual'])

def set_visual(self, visual: str, package: str = "") -> None:
if package:
self.visual = os.path.join("$(find " + package + ")",
File.clean(visual, make_abs=False))
else:
self.visual = File.clean(visual, make_abs=True)
def set_visual(self, visual: dict) -> None:
if visual:
self.visual.from_dict(visual)

def get_visual(self) -> str:
return self.visual
def get_visual(self) -> dict:
return self.visual.to_dict()
44 changes: 9 additions & 35 deletions clearpath_config/platform/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os

from clearpath_config.common.types.config import BaseConfig
from clearpath_config.common.types.file import File
from clearpath_config.common.types.package_path import PackagePath
from clearpath_config.common.types.platform import Platform
from clearpath_config.common.utils.dictionary import (
flatten_dict,
Expand Down Expand Up @@ -139,7 +138,6 @@ def __new__(cls, platform: str) -> dict:
class ExtrasConfig(BaseConfig):

EXTRAS = "extras"
URDF_PACKAGE = "urdf_package"
URDF = "urdf"
ROS_PARAMETERS = "ros_parameters"

Expand All @@ -156,7 +154,6 @@ class ExtrasConfig(BaseConfig):

TEMPLATE = {
EXTRAS: {
URDF_PACKAGE: URDF_PACKAGE,
URDF: URDF,
ROS_PARAMETERS: {
PLATFORM_VELOCITY_CONTROLLER: {
Expand All @@ -178,16 +175,14 @@ class ExtrasConfig(BaseConfig):
KEYS[ROS_PARAMETERS] = ".".join([EXTRAS, ROS_PARAMETERS])

DEFAULTS = {
URDF_PACKAGE: "",
URDF: "empty.urdf.xacro",
URDF: "",
ROS_PARAMETERS: ROSParameterDefaults(BaseConfig.get_platform_model()),
}

def __init__(
self,
config: dict = {},
urdf_package: str = DEFAULTS[URDF_PACKAGE],
urdf: str = DEFAULTS[URDF],
urdf: dict = DEFAULTS[URDF],
ros_parameters: dict = {},
) -> None:
# ROS Parameter Setter Template
Expand All @@ -204,14 +199,12 @@ def __init__(
}
# Setter Template
self.setters = {
self.KEYS[self.URDF_PACKAGE]: ExtrasConfig.urdf_package,
self.KEYS[self.URDF]: ExtrasConfig.urdf,
self.KEYS[self.ROS_PARAMETERS]: ExtrasConfig.ros_parameters,
}
# Initialization
self._init_ros_parameter()
self._config = {}
self.urdf_package = urdf_package
self.urdf = urdf
self.ros_parameters = ros_parameters
# Set from Config
Expand All @@ -222,39 +215,20 @@ def update(self, serial_number: bool = False) -> None:
self._update_ros_parameter()

@property
def urdf_package(self) -> str:
urdf_package = (None if self._is_default(self._urdf_package, self.URDF_PACKAGE)
else str(self._urdf_package))
self.set_config_param(
key=self.KEYS[self.URDF_PACKAGE],
value=urdf_package
)
return urdf_package

@urdf_package.setter
def urdf_package(self, value: str) -> None:
if value is None or value == "None":
return
self._urdf_package = value

@property
def urdf(self) -> str:
urdf = None if self._is_default(self._urdf, self.URDF) else str(self._urdf)
def urdf(self) -> dict:
urdf = None if self._is_default(self._urdf, self.URDF) else dict(self._urdf.to_dict())
self.set_config_param(
key=self.KEYS[self.URDF],
value=urdf
value=urdf,
)
return urdf

@urdf.setter
def urdf(self, value: str) -> None:
def urdf(self, value: dict) -> None:
if value is None or value == "None":
return
elif self._urdf_package:
self._urdf = os.path.join("$(find " + self._urdf_package + ")",
File.clean(path=str(value), make_abs=False))
else:
self._urdf = File.clean(path=str(value), make_abs=True)
self._urdf = PackagePath()
self._urdf.from_dict(value)

def _is_default(self, curr: str, key: str) -> bool:
return curr == str(File(self.DEFAULTS[key]))
Expand Down
42 changes: 1 addition & 41 deletions clearpath_config/platform/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,14 @@
# POSSIBILITY OF SUCH DAMAGE.
from clearpath_config.common.types.platform import Platform
from clearpath_config.common.types.config import BaseConfig
from clearpath_config.common.types.package_path import PackagePath
from clearpath_config.common.utils.dictionary import flip_dict
from clearpath_config.platform.battery import BatteryConfig
from clearpath_config.platform.extras import ExtrasConfig
from clearpath_config.platform.attachments.config import AttachmentsConfig
from clearpath_config.platform.attachments.mux import AttachmentsConfigMux


class PackagePath:
PACKAGE = "package"
PATH = "path"

def __init__(
self,
package: str = None,
path: str = None,
) -> None:
self.package = package
self.path = path

def from_dict(self, config: dict) -> None:
if self.PACKAGE in config:
self.package = config[self.PACKAGE]
if self.PATH in config:
self.path = config[self.PATH]

def to_dict(self) -> dict:
return {
self.PACKAGE: self.package,
self.PATH: self.path,
}

@property
def package(self) -> str:
return self._package

@package.setter
def package(self, value: str) -> None:
self._package = value

@property
def path(self) -> str:
return self._path

@path.setter
def path(self, value: str) -> None:
self._path = value


class DescriptionPackagePath(PackagePath):
MACRO = "macro"
PARAMETERS = "parameters"
Expand Down

0 comments on commit 115828a

Please sign in to comment.