-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from KazuyaOguma18/feat-math-expression
feat: 数式表現によりshared, axis, buttonの値を抽出可能に
- Loading branch information
Showing
23 changed files
with
216 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import rclpy.logging | ||
|
||
from .action import Action | ||
from ..shared import Shared | ||
|
||
|
||
class SharedValue(Action): | ||
NAME = "shared_value" | ||
|
||
def __init__(self, definition, node): | ||
super(SharedValue, self).__init__(definition, node) | ||
Action.actions[self.__class__.NAME] = self.__class__ | ||
|
||
self.__key = self.get_required("key") | ||
|
||
self.__value = self.get("value") | ||
|
||
self.__step = self.get("step", 0) | ||
|
||
self.__enable_button = self.get("enable_button") | ||
|
||
if self.has("initial"): | ||
Shared.add(self.__key, self.get("initial", self.__value)) | ||
|
||
def execute(self, named_joy=None): | ||
if not named_joy or not self.__enable_button: | ||
Shared.update(self.__key, self.__value) | ||
rclpy.logging.get_logger("shared_value").info( | ||
"{0} : {1}".format(self.__key, self.__value) | ||
) | ||
return | ||
|
||
named_buttons = named_joy["buttons"] | ||
active_button = ( | ||
named_buttons[self.__enable_button].value | ||
if self.__enable_button in named_joy["buttons"] | ||
else None | ||
) | ||
if active_button: | ||
value = self.__next_value__() | ||
Shared.update(self.__key, value) | ||
|
||
rclpy.logging.get_logger("shared_value").info("{0} : {1}".format(self.__key, value)) | ||
|
||
def __next_value__(self): | ||
if self.__value: | ||
return self.__value | ||
|
||
current_val = Shared.get(self.__key) | ||
|
||
return current_val + self.__step | ||
|
||
|
||
Action.register_preset(SharedValue) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from functools import partial | ||
import re | ||
|
||
import sympy | ||
|
||
from .shared import Shared | ||
|
||
|
||
class MathExpression: | ||
|
||
def __init__(self): | ||
pass | ||
|
||
@staticmethod | ||
def expressions(values: dict, named_buttons, named_axes) -> dict: | ||
for key, value in values.items(): | ||
success = True | ||
if isinstance(value, dict): | ||
values[key], success = MathExpression.expressions(value, named_buttons, named_axes) | ||
if isinstance(value, str): | ||
values[key], success = MathExpression.__expression__( | ||
value, named_buttons, named_axes | ||
) | ||
if not success: | ||
return values, False | ||
return values, True | ||
|
||
@staticmethod | ||
def __shared__(key): | ||
return Shared.get(str(key)) | ||
|
||
@staticmethod | ||
def __axis__(axis, named_axes): | ||
axis_str = str(axis) | ||
return named_axes[axis_str].value if axis_str in named_axes else 0 | ||
|
||
@staticmethod | ||
def __button__(button, named_buttons): | ||
return 1 if named_buttons[str(button)].value else 0 | ||
|
||
@staticmethod | ||
def __expression__(value, named_buttons, named_axes): | ||
variables = { | ||
"shared": MathExpression.__shared__, | ||
"axis": partial(MathExpression.__axis__, named_axes=named_axes), | ||
"button": partial(MathExpression.__button__, named_buttons=named_buttons), | ||
} | ||
|
||
match = re.match(r"\${(.+)}", value) | ||
if match: | ||
try: | ||
expr = sympy.sympify(match.group(1), locals=variables) | ||
return expr, True | ||
except (sympy.SympifyError, ValueError, AttributeError): | ||
return value, False | ||
return value, True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.