Skip to content

Commit

Permalink
ENH: Successfully replumbed action node work to be facilliated by base
Browse files Browse the repository at this point in the history
Worker class
  • Loading branch information
joshc-slac committed Aug 13, 2024
1 parent 6d6518b commit b289496
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 29 deletions.
1 change: 0 additions & 1 deletion beams/behavior_tree/ActionNode.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def setup(self, **kwargs: int) -> None:
)

# Having this in setup means the workthread should always be running.
print("LAUNCHING JAWN")
self.worker.start_work()
atexit.register(
self.worker.stop_work
Expand Down
4 changes: 3 additions & 1 deletion beams/behavior_tree/ActionWorker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
A worker specialized to execute ActionNode work functions
"""
from typing import Callable, Any, Optional

from epics.multiproc import CAProcess
Expand All @@ -18,6 +21,5 @@ def __init__(self,
work_func=work_func,
proc_type=CAProcess,
add_args=(comp_cond, volatile_status))
print(f"YO SELF: {self}")
self.comp_condition = comp_cond
self.__volatile_status__ = volatile_status
2 changes: 1 addition & 1 deletion beams/behavior_tree/CheckAndDo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class CheckAndDo(py_trees.composites.Selector):
def __init__(self, name: str, check: ConditionNode, do: ActionNode) -> None:
super().__init__(name, memory=True)
super().__init__(name, memory=False)
self.name = name
self.check = check
self.do = do
Expand Down
6 changes: 3 additions & 3 deletions beams/sequencer/helpers/Worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ def start_work(self):
logging.error("Already working, not starting work")
return
self.do_work.value = True
print("JEEZY")
print(self.work_func)
# breakpoint()
self.work_proc.start()
logging.debug(f"Starting work on: {self.work_proc.pid}")
print(f"Starting work on: {self.work_proc.pid}")

def stop_work(self):
logging.info(f"Calling stop work on: {self.work_proc.pid}")
if (not self.do_work.value):
logging.error("Not working, not stopping work")
return
self.do_work.value = False
logging.info(f"Sending terminate signal to{self.work_proc.pid}")
# Send kill signal to work process. # TODO: the exact location of this is important. Reflect
self.work_proc.terminate()
if (self.stop_func is not None):
self.stop_func()

Expand Down
6 changes: 3 additions & 3 deletions beams/tests/artifacts/eggs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
"name": "self_test",
"description": "",
"check": {
"name": "",
"name": "self_test_check",
"description": "",
"pv": "PERC:COMP",
"value": 100,
"operator": "ge"
},
"do": {
"IncPVActionItem": {
"name": "",
"name": "self_test_do",
"description": "",
"loop_period_sec": 1.0,
"loop_period_sec": 0.01,
"pv": "PERC:COMP",
"increment": 10,
"termination_check": {
Expand Down
6 changes: 3 additions & 3 deletions beams/tests/artifacts/eggs2.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
"name": "ret_find",
"description": "",
"check": {
"name": "",
"name": "ret_find_check",
"description": "",
"pv": "RET:FOUND",
"value": 1,
"operator": "ge"
},
"do": {
"SetPVActionItem": {
"name": "",
"name": "ret_find_do",
"description": "",
"loop_period_sec": 1.0,
"loop_period_sec": 0.01,
"pv": "RET:FOUND",
"value": 1,
"termination_check": {
Expand Down
2 changes: 0 additions & 2 deletions beams/tests/test_leaf_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from beams.behavior_tree.ActionNode import ActionNode
from beams.behavior_tree.ConditionNode import ConditionNode

import pytest


class TestTask:
def test_action_node(self, capsys):
Expand Down
14 changes: 8 additions & 6 deletions beams/tests/test_tree_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def test_tree_obj_execution(request):
tree.root.status not in (py_trees.common.Status.SUCCESS,
py_trees.common.Status.FAILURE)
):
tree.tick()
time.sleep(0.05)
for n in tree.root.tick():
print(n)
time.sleep(0.05)

rel_val = caget("PERC:COMP")
assert rel_val >= 100
Expand All @@ -56,10 +57,11 @@ def test_father_tree_execution(request):
py_trees.common.Status.FAILURE)
and ct < 50
):
ct += 1
print((tree.root.status, tree.root.status, ct))
tree.tick()
time.sleep(0.05)
for n in tree.root.tick():
ct += 1
print(n)
print((tree.root.status, tree.root.status, ct))
time.sleep(0.05)

check_insert = caget("RET:INSERT")

Expand Down
18 changes: 9 additions & 9 deletions beams/tree_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def get_tree(self) -> ActionNode:
wait_for_tick = Event()
wait_for_tick_lock = Lock()

def work_func(self):
def work_func(myself, comp_condition, volatile_status):
py_trees.console.logdebug(f"WAITING FOR INIT {os.getpid()} "
f"from node: {self.name}")
wait_for_tick.wait()
Expand All @@ -186,29 +186,29 @@ def work_func(self):
value = 0

# While termination_check is not True
while not self.comp_condition(): # TODO check work_gate.is_set()
while not comp_condition(): # TODO check work_gate.is_set()
py_trees.console.logdebug(
f"CALLING CAGET FROM {os.getpid()} from node: "
f"{self.name}"
)
value = caget(self.termination_check.pv)

if self.comp_condition():
self.volatile_status.set_value(py_trees.common.Status.SUCCESS)
if comp_condition():
volatile_status.set_value(py_trees.common.Status.SUCCESS)
py_trees.console.logdebug(
f"{self.name}: Value is {value}, BT Status: "
f"{self.volatile_status.get_value()}"
f"{volatile_status.get_value()}"
)

# specific caput logic to SetPVActionItem
caput(self.pv, self.value)
time.sleep(self.loop_period_sec)

# one last check
if self.comp_condition():
self.volatile_status.set_value(py_trees.common.Status.SUCCESS)
if comp_condition():
volatile_status.set_value(py_trees.common.Status.SUCCESS)
else:
self.volatile_status.set_value(py_trees.common.Status.FAILURE)
volatile_status.set_value(py_trees.common.Status.FAILURE)

comp_cond = self.termination_check.get_condition_function()

Expand All @@ -235,7 +235,7 @@ def get_tree(self) -> ActionNode:
wait_for_tick = Event()
wait_for_tick_lock = Lock()

def work_func(comp_condition, volatile_status):
def work_func(myself, comp_condition, volatile_status):
py_trees.console.logdebug(f"WAITING FOR INIT {os.getpid()} "
f"from node: {self.name}")
wait_for_tick.wait()
Expand Down

0 comments on commit b289496

Please sign in to comment.