Skip to content

Commit

Permalink
Python blackboard fixed
Browse files Browse the repository at this point in the history
blackboard should be used os in cpp, that is as a dictionary
  • Loading branch information
mgonzs13 committed Jul 13, 2024
1 parent 0445830 commit c2090a8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
20 changes: 14 additions & 6 deletions yasmin/tests/python/test_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self):
def execute(self, blackboard):
if self.counter < 3:
self.counter += 1
blackboard.foo_str = f"Counter: {self.counter}"
blackboard["foo_str"] = f"Counter: {self.counter}"
return "outcome1"
else:
return "outcome2"
Expand All @@ -46,11 +46,19 @@ def setUp(self):

self.sm = StateMachine(outcomes=["outcome4", "outcome5"])

self.sm.add_state("FOO", FooState(),
transitions={"outcome1": "BAR",
"outcome2": "outcome4"})
self.sm.add_state("BAR", BarState(),
transitions={"outcome2": "FOO"})
self.sm.add_state(
"FOO",
FooState(),
transitions={
"outcome1": "BAR",
"outcome2": "outcome4"
})
self.sm.add_state(
"BAR",
BarState(),
transitions={
"outcome2": "FOO"
})

def test_state_machine_get_states(self):

Expand Down
8 changes: 4 additions & 4 deletions yasmin_demo/yasmin_demo/action_client_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self) -> None:
def create_goal_handler(self, blackboard: Blackboard) -> Fibonacci.Goal:

goal = Fibonacci.Goal()
goal.order = blackboard.n
goal.order = blackboard["n"]
return goal

def response_handler(
Expand All @@ -49,7 +49,7 @@ def response_handler(
response: Fibonacci.Result
) -> str:

blackboard.fibo_res = response.sequence
blackboard["fibo_res"] = response.sequence
return SUCCEED

def print_feedback(
Expand All @@ -61,7 +61,7 @@ def print_feedback(


def print_result(blackboard: Blackboard) -> str:
print(f"Result: {blackboard.fibo_res}")
print(f"Result: {blackboard['fibo_res']}")
return SUCCEED


Expand Down Expand Up @@ -99,7 +99,7 @@ def main():

# create an initial blackoard
blackboard = Blackboard()
blackboard.n = 10
blackboard["n"] = 10

# execute FSM
outcome = sm(blackboard)
Expand Down
22 changes: 11 additions & 11 deletions yasmin_demo/yasmin_demo/nav_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ def __init__(self) -> None:
def create_goal_handler(self, blackboard: Blackboard) -> NavigateToPose.Goal:

goal = NavigateToPose.Goal()
goal.pose.pose = blackboard.pose
goal.pose.pose = blackboard["pose"]
goal.pose.header.frame_id = "map"
return goal


def create_waypoints(blackboard: Blackboard) -> str:
blackboard.waypoints = {
blackboard["waypoints"] = {
"entrance": [1.25, 6.30, -0.78, 0.67],
"bathroom": [4.89, 1.64, 0.0, 1.0],
"livingroom": [1.55, 4.03, -0.69, 0.72],
Expand All @@ -63,19 +63,19 @@ def create_waypoints(blackboard: Blackboard) -> str:


def take_random_waypoint(blackboard) -> str:
blackboard.random_waypoints = random.sample(
list(blackboard.waypoints.keys()),
blackboard.waypoints_num)
blackboard["random_waypoints"] = random.sample(
list(blackboard["waypoints"].keys()),
blackboard["waypoints_num"])
return SUCCEED


def get_next_waypoint(blackboard: Blackboard) -> str:

if not blackboard.random_waypoints:
if not blackboard["random_waypoints"]:
return END

wp_name = blackboard.random_waypoints.pop(0)
wp = blackboard.waypoints[wp_name]
wp_name = blackboard["random_waypoints"].pop(0)
wp = blackboard["waypoints"][wp_name]

pose = Pose()
pose.position.x = wp[0]
Expand All @@ -84,8 +84,8 @@ def get_next_waypoint(blackboard: Blackboard) -> str:
pose.orientation.z = wp[2]
pose.orientation.w = wp[3]

blackboard.pose = pose
blackboard.text = f"I have reached waypoint {wp_name}"
blackboard["pose"] = pose
blackboard["text"] = f"I have reached waypoint {wp_name}"

return HAS_NEXT

Expand Down Expand Up @@ -151,7 +151,7 @@ def main():

# execute FSM
blackboard = Blackboard()
blackboard.waypoints_num = 2
blackboard["waypoints_num"] = 2
outcome = sm(blackboard)
print(outcome)

Expand Down
12 changes: 6 additions & 6 deletions yasmin_demo/yasmin_demo/service_client_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def __init__(self) -> None:
def create_request_handler(self, blackboard: Blackboard) -> AddTwoInts.Request:

req = AddTwoInts.Request()
req.a = blackboard.a
req.b = blackboard.b
req.a = blackboard["a"]
req.b = blackboard["b"]
return req

def response_handler(
Expand All @@ -49,18 +49,18 @@ def response_handler(
response: AddTwoInts.Response
) -> str:

blackboard.sum = response.sum
blackboard["sum"] = response.sum
return "outcome1"


def set_ints(blackboard: Blackboard) -> str:
blackboard.a = 10
blackboard.b = 5
blackboard["a"] = 10
blackboard["b"] = 5
return SUCCEED


def print_sum(blackboard: Blackboard) -> str:
print(f"Sum: {blackboard.sum}")
print(f"Sum: {blackboard['sum']}")
return SUCCEED


Expand Down
4 changes: 2 additions & 2 deletions yasmin_demo/yasmin_demo/yasmin_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def execute(self, blackboard: Blackboard) -> str:

if self.counter < 3:
self.counter += 1
blackboard.foo_str = f"Counter: {self.counter}"
blackboard["foo_str"] = f"Counter: {self.counter}"
return "outcome1"
else:
return "outcome2"
Expand All @@ -51,7 +51,7 @@ def execute(self, blackboard: Blackboard) -> str:
print("Executing state BAR")
time.sleep(3)

print(blackboard.foo_str)
print(blackboard["foo_str"])
return "outcome3"


Expand Down

0 comments on commit c2090a8

Please sign in to comment.