YASMIN is a project focused on implementing robot behaviors using Finite State Machines (FSM). It is available for ROS 2, Python and C++.
| ROS 2 Distro | Build and Test | Docker Image |
| Foxy | | |
| Galactic | | |
| Humble | | |
| Iron | | |
| Jazzy | | |
| Kilted | | |
| Rolling | | |
Table of Contents
- Key Features
- Installation
- Debian Packages
- Building from Source
- Docker
- Demos
- Cross-Language ROS Interface Communication
- TF States
- YASMIN Editor
- YASMIN Viewer
- YASMIN CLI
- Available Commands
- Usage Examples
- YASMIN Factory
- YASMIN PCL
- YASMIN Plugins Manager
- Citations
Key Features
- ROS 2 Integration: Integrates with ROS 2 for easy deployment and interaction.
- Support for Python and C++: Available for both Python and C++, making it flexible for a variety of use cases.
- Rapid Prototyping: Designed for fast prototyping, allowing quick iteration of state machine behaviors.
- Predefined States: Includes states for interacting with ROS 2 action clients, service clients, and topics.
- Data Sharing with Blackboards: Utilizes blackboards for data sharing between states and state machines.
- State Management: Supports cancellation and stopping of state machines, including halting the current executing state.
- Concurrence: Run multiple states in parallel with configurable join policies.
- XML State Machines: Define state machines in XML files using the Factory and load them at runtime with plugins.
- Graphical Editor: Visual editor for building state machines with drag-and-drop functionality.
- Web Viewer: Features an integrated web viewer for real-time monitoring of state machine execution.
- CLI Tool: Command-line interface for listing, inspecting, running, validating, editing, and testing state machines.
- PCL Integration: Predefined states for Point Cloud Library (PCL) operations, including filtering, segmentation, and point cloud I/O.
- Plugin Architecture: Extensible plugin system for registering custom states usable across C++ and Python.
- State Metadata: States and state machines support descriptions and typed input/output blackboard key annotations.
- Cross-Language Serialization: Share ROS interfaces between Python and C++ states via binary serialization.
- TF Integration: TfBufferState creates and shares a tf2 buffer and transform listener through the blackboard so all states in the machine can perform coordinate frame lookups.
Installation
Debian Packages
To install YASMIN and its packages, use the following command:
sudo apt install ros-$ROS_DISTRO-yasmin ros-$ROS_DISTRO-yasmin-*
Building from Source
Follow these steps to build the source code from this repository:
cd ~/ros2_ws/src
git clone https://github.com/uleroboticsgroup/yasmin.git
cd ~/ros2_ws
rosdep install --from-paths src --ignore-src -r -y
colcon build
Then, you can run the tests as follow:
colcon test --packages-select yasmin yasmin_ros yasmin_factory yasmin_pcl
colcon test-result --verbose
Docker
If your operating system doesn't support ROS 2, docker is a great alternative. You can use an image from Dockerhub or create your own images. First of all, to build the image you have to use the following command:
## Assuming you are in the YASMIN project directory
docker build -t yasmin .
To use a shortcut the docker build, you may use the following command:
## Assuming you are in the YASMIN project directory
make docker_build
After the image is created, run a docker container with the following command:
docker run -it --net=host --ipc=host --privileged --env="DISPLAY" --env="QT_X11_NO_MITSHM=1" --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" --volume="${XAUTHORITY}:/root/.Xauthority" --entrypoint /bin/bash yasmin
To use a shortcut the docker run, you may use following command:
## Assuming you are in the YASMIN project directory
make docker_run
Demos
There are some examples, for both Python and C++, that can be found in yasmin_demos.
Python
Vanilla Demo (FSM)
Click to expand
ros2 run yasmin_demos yasmin_demo.py
import time
import rclpy
import yasmin
from yasmin import Blackboard, State, StateMachine
from yasmin_ros import set_ros_loggers
from yasmin_viewer import YasminViewerPub
"""
Represents the Foo state in the state machine.
"""
def __init__(self) -> None:
"""
Initializes the FooState instance, setting up the outcomes.
Outcomes:
outcome1: Indicates the state should continue to the Bar state.
outcome2: Indicates the state should finish execution and return.
"""
super().__init__(["outcome1", "outcome2"])
self.counter = 0
self.set_description(
"Increments an internal counter, writes the formatted counter string to the blackboard, and controls the loop outcome."
)
self.add_output_key(
"foo_str",
"Formatted counter string written by FooState.",
)
def execute(self, blackboard: Blackboard) -> str:
"""
Executes the logic for the Foo state.
Args:
blackboard (Blackboard): The shared data structure for states.
Returns:
str: The outcome of the execution, which can be "outcome1" or "outcome2".
Raises:
Exception: May raise exceptions related to state execution.
"""
yasmin.YASMIN_LOG_INFO("Executing state FOO")
time.sleep(3)
if self.counter < 3:
self.counter += 1
blackboard["foo_str"] = f"Counter: {self.counter}"
return "outcome1"
else:
return "outcome2"
"""
Represents the Bar state in the state machine.
"""
def __init__(self) -> None:
"""
Initializes the BarState instance, setting up the outcome.
Outcomes:
outcome3: Indicates the state should transition back to the Foo state.
"""
super().__init__(outcomes=["outcome3"])
self.set_description(
"Reads the counter string from the blackboard, logs it, and transitions back to FooState."
)
self.add_input_key(
"foo_str",
"Formatted counter string produced by FooState.",
)
def execute(self, blackboard: Blackboard) -> str:
"""
Executes the logic for the Bar state.
Args:
blackboard (Blackboard): The shared data structure for states.
Returns:
str: The outcome of the execution, which will always be "outcome3".
Raises:
Exception: May raise exceptions related to state execution.
"""
yasmin.YASMIN_LOG_INFO("Executing state BAR")
time.sleep(3)
yasmin.YASMIN_LOG_INFO(blackboard["foo_str"])
return "outcome3"
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("yasmin_demo")
sm = StateMachine(outcomes=["outcome4"], handle_sigint=True)
sm.set_description(
"Runs a simple loop between FooState and BarState until FooState reaches its terminal outcome."
)
sm.add_output_key(
"foo_str",
"Formatted counter string produced by FooState and read by BarState.",
)
sm.add_state(
"FOO",
transitions={
"outcome1": "BAR",
"outcome2": "outcome4",
},
)
sm.add_state(
"BAR",
transitions={
"outcome3": "FOO",
},
)
YasminViewerPub(sm, "YASMIN_DEMO")
try:
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
Represents the "Bar" state in the state machine.
Definition test_concurrence.cpp:47
Represents the "Foo" state in the state machine.
Definition test_concurrence.cpp:34
int main(int argc, char **argv)
Definition test_blackboard.cpp:118
Blackboard Remapping Demo
Click to expand
ros2 run yasmin_demos remap_demo.py
import rclpy
import yasmin
from yasmin import Blackboard, State, StateMachine
from yasmin_ros import set_ros_loggers
from yasmin_viewer import YasminViewerPub
class Foo(State):
"""
Represents the Foo state in the state machine.
"""
def __init__(self):
"""
Initializes the FooState instance, setting up the outcomes.
Outcomes:
SUCCEED: Indicates the state should continue to the next state.
"""
super().__init__(outcomes=[SUCCEED])
self.set_description(
"Reads input data from the blackboard, logs it, and writes it back as output."
)
self.add_input_key(
"foo_data",
"Input data read by the Foo state.",
)
self.add_output_key(
"foo_out_data",
"Output data written by the Foo state.",
)
def execute(self, blackboard: Blackboard):
"""
Executes the logic for the Foo state.
Args:
blackboard (Blackboard): The shared data structure for states.
Returns:
str: The outcome of the execution, which can be SUCCEED.
Raises:
Exception: May raise exceptions related to state execution.
"""
data = blackboard["foo_data"]
yasmin.YASMIN_LOG_INFO(f"{data}")
blackboard["foo_out_data"] = data
return SUCCEED
"""
Represents the Bar state in the state machine.
"""
def __init__(self):
"""
Initializes the BarState instance, setting up the outcomes.
Outcomes:
SUCCEDED: Indicates the state should continue to the next state.
"""
super().__init__(outcomes=[SUCCEED])
self.set_description("Reads remapped input data from the blackboard and logs it.")
self.add_input_key(
"bar_data",
"Input data read by the Bar state.",
)
def execute(self, blackboard: Blackboard):
"""
Executes the logic for the Bar state.
Args:
blackboard (Blackboard): The shared data structure for states.
Returns:
str: The outcome of the execution, which can be SUCCEED.
Raises:
Exception: May raise exceptions related to state execution.
"""
data = blackboard["bar_data"]
yasmin.YASMIN_LOG_INFO(f"{data}")
return SUCCEED
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("yasmin_remapping_demo")
blackboard = Blackboard()
blackboard["msg1"] = "test1"
blackboard["msg2"] = "test2"
sm = StateMachine(outcomes=[SUCCEED], handle_sigint=True)
sm.set_description(
"Demonstrates blackboard remapping by forwarding values through multiple states."
)
sm.add_input_key(
"msg1",
"Initial input value remapped to the first Foo state.",
)
sm.add_input_key(
"msg2",
"Initial input value remapped to the second Foo state.",
)
sm.add_output_key(
"foo_out_data",
"Output data written by Foo and forwarded to Bar through remapping.",
)
sm.add_state(
"STATE1",
Foo(),
transitions={SUCCEED: "STATE2"},
remappings={"foo_data": "msg1"},
)
sm.add_state(
"STATE2",
Foo(),
transitions={SUCCEED: "STATE3"},
remappings={"foo_data": "msg2"},
)
sm.add_state(
"STATE3",
transitions={SUCCEED: SUCCEED},
remappings={"bar_data": "foo_out_data"},
)
YasminViewerPub(sm, "YASMIN_REMAPPING_DEMO")
try:
outcome = sm(blackboard)
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
Provides common outcome constants for ROS 2.
Concurrence Demo
Click to expand
ros2 run yasmin_demos concurrence_demo.py
import time
import rclpy
import yasmin
from yasmin import Blackboard, Concurrence, State, StateMachine
from yasmin_ros import set_ros_loggers
from yasmin_viewer import YasminViewerPub
"""
Represents the Foo state in the state machine.
"""
def __init__(self) -> None:
"""
Initializes the FooState instance, setting up the outcomes.
Outcomes:
outcome1: Indicates the state should continue.
outcome2: Indicates the state should continue.
outcome3: Indicates the state should finish execution and return.
"""
super().__init__(["outcome1", "outcome2", "outcome3"])
self.counter = 0
self.set_description(
"Produces a counter string and stores it in the blackboard while cycling through outcomes based on the internal counter."
)
self.add_output_key(
"foo_str",
"String containing the current counter value produced by FooState.",
)
def execute(self, blackboard: Blackboard) -> str:
"""
Executes the logic for the Foo state.
Args:
blackboard (Blackboard): The shared data structure for states.
Returns:
str: The outcome of the execution.
Raises:
Exception: May raise exceptions related to state execution.
"""
yasmin.YASMIN_LOG_INFO("Executing state FOO")
time.sleep(2)
blackboard["foo_str"] = f"Counter: {self.counter}"
if self.counter < 3:
outcome = "outcome1"
elif self.counter < 5:
outcome = "outcome2"
else:
outcome = "outcome3"
yasmin.YASMIN_LOG_INFO("Finishing state FOO")
self.counter += 1
return outcome
"""
Represents the Bar state in the state machine.
"""
def __init__(self) -> None:
"""
Initializes the BarState instance, setting up the outcome.
Outcomes:
outcome3: This state will always return this outcome
"""
super().__init__(outcomes=["outcome3"])
self.set_description(
"Reads and prints the value stored in 'foo_str' from the blackboard."
)
self.add_input_key(
"foo_str",
"String produced by FooState containing the counter value.",
)
def execute(self, blackboard: Blackboard) -> str:
"""
Executes the logic for the Bar state.
Args:
blackboard (Blackboard): The shared data structure for states.
Returns:
str: The outcome of the execution, which will always be "outcome3".
Raises:
Exception: May raise exceptions related to state execution.
"""
yasmin.YASMIN_LOG_INFO("Executing state BAR")
time.sleep(4)
if "foo_str" in blackboard:
yasmin.YASMIN_LOG_INFO(blackboard["foo_str"])
else:
yasmin.YASMIN_LOG_INFO("Blackboard does not yet contain 'foo_str'")
yasmin.YASMIN_LOG_INFO("Finishing state BAR")
return "outcome3"
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("yasmin_concurrence_demo")
sm = StateMachine(outcomes=["outcome4"], handle_sigint=True)
sm.set_description(
"Runs FooState and BarState concurrently until the concurrence state no longer matches the configured outcome map."
)
sm.add_output_key(
"foo_str",
"String containing the current counter value produced during the concurrent execution.",
)
concurrence_state = Concurrence(
states={
"FOO": foo_state,
"BAR": bar_state,
},
default_outcome="defaulted",
outcome_map={
"outcome1": {
"FOO": "outcome1",
"BAR": "outcome3",
},
"outcome2": {
"FOO": "outcome2",
"BAR": "outcome3",
},
},
)
concurrence_state.set_description(
"Executes FooState and BarState in parallel and maps their combined outcomes to the next transition."
)
concurrence_state.add_input_key(
"foo_str",
"String read by BarState during the concurrent execution.",
)
concurrence_state.add_output_key(
"foo_str",
"String written by FooState during the concurrent execution.",
)
sm.add_state(
"CONCURRENCE",
concurrence_state,
transitions={
"outcome1": "CONCURRENCE",
"outcome2": "CONCURRENCE",
"defaulted": "outcome4",
},
)
YasminViewerPub(sm, "YASMIN_CONCURRENCE_DEMO")
try:
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
Publisher Demo (FSM + ROS 2 Publisher)
Click to expand
ros2 run yasmin_demos publisher_demo.py
import time
import rclpy
from std_msgs.msg import Int32
import yasmin
from yasmin import Blackboard, CbState, StateMachine
from yasmin_ros import PublisherState, set_ros_loggers
from yasmin_viewer import YasminViewerPub
"""
PublishIntState is a YASMIN ROS publisher state that sends incrementing integers
to the 'count' topic using std_msgs.msg.Int32 messages.
This state increments a counter on the blackboard and publishes it.
"""
def __init__(self):
"""
Initializes the PublishIntState with the topic 'count' and a message creation callback.
"""
super().__init__(Int32, "count", self.create_int_msg)
self.set_description(
"Publishes an incrementing integer to the 'count' topic using values stored in the blackboard."
)
self.add_input_key(
"counter",
"Current counter value stored in the blackboard.",
0,
)
self.add_output_key(
"counter",
"Updated counter value after incrementing.",
)
def create_int_msg(self, blackboard: Blackboard) -> Int32:
"""
Generates a std_msgs.msg.Int32 message with an incremented counter value.
Args:
blackboard (Blackboard): The shared data store between states.
Returns:
Int32: A ROS message containing the updated counter.
"""
counter = blackboard.get("counter")
counter += 1
blackboard.set("counter", counter)
yasmin.YASMIN_LOG_INFO(f"Creating message {counter}")
msg = Int32()
msg.data = counter
return msg
"""
Checks the current counter against a max threshold to determine state transition.
Args:
blackboard (Blackboard): The shared data store between states.
Returns:
str: The outcome string ('outcome1' or 'outcome2').
"""
time.sleep(1)
count = blackboard.get("counter")
max_count = blackboard.get("max_count")
yasmin.YASMIN_LOG_INFO(f"Checking count: {count}")
if count >= max_count:
return "outcome1"
else:
return "outcome2"
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("yasmin_publisher_demo")
sm = StateMachine([SUCCEED], handle_sigint=True)
sm.set_description(
"Publishes incrementing integers until the configured maximum count is reached."
)
sm.add_input_key(
"counter",
"Current counter value stored in the blackboard.",
0,
)
sm.add_input_key(
"max_count",
"Maximum counter threshold used to stop publishing.",
10,
)
sm.add_output_key(
"counter",
"Updated counter value after each publish step.",
)
checking_counts_state = CbState(["outcome1", "outcome2"], check_count)
checking_counts_state.set_description(
"Checks whether the counter stored in the blackboard reached the configured maximum."
)
checking_counts_state.add_input_key(
"counter",
"Current counter value.",
0,
)
checking_counts_state.add_input_key(
"max_count",
"Maximum counter threshold used to stop publishing.",
10,
)
sm.add_state(
"PUBLISHING_INT",
{
SUCCEED: "CHECKING_COUNTS",
},
)
sm.add_state(
"CHECKING_COUNTS",
checking_counts_state,
{
"outcome1": SUCCEED,
"outcome2": "PUBLISHING_INT",
},
)
YasminViewerPub(sm, "YASMIN_PUBLISHER_DEMO")
blackboard = Blackboard()
blackboard.set("counter", 0)
blackboard.set("max_count", 10)
try:
outcome = sm(blackboard)
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
A state that publishes ints.
Definition publisher_demo.cpp:42
PublisherState(const std::string &topic_name, CreateMessageHandler create_message_handler, rclcpp::QoS qos=10, rclcpp::CallbackGroup::SharedPtr callback_group=nullptr)
Definition publisher_state.hpp:62
std::string check_count(yasmin::Blackboard::SharedPtr blackboard)
Check the count in the blackboard and return an outcome based on it.
Definition publisher_demo.cpp:94
Monitor Demo (FSM + ROS 2 Subscriber)
Click to expand
ros2 run yasmin_demos monitor_demo.py
import rclpy
from nav_msgs.msg import Odometry
from rclpy.qos import qos_profile_sensor_data
import yasmin
from yasmin import Blackboard, StateMachine
from yasmin_ros import MonitorState, set_ros_loggers
from yasmin_viewer import YasminViewerPub
"""
MonitorState subclass to handle Odometry messages.
This state monitors Odometry messages from the specified ROS topic,
logging them and transitioning based on the number of messages received.
"""
def __init__(self) -> None:
"""
Initializes the PrintOdometryState.
"""
super().__init__(
Odometry,
"odom",
["outcome1", "outcome2"],
self.monitor_handler,
qos=qos_profile_sensor_data,
msg_queue=10,
timeout=10,
)
self.times = 5
self.set_description(
"Monitors Odometry messages from the 'odom' topic and logs them until a predefined number of messages has been received."
)
self.add_input_key(
"odom",
"Odometry message received from the monitored ROS topic.",
)
def monitor_handler(self, blackboard: Blackboard, msg: Odometry) -> str:
"""
Handles incoming Odometry messages.
This method is called whenever a new Odometry message is received.
It logs the message, decrements the count of messages to process,
and determines the next state outcome.
Args:
blackboard (Blackboard): The shared data storage for states.
msg (Odometry): The incoming Odometry message.
Returns:
str: The next state outcome, either "outcome1" to continue
monitoring or "outcome2" to transition to the next state.
Exceptions:
None
"""
yasmin.YASMIN_LOG_INFO(msg)
self.times -= 1
if self.times <= 0:
return "outcome2"
return "outcome1"
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("yasmin_monitor_demo")
sm = StateMachine(outcomes=["outcome4"], handle_sigint=True)
sm.set_description(
"Continuously monitors the 'odom' topic and logs received Odometry messages until a fixed number of messages has been processed."
)
sm.add_input_key(
"odom",
"Odometry messages received from the monitored ROS topic.",
)
sm.add_state(
"PRINTING_ODOM",
transitions={
"outcome1": "PRINTING_ODOM",
"outcome2": "outcome4",
TIMEOUT: "outcome4",
CANCEL: "outcome4",
},
)
YasminViewerPub(sm, "YASMIN_MONITOR_DEMO")
try:
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
A state that monitors odometry data and transitions based on a specified count.
Definition monitor_demo.cpp:43
MonitorState(const std::string &topic_name, const yasmin::Outcomes &outcomes, MonitorHandler monitor_handler, rclcpp::QoS qos=10, int msg_queue=10, int timeout=-1, int maximum_retry=3)
Definition monitor_state.hpp:73
Service Demo (FSM + ROS 2 Service Client)
Click to expand
ros2 run yasmin_demos add_two_ints_server
ros2 run yasmin_demos service_client_demo.py
import rclpy
from example_interfaces.srv import AddTwoInts
import yasmin
from yasmin import Blackboard, CbState, StateMachine
from yasmin_ros import ServiceState, set_ros_loggers
from yasmin_viewer import YasminViewerPub
"""
A state that calls the AddTwoInts service to add two integers.
This class is a state in a finite state machine that sends a request
to the AddTwoInts service, retrieves the response, and updates the
blackboard with the result.
"""
def __init__(self) -> None:
"""
Initializes the AddTwoIntsState.
Calls the parent constructor with the specific service type,
service name, request handler, outcomes, and response handler.
"""
super().__init__(
AddTwoInts,
"/add_two_ints",
self.create_request_handler,
["outcome1"],
self.response_handler,
)
self.set_description(
"Calls the AddTwoInts service using the values stored in the blackboard and writes the resulting sum back to the blackboard."
)
self.add_input_key("a", "First integer used for the service request.")
self.add_input_key("b", "Second integer used for the service request.")
self.add_output_key("sum", "Sum returned by the AddTwoInts service.")
def create_request_handler(self, blackboard: Blackboard) -> AddTwoInts.Request:
"""
Creates the service request from the blackboard data.
Args:
blackboard (Blackboard): The blackboard containing the input values.
Returns:
AddTwoInts.Request: The request object populated with values from the blackboard.
"""
req = AddTwoInts.Request()
req.a = blackboard["a"]
req.b = blackboard["b"]
return req
def response_handler(
self, blackboard: Blackboard, response: AddTwoInts.Response
) -> str:
"""
Processes the response from the AddTwoInts service.
Updates the blackboard with the sum result from the response.
Args:
blackboard (Blackboard): The blackboard to update with the sum.
response (AddTwoInts.Response): The response from the service call.
Returns:
str: The outcome of the operation, which is "outcome1".
"""
blackboard["sum"] = response.sum
return "outcome1"
def set_ints(blackboard: Blackboard) -> str:
"""
Sets the integer values in the blackboard.
This function initializes the blackboard with two integer values to be added.
Args:
blackboard (Blackboard): The blackboard to update with integer values.
Returns:
str: The outcome of the operation, which is SUCCEED.
"""
blackboard["a"] = 10
blackboard["b"] = 5
return SUCCEED
def print_sum(blackboard: Blackboard) -> str:
"""
Logs the sum value from the blackboard.
This function retrieves the sum from the blackboard and logs it.
Args:
blackboard (Blackboard): The blackboard from which to retrieve the sum.
Returns:
str: The outcome of the operation, which is SUCCEED.
"""
yasmin.YASMIN_LOG_INFO(f"Sum: {blackboard['sum']}")
return SUCCEED
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("yasmin_service_client_demo")
sm = StateMachine(outcomes=["outcome4"], handle_sigint=True)
sm.set_description(
"Sets two integers in the blackboard, calls the AddTwoInts service, and prints the resulting sum."
)
sm.add_output_key("a", "First integer used for the service request.")
sm.add_output_key("b", "Second integer used for the service request.")
sm.add_output_key("sum", "Sum returned by the AddTwoInts service.")
setting_ints_state = CbState([SUCCEED], set_ints)
setting_ints_state.set_description(
"Writes the two input integers for the AddTwoInts service into the blackboard."
)
setting_ints_state.add_output_key(
"a",
"First integer used for the service request.",
)
setting_ints_state.add_output_key(
"b",
"Second integer used for the service request.",
)
printing_sum_state = CbState([SUCCEED], print_sum)
printing_sum_state.set_description(
"Reads the computed sum from the blackboard and logs it."
)
printing_sum_state.add_input_key(
"sum",
"Sum previously written to the blackboard by the service state.",
)
sm.add_state(
"SETTING_INTS",
setting_ints_state,
transitions={SUCCEED: "ADD_TWO_INTS"},
)
sm.add_state(
"ADD_TWO_INTS",
transitions={
"outcome1": "PRINTING_SUM",
SUCCEED: "outcome4",
ABORT: "outcome4",
},
)
sm.add_state(
"PRINTING_SUM",
printing_sum_state,
transitions={
SUCCEED: "outcome4",
},
)
YasminViewerPub(sm, "YASMIN_SERVICE_CLIENT_DEMO")
try:
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
State for calling the AddTwoInts service in ROS 2.
Definition service_client_demo.cpp:72
ServiceState(const std::string &srv_name, CreateRequestHandler create_request_handler, int wait_timeout=-1, int response_timeout=-1, int maximum_retry=3)
Definition service_state.hpp:79
std::string set_ints(yasmin::Blackboard::SharedPtr blackboard)
Sets two integer values in the blackboard.
Definition service_client_demo.cpp:43
std::string print_sum(yasmin::Blackboard::SharedPtr blackboard)
Prints the sum stored in the blackboard.
Definition service_client_demo.cpp:57
Action Demo (FSM + ROS 2 Action)
Click to expand
ros2 run yasmin_demos fibonacci_action_server
ros2 run yasmin_demos action_client_demo.py
import rclpy
from example_interfaces.action import Fibonacci
import yasmin
from yasmin import Blackboard, CbState, StateMachine
from yasmin_ros import ActionState, set_ros_loggers
from yasmin_viewer import YasminViewerPub
"""
Class representing the state of the Fibonacci action.
Inherits from ActionState and implements methods to handle the
Fibonacci action in a finite state machine.
"""
def __init__(self) -> None:
"""
Initializes the FibonacciState.
Sets up the action type and the action name for the Fibonacci
action. Initializes goal, response handler, and feedback
processing callbacks.
"""
super().__init__(
Fibonacci,
"/fibonacci",
self.create_goal_handler,
set(),
self.response_handler,
self.print_feedback,
)
self.set_description(
"Calls the ROS2 Fibonacci action server and computes the Fibonacci sequence up to the requested order."
)
self.add_input_key("n", "Order of the Fibonacci sequence to compute.", 10)
self.add_output_key(
"fibo_res",
"Computed Fibonacci sequence returned by the action server.",
)
def create_goal_handler(self, blackboard: Blackboard) -> Fibonacci.Goal:
"""
Creates the goal for the Fibonacci action.
This method retrieves the input value from the blackboard and
populates the Fibonacci goal.
Args:
blackboard (Blackboard): The blackboard containing the state
information.
Returns:
Fibonacci.Goal: The populated goal object for the Fibonacci action.
Raises:
KeyError: If the expected key is not present in the blackboard.
"""
goal = Fibonacci.Goal()
goal.order = blackboard["n"]
return goal
def response_handler(self, blackboard: Blackboard, response: Fibonacci.Result) -> str:
"""
Handles the response from the Fibonacci action.
This method processes the result of the Fibonacci action and
stores it in the blackboard.
Args:
blackboard (Blackboard): The blackboard to store the result.
response (Fibonacci.Result): The result object from the Fibonacci action.
Returns:
str: Outcome of the operation, typically SUCCEED.
Raises:
None
"""
blackboard["fibo_res"] = (
response.sequence
)
return SUCCEED
def print_feedback(
self, blackboard: Blackboard, feedback: Fibonacci.Feedback
) -> None:
"""
Prints feedback from the Fibonacci action.
This method logs the partial sequence received during the action.
Args:
blackboard (Blackboard): The blackboard (not used in this method).
feedback (Fibonacci.Feedback): The feedback object from the Fibonacci action.
Returns:
None
Raises:
None
"""
yasmin.YASMIN_LOG_INFO(f"Received feedback: {list(feedback.sequence)}")
"""
Prints the result of the Fibonacci action.
This function logs the final result stored in the blackboard.
Args:
blackboard (Blackboard): The blackboard containing the result.
Returns:
str: Outcome of the operation, typically SUCCEED.
Raises:
None
"""
yasmin.YASMIN_LOG_INFO(f"Result: {blackboard['fibo_res']}")
return SUCCEED
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("yasmin_action_client_demo")
sm = StateMachine(outcomes=["outcome4"], handle_sigint=True)
sm.set_description(
"Calls a Fibonacci action server, stores the resulting sequence in the blackboard, and prints the result."
)
sm.add_input_key("n", "Order of the Fibonacci sequence to compute.", 10)
sm.add_output_key(
"fibo_res",
"Computed Fibonacci sequence returned by the action server.",
)
printing_result_state = CbState([SUCCEED], print_result)
printing_result_state.set_description(
"Reads the computed Fibonacci sequence from the blackboard and logs it."
)
printing_result_state.add_input_key(
"fibo_res",
"Computed Fibonacci sequence returned by the action server.",
)
sm.add_state(
"CALLING_FIBONACCI",
transitions={
SUCCEED: "PRINTING_RESULT",
CANCEL: "outcome4",
ABORT: "outcome4",
},
)
sm.add_state(
"PRINTING_RESULT",
printing_result_state,
transitions={
SUCCEED: "outcome4",
},
)
YasminViewerPub(sm, "YASMIN_ACTION_CLIENT_DEMO")
blackboard = Blackboard()
blackboard["n"] = 10
try:
outcome = sm(blackboard)
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
std::string print_result(yasmin::Blackboard::SharedPtr blackboard)
Prints the result of the Fibonacci action.
Definition action_client_demo.cpp:45
Represents the action state for the Fibonacci action.
Definition action_client_demo.cpp:71
ActionState(const std::string &action_name, CreateGoalHandler create_goal_handler, const yasmin::Outcomes &outcomes, int wait_timeout=-1, int response_timeout=-1, int maximum_retry=3)
Definition action_state.hpp:97
Parameters Demo (FSM + ROS 2 Parameters)
Click to expand
ros2 run yasmin_demos parameters_demo.py --ros-args -p max_counter:=5
import time
import rclpy
import yasmin
from yasmin import Blackboard, State, StateMachine
from yasmin_ros import GetParametersState, set_ros_loggers
from yasmin_viewer import YasminViewerPub
"""
Represents the Foo state in the state machine.
"""
def __init__(self) -> None:
"""
Initializes the FooState instance, setting up the outcomes.
Outcomes:
outcome1: Indicates the state should continue to the Bar state.
outcome2: Indicates the state should finish execution and return.
"""
super().__init__(["outcome1", "outcome2"])
self.counter = 0
self.set_description(
"Increments a counter until the value from 'max_counter' is reached and writes the formatted counter string to the blackboard."
)
self.add_input_key(
"max_counter",
"Maximum number of iterations before the state finishes.",
)
self.add_input_key(
"counter_str",
"Prefix used when formatting the counter string.",
"Counter",
)
self.add_output_key(
"foo_str",
"Formatted counter string written to the blackboard.",
)
def execute(self, blackboard: Blackboard) -> str:
"""
Executes the logic for the Foo state.
Args:
blackboard (Blackboard): The shared data structure for states.
Returns:
str: The outcome of the execution, which can be "outcome1" or "outcome2".
Raises:
Exception: May raise exceptions related to state execution.
"""
yasmin.YASMIN_LOG_INFO("Executing state FOO")
time.sleep(3)
if self.counter < blackboard["max_counter"]:
self.counter += 1
blackboard["foo_str"] = f"{blackboard['counter_str']}: {self.counter}"
return "outcome1"
else:
return "outcome2"
"""
Represents the Bar state in the state machine.
"""
def __init__(self) -> None:
"""
Initializes the BarState instance, setting up the outcome.
Outcomes:
outcome3: Indicates the state should transition back to the Foo state.
"""
super().__init__(outcomes=["outcome3"])
self.set_description(
"Reads and prints the formatted counter string stored in 'foo_str' from the blackboard."
)
self.add_input_key(
"foo_str",
"Formatted counter string produced by FooState.",
)
def execute(self, blackboard: Blackboard) -> str:
"""
Executes the logic for the Bar state.
Args:
blackboard (Blackboard): The shared data structure for states.
Returns:
str: The outcome of the execution, which will always be "outcome3".
Raises:
Exception: May raise exceptions related to state execution.
"""
yasmin.YASMIN_LOG_INFO("Executing state BAR")
time.sleep(3)
yasmin.YASMIN_LOG_INFO(blackboard["foo_str"])
return "outcome3"
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("yasmin_parameters_demo")
sm = StateMachine(outcomes=["outcome4"], handle_sigint=True)
sm.set_description(
"Loads configuration values into the blackboard and then runs a loop that formats and prints a counter string until the configured maximum is reached."
)
sm.add_input_key(
"max_counter",
"Maximum number of iterations before the state machine finishes.",
3,
)
sm.add_input_key(
"counter_str",
"Prefix used when formatting the counter string.",
"Counter",
)
sm.add_output_key(
"foo_str",
"Formatted counter string produced during execution.",
)
getting_parameters_state = GetParametersState(
parameters={
"max_counter": 3,
"counter_str": "Counter",
},
)
getting_parameters_state.set_description(
"Loads the configured parameters and stores them in the blackboard."
)
getting_parameters_state.add_output_key(
"max_counter",
"Maximum number of iterations before the state machine finishes.",
)
getting_parameters_state.add_output_key(
"counter_str",
"Prefix used when formatting the counter string.",
)
sm.add_state(
"GETTING_PARAMETERS",
getting_parameters_state,
transitions={
SUCCEED: "FOO",
ABORT: "outcome4",
},
)
sm.add_state(
"FOO",
transitions={
"outcome1": "BAR",
"outcome2": "outcome4",
},
)
sm.add_state(
"BAR",
transitions={
"outcome3": "FOO",
},
)
YasminViewerPub(sm, "YASMIN_PARAMETERS_DEMO")
try:
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
Factory Demo (Plugins)
Click to expand
Note: When mixing Python and C++ states in the same state machine, they can communicate through the blackboard, but only with primitive data types: int, float, bool, and string. Complex objects or ROS messages cannot be directly shared between Python and C++ states.
ros2 run yasmin_demos factory_demo.py
<StateMachine outcomes="outcome4">
<State name="Foo" type="cpp" class="yasmin_demos/FooState">
<Transition from="outcome1" to="Bar"/>
<Transition from="outcome2" to="outcome4"/>
</State>
<State name="Bar" type="py" module="yasmin_demos.bar_state" class="BarState">
<Transition from="outcome3" to="Foo"/>
</State>
</StateMachine>
import os
import rclpy
import yasmin
from yasmin_ros import set_ros_loggers
from yasmin_viewer import YasminViewerPub
from yasmin_factory import YasminFactory
from ament_index_python import get_package_share_directory
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("YASMIN_FACTORY_DEMO")
factory = YasminFactory()
sm = factory.create_sm_from_file(
os.path.join(
get_package_share_directory("yasmin_demos"), "state_machines", "demo_1.xml"
)
)
sm.set_sigint_handler(True)
YasminViewerPub(sm, "plugin_demo")
try:
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
Multiple States Demo (States in Separate Files)
Click to expand
ros2 run yasmin_demos multiple_states_demo.py
import rclpy
import yasmin
from yasmin import StateMachine
from yasmin_demos import BarState, FooState
from yasmin_ros import set_ros_loggers
from yasmin_viewer import YasminViewerPub
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("yasmin_multiple_states_demo")
sm = StateMachine(outcomes=["outcome4"], handle_sigint=True)
sm.set_description(
"Runs a simple loop between FooState and BarState demonstrating how states can be organized across multiple files."
)
sm.add_output_key(
"foo_str",
"Formatted counter string produced by FooState and read by BarState.",
)
sm.add_state(
"FOO",
transitions={
"outcome1": "BAR",
"outcome2": "outcome4",
},
)
sm.add_state(
"BAR",
transitions={
"outcome3": "FOO",
},
)
YasminViewerPub(sm, "YASMIN_MULTIPLE_STATES_DEMO")
try:
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
Nav2 Demo (Hierarchical FSM + ROS 2 Action)
Click to expand
import random
import rclpy
from geometry_msgs.msg import Pose
from nav2_msgs.action import NavigateToPose
import yasmin
from yasmin import CbState, Blackboard, StateMachine
from yasmin_ros import ActionState
from yasmin_ros import set_ros_loggers
from yasmin_viewer import YasminViewerPub
HAS_NEXT = "has_next"
END = "end"
class Nav2State(ActionState):
"""
ActionState for navigating to a specified pose using ROS 2 Navigation.
"""
def __init__(self) -> None:
"""
Initializes the Nav2State.
Calls the parent constructor to set up the action with:
- Action type: NavigateToPose
- Action name: /navigate_to_pose
- Callback for goal creation: create_goal_handler
- Outcomes: None, since it will use default outcomes (SUCCEED, ABORT, CANCEL)
"""
super().__init__(
NavigateToPose,
"/navigate_to_pose",
self.create_goal_handler,
None,
None,
)
def create_goal_handler(self, blackboard: Blackboard) -> NavigateToPose.Goal:
"""
Creates a goal for navigation based on the current pose in the blackboard.
Args:
blackboard (Blackboard): The blackboard instance holding current state data.
Returns:
NavigateToPose.Goal: The constructed goal for the navigation action.
"""
goal = NavigateToPose.Goal()
goal.pose.pose = blackboard["pose"]
goal.pose.header.frame_id = "map"
return goal
def create_waypoints(blackboard: Blackboard) -> str:
"""
Initializes waypoints in the blackboard for navigation.
Args:
blackboard (Blackboard): The blackboard instance to store waypoints.
Returns:
str: Outcome indicating success (SUCCEED).
"""
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],
"kitchen": [3.79, 6.77, 0.99, 0.12],
"bedroom": [7.50, 4.89, 0.76, 0.65],
}
return SUCCEED
def take_random_waypoint(blackboard: Blackboard) -> str:
"""
Selects a random set of waypoints from the available waypoints.
Args:
blackboard (Blackboard): The blackboard instance to store random waypoints.
Returns:
str: Outcome indicating success (SUCCEED).
"""
blackboard["random_waypoints"] = random.sample(
list(blackboard["waypoints"].keys()), blackboard["waypoints_num"]
)
return SUCCEED
def get_next_waypoint(blackboard: Blackboard) -> str:
"""
Retrieves the next waypoint from the list of random waypoints.
Updates the blackboard with the pose of the next waypoint.
Args:
blackboard (Blackboard): The blackboard instance holding current state data.
Returns:
str: Outcome indicating whether there is a next waypoint (HAS_NEXT) or if
navigation is complete (END).
"""
if not blackboard["random_waypoints"]:
return END
wp_name = blackboard["random_waypoints"].pop(0)
wp = blackboard["waypoints"][wp_name]
pose = Pose()
pose.position.x = wp[0]
pose.position.y = wp[1]
pose.orientation.z = wp[2]
pose.orientation.w = wp[3]
blackboard["pose"] = pose
blackboard["text"] = f"I have reached waypoint {wp_name}"
return HAS_NEXT
rclpy.init()
set_ros_loggers()
yasmin.YASMIN_LOG_INFO("yasmin_nav2_demo")
sm = StateMachine(outcomes=[SUCCEED, ABORT, CANCEL], handle_sigint=True)
nav_sm = StateMachine(outcomes=[SUCCEED, ABORT, CANCEL])
sm.add_state(
"CREATING_WAYPOINTS",
CbState([SUCCEED], create_waypoints),
transitions={
SUCCEED: "TAKING_RANDOM_WAYPOINTS",
},
)
sm.add_state(
"TAKING_RANDOM_WAYPOINTS",
CbState([SUCCEED], take_random_waypoint),
transitions={
SUCCEED: "NAVIGATING",
},
)
nav_sm.add_state(
"GETTING_NEXT_WAYPOINT",
CbState([END, HAS_NEXT], get_next_waypoint),
transitions={
END: SUCCEED,
HAS_NEXT: "NAVIGATING",
},
)
nav_sm.add_state(
"NAVIGATING",
Nav2State(),
transitions={
SUCCEED: "GETTING_NEXT_WAYPOINT",
CANCEL: CANCEL,
ABORT: ABORT,
},
)
sm.add_state(
"NAVIGATING",
nav_sm,
transitions={
SUCCEED: SUCCEED,
CANCEL: CANCEL,
ABORT: ABORT,
},
)
YasminViewerPub(sm, "YASMIN_NAV2_DEMO")
blackboard = Blackboard()
blackboard["waypoints_num"] = 2
try:
outcome = sm(blackboard)
yasmin.YASMIN_LOG_INFO(outcome)
except Exception as e:
yasmin.YASMIN_LOG_WARN(e)
if rclpy.ok():
rclpy.shutdown()
if __name__ == "__main__":
Cpp
Vanilla Demo
Click to expand
ros2 run yasmin_demos yasmin_demo
public:
"Increments an internal counter, writes the formatted counter string "
"to the blackboard, and controls the loop outcome.");
"Formatted counter string written by FooState.");
};
std::this_thread::sleep_for(std::chrono::seconds(3));
blackboard->set<std::string>("foo_str",
"Counter: " + std::to_string(this->
counter));
return "outcome1";
} else {
return "outcome2";
}
};
};
public:
this->
set_description(
"Reads the counter string from the blackboard, logs "
"it, and transitions back to FooState.");
"Formatted counter string produced by FooState.");
}
std::this_thread::sleep_for(std::chrono::seconds(3));
return "outcome3";
}
};
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto sm = yasmin::StateMachine::make_shared(
std::initializer_list<std::string>{"outcome4"}, true);
sm->set_description("Runs a simple loop between FooState and BarState until "
"FooState reaches its terminal outcome.");
sm->add_output_key(
"foo_str",
"Formatted counter string produced by FooState and read by BarState.");
sm->add_state("FOO", std::make_shared<FooState>(),
{
{"outcome1", "BAR"},
{"outcome2", "outcome4"},
});
sm->add_state("BAR", std::make_shared<BarState>(),
{
{"outcome3", "FOO"},
});
try {
std::string outcome = (*sm.get())();
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
std::string execute(yasmin::Blackboard::SharedPtr blackboard) override
Executes the state's specific logic.
Definition test_concurrence.cpp:51
BarState()
Definition test_concurrence.cpp:49
std::string execute(yasmin::Blackboard::SharedPtr blackboard) override
Executes the state's specific logic.
Definition test_concurrence.cpp:38
FooState()
Definition test_concurrence.cpp:36
int counter
Counter to track the number of executions.
Definition test_state_machine.cpp:36
std::shared_ptr< Blackboard > SharedPtr
Shared pointer type for Blackboard.
Definition blackboard.hpp:100
Represents a state in a state machine.
Definition state.hpp:49
State(const Outcomes &outcomes)
Shared pointer type for State.
Definition state.cpp:64
void add_input_key(const BlackboardKeyInfo &key_info)
Adds an input key to the state's metadata.
Definition state.cpp:198
void add_output_key(const BlackboardKeyInfo &key_info)
Adds an output key to the state's metadata.
Definition state.cpp:213
void set_description(const std::string &description)
Sets the description for this state.
Definition state.cpp:156
Publishes state machine data for visualization.
Definition yasmin_viewer_pub.hpp:40
#define YASMIN_LOG_INFO(text,...)
Definition logs.hpp:169
#define YASMIN_LOG_WARN(text,...)
Definition logs.hpp:164
void set_ros_loggers(rclcpp::Node::SharedPtr node=nullptr)
Sets the logging functions for ROS, linking ROS 2 log levels to YASMIN loggers.
Definition ros_logs.cpp:66
Definition blackboard.hpp:34
Blackboard Remapping Demo
Click to expand
ros2 run yasmin_demos remap_demo
public:
this->
set_description(
"Reads input data from the blackboard, logs it, and "
"writes it back as output.");
this->
add_input_key(
"foo_data",
"Input data read by the Foo state.");
"Output data written by the Foo state.");
};
std::string data = blackboard->get<std::string>("foo_data");
blackboard->set<std::string>("foo_out_data", data);
};
};
public:
"Reads remapped input data from the blackboard and logs it.");
this->
add_input_key(
"bar_data",
"Input data read by the Bar state.");
}
std::string datga = blackboard->get<std::string>("bar_data");
}
};
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto blackboard = std::make_shared<yasmin::Blackboard>();
blackboard->set<std::string>("msg1", "test1");
blackboard->set<std::string>("msg2", "test2");
auto sm = yasmin::StateMachine::make_shared(
true);
sm->set_description("Demonstrates blackboard remapping by forwarding values "
"through multiple states.");
sm->add_input_key("msg1",
"Initial input value remapped to the first Foo state.");
sm->add_input_key("msg2",
"Initial input value remapped to the second Foo state.");
sm->add_output_key(
"foo_out_data",
"Output data written by Foo and forwarded to Bar through remapping.");
sm->add_state("STATE1", std::make_shared<FooState>(),
{
},
{
{"foo_data", "msg1"},
});
sm->add_state("STATE2", std::make_shared<FooState>(),
{
},
{
{"foo_data", "msg2"},
});
sm->add_state("STATE3", std::make_shared<BarState>(),
{
},
{
{"bar_data", "foo_out_data"},
});
try {
std::string outcome = (*sm.get())(blackboard);
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
constexpr char SUCCEED[]
Constant representing a successful action outcome.
Definition basic_outcomes.hpp:39
Concurrence Demo
Click to expand
ros2 run yasmin_demos concurrence_demo
public:
"Produces a counter string and stores it in the blackboard while "
"cycling through outcomes based on the internal counter.");
"foo_str",
"String containing the current counter value produced by FooState.");
};
std::this_thread::sleep_for(std::chrono::seconds(2));
std::string outcome;
blackboard->set<std::string>("foo_str",
"Counter: " + std::to_string(this->
counter));
outcome = "outcome1";
outcome = "outcome2";
} else {
outcome = "outcome3";
}
return outcome;
};
};
public:
"Reads and prints the value stored in 'foo_str' from the blackboard.");
"foo_str", "String produced by FooState containing the counter value.");
}
std::this_thread::sleep_for(std::chrono::seconds(4));
if (blackboard->contains("foo_str")) {
} else {
}
return "outcome3";
}
};
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto sm = yasmin::StateMachine::make_shared(
std::initializer_list<std::string>{"outcome4"}, true);
sm->set_description(
"Runs FooState and BarState concurrently until the concurrence state no "
"longer matches the configured outcome map.");
sm->add_output_key(
"foo_str", "String containing the current counter value produced during "
"the concurrent execution.");
auto foo_state = std::make_shared<FooState>();
auto bar_state = std::make_shared<BarState>();
auto concurrent_state = yasmin::Concurrence::make_shared(
{"FOO", foo_state},
{"BAR", bar_state},
},
"defaulted",
{"outcome1", {{"FOO", "outcome1"}, {"BAR", "outcome3"}}},
{"outcome2", {{"FOO", "outcome2"}, {"BAR", "outcome3"}}},
});
concurrent_state->set_description(
"Executes FooState and BarState in parallel and maps their combined "
"outcomes to the next transition.");
concurrent_state->add_input_key(
"foo_str", "String read by BarState during the concurrent execution.");
concurrent_state->add_output_key(
"foo_str", "String written by FooState during the concurrent execution.");
sm->add_state("CONCURRENCE", concurrent_state,
{
{"outcome1", "CONCURRENCE"},
{"outcome2", "CONCURRENCE"},
{"defaulted", "outcome4"},
});
try {
std::string outcome = (*sm.get())();
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
std::unordered_map< std::string, StateOutcomeMap > OutcomeMap
Map of outcomes to state outcome maps.
Definition types.hpp:81
std::unordered_map< std::string, std::shared_ptr< State > > StateMap
Map of state names to state pointers.
Definition types.hpp:108
Publisher Demo (FSM + ROS 2 Publisher)
Click to expand
ros2 run yasmin_demos publisher_demo
using std::placeholders::_1;
using std::placeholders::_2;
public:
"count",
_1)
) {
"topic using values stored in the blackboard.");
"counter", "Current counter value stored in the blackboard.", 0);
"Updated counter value after incrementing.");
};
std_msgs::msg::Int32
int counter = blackboard->get<int>("counter");
counter++;
blackboard->set<int>("counter", counter);
std_msgs::msg::Int32 msg;
msg.data = counter;
return msg;
};
};
rclcpp::sleep_for(std::chrono::seconds(1));
if (blackboard->get<int>("counter") >= blackboard->get<int>("max_count")) {
return "outcome1";
} else {
return "outcome2";
}
}
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto sm = yasmin::StateMachine::make_shared(
true);
sm->set_description("Publishes incrementing integers until the configured "
"maximum count is reached.");
sm->add_input_key<int>("counter",
"Current counter value stored in the blackboard.", 0);
sm->add_input_key<int>(
"max_count", "Maximum counter threshold used to stop publishing.", 10);
sm->add_output_key("counter",
"Updated counter value after each publish step.");
auto checking_counts_state = yasmin::CbState::make_shared(
std::initializer_list<std::string>{
"outcome1",
"outcome2"},
check_count);
checking_counts_state->set_description(
"Checks whether the counter stored in the blackboard reached the "
"configured maximum.");
checking_counts_state->add_input_key<int>("counter", "Current counter value.",
0);
checking_counts_state->add_input_key<int>(
"max_count", "Maximum counter threshold used to stop publishing.", 10);
sm->add_state("PUBLISHING_INT", std::make_shared<PublishIntState>(),
{
"CHECKINNG_COUNTS"},
});
sm->add_state("CHECKINNG_COUNTS", checking_counts_state,
{"outcome2", "PUBLISHING_INT"}});
blackboard->set<int>("counter", 0);
blackboard->set<int>("max_count", 10);
try {
std::string outcome = (*sm.get())(blackboard);
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
std_msgs::msg::Int32 create_int_msg(yasmin::Blackboard::SharedPtr blackboard)
Create a new Int message.
Definition publisher_demo.cpp:71
PublishIntState()
Constructor for the PublishIntState class.
Definition publisher_demo.cpp:48
static std::shared_ptr< Blackboard > make_shared(Args &&...args)
Definition blackboard.hpp:100
Template class to publish ROS 2 messages.
Definition publisher_state.hpp:43
Definition action_state.hpp:37
Monitor Demo (FSM + ROS 2 Subscriber)
Click to expand
ros2 run yasmin_demos monitor_demo
using std::placeholders::_1;
using std::placeholders::_2;
public:
"odom",
{"outcome1", "outcome2"},
_2),
10,
10,
10
) {
this->times = 5;
"and logs the received positions.");
"odom", "Odometry message received from the monitored ROS topic.");
};
nav_msgs::msg::Odometry::SharedPtr msg) {
(void)blackboard;
return "outcome2";
}
return "outcome1";
};
};
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto sm = yasmin::StateMachine::make_shared(
std::initializer_list<std::string>{"outcome4"}, true);
sm->set_description(
"Continuously monitors the 'odom' topic and logs received odometry "
"messages until a fixed number of messages has been processed.");
sm->add_input_key("odom",
"Odometry messages received from the monitored ROS topic.");
sm->add_state(
"PRINTING_ODOM", std::make_shared<PrintOdometryState>(),
{
{"outcome1",
"PRINTING_ODOM"},
{"outcome2", "outcome4"},
"outcome4"},
});
try {
std::string outcome = (*sm.get())();
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
std::string monitor_handler(yasmin::Blackboard::SharedPtr blackboard, nav_msgs::msg::Odometry::SharedPtr msg)
Handler for processing odometry data.
Definition monitor_demo.cpp:83
PrintOdometryState()
Constructor for the PrintOdometryState class.
Definition monitor_demo.cpp:52
int times
The number of times the state will process messages.
Definition monitor_demo.cpp:47
Template class to monitor a ROS 2 topic and process incoming messages.
Definition monitor_state.hpp:48
MonitorHandler monitor_handler
Definition monitor_state.hpp:235
constexpr char CANCEL[]
Constant representing a canceled action outcome.
Definition basic_outcomes.hpp:63
constexpr char TIMEOUT[]
Constant representing a timed-out action outcome.
Definition basic_outcomes.hpp:71
Service Demo (FSM + ROS 2 Service Client)
Click to expand
ros2 run yasmin_demos add_two_ints_server
ros2 run yasmin_demos service_client_demo
using std::placeholders::_1;
using std::placeholders::_2;
blackboard->set<int>("a", 10);
blackboard->set<int>("b", 5);
}
std::stringstream ss;
ss << "Sum: " << blackboard->get<int>("sum");
}
public:
: yasmin_ros::
ServiceState<example_interfaces::srv::AddTwoInts>(
"/add_two_ints",
{"outcome1"},
"Calls the AddTwoInts service using the values stored in the "
"blackboard and writes the resulting sum back to the blackboard.");
this->
add_input_key(
"a",
"First integer used for the service request.");
this->
add_input_key(
"b",
"Second integer used for the service request.");
this->
add_output_key(
"sum",
"Sum returned by the AddTwoInts service.");
};
example_interfaces::srv::AddTwoInts::Request::SharedPtr
auto request =
std::make_shared<example_interfaces::srv::AddTwoInts::Request>();
request->a = blackboard->get<int>("a");
request->b = blackboard->get<int>("b");
return request;
};
example_interfaces::srv::AddTwoInts::Response::SharedPtr response) {
blackboard->set<int>("sum", response->sum);
return "outcome1";
};
};
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto sm = yasmin::StateMachine::make_shared(
std::initializer_list<std::string>{"outcome4"}, true);
sm->set_description("Sets two integers in the blackboard, calls the "
"AddTwoInts service, and prints the resulting sum.");
sm->add_output_key("a", "First integer used for the service request.");
sm->add_output_key("b", "Second integer used for the service request.");
sm->add_output_key("sum", "Sum returned by the AddTwoInts service.");
auto setting_ints_state = yasmin::CbState::make_shared(
setting_ints_state->set_description(
"Writes the two input integers for the AddTwoInts service into the "
"blackboard.");
setting_ints_state->add_output_key(
"a", "First integer used for the service request.");
setting_ints_state->add_output_key(
"b", "Second integer used for the service request.");
auto printing_sum_state = yasmin::CbState::make_shared(
printing_sum_state->set_description(
"Reads the computed sum from the blackboard and prints it.");
printing_sum_state->add_input_key(
"sum", "Sum previously written to the blackboard by the service state.");
sm->add_state("SETTING_INTS", setting_ints_state,
{
});
sm->add_state("ADD_TWO_INTS", std::make_shared<AddTwoIntsState>(),
{
{"outcome1", "PRINTING_SUM"},
});
sm->add_state("PRINTING_SUM", printing_sum_state,
{
});
try {
std::string outcome = (*sm.get())();
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
example_interfaces::srv::AddTwoInts::Request::SharedPtr create_request_handler(yasmin::Blackboard::SharedPtr blackboard)
Creates a service request using values from the blackboard.
Definition service_client_demo.cpp:105
AddTwoIntsState()
Constructor for AddTwoIntsState.
Definition service_client_demo.cpp:80
A state class that interacts with a ROS 2 service.
Definition service_state.hpp:48
CreateRequestHandler create_request_handler
Definition service_state.hpp:330
ResponseHandler response_handler
Definition service_state.hpp:332
constexpr char ABORT[]
Constant representing an aborted action outcome.
Definition basic_outcomes.hpp:47
Action Demo (FSM + ROS 2 Action)
Click to expand
ros2 run yasmin_demos fibonacci_action_server
ros2 run yasmin_demos action_client_demo
using std::placeholders::_1;
using std::placeholders::_2;
using Fibonacci = example_interfaces::action::Fibonacci;
auto fibo_res = blackboard->get<std::vector<int>>("fibo_res");
std::stringstream ss;
ss << "Result: [";
for (size_t i = 0; i < fibo_res.size(); i++) {
ss << fibo_res[i];
if (i < fibo_res.size() - 1) {
ss << ", ";
}
}
ss << "]";
}
public:
"/fibonacci",
"resulting sequence in the blackboard.");
10);
"Computed Fibonacci sequence returned by the action.");
};
Fibonacci::Goal
auto goal = Fibonacci::Goal();
goal.order = blackboard->get<int>("n");
return goal;
};
Fibonacci::Result::SharedPtr response) {
blackboard->set<std::vector<int>>("fibo_res", response->sequence);
};
std::shared_ptr<const Fibonacci::Feedback> feedback) {
(void)blackboard;
std::stringstream ss;
ss << "Received feedback: [";
for (size_t i = 0; i < feedback->sequence.size(); i++) {
ss << feedback->sequence[i];
if (i < feedback->sequence.size() - 1) {
ss << ", ";
}
}
ss << "]";
};
};
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto sm = yasmin::StateMachine::make_shared(
std::initializer_list<std::string>{"outcome4"}, true);
sm->set_description("Calls the Fibonacci action server, stores the resulting "
"sequence, and prints it.");
sm->add_input_key<int>("n", "Order of the Fibonacci sequence to compute.",
10);
sm->add_output_key("fibo_res",
"Computed Fibonacci sequence returned by the action.");
auto printing_result_state = yasmin::CbState::make_shared(
printing_result_state->set_description(
"Reads the computed Fibonacci sequence from the blackboard and prints "
"it.");
printing_result_state->add_input_key(
"fibo_res", "Computed Fibonacci sequence returned by the action.");
sm->add_state("CALLING_FIBONACCI", std::make_shared<FibonacciState>(),
{
});
sm->add_state("PRINTING_RESULT", printing_result_state,
{
});
blackboard->set<int>("n", 10);
try {
std::string outcome = (*sm.get())(blackboard);
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
example_interfaces::action::Fibonacci Fibonacci
Definition action_client_demo.cpp:33
std::string response_handler(yasmin::Blackboard::SharedPtr blackboard, Fibonacci::Result::SharedPtr response)
Callback for handling the action response.
Definition action_client_demo.cpp:117
void print_feedback(yasmin::Blackboard::SharedPtr blackboard, std::shared_ptr< const Fibonacci::Feedback > feedback)
Callback for printing action feedback.
Definition action_client_demo.cpp:134
FibonacciState()
Constructs a new FibonacciState object and initializes callbacks.
Definition action_client_demo.cpp:77
Fibonacci::Goal create_goal_handler(yasmin::Blackboard::SharedPtr blackboard)
Callback for creating the Fibonacci action goal.
Definition action_client_demo.cpp:100
A state class for handling ROS 2 action client operations.
Definition action_state.hpp:48
CreateGoalHandler create_goal_handler
Definition action_state.hpp:455
Parameters Demo (FSM + ROS 2 Parameters)
Click to expand
ros2 run yasmin_demos parameters_demo --ros-args -p max_counter:=5
public:
"Increments a counter until the value from 'max_counter' is reached "
"and writes the formatted counter string to the blackboard.");
"max_counter",
"Maximum number of iterations before the state finishes.");
"counter_str", "Prefix used when formatting the counter string.",
std::string("Counter"));
"Formatted counter string written to the blackboard.");
};
std::this_thread::sleep_for(std::chrono::seconds(3));
blackboard->set<std::string>("foo_str",
blackboard->get<std::string>("counter_str") +
": " + std::to_string(this->
counter));
return "outcome1";
} else {
return "outcome2";
}
};
};
public:
"stored in 'foo_str' from the blackboard.");
"Formatted counter string produced by FooState.");
}
std::this_thread::sleep_for(std::chrono::seconds(3));
return "outcome3";
}
};
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto sm = yasmin::StateMachine::make_shared(
std::initializer_list<std::string>{"outcome4"}, true);
sm->set_description("Loads configuration values into the blackboard and then "
"runs a loop that formats and prints a counter string "
"until the configured maximum is reached.");
sm->add_input_key<int>(
"max_counter",
"Maximum number of iterations before the state machine finishes.", 3);
sm->add_input_key<std::string>(
"counter_str", "Prefix used when formatting the counter string.",
std::string("Counter"));
sm->add_output_key("foo_str",
"Formatted counter string produced during execution.");
auto getting_parameters_state = yasmin_ros::GetParametersState::make_shared(
{"max_counter", 3},
{"counter_str", std::string("Counter")},
});
getting_parameters_state->set_description(
"Loads the configured parameters and stores them in the blackboard.");
getting_parameters_state->add_output_key(
"max_counter",
"Maximum number of iterations before the state machine finishes.");
getting_parameters_state->add_output_key(
"counter_str", "Prefix used when formatting the counter string.");
sm->add_state("GETTING_PARAMETERS", getting_parameters_state,
{
});
sm->add_state("FOO", std::make_shared<FooState>(),
{
{"outcome1", "BAR"},
{"outcome2", "outcome4"},
});
sm->add_state("BAR", std::make_shared<BarState>(),
{
{"outcome3", "FOO"},
});
try {
std::string outcome = (*sm.get())();
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
Factory Demo (Plugins)
Click to expand
Note: When mixing Python and C++ states in the same state machine, they can communicate through the blackboard, but only with primitive data types: int, float, bool, and string. Complex objects or ROS messages cannot be directly shared between Python and C++ states.
ros2 run yasmin_demos factory_demo
<StateMachine outcomes="outcome4">
<State name="Foo" type="py" module="yasmin_demos.foo_state" class="FooState">
<Transition from="outcome1" to="Bar"/>
<Transition from="outcome2" to="outcome4"/>
</State>
<State name="Bar" type="cpp" class="yasmin_demos/BarState">
<Transition from="outcome3" to="Foo"/>
</State>
</StateMachine>
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
yasmin_factory::YasminFactory factory;
std::string xml_file =
#if __has_include("rclcpp/version.h")
#include "rclcpp/version.h"
#if RCLCPP_VERSION_GTE(29, 5, 1)
([]() {
std::filesystem::path p;
ament_index_cpp::get_package_share_directory("yasmin_demos", p);
return (p / "state_machines/demo_2.xml").string();
})();
#else
ament_index_cpp::get_package_share_directory("yasmin_demos") +
"/state_machines/demo_2.xml";
#endif
#else
ament_index_cpp::get_package_share_directory("yasmin_demos") +
"/state_machines/demo_2.xml";
#endif
auto sm = factory.create_sm_from_file(xml_file);
sm->set_sigint_handler(true);
try {
std::string outcome = (*sm.get())();
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
Multiple States Demo (States in Separate Files)
Click to expand
ros2 run yasmin_demos multiple_states_demo
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto sm = yasmin::StateMachine::make_shared(
std::initializer_list<std::string>{"outcome4"}, true);
sm->set_description(
"Runs a simple loop between FooState and BarState demonstrating how "
"states can be organized across multiple files.");
sm->add_output_key(
"foo_str",
"Formatted counter string produced by FooState and read by BarState.");
sm->add_state("FOO", std::make_shared<FooState>(),
{
{"outcome1", "BAR"},
{"outcome2", "outcome4"},
});
sm->add_state("BAR", std::make_shared<BarState>(),
{
{"outcome3", "FOO"},
});
try {
std::string outcome = (*sm.get())();
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
ROS Serialization Demo (Factory + Cross-Language)
Click to expand
ros2 run yasmin_demos ros_serialization_demo
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
yasmin_factory::YasminFactory factory;
std::string xml_file =
#if __has_include("rclcpp/version.h")
#include "rclcpp/version.h"
#if RCLCPP_VERSION_GTE(29, 5, 1)
([]() {
std::filesystem::path p;
ament_index_cpp::get_package_share_directory("yasmin_demos", p);
return (p / "state_machines/demo_3.xml").string();
})();
#else
ament_index_cpp::get_package_share_directory("yasmin_demos") +
"/state_machines/demo_3.xml";
#endif
#else
ament_index_cpp::get_package_share_directory("yasmin_demos") +
"/state_machines/demo_3.xml";
#endif
auto sm = factory.create_sm_from_file(xml_file);
sm->set_description(
"Loads a state machine from an XML file that demonstrates ROS interface "
"serialization and executes it.");
sm->set_sigint_handler(true);
"YASMIN_ROS_SERIALIZATION_DEMO");
try {
std::string outcome = (*sm.get())();
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
Nav2 Demo (Hierarchical FSM + ROS 2 Action)
Click to expand
#include <algorithm>
#include <iostream>
#include <map>
#include <memory>
#include <random>
#include <string>
#include <vector>
#include <geometry_msgs/msg/pose.hpp>
#include <nav2_msgs/action/navigate_to_pose.hpp>
using std::placeholders::_1;
using std::placeholders::_2;
using NavigateToPose = nav2_msgs::action::NavigateToPose;
using Pose = geometry_msgs::msg::Pose;
const std::string HAS_NEXT = "has_next";
const std::string END = "end";
public:
Nav2State()
"/navigate_to_pose",
std::bind(&Nav2State::create_goal_handler, this, _1)) {}
NavigateToPose::Goal goal;
goal.pose.pose = blackboard->get<Pose>("pose");
goal.pose.header.frame_id = "map";
return goal;
}
};
std::string
std::map<std::string, std::vector<double>> 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}},
{"kitchen", {3.79, 6.77, 0.99, 0.12}},
{"bedroom", {7.50, 4.89, 0.76, 0.65}}};
blackboard->set<std::map<std::string, std::vector<double>>>("waypoints",
waypoints);
}
std::string take_random_waypoint(
auto waypoints =
blackboard->get<std::map<std::string, std::vector<double>>>("waypoints");
int waypoints_num = blackboard->get<int>("waypoints_num");
std::vector<std::string> waypoint_names;
for (const auto &pair : waypoints) {
waypoint_names.push_back(pair.first);
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(waypoint_names.begin(), waypoint_names.end(), g);
std::vector<std::string> random_waypoints(
waypoint_names.begin(), waypoint_names.begin() + waypoints_num);
blackboard->set<std::vector<std::string>>("random_waypoints",
random_waypoints);
}
std::string
auto random_waypoints =
blackboard->get<std::vector<std::string>>("random_waypoints");
auto waypoints =
blackboard->get<std::map<std::string, std::vector<double>>>("waypoints");
if (random_waypoints.empty()) {
return END;
}
std::string wp_name = random_waypoints.back();
random_waypoints.pop_back();
blackboard->set<std::vector<std::string>>("random_waypoints",
random_waypoints);
auto wp = waypoints.at(wp_name);
Pose pose;
pose.position.x = wp[0];
pose.position.y = wp[1];
pose.orientation.z = wp[2];
pose.orientation.w = wp[3];
blackboard->set<Pose>("pose", pose);
blackboard->set<std::string>("text", "I have reached waypoint " + wp_name);
return HAS_NEXT;
}
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto sm = yasmin::StateMachine::make_shared(
true);
auto nav_sm = yasmin::StateMachine::make_shared(
sm->add_state(
"CREATING_WAYPOINTS",
yasmin::CbState::make_shared(
std::initializer_list<std::string>{
create_waypoints),
std::map<std::string, std::string>{
sm->add_state("TAKING_RANDOM_WAYPOINTS",
yasmin::CbState::make_shared(
std::initializer_list<std::string>{
take_random_waypoint),
std::map<std::string, std::string>{
nav_sm->add_state(
"GETTING_NEXT_WAYPOINT",
yasmin::CbState::make_shared(
std::initializer_list<std::string>{END, HAS_NEXT}, get_next_waypoint),
std::map<std::string, std::string>{
{HAS_NEXT, "NAVIGATING"}});
nav_sm->add_state(
"NAVIGATING", std::make_shared<Nav2State>(),
std::map<std::string, std::string>{
sm->add_state(
"NAVIGATING", nav_sm,
blackboard->set<int>("waypoints_num",
2);
try {
std::string outcome = (*sm.get())(blackboard);
} catch (const std::exception &e) {
}
rclcpp::shutdown();
return 0;
}
Cross‑Language ROS Interface Communication
When mixing Python and C++ states in the same YASMIN state machine, the blackboard can be used to exchange data between them. However, due to language boundaries, only data types that can be safely serialized across runtimes should be used.
Supported Blackboard Types
The following types are guaranteed to work between Python and C++:
| Type | Description |
| int | Integer values |
| float | Floating-point values |
| bool | Boolean values |
| string | UTF-8 text |
| bytes / std::vector<uint8_t> | Binary data |
Sharing ROS Interfaces Between Languages
ROS messages cannot be directly stored in the blackboard when communicating between Python and C++ states. Instead, they should be serialized into raw bytes.
Option A — Serialization States
YASMIN provides dedicated states for this in yasmin_ros:
| State | Language | Direction |
| RosSerializeCppState | C++ | ROS interface → std::vector<uint8_t> |
| RosDeserializeCppState | C++ | std::vector<uint8_t> → ROS interface |
| RosSerializePyState | Python | ROS interface → bytes |
| RosDeserializePyState | Python | bytes → ROS interface |
All four states read from the blackboard key input and write their result to output. The target interface type is configured through the interface_type parameter (e.g. geometry_msgs/msg/Pose).
from yasmin_ros import RosSerializePyState, RosDeserializePyState
serialize_state = RosSerializePyState()
serialize_state.set_parameter("interface_type", "geometry_msgs/msg/Pose")
serialize_state.configure()
The C++ states are registered as pluginlib plugins (yasmin_ros/RosSerializeCppState, yasmin_ros/RosDeserializeCppState) and can be used directly from XML state machine files.
Option B — Low-level helper utilities
YASMIN also exposes low-level serialization utilities in:
yasmin_ros/interface_serialization.hpp
These allow converting ROS interfaces to binary data inline inside a custom state.
Example (C++)
#include <geometry_msgs/msg/pose.hpp>
geometry_msgs::msg::Pose pose;
pose.position.x = 1.0;
pose.position.y = 2.0;
pose.position.z = 3.0;
blackboard->set<std::vector<uint8_t>>("pose_bytes", bytes);
std::vector< uint8_t > serialize_interface(const InterfaceT &interface_value)
Serialize a ROS 2 interface instance to a byte array.
Definition interface_serialization.hpp:38
Example (Python)
import rclpy.serialization
from geometry_msgs.msg import Pose
pose_bytes = blackboard["pose_bytes"]
pose = rclpy.serialization.deserialize_message(pose_bytes, Pose)
TF States
The TfBufferState (available in both C++ and Python through yasmin_ros) creates a shared tf2 buffer and transform listener and stores them in the YASMIN blackboard under the keys tf_buffer and tf_listener. Subsequent states retrieve these objects from the blackboard to perform transform lookups without duplicating listener infrastructure.
TfBufferState
| |
| Outcomes | succeeded, aborted |
| Output keys | tf_buffer, tf_listener |
| Parameters | cache_time_sec (default 10.0) |
| Plugin name | yasmin_ros/TfBufferState |
Python Example
import rclpy
from tf2_ros import Buffer, TransformListener
import yasmin
from yasmin import Blackboard, State, StateMachine
from yasmin_ros import TfBufferState, set_ros_loggers
class LookupTransformState(State):
def __init__(self):
super().__init__([SUCCEED, ABORT])
def execute(self, blackboard: Blackboard) -> str:
tf_buffer: Buffer = blackboard["tf_buffer"]
try:
t = tf_buffer.lookup_transform("map", "base_link", rclpy.time.Time())
yasmin.YASMIN_LOG_INFO(
f"x={t.transform.translation.x:.3f} "
f"y={t.transform.translation.y:.3f}"
)
return SUCCEED
except Exception as exc:
yasmin.YASMIN_LOG_WARN(f"Lookup failed: {exc}")
return ABORT
rclpy.init()
set_ros_loggers()
tf_state = TfBufferState()
tf_state.set_parameter("cache_time_sec", 10.0)
tf_state.configure()
sm = StateMachine(outcomes=["done", "failed"])
sm.add_state("INIT_TF", tf_state, {SUCCEED: "LOOKUP", ABORT: "failed"})
sm.add_state("LOOKUP", LookupTransformState(), {SUCCEED: "done", ABORT: "failed"})
outcome = sm()
yasmin.YASMIN_LOG_INFO(outcome)
rclpy.shutdown()
if __name__ == "__main__":
C++ Example
#include <tf2_ros/buffer.hpp>
int main(
int argc,
char *argv[]) {
rclcpp::init(argc, argv);
auto sm = std::make_shared<yasmin::StateMachine>(
std::vector<std::string>{"done", "failed"});
sm->add_state("INIT_TF",
std::make_shared<yasmin_ros::TfBufferState>(),
std::string outcome = (*sm)();
rclcpp::shutdown();
return 0;
}
YASMIN Editor
The YASMIN Editor is a graphical user interface application for building YASMIN state machines using state plugins. It enables intuitive creation of state machines through drag-and-drop functionality, allowing you to:
- Load Python and C++ states
- Load XML state machines
- Define transitions between states
- Create outcomes
- Visualize state machine structure
State machines can be exported and saved in XML format for reuse and sharing.

Getting Started
ros2 run yasmin_editor yasmin_editor
YASMIN Viewer
The YASMIN Viewer provides a convenient way to monitor YASMIN's Finite State Machines (FSM). It is built using a C++ HTTP server (Boost Beast) with a Mermaid.js front-end and includes a filter to focus on a single FSM at a time.

Getting Started
ros2 run yasmin_viewer yasmin_viewer_node
Once started, open http://localhost:5000/ in your browser to view your state machines.
Custom host and port
You can specify a custom host and port by using the following command:
ros2 run yasmin_viewer yasmin_viewer_node --ros-args -p host:=127.0.0.1 -p port:=5032
After running the command, access your state machines at http://127.0.0.1:5032/.
YASMIN CLI
The YASMIN CLI extends the ros2 command-line interface with the yasmin verb, providing tools to discover, inspect, validate, run, and test YASMIN state plugins and XML state machines. It is built as a ros2cli extension and supports tab completion.
Available Commands
| Command | Description |
| list | List discovered YASMIN plugins (C++, Python, and XML state machines) |
| info | Show detailed metadata for a specific plugin (outcomes, keys, parameters) |
| print | Parse and display the structure of an XML state machine file |
| validate | Validate one or all XML state machines by loading them through the factory |
| run | Run a state machine from an XML file, with optional input/parameter overrides |
| test | Test a single state plugin in an auto-generated temporary state machine |
| edit | Open an XML state machine in the YASMIN Editor GUI |
| viewer | Start the YASMIN Viewer web server for real-time FSM monitoring |
Usage Examples
# List all discovered plugins (states and XML state machines)
ros2 yasmin list
# Filter by plugin type
ros2 yasmin list --type cpp
ros2 yasmin list --type xml
# Search plugins by name
ros2 yasmin list --search foo
# Show detailed info about a specific plugin
ros2 yasmin info <plugin_id>
# Print the structure of an XML state machine
ros2 yasmin print /path/to/state_machine.xml
# Validate a specific XML state machine
ros2 yasmin validate /path/to/state_machine.xml
# Validate all XML state machines from installed packages
ros2 yasmin validate
# Run a state machine from an XML file
ros2 yasmin run /path/to/state_machine.xml
# Run with input key and parameter overrides
ros2 yasmin run /path/to/state_machine.xml --input key1=value1 --param param1=value2
# Test a single state plugin in isolation
ros2 yasmin test <plugin_id>
ros2 yasmin test <plugin_id> --input key1=value1
# Open the editor (optionally with an XML file)
ros2 yasmin edit
ros2 yasmin edit /path/to/state_machine.xml
# Start the viewer
ros2 yasmin viewer
ros2 yasmin viewer --host 0.0.0.0 --port 8080
YASMIN Factory
The YASMIN Factory enables the creation of state machines from XML configuration files using a plugin architecture. States are registered as plugins via pluginlib, allowing both C++ and Python states to be loaded dynamically at runtime.
Getting Started
ros2 run yasmin_factory yasmin_factory_node --ros-args -p state_machine_file:=/path/to/state_machine.xml
For detailed information on XML syntax, plugin creation, and cross-language support, see the XML State Machines documentation.
YASMIN PCL
The YASMIN PCL package provides predefined states for Point Cloud Library (PCL) operations, enabling point cloud processing pipelines within YASMIN state machines.
Available States
Filters:
- CropBoxState — Crop a point cloud to a specified bounding box
- ExtractIndicesState — Extract points by index
- PassThroughState — Filter points along an axis within a range
- ProjectInliersState — Project points onto a model
- RadiusOutlierRemovalState — Remove outliers based on radius neighbor count
- RandomSampleState — Randomly downsample a point cloud
- StatisticalOutlierRemovalState — Remove statistical outliers
- VoxelGridState — Downsample using a voxel grid
I/O:
- LoadPcdState / SavePcdState — Read and write PCD files
- LoadPlyState / SavePlyState — Read and write PLY files
- PclToRosPointCloud2State — Convert PCL point cloud to ROS PointCloud2
- RosToPclPointCloud2State — Convert ROS PointCloud2 to PCL point cloud
YASMIN Plugins Manager
The YASMIN Plugins Manager discovers and caches all YASMIN plugins across the ROS 2 workspace. It supports three plugin types: C++ states exported via pluginlib, Python State subclasses found by package introspection, and XML state machines located in package share directories. Discovered metadata (outcomes, keys, parameters) is shared with the CLI, Editor, and Factory tools.
Run a discovery scan with:
ros2 run yasmin_plugins_manager discover_plugins
Force a full rescan (ignore cache):
ros2 run yasmin_plugins_manager discover_plugins --force-refresh
Discovery results are cached in ~/.cache/yasmin_plugins_manager/plugins_cache.json (override with the YASMIN_CACHE env var). The cache is invalidated automatically on ROS environment changes, file modifications, or package list changes.
Ignoring Packages and Files
Three mechanisms are available to exclude plugins from discovery.
Environment variable — comma-, semicolon-, or colon-separated package names. The ignore list is stored as part of the cache state, so changing it automatically invalidates the cache and triggers re-discovery:
export YASMIN_DISCOVERY_IGNORE_PACKAGES="pkg_a,pkg_b"
Package-level — add the following export to the package's package.xml:
<export>
<yasmin_plugins_manager ignore="true"/>
</export>
File-level — place this comment as the first non-empty line of an XML state machine file:
<StateMachine>
...
</StateMachine>
Citations
@InProceedings{10.1007/978-3-031-21062-4_43,
author="Gonz{\'a}lez-Santamarta, Miguel {\'A}.
and Rodr{\'i}guez-Lera, Francisco J.
and Matell{\'a}n-Olivera, Vicente
and Fern{\'a}ndez-Llamas, Camino",
editor="Tardioli, Danilo
and Matell{\'a}n, Vicente
and Heredia, Guillermo
and Silva, Manuel F.
and Marques, Lino",
title="YASMIN: Yet Another State MachINe",
booktitle="ROBOT2022: Fifth Iberian Robotics Conference",
year="2023",
publisher="Springer International Publishing",
address="Cham",
pages="528--539",
abstract="State machines are a common mechanism for defining behaviors in robots where each behavior is based on identifiable stages. There are several libraries available for easing the implementation of state machines in ROS 1, however, the community was focused on SMACH or SMACC. Although these tools are still predominant, there are fewer alternatives for ROS 2. Besides, Behavior Trees are spreading fast, but there is a niche for using State Machines. Here, YASMIN is presented as yet another library specifically designed for ROS 2 for easing the design of robotic behaviors using state machines. It is available in C++ and Python, and provides some default states to speed up the development, in addition to a web viewer for monitoring the execution of the system and helping in the debugging.",
isbn="978-3-031-21062-4"
}
@misc{yasmin,
doi = {10.48550/ARXIV.2205.13284},
url = {https://arxiv.org/abs/2205.13284},
author = {González-Santamarta, Miguel Ángel and Rodríguez-Lera, Francisco Javier and Llamas, Camino Fernández and Rico, Francisco Martín and Olivera, Vicente Matellán},
keywords = {Robotics (cs.RO), FOS: Computer and information sciences, FOS: Computer and information sciences},
title = {YASMIN: Yet Another State MachINe library for ROS 2},
publisher = {arXiv},
year = {2022},
copyright = {Creative Commons Attribution Non Commercial No Derivatives 4.0 International}
}