flojoy-ai / blocks

Flojoy Standard Blocks Library (now merged into flojoy-ai/studio)
https://blocks.flojoy.ai
4 stars 1 forks source link

[BLO-92] ROS 2 [RFC]: Stateful Blocks #29

Open IsabelParedes opened 8 months ago

IsabelParedes commented 8 months ago

Context

Example of a minimal ROS 2 publisher block:


@flojoy
def ROS_PUBLISHER():

    ros_pub = Publisher()
    ros_pub.publish_message("Some message")
    rclpy.spin(ros_pub)
    ros_pub.destroy_node()

    return

Generally, ROS nodes are spun with rclpy.spin(ros_node).

rclpy.spin creates an event loop, which repeatedly checks for events and handles them It is essentially an infinite loop that repeatedly calls spin_once()

Publishers have a timer which determines the frequency at which messages are published to a given topic. Each spin of a publisher is triggered by the the timer.

For subscribers, they spin every time a new message is published to the given topic.

Problem

Since block execution in flojoy is function based, the publisher must be created and destroyed every time the ROS_PUBLISHER block is executed. This is inefficient and also poses a problem when the user wants to publish messages repeatedly.

Under normal circumstances, a ROS node will spin indefinitely until the node is killed. However, doing this in a flojoy block would impede the execution of any other blocks.

Proposed Solution

Enable the storage of block states. When a publisher or a subscriber are created, they should be reusable to save resources. This would also allow us to publish a message and return control to the user without blocking execution.

References

From SyncLinear.com | BLO-92

TheBigSasha commented 8 months ago

The saved resources solution is similar to how we handle the connections to the Mecademic robot. Check out studio/PYTHON/utils/mecademic_state -- maybe you could use some of our solution or come up with something better that might improve hardware connections in general

It may also be relevant to check out the work on the reactive nodes system, as it addresses a lot of the same problems that this is solving :)