-
Notifications
You must be signed in to change notification settings - Fork 1
Basic porting of the Java code into Python #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NuwanJ
wants to merge
9
commits into
dev
Choose a base branch
from
java-to-python-porting
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2192c4c
Add MQTT and robot settings modules, restructure imports, and enhance…
NuwanJ c8d1fe5
Update CI workflow to trigger on pull requests for dev and main branches
NuwanJ 11d0e37
Add flake8 configuration, refactor code for consistency, and improve …
NuwanJ 82ee927
Update CI workflow to install dependencies and add flake8 and pytest …
NuwanJ a24e223
fix: Address CoPilot settings
NuwanJ 6cf4e7d
fix: Improve debug output for unknown message handling in ColorSensor
NuwanJ bae534f
Merge remote-tracking branch 'origin/dev' into java-to-python-porting
NuwanJ db6df50
docs: Update API documentation to include new modules and correct ind…
NuwanJ 387b21c
Merge remote-tracking branch 'origin/dev' into java-to-python-porting
NuwanJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.