roboflex


Nameroboflex JSON
Version 0.1.36 PyPI version JSON
download
home_pagehttps://github.com/flexrobotics/roboflex
SummaryRoboflex Core Library: a C++20 and python library for distributed robotics and automation.
upload_time2024-03-06 21:37:38
maintainer
docs_urlNone
authorColin Prepscius
requires_python>=3.6
licenseMIT
keywords robotics middleware flexbuffers python c++ c++20
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # roboflex.core

At its core, roboflex is a library to make `Node`s that create, signal, and receive `Message`s to and from other nodes, in a distributed manner.

![](roboflex_nodes_messages_2.png)

Roboflex.core defines what a `Message` is and what a `Node` is. It provides serialization services for eigen and xtensor. It provides a small library of core message types and useful Node sub-classes. The core only supports sending messages via direct function call to other nodes; the nodes in transport/ (zmq and mqtt so far) support sending messages from one thread to another, from one process to another, and from one computer to another via multiple methods.

One node may connect to multiple downstream nodes, supporting the pub-sub pattern.

## Basic Types

### Message

A roboflex Message is defined as:
1. An 8-byte header. The first four bytes are the letters 'RFLX', and the next four bytes
are a uint32, which is the size of the message in bytes (including the header).
2. A data portion, encoded in flexbuffers.h. Please see [MESSAGEFORMAT.md](MESSAGEFORMAT.md).
3. roboflex.core provides the Message class to facilitate this, as well as meta data and 0-copy functionality. This class is designed to be inherited. Please see [core_messages.h](core_messages/core_messages.h) for examples.

### Node

A roboflex Node represents a basic unit of computation. Nodes can connect to other nodes, can signal Messages, and can receive them. RunnableNode inherits from Node, and adds the ability to run a function in a thread. Both Node and Runnable are designed to be inherited: in order to perform custom logic on message reception, custom nodes should inherit from Node and override receive. In order to run in a thread, custom nodes should inherit RunnableNode and override child_thread_fn.


#### Nodes are designed for sub-classing, in python:

    class MyNode(roboflex.Node):
        def receive(self, msg):
            signal(somenewmsg)
    
    # or, to make a runnable (root) node that runs in a thread:

    class MyRunnable(roboflex.RunnableNode):
        def child_thread_fn(self):
            do whatever
        def start(self):
            override if you want, probably not

#### and in c++:

    struct MyNode: public roboflex::core::Node {
        MyNode(): Node("n007") {}
        void receive(core::MessagePtr m) override {
            std::cout << get_name() << " " << m->to_string() << std::endl;
            signal(m);
        }
    };

    struct MyRunnableNode: public roboflex::core::RunnableNode {
        MyRunnableNode(): roboflex::core::RunnableNode("n121") {}
        void child_thread_fn() override {
            // maybe read from a sensor in a loop? control a robot?
        }
    };

    // The examples in roboflex/core/core_nodes explore subclassing further.


## Building (Only if you're doing c++)

    mkdir build && cd build
    cmake ..
    make
    make install


## Install for python

    pip install roboflex
    

