.. _howto: How To Guides ============= .. _scxml_howto: High Level SCXML Model Implementation ------------------------------------- SCXML is the language of choice to model the autonomous systems that are processed by AS2FM. It relies on multiple SCXML files, each one representing a different state-based automaton, to represent a complete system. Those automata can exchange data and synchronize their execution through the use of **events**. A simple, exemplary SCXML model is shown below: .. code-block:: xml In this example, the SCXML model consists of three states, `s0`, `s1`, and `s2`, and three transitions, triggered by the events `e1`, `e2` and `e3`, respectively. Each transition advances the automaton's current state to the next one. Additionally, on each transition, a counter is incremented. The events are expected to be sent by another SCXML model, similarly to how it is done in the `s2` state. In order to make SCXML fit more to the typical robotics tech-stack, we extended the default SCXML language to support ROS specific features and Behavior Trees. The following sections guide you through the process of :ref:`creating a SCXML model of a ROS node ` and of a :ref:`BT plugin ` that can be processed by AS2FM. .. _ros_node_scxml: Creating an SCXML model of a ROS node ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In AS2FM, we extended the SCXML language with some additional functionalities, to support the following ROS specific features: * :ref:`ROS Timers `: to trigger callbacks at a specific rate * :ref:`ROS Topics `: to publish-receive messages via a ROS topic * :ref:`ROS Services `: to call a ROS service and implement service servers * :ref:`ROS Actions `: to call a ROS action and implement action servers (under development) All functionalities require the interface to be declared before being used, similarly to how ROS requires the interfaces to be declared in a node. In (ROS) SCXML, this is done similarly to how variables are defined in the data model. .. _ros_timers: ROS Timers ___________ ROS Timers are used to trigger callbacks (behaving like an SCXML transition) at a specific rate. They can be declared as follows: .. code-block:: xml This will create a ROS timer that triggers the related callbacks at a rate of 1 Hz, w.r.t. the internal, simulated time. The timer callbacks can be used similarly to SCXML transitions, and are specified as follows: .. code-block:: xml Assuming the automaton is in the `src_state`, the transition to `target_state` will be triggered by the timer `my_timer`, if the condition `cond_expression` holds. Additionally, the internal variable `internal_var` will be updated with the value of `some_expression` when that transition is performed. .. _ros_topics: ROS Topics ___________ ROS topics are used to publish (via a ROS Publisher) and receive (via a ROS Subscriber) messages via a ROS topic across different automata. They can be declared as follows: .. code-block:: xml The two declarations above will create a ROS subscriber called `bool_topic` that reads messages of type `std_msgs/Bool` from the topic `/topic1` and a ROS publisher called `int_topic` that writes messages of type `std_msgs/Int32` on the topic `/topic2`. The `name` argument is optional, and if not provided, it will be set to the same value as the `topic` argument. Once created, subscribers and publishers can be referenced using their names (`bool_topic` and `int_topic`), and can be used in the states to send messages and perform callbacks upon receiving messages: .. code-block:: xml Note that the `ros_topic_publish` can be used where one would normally use executable content in SCXML: in `transition`, in `onentry` and `onexit` tags. The `ros_topic_callback` tag is similarly to the `ros_rate_callback` used like a transition and will transition the state machine to the state declared in `target` upon receiving a message. Executable content within it can use `_msg` to access the message content. .. _ros_services: ROS Services ____________ ROS services are used to provide, for a given service name, one server and, possibly, multiple clients. The clients make a request and the server provides a response to that request only to the client that made the request. The declaration of a ROS service server and the one of a client can be achieved like this: .. code-block:: xml Once created, servers and clients can be referenced using the provided `name` (i.e., `the_srv` and `the_client`), and can be used in the states of an SCXML model to provide and request services. In the following, an exemplary client is provided: .. code-block:: xml To send a request, the `ros_service_send_request` can be used where any other executable content may be used. After the server has processed the service, `ros_service_handle_response` can be used similarly to an SCXML transition and is triggered when a response from the server is received. The data of the request can be accessed with the `_res` field. And here comes an example of a server: .. code-block:: xml A service request from a client will trigger the `ros_service_handle_request` callback which transitions the automaton to the state declared in `target` (it is a self loop in the example). After processing the request the server must use the `ros_service_send_response` to send the response. .. _ros_actions: ROS Actions ___________ TODO .. _bt_plugin_scxml: Creating an SCXML model of a BT plugin ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ As for ROS nodes, in AS2FM we support the implementation of custom BT plugins using ROS-SCXML. Since BT plugins rely on a specific interface, we extended the SCXML language to support the following features: * :ref:`BT communication `: A set of XML tags for modeling the BT Communication interface, based on BT ticks and BT responses. * :ref:`BT Ports `: A special BT interface to parametrize a specific plugin instance. .. _bt_communication: BT Communication _________________ Normally, a BT plugin (or BT node), is idle until it receives a BT tick from a control node. The BT tick is used to trigger the execution of the BT plugin, which will then return a BT response to the control node that sent the tick. The BT plugin `AlwaysSuccess`, that returns `SUCCESS` each time it is ticked, can be implemented as follows: .. code-block:: xml In this example, there is only the `idle` state, always listening for an incoming `bt_tick` event. When the tick is received, the plugin starts executing the body of the `bt_tick` tag, that returns a `SUCCESS` response and starts listening for a new `bt_tick`. The BT plugin could receive also an `halt` request from the BT controller, that starts the execution of the `bt_halt` body. In this example the `bt_halt` body contains only the `bt_return_halted` tag, that signals to the node that requested the halt that this was handled. All BT plugins are expected to contain at least `bt_tick` and `bt_halt` tags. Additionally, it is possible to model BT control nodes, that can send ticks to their children (that, in turns, are BT nodes as well) and receive their responses: .. code-block:: xml In this example, the `Inverter` control node waits for a tick, then sends a tick to its child (identified by the id `0`), and waits for the response. Once the child response is available, the control node inverts the response and sends it back to the control node that ticked it in the first place. Similarly, in case it receives a halt request, the node sends a halt request to its child and waits for its response, before responding to its parent node that the halting request was fulfilled. In this model, the `CHILDREN_COUNT` BT port is used to access the number of children of a control node instance, to check it is correctly configured. Additional control nodes implementations are available in the `src/as2fm/resources `_ folder, and can be used as a reference to implement new ones. .. _bt_ports: BT Ports ________ When loading a BT plugin in the BT XML tree, it is possible to configure a specific plugin instance by means of the BT ports. As in the case of ROS functionalities, BT ports need to be declared before being used, to provide the port name and expected type. .. code-block:: xml Once declared, it is possible to reference to the port in multiple SCXML entries. For example, we can use `my_string_port` to define the topic used by a ROS publisher. .. code-block:: xml Or we can use `start_value` to define the initial value of a variable. .. code-block:: xml Finally, we can store a specific value to the blackboard (only for output ports). .. code-block:: xml ... ... ... BT Ports can be declared either as input or output ports: * input ports can refer to either fixed or mutable variables (i.e. blackboard variables) * output ports on only refer to mutable variables When a BT plugin declares an output port, this must be referenced to a `BT Blackboard` variable. This is defined in the BT XML file, by providing a blackboard variable name wrapped by curly braces. .. _main_xml_howto: The System Description (High Level XML file) --------------------------------------------- This file references all the components defining the system, including the Behavior Tree, its plugins and the additional nodes that might be running on the side. Additionally, it contains additional configuration for the model, e.g. the maximum time the clock can reach or the tick rate of a Behavior Tree. An exemplary system description is the following: .. code-block:: xml .. _mc_parameters: Available Parameters ~~~~~~~~~~~~~~~~~~~~~ AS2FM provides a number of parameters to control the generation of the formal model. They are all contained in the tag ``. Max Time ____________ The maximum time the global clock is allowed to reach. The tag is called `max_time`. The `value` argument is the max time, and the argument `unit` specifies the time unit of the provided value. Supported units are `s`, `ms`, `us`, `ns`. For example `` would allow the model to run for 100 seconds. Max Array Size _________________ The maximum size assigned to a dynamic array. The tag is called `max_array_size`. The `value` argument defines the max size the dynamic array can reach, and is 100 by default. For example `` would allow dynamic arrays to contain up tp 100 entries. BT Tick Rate _________________ The tick rate of the Behavior Tree (in Hz). The tag is called `bt_tick_rate`. The `value` argument defines the tick rate in Hz, and is 1.0 by default. For example `` would tick the behavior tree with a frequency of _10Hz_. BT Tick If Not Running _________________________ Whether we shall keep ticking a Behavior Tree after it returns something different from `RUNNING` (i.e. `SUCCESS` or `FAILURE`). The tag is called `bt_tick_if_not_running`. The `value` argument enables or disables the ticking of non-running Behavior Trees, and is set to `false` by default. After the tree is stopped, the model execution will stop as well. For example `` would stop ticking the tree after it returned either _SUCCESS_ or _FAILURE_.