iRobotEducation / irobot-edu-python-sdk

Python SDK for iRobot Edu robots (Root or Create 3)
BSD 3-Clause "New" or "Revised" License
16 stars 6 forks source link

Docking sensor event not exist #11

Closed scottcandy34 closed 8 months ago

scottcandy34 commented 1 year ago

I have create3 robot Firmware version (G.3.1) and have found that the docking sensor event has not been added yet for the robot found here https://github.com/iRobotEducation/root-robot-ble-protocol#command-0---docking-sensor-event

I have done an override class to implement it

class DockingSensor:
    def __init__(self):
        self.contact = 0
        self.sensor_0 = 0
        self.sensor_1 = 0

class Create3(Create3):
    def __init__(self, backend):
        super().__init__(backend)

        self._events[(19, 0)] = self._when_docking_sensor_handler

        self.docking_sensor = DockingSensor()

        self._when_docking_sensor: list[Event] = []

    def when_docking_sensor(self, callback: Callable[[bool], Awaitable[None]]):
        self._when_docking_sensor.append(Event(True, callback))

    async def _when_docking_sensor_handler(self, packet):
        for event in self._when_docking_sensor:
            if len(packet.payload) > 4:
                self.docking_sensor.contact = packet.payload[4] != 0
                self.docking_sensor.sensor_0 = packet.payload[5] != 0
                self.docking_sensor.sensor_1 = packet.payload[6] != 0
                await event.run(self)

I only did a simple implementation of this. Can this be added to the create3.py, maybe with [int, int, int] or something similar to have a more specific control over what it is sensing.

shamlian commented 8 months ago

The basics of this are in pre_0.5.0 and I will plan to improve the docking sensor event and getter in a future update. See https://github.com/iRobotEducation/irobot-edu-python-sdk/issues/30 . I will close this when I merge 0.5.0 to main.

shamlian commented 7 months ago

Because I saw your other question -- there was some merge weirdness here because someone pressed the wrong button, and then I did a bad job rewriting the history. But rest assured, this is merged in.

scottcandy34 commented 7 months ago

Ah, I figured once I dug into it I noticed that it was merged so I deleted my comment. I like what you did with it much better and improved.