Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/uleroboticsgroup/yasmin

YASMIN (Yet Another State MachINe)
https://github.com/uleroboticsgroup/yasmin

cpp python ros2 state-machine

Last synced: 2 months ago
JSON representation

YASMIN (Yet Another State MachINe)

Lists

README

        

# YASMIN (Yet Another State MachINe)



YASMIN is a project focused on implementing robot behaviors using Finite State Machines (FSM). It is available for ROS 2, Python and C++.

## Table of Contents

1. [Features](#features)
2. [Installation](#installation)
3. [Demos](#demos)
- [Python](#python)
- [Cpp](#cpp)
4. [YASMIN Viewer](#yasmin-viewer)
5. [Citations](#citations)

## Features

- Fully integrated into ROS 2.
- Available for Python and C++.
- Fast prototyping.
- Default states for ROS 2 action and service clients.
- Blackboards are used to share data between states and state machines.
- State machines can be canceled and stopped, which means stopping the current executing state.
- A web viewer is included, which allows monitoring of the execution of the state machines.

## Installation

```shell
# clone
$ cd ~/ros2_ws/src
$ git clone https://github.com/uleroboticsgroup/simple_node.git
$ git clone https://github.com/uleroboticsgroup/yasmin.git

# dependencies
$ cd yasmin
$ pip3 install -r requirements.txt

# colcon
$ cd ~/ros2_ws
$ colcon build
```

## Demos

A Python and a C++ demo are included.



### Python

```python
#!/usr/bin/env python3

import time
import rclpy

from simple_node import Node

from yasmin import State
from yasmin import StateMachine
from yasmin_viewer import YasminViewerPub

# define state Foo
class FooState(State):
def __init__(self):
super().__init__(["outcome1", "outcome2"])
self.counter = 0

def execute(self, blackboard):
print("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"

# define state Bar
class BarState(State):
def __init__(self):
super().__init__(outcomes=["outcome3"])

def execute(self, blackboard):
print("Executing state BAR")
time.sleep(3)

print(blackboard.foo_str)
return "outcome3"

class DemoNode(Node):

def __init__(self):
super().__init__("yasmin_node")

# create a state machine
sm = StateMachine(outcomes=["outcome4"])

# add states
sm.add_state("FOO", FooState(),
transitions={"outcome1": "BAR",
"outcome2": "outcome4"})
sm.add_state("BAR", BarState(),
transitions={"outcome3": "FOO"})

# pub
YasminViewerPub(self, "YASMIN_DEMO", sm)

# execute
outcome = sm()
print(outcome)

# main
def main(args=None):

print("yasmin_demo")
rclpy.init(args=args)
node = DemoNode()
node.join_spin()
rclpy.shutdown()

if __name__ == "__main__":
main()
```

### Cpp

```cpp
#include
#include
#include
#include

#include "simple_node/node.hpp"

#include "yasmin/state.hpp"
#include "yasmin/state_machine.hpp"
#include "yasmin_viewer/yasmin_viewer_pub.hpp"

// define state Foo
class FooState : public yasmin::State {
public:
int counter;

FooState() : yasmin::State({"outcome1", "outcome2"}) { this->counter = 0; };

std::string
execute(std::shared_ptr blackboard) {
std::cout << "Executing state FOO\n";
std::this_thread::sleep_for(std::chrono::seconds(3));

if (this->counter < 3) {
this->counter += 1;
blackboard->set("foo_str",
"Counter: " + std::to_string(this->counter));
return "outcome1";

} else {
return "outcome2";
}
}

std::string to_string() { return "FooState"; }
};

// define state Bar
class BarState : public yasmin::State {
public:
BarState() : yasmin::State({"outcome3"}){};

std::string
execute(std::shared_ptr blackboard) {
std::cout << "Executing state BAR\n";
std::this_thread::sleep_for(std::chrono::seconds(3));

std::cout << blackboard->get("foo_str") << "\n";

return "outcome3";
}

std::string to_string() { return "BarState"; }
};

class DemoNode : public simple_node::Node {
public:
std::unique_ptr yamin_pub;

DemoNode() : simple_node::Node("yasmin_node") {

// create a state machine
auto sm = std::make_shared(
yasmin::StateMachine({"outcome4"}));

// add states
sm->add_state("FOO", std::make_shared(),
{{"outcome1", "BAR"}, {"outcome2", "outcome4"}});
sm->add_state("BAR", std::make_shared(), {{"outcome3", "FOO"}});

// pub
this->yamin_pub = std::make_unique(
yasmin_viewer::YasminViewerPub(this, "YASMIN_DEMO", sm));

// execute
std::string outcome = (*sm.get())();
std::cout << outcome << "\n";
}
};

int main(int argc, char *argv[]) {

std::cout << "yasmin_demo\n";
rclcpp::init(argc, argv);
auto node = std::make_shared();
node->join_spin();
rclcpp::shutdown();

return 0;
}
```

## YASMIN Viewer

This viewer allows monitoring YASMIN's FSM. It is implemented with Flask and ReactJS. A filter is provided to show only one FSM.

![](./docs/viewer.gif)

### Usage

```shell
$ ros2 run yasmin_viewer yasmin_viewer_node
```

http://localhost:5000/

### Also you can custom host and port

```shell
$ ros2 run yasmin_viewer yasmin_viewer_node --ros-args -p host:=127.0.0.1 -p port:=5032
```

http://127.0.0.1:5032/

## Citations

```bibtex
@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"
}

```

```bibtex
@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}
}
```