## Just Show Me The Code Example (in python):

    import time
    import numpy as np
    from roboflex import FrequencyGenerator, MapFun, MessagePrinter, CallbackFun


    # This example shows how to create a graph of nodes that pass messages containing
    # numpy tensors (that can be interpreted at the c++-level as xtensor or eigen 
    # objects) between each other in a chain.


    # ----------- 
    # create nodes of the graph

    # The first node will signal at 2 hz. FrequencyGenerator is a library node that
    # simply signals a BlankMessage at a given frequency. It runs in a thread, and must
    # be started and stopped.
    frequency_generator = FrequencyGenerator(2.0)

    # Next, this MapFunction (a node that maps a message to another message) node 
    # creates a message containing a numpy tensor. The python dict will be 
    # encapsulated into a DynoFlex message, and be passed to the next node in 
    # the graph.
    tensor_creator = MapFun(lambda m: {"t": np.ones((2, 3)) * m.message_counter})

    # These nodes print stuff out.
    message_printer = MessagePrinter("MESSAGE IS:")
    tensor_printer = CallbackFun(lambda m: print("TENSOR IS:", type(m["t"]), m["t"].shape, m["t"].dtype, "\n", m["t"]))


    # ----------- 
    # connect nodes of the graph. It's easy to distribute the graph into
    # multiple cpus using nodes in roboflex/transport.
    #
    # 'frequency_generator > tensor_creator' is syntactic sugar for
    # 'frequency_generator.connect(tensor_creator)'.
    #
    frequency_generator > tensor_creator > message_printer > tensor_printer

    # ----------- 
    # start the root node (the other nodes, in this case, will run in the root node's thread).
    frequency_generator.start()

    # ----------- 
    # go for a while
    time.sleep(3)
        
    # ----------- 
    # stop the root node
    frequency_generator.stop()


## Examples

