Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/robot-library-python.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/robot_emulator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#main API
from .base import RobotState
88 changes: 88 additions & 0 deletions src/robot_emulator/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Robot emulator base class
# This file holds core framework for robot emulation
# RobotState - define robot state
# Motion - send move/ rotate command over mqtt
# Virtual Robot - base class for robot emulation

import time
import threading
import paho.mqtt.client as mqtt

class RobotState:
"""Define robot states"""

BEGIN = "BEGIN"
RUN = "RUN"
WAIT ="WAIT"

class Motion:
"""Handle movement"""
def __init__(self,mqtt_client,robot_id):
self.client = mqtt_client
self.robot_id = robot_id

def move_distance(self, speed, distance):
"""Move forward a fixed distance"""
self.client.publish(f"robots/{self.robot_id}/motion", f"MOVE {speed} {distance}")

def rotate_degree(self, speed, angle):
"""Rotate by an angle"""
self.client.publish(f"robots/{self.robot_id}/motion", f"ROTATE {speed} {angle}")

def stop(self):
"""Stop movement"""
self.client.publish(f"robots/{self.robot_id}/motion", "STOP")

def move(self, left_speed, right_speed, duration):
"""Move with differential speeds for a duration"""
self.client.publish(f"robots/{self.robot_id}/motion", f"DRIVE {left_speed} {right_speed} {duration}")

class VirtualRobot(threading.Thread):
"""Base class for virtual robot emulation"""
"""
Extend this class and implement 'setup()' and 'loop()'
"""

def __init__(self, robot_id, x=0, y=0, heading=0, mqtt_config = None):
super().__init__()
self.robot_id = robot_id
self.x = x
self.y = y
self.heading = heading
self.state = RobotState.BEGIN
self.client = mqtt.Client()
self.motion = Motion(self.client, self.robot_id)

#configure mqtt
if mqtt_config:
self.client.username_pw_set(
mqtt_config.get("username"),
mqtt_config.get("password")
)
self.client.connect(
mqtt_config.get("server","127.0.0.1"),
mqtt_config.get("port",1883),
mqtt_config.get("keepalive",60)
)

def setup(self):
"""Run once when the robot starts"""
print(f"Robot {self.robot_id} setup complete")
self.state = RobotState.RUN

def loop(self):
"""Main loop, override this method"""
print(f"Robot {self.robot_id} running at position ({self.x}, {self.y}) heading {self.heading}")
pass

def run(self):
"""Mian execution loop"""
self.setup()
while True:
try:
self.loop()
time.sleep(0.1) # Small delay to prevent CPU overload

except Exception as e:
print(f"Error in robot {self.robot_id}: {e}")
break
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added src/robot_emulator/sensors.py
Empty file.