Skip to content

Commit

Permalink
修改了UI,增加了一些节点
Browse files Browse the repository at this point in the history
  • Loading branch information
jayscoder committed Apr 1, 2024
1 parent 59d89df commit 3cfa822
Show file tree
Hide file tree
Showing 24 changed files with 1,644 additions and 305 deletions.
Binary file added README.assets/DEMO_Sequence 10-10 _ 100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added README.assets/image-20240401211552611.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added README.assets/image-20240401211609525.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ pybts --dir=logs --debug --host=localhost --port=10000

After running the appropriate command, you can open a web browser and navigate to `http://localhost:10000` to view and interact with the behavior tree visualizations and management tools provided by pybts.

![image-20240329031220580](README.assets/image-20240329031220580.png)
![DEMO_Sequence 10-10 _ 100](README.assets/DEMO_Sequence%20%2010-10%20_%20100.png)

![image-20240329031233459](README.assets/image-20240329031233459.png)
![image-20240401211609525](README.assets/image-20240401211609525.png)



![image-20240401211552611](README.assets/image-20240401211552611.png)


## Acknowledgements
Expand Down
145 changes: 145 additions & 0 deletions demo_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import logging
import time

import py_trees
from py_trees import common
from py_trees.common import Status

import pybts


class ToggleStatus(pybts.Condition):
def __init__(self, status_list: list, name: str = ''):
super().__init__(name=name)
self.init_count = 0
self.update_count = 0
self.terminate_count = 0
self.status_list = status_list

def update(self) -> Status:
self.update_count += 1
return self.status_list[(self.update_count - 1) % len(self.status_list)]

def to_data(self):
return {
'init_count' : self.init_count,
'update_count' : self.update_count,
'terminate_count': self.terminate_count,
'status_list' : self.status_list
}

def terminate(self, new_status: common.Status) -> None:
self.terminate_count += 1

def initialise(self) -> None:
self.init_count += 1


class Success(pybts.Success):
def __init__(self, name: str = ''):
super().__init__(name=name)
self.update_count = 0
self.stop_count = 0

def update(self) -> Status:
self.update_count += 1
return super().update()

def to_data(self):
return {
'update_count': self.update_count,
'stop_count' : self.stop_count,
}

def stop(self, new_status: common.Status) -> None:
super().stop(new_status)
self.stop_count += 1


class Failure(pybts.Failure):
def __init__(self, name: str = ''):
super().__init__(name=name)
self.update_count = 0
self.stop_count = 0

def update(self) -> Status:
self.update_count += 1
return super().update()

def to_data(self):
return {
'update_count': self.update_count,
'stop_count' : self.stop_count,
}

def stop(self, new_status: common.Status) -> None:
super().stop(new_status)
self.stop_count += 1


class Running(pybts.Running):
def __init__(self, name: str = ''):
super().__init__(name=name)
self.update_count = 0
self.stop_count = 0

def update(self) -> Status:
self.update_count += 1
return super().update()

def to_data(self):
return {
'update_count': self.update_count,
'stop_count' : self.stop_count,
}

def stop(self, new_status: common.Status) -> None:
super().stop(new_status)
self.stop_count += 1


_name_index = -1


def new_name():
global _name_index
_name_index += 1
return str(_name_index)


if __name__ == '__main__':
pybts.logging.level = pybts.logging.Level.DEBUG

status_list_1 = [Status.SUCCESS, Status.RUNNING, Status.RUNNING, Status.FAILURE]
node = pybts.Selector(
children=[
pybts.Parallel(
children=[
ToggleStatus(name=new_name(), status_list=status_list_1),
ToggleStatus(name=new_name(), status_list=status_list_1),
ToggleStatus(name=new_name(), status_list=status_list_1),
],
),
pybts.Parallel(
children=[
ToggleStatus(name=new_name(), status_list=status_list_1),
ToggleStatus(name=new_name(), status_list=status_list_1),
ToggleStatus(name=new_name(), status_list=status_list_1),
],
)
]
)

tree = pybts.Tree(node, name='DEMO_Sequence')
tree.setup()
board = pybts.Board(tree=tree, log_dir='./logs')
board.clear()

for r in range(10):
tree.reset()
for i in range(10):
tree.tick()
board.track()
time.sleep(0.5)

print(tree.tip().name)
2 changes: 2 additions & 0 deletions pybts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from . import board
from .tree import Tree
from .node import *
from .composites import *
from .board import Board
from .builder import Builder
from .decorators import *
from py_trees import logging
23 changes: 14 additions & 9 deletions pybts/board.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import os
import time
import typing

from pybts import utility
from pybts.tree import Tree
import jsonpickle


class Board:
Expand All @@ -26,17 +27,23 @@ def track(self, info: dict = None):
'id' : self.track_id,
'step' : self.tree.count,
'round': self.tree.round,
'stage': f'{self.tree.round}-{self.tree.count}',
'info' : info,
'tree' : utility.bt_to_json(self.tree.root),
'time' : int(time.time() * 1000) # ms时间戳
}

json_text = jsonpickle.dumps(json_data, indent=4)
history_path = os.path.join(self.history_dir, f'{self.track_id}.json')
with open(history_path, 'w') as f:
f.write(json_text)
tree_data = utility.bt_to_json(self.tree.root)
try:
utility.json_dump({
**json_data,
'tree': tree_data
}, f, ensure_ascii=False)
except Exception as e:
print(e)
raise e
with open(self.current_path, 'w') as f:
f.write(json_text)
utility.json_dump(json_data, f, ensure_ascii=False)

def clear(self):
self.track_id = 0
Expand All @@ -56,7 +63,5 @@ def iterate(self) -> typing.Iterator[dict]:
if filename.endswith('.json'):
filepath = os.path.join(self.history_dir, filename)
with open(filepath, 'r', encoding='utf') as f:
json_data = jsonpickle.loads(f.read())
json_data = utility.json_loads(f.read())
yield json_data


Loading

0 comments on commit 3cfa822

Please sign in to comment.