see [examples/README.md](examples/README.md)



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/flexrobotics/roboflex",
    "name": "roboflex",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "robotics,middleware,flexbuffers,python,c++,c++20",
    "author": "Colin Prepscius",
    "author_email": "colinprepscius@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/87/7f/692116f06b77056b72ed70e65258cc57c9105d1045237629a6e831038bad/roboflex-0.1.36.tar.gz",
    "platform": null,
    "description": "# roboflex.core\n\nAt its core, roboflex is a library to make `Node`s that create, signal, and receive `Message`s to and from other nodes, in a distributed manner.\n\n![](roboflex_nodes_messages_2.png)\n\nRoboflex.core defines what a `Message` is and what a `Node` is. It provides serialization services for eigen and xtensor. It provides a small library of core message types and useful Node sub-classes. The core only supports sending messages via direct function call to other nodes; the nodes in transport/ (zmq and mqtt so far) support sending messages from one thread to another, from one process to another, and from one computer to another via multiple methods.\n\nOne node may connect to multiple downstream nodes, supporting the pub-sub pattern.\n\n## Basic Types\n\n### Message\n\nA roboflex Message is defined as:\n1. An 8-byte header. The first four bytes are the letters 'RFLX', and the next four bytes\nare a uint32, which is the size of the message in bytes (including the header).\n2. A data portion, encoded in flexbuffers.h. Please see [MESSAGEFORMAT.md](MESSAGEFORMAT.md).\n3. roboflex.core provides the Message class to facilitate this, as well as meta data and 0-copy functionality. This class is designed to be inherited. Please see [core_messages.h](core_messages/core_messages.h) for examples.\n\n### Node\n\nA roboflex Node represents a basic unit of computation. Nodes can connect to other nodes, can signal Messages, and can receive them. RunnableNode inherits from Node, and adds the ability to run a function in a thread. Both Node and Runnable are designed to be inherited: in order to perform custom logic on message reception, custom nodes should inherit from Node and override receive. In order to run in a thread, custom nodes should inherit RunnableNode and override child_thread_fn.\n\n\n#### Nodes are designed for sub-classing, in python:\n\n    class MyNode(roboflex.Node):\n        def receive(self, msg):\n            signal(somenewmsg)\n    \n    # or, to make a runnable (root) node that runs in a thread:\n\n    class MyRunnable(roboflex.RunnableNode):\n        def child_thread_fn(self):\n            do whatever\n        def start(self):\n            override if you want, probably not\n\n#### and in c++:\n\n    struct MyNode: public roboflex::core::Node {\n        MyNode(): Node(\"n007\") {}\n        void receive(core::MessagePtr m) override {\n            std::cout << get_name() << \" \" << m->to_string() << std::endl;\n            signal(m);\n        }\n    };\n\n    struct MyRunnableNode: public roboflex::core::RunnableNode {\n        MyRunnableNode(): roboflex::core::RunnableNode(\"n121\") {}\n        void child_thread_fn() override {\n            // maybe read from a sensor in a loop? control a robot?\n        }\n    };\n\n    // The examples in roboflex/core/core_nodes explore subclassing further.\n\n\n## Building (Only if you're doing c++)\n\n    mkdir build && cd build\n    cmake ..\n    make\n    make install\n\n\n## Install for python\n\n    pip install roboflex\n    \n\n## Just Show Me The Code Example (in python):\n\n    import time\n    import numpy as np\n    from roboflex import FrequencyGenerator, MapFun, MessagePrinter, CallbackFun\n\n\n    # This example shows how to create a graph of nodes that pass messages containing\n    # numpy tensors (that can be interpreted at the c++-level as xtensor or eigen \n    # objects) between each other in a chain.\n\n\n    # ----------- \n    # create nodes of the graph\n\n    # The first node will signal at 2 hz. FrequencyGenerator is a library node that\n    # simply signals a BlankMessage at a given frequency. It runs in a thread, and must\n    # be started and stopped.\n    frequency_generator = FrequencyGenerator(2.0)\n\n    # Next, this MapFunction (a node that maps a message to another message) node \n    # creates a message containing a numpy tensor. The python dict will be \n    # encapsulated into a DynoFlex message, and be passed to the next node in \n    # the graph.\n    tensor_creator = MapFun(lambda m: {\"t\": np.ones((2, 3)) * m.message_counter})\n\n    # These nodes print stuff out.\n    message_printer = MessagePrinter(\"MESSAGE IS:\")\n    tensor_printer = CallbackFun(lambda m: print(\"TENSOR IS:\", type(m[\"t\"]), m[\"t\"].shape, m[\"t\"].dtype, \"\\n\", m[\"t\"]))\n\n\n    # ----------- \n    # connect nodes of the graph. It's easy to distribute the graph into\n    # multiple cpus using nodes in roboflex/transport.\n    #\n    # 'frequency_generator > tensor_creator' is syntactic sugar for\n    # 'frequency_generator.connect(tensor_creator)'.\n    #\n    frequency_generator > tensor_creator > message_printer > tensor_printer\n\n    # ----------- \n    # start the root node (the other nodes, in this case, will run in the root node's thread).\n    frequency_generator.start()\n\n    # ----------- \n    # go for a while\n    time.sleep(3)\n        \n    # ----------- \n    # stop the root node\n    frequency_generator.stop()\n\n\n## Examples\n\nsee [examples/README.md](examples/README.md)\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Roboflex Core Library: a C++20 and python library for distributed robotics and automation.",
    "version": "0.1.36",
    "project_urls": {
        "Homepage": "https://github.com/flexrobotics/roboflex"
    },
    "split_keywords": [
        "robotics",
        "middleware",
        "flexbuffers",
        "python",
        "c++",
        "c++20"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "877f692116f06b77056b72ed70e65258cc57c9105d1045237629a6e831038bad",
                "md5": "ee32bf5735a9016542655ef14be03d9d",
                "sha256": "a78b7d7e9ffb8853c08698163507058879dffe8bad11ce5c3a9ce74c463af60b"
            },
            "downloads": -1,
            "filename": "roboflex-0.1.36.tar.gz",
            "has_sig": false,
            "md5_digest": "ee32bf5735a9016542655ef14be03d9d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 76447,
            "upload_time": "2024-03-06T21:37:38",
            "upload_time_iso_8601": "2024-03-06T21:37:38.162225Z",
            "url": "https://files.pythonhosted.org/packages/87/7f/692116f06b77056b72ed70e65258cc57c9105d1045237629a6e831038bad/roboflex-0.1.36.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-06 21:37:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "flexrobotics",
    "github_project": "roboflex",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "roboflex"
}
        
Elapsed time: 0.21469s