Package: micropython-rosserial
uPy-rosserialrosserialis a method used by ROS in order to establish communication via serial, basically a middleware, mostly used with microcontrollers, which in this case are the ones responsible in some ROS applications for actuators and sensors usage.This library targets the communication between ROS and uPy with rosserial as middleware.Note: All of this library was coded using an ESP32, can't guarantee to work with other boards.FeaturesAdvertising TopicsPublishingSubscribingServicesTo Do: Implement services usage.InstallationBefore using this library you must have ROS installed, as well as rosserial which would be with the following command:sudo apt install ros-<version>-rosserial ros-<version>-rosserial-arduinoIn theory every board with the kind of genericUARTclass for ESP32 is capable of using this library, but it must be known exactly whichUART IDis being used, this means for example, for ESP32 defined pins correspond to TX0 and RX0 for UART0, and so on. In the examples below, UART2 is used.Copying source filesIn order to use ros node communication, have in mind a python class for each message must be available. this means a dependency of this library isuPy GenpyanduPy rosserial_msgs,ugenpyused to create Python classes for messages from*.msgfiles whilerosserial_msgshas theTopicInfoclass for topic negotiation. The folders fromsrcfrom this current repo and the other two must be copiedI strongly recommend usingrshell.Using upipNow available with upip, could be installed with:importupipupip.install('micropython-rosserial')Ifmicropython-rosserialis installed, because of requirementes,ugenpyandTopicInfowill too.Note: must be connected to WiFi to use upip like this.Have in mind before publishing or subscribing to a topic, the message class must be generated withugenpyUsageEverytime before establishing rosserial communication, this command must be run, even before running the script in uPy, will be improved afterwards:rosrun rosserial_arduino serial_node.py _port:=/dev/ttyUSB0 _baud:=115200Note port and baudrate can be changed, in ESP32 I prefer using 115200 for baudrate.Publish exampleSupposeColorRGBA.pyhas been created usingugenpyandColorRGBA.msgfile:float32 r
float32 g
float32 b
float32 aAnd then running the following e.g.:importurosfromstd_msgsimportColorRGBA#message object ColorRGBAfromtimeimportsleepnode=uros.NodeHandle(2,115200)#node initialized, for tx2/rx2 and 115200 baudratemsg=ColorRGBA()#msg object initmsg.r=1#values to variables assignedmsg.g=3msg.b=4msg.a=1whileTrue:node.publish('Colorsh',msg)#publish data to node Colorshsleep(1)Subscribe exampleimporturosfromstd_msgsimportStringdefcb(msg):print(msg.data)node=uros.NodeHandle(2,115200)node.subscribe('chatter',String,cb)Mixed exampleimporturosfromstd_msgsimportStringdefcb(msg):print(msg.data)packet=String()packet.data='hola fpy'node=uros.NodeHandle(2,115200)node.subscribe('chatter',String,cb)whileTrue:node.publish('greet',packet)Classesuros.NodeHandleConstructoruros.NodeHandle(serial_id = 2, baudrate = 115200, **kwargs)Initiates the class which handles the node, advertised topics, publishes and subscribe.serial_id: corresponds to the UART ID, in case of ESP32, it has 3 UARTS, in the examples UART2 is used.baudrate: is the baudrate in which the board will communicate.**kwargs: used in order to be able to define the serial communication with uart in different ways, in case a custom UART object is wanted to be used, it should be defined asuros.NodeHandle(serial = uart_object)or in case certaintxandrxpins are wanted for the uart with certain ID, it should be likenode = uros.NodeHandle(2, 115200, tx=16, rx= 16).Methodsuros.NodeHandle.publish(topic_name, msg, buffer_size=1024)Publishes data to a defined topic, with a defined message class.topic_name: the topic where the message will be put or published.msg: the msg class initiated with its slots values defined.buffer_size: the amount of bytes that will be published as a maximum from this particular topic, 1024 is by default.uros.NodeHandle.subscribe(topic_name, msgobj, cb, buffer_size=1024)Subscribe to a defined topic.topic_name: same as publish.msgobj: is the object class, but not instantiated, just the class passed.cb: must be defined, it is a callback function, with a single argument that corresponds to the inconming message class.buffer_size: same as publish.