Controling XL-320 RGB leds in pypot

Here’s a small piece of python to add controls on XL-320 leds.

"""
io_320 monkey patch
Adds accessors to rgb leds.
Ex:
>>> dxl = Dxl320IO(get_available_ports()[0],baudrate=1e6)
>>> dxl._set_LEDRGB({17:5})
"""
from pypot.dynamixel.io import io_320
io_320._add_control('LEDRGB',**{
    'address': 0x19,
    'length': 1,
    'dxl_to_si': lambda v,_ : int(v),
    'si_to_dxl': lambda v,_ : int(v) & 0b111,
    'setter_name': '_set_LEDRGB',
    'getter_name': '_get_LEDRGB'
    })
2 Likes

You can open a pull request for that :slight_smile:

Done.
I’ve also written this example primitive.

class ChristmasBlink(LoopPrimitive):
    """
    _set_LEDRGB example

    >>> poppy = PoppyErgoJr()
    >>> poppy.attach_primitive(ChristmasBlink(poppy, 3),'blink')
    >>> poppy.blink.start()
    """

    def setup(self):
        self.controller_io = self.robot._controllers[0].io
        self.motors = self.robot.motors
        self.motors_sz = len(self.robot.motors)
        self.idx = 0

    def update(self):
        for num, motor in zip(xrange(self.motors_sz),self.motors):
            self.controller_io._set_LEDRGB({ motor.id: (self.idx + num) % 3 })
        self.idx = (self.idx + 1) % self.motors_sz

Accessing to RGB LED via robot._controllers is quite ugly.
There might be a smarter way to do write it, by creating a led controller or whatever.

Thanks @aristofor ! I’ve just merged it:

I add an Enum for the color so now you can do something like:

io.set_LED_color({motor.id: 'pink'})