-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
478c6dd
commit d5a2e33
Showing
1 changed file
with
65 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import logging | ||
from dataclasses import dataclass | ||
|
||
import py_trees | ||
from py_trees.composites import Sequence | ||
|
||
from epics import caget | ||
|
||
from beams.behavior_tree.action_node import ActionNode, wrapped_action_work | ||
|
||
from beams.tree_config.base import BaseItem | ||
from beams.tree_config.value import BlackBoardValue, EPICSValue | ||
from beams.tree_config.action import SetPVActionItem | ||
from beams.tree_config.condition import BinaryConditionItem, ConditionOperator | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@dataclass | ||
class ResetIOCItem(BaseItem): | ||
ioc_prefix: str = "" | ||
HEARTBEAT_POSTFIX = ":HEARTBEART" | ||
SYSRESET_POSTFIX = ":SysReset" | ||
HEARTBEAT_KEY_NAME = "heartbeat" | ||
|
||
def __post_init__(self): | ||
# non dataclass PVss | ||
self.hbeat_val = BlackBoardValue(bb_name=f"{self.ioc_prefix}_reset", | ||
key_name=self.HEARTBEAT_KEY_NAME) | ||
self.name = f"{self.ioc_prefix}_reset_tree" | ||
|
||
def get_tree(self) -> Sequence: | ||
def check_acquired_current_hbeat(): | ||
self.hbeat_val.get_value() is not None | ||
|
||
# get the current heartbeat of IOC | ||
@wrapped_action_work(loop_period_sec=3.0) | ||
def cache_hbeat_wfunc(): | ||
bb_client = py_trees.blackboard.Client(name=self.bb_name) | ||
bb_client.register_key(key=self.HEARTBEAT_KEY_NAME, access=py_trees.common.Access.WRITE) | ||
|
||
current_hbeat = caget(self.ioc_prefix+self.HEARTBEAT_POSTFIX) | ||
bb_client.set(f"{self.HEARTBEAT_KEY_NAME}", current_hbeat) | ||
logger.debug(f"<<-- Aquired ioc: {self.ioc_prefix} hbeat count: {current_hbeat} and put to blackboard") | ||
|
||
cache_current_heartbeat = ActionNode(name=f"{self.ioc_prefix}_hbeat_cache", | ||
work_func=cache_hbeat_wfunc, | ||
completion_condition=check_acquired_current_hbeat | ||
) | ||
|
||
# send the reset command | ||
reset_success_termination_condiiton = BinaryConditionItem( | ||
left_value=EPICSValue(pv_name=f"{self.ioc_prefix+self.HEARTBEAT_POSTFIX}"), | ||
right_value=self.hbeat_val, | ||
operator=ConditionOperator.less) | ||
send_reset = SetPVActionItem(name=f"reset_{self.ioc_prefix}", | ||
pv=f"{self.ioc_prefix}:SysReset", | ||
value=1, | ||
loop_period_sec=3.0, # this is greater than work_timeout period, should only happen once. | ||
termination_check=reset_success_termination_condiiton) | ||
|
||
root = Sequence(name=self.name, | ||
memory=False, | ||
children=[cache_current_heartbeat, send_reset]) | ||
return root |