Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 110
extend-ignore = E203
exclude = .venv, .git, __pycache__, build, dist
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: CI

on: [push, pull_request]
on:
pull_request:
branches:
- dev
- main

jobs:
build:
Expand All @@ -13,10 +17,9 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install -r requirements.txt
- name: Lint with flake8
run: |
pip install flake8
flake8 src tests
- name: Run tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"useTabs": false,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 120,
"printWidth": 110,
"semi": true,
"endOfLine": "lf",
"overrides": [
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"python.analysis.typeCheckingMode": "basic",
"python.analysis.autoImportCompletions": true,
"python.defaultInterpreterPath": ".venv/bin/python",
"python.terminal.activateEnvInCurrentTerminal": true
"python.terminal.activateEnvInCurrentTerminal": true,
"cSpell.words": ["MQTT"]
}
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ apidoc:
html:
$(SPHINXBUILD) -b html "$(SOURCEDIR)" "$(BUILDDIR)/html"

livehtml:
livehtml:clean
sphinx-autobuild --host 0.0.0.0 --port ${PORT} -c . "$(SOURCEDIR)" "$(BUILDDIR)/html"
8 changes: 8 additions & 0 deletions docs/api/robot.communication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ robot.communication package
Submodules
----------

robot.communication.communication module
----------------------------------------

.. automodule:: robot.communication.communication
:members:
:show-inheritance:
:undoc-members:

robot.communication.directed\_comm module
-----------------------------------------

Expand Down
10 changes: 10 additions & 0 deletions docs/api/robot.exception.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
robot.exception package
=======================

Module contents
---------------

.. automodule:: robot.exception
:members:
:show-inheritance:
:undoc-members:
8 changes: 8 additions & 0 deletions docs/api/robot.helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ robot.helpers.coordinate module
:show-inheritance:
:undoc-members:

robot.helpers.motion\_controller module
---------------------------------------

.. automodule:: robot.helpers.motion_controller
:members:
:show-inheritance:
:undoc-members:

robot.helpers.robot\_mqtt module
--------------------------------

Expand Down
8 changes: 8 additions & 0 deletions docs/api/robot.indicators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ robot.indicators package
Submodules
----------

robot.indicators.abstract\_indicator module
-------------------------------------------

.. automodule:: robot.indicators.abstract_indicator
:members:
:show-inheritance:
:undoc-members:

robot.indicators.neopixel module
--------------------------------

Expand Down
10 changes: 10 additions & 0 deletions docs/api/robot.interfaces.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
robot.interfaces package
========================

Module contents
---------------

.. automodule:: robot.interfaces
:members:
:show-inheritance:
:undoc-members:
29 changes: 29 additions & 0 deletions docs/api/robot.mqtt.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
robot.mqtt package
==================

Submodules
----------

robot.mqtt.mqtt\_msg module
---------------------------

.. automodule:: robot.mqtt.mqtt_msg
:members:
:show-inheritance:
:undoc-members:

robot.mqtt.robot\_mqtt\_client module
-------------------------------------

.. automodule:: robot.mqtt.robot_mqtt_client
:members:
:show-inheritance:
:undoc-members:

Module contents
---------------

.. automodule:: robot.mqtt
:members:
:show-inheritance:
:undoc-members:
12 changes: 12 additions & 0 deletions docs/api/robot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ Subpackages
:maxdepth: 4

robot.communication
robot.exception
robot.helpers
robot.indicators
robot.interfaces
robot.mqtt
robot.sensors
robot.types

Submodules
----------
Expand Down Expand Up @@ -39,6 +43,14 @@ robot.robot\_base module
:show-inheritance:
:undoc-members:

robot.virtual\_robot module
---------------------------

.. automodule:: robot.virtual_robot
:members:
:show-inheritance:
:undoc-members:

Module contents
---------------

Expand Down
8 changes: 8 additions & 0 deletions docs/api/robot.sensors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ robot.sensors package
Submodules
----------

robot.sensors.abstract\_sensor module
-------------------------------------

.. automodule:: robot.sensors.abstract_sensor
:members:
:show-inheritance:
:undoc-members:

robot.sensors.color module
--------------------------

Expand Down
29 changes: 29 additions & 0 deletions docs/api/robot.types.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
robot.types package
===================

Submodules
----------

robot.types.proximity\_reading\_type module
-------------------------------------------

.. automodule:: robot.types.proximity_reading_type
:members:
:show-inheritance:
:undoc-members:

robot.types.rgb\_color\_type module
-----------------------------------

.. automodule:: robot.types.rgb_color_type
:members:
:show-inheritance:
:undoc-members:

Module contents
---------------

.. automodule:: robot.types
:members:
:show-inheritance:
:undoc-members:
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ Welcome to the documentation for the Python robot library. This site includes in
README
installation
usage
api/index
api/modules
```
43 changes: 43 additions & 0 deletions examples/my_test_robot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import annotations

import threading
import time

from robot import MQTTSettings, VirtualRobot
from robot.interfaces import RobotState


class MyTestRobot(VirtualRobot):
def setup(self) -> None: # type: ignore[override]
print("My Test Robot Started")
super().setup()

def loop(self) -> None: # type: ignore[override]
super().loop()
if self.state == RobotState.RUN:
print("Test")
self.delay(1000)

def communication_interrupt(self, msg: str) -> None:
print(
f"communicationInterrupt on {self.id} "
f"with msg:{msg}"
)


if __name__ == "__main__":
# Configure MQTT (fill with your broker details)
MQTTSettings.server = "localhost"
MQTTSettings.port = 1883
MQTTSettings.user_name = ""
MQTTSettings.password = ""
MQTTSettings.channel = "v1"

robot = MyTestRobot(10, 0, 0, 90)
t = threading.Thread(target=robot.run, daemon=True)
t.start()

# Example to send start after a short delay
time.sleep(1)
robot.start()
t.join()
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ version = "0.1.0"
description = "A modular robot control library"
authors = [
{ name = "Nuwan Jaliyagoda", email = "[email protected]" },
{ name = "Nuwan Jaliyagoda", email = "[email protected]" },
{ name = "Kavindu Prabhath Methpura", email = "[email protected]" },
]
readme = "README.md"
Expand Down
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
paho-mqtt==2.1.0
flake8==7.3.0
packaging==25.0
paho-mqtt==2.1.0
pytest==8.4.2
12 changes: 9 additions & 3 deletions src/robot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
"""Top-level package for robot library."""
"""Top-level package for robot library (Python port of robot-library-java)."""

from .robot_base import Robot
from .mqtt_client import RobotMqttClient
from .motion import MotionController
from .virtual_robot import VirtualRobot

# Expose common subpackages
from .configs.mqtt_settings import MQTTSettings
from .configs.robot_settings import RobotSettings

__all__ = [
"Robot",
"RobotMqttClient",
"MotionController",
"VirtualRobot",
"MQTTSettings",
"RobotSettings",
]
16 changes: 16 additions & 0 deletions src/robot/communication/communication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import annotations

from robot.interfaces import IMqttHandler
from robot.mqtt.robot_mqtt_client import RobotMqttClient


class Communication(IMqttHandler):
def __init__(self, robot_id: int, mqtt_client: RobotMqttClient):
self.robot_mqtt_client = mqtt_client
self.robot_id = robot_id

def send_message(self, msg: str) -> None: # abstract
raise NotImplementedError

def send_message_with_distance(self, msg: str, distance: int) -> None: # abstract
raise NotImplementedError
36 changes: 32 additions & 4 deletions src/robot/communication/directed_comm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
"""Directed communication module."""
"""Directed communication module mirroring Java logic."""

from __future__ import annotations

class DirectedCommunication:
"""Placeholder directed communication class."""
import json

pass
from robot.communication.communication import Communication
from robot.mqtt.mqtt_msg import MqttMsg
from robot.mqtt.robot_mqtt_client import RobotMqttClient


class DirectedCommunication(Communication):
def __init__(self, robot_id: int, mqtt_client: RobotMqttClient):
super().__init__(robot_id, mqtt_client)
self._topics_sub: dict[str, str] = {}
self._subscribe("COMMUNICATION_IN_DIR", f"comm/in/direct/{robot_id}")

def _subscribe(self, key: str, topic: str) -> None:
self._topics_sub[key] = topic
self.robot_mqtt_client.subscribe(topic)

def send_message(self, msg: str) -> None:
obj = {"id": self.robot_id, "msg": msg}
self.robot_mqtt_client.publish("comm/out/direct", json.dumps(obj))

def send_message_with_distance(self, msg: str, distance: int) -> None:
obj = {"id": self.robot_id, "msg": msg, "dist": distance}
self.robot_mqtt_client.publish("comm/out/direct", json.dumps(obj))

def handle_subscription(self, robot, m: MqttMsg) -> None:
topic, msg = m.topic, m.message
if topic == self._topics_sub.get("COMMUNICATION_IN_DIR"):
robot.communication_interrupt(msg)
else:
print(f"Received (unknown dir): {topic}> {msg}")
Loading