Processor
In this section we will discuss the concept of a Processor.
A processor can be used to convert messages without changing the data type.
Therefore, creating a Processor is similar to creating a Converter, but instead of having two message types, there is only one.
In this section we will go through the process of creating the AngleDecomposition Processor.
This will decompose one of the entries of a Float32MultiArray into a sine and cosine component.
This processor can be used when dealing with angular positions, since learning on the sine and cosine is often more efficient due to the discontinuities in the angular position.
The Processor base class has one class variable:
MSG_TYPE
and has 3 abstract methods:
spec()
MSG_TYPE
The class variable MSG_TYPE is the type of the message that will processed.
The message type will be a Float32MultiArray, since this ROS message can be used for multidimensional data communication over ROS topics.
# ROS IMPORTS
from std_msgs.msg import Float32MultiArray
# RX IMPORTS
import eagerx.core.register as register
from eagerx import Processor
import numpy as np
class AngleDecomposition(Processor):
MSG_TYPE = Float32MultiArray
spec
The spec() method can be used to specify with which arguments the Processor will be initialized.
In our case, we add angle_idx to the config.
@staticmethod
@register.spec("AngleDecomposition", Processor)
def spec(spec, angle_idx: int = 0):
spec.config.angle_idx = angle_idx
Note
Mind the use of the spec() decorator.
initialize
Next, we implement the initialize() method.
Here, the arguments is the ones we have just defined in the spec() method: angle_idx.
def initialize(self, dtype="float32"):
self.angle_idx = angle_idx
convert
The convert() method takes as an argument a message of type MSG_TYPE and processes it.
def convert(self, msg):
if msg.data == []:
return msg
angle = msg.data[self.angle_idx]
new_data = np.concatenate(([np.sin(angle), np.cos(angle)], msg.data[self.angle_idx + 1 :]), axis=0)
return Float32MultiArray(data=new_data)
make
In order to use this Processor, the user should call the make() method with the arguments of the spec() method.