|
1 | | -# Robot Library Python |
| 1 | +# Robot Library (Python) |
2 | 2 |
|
3 | | -A modular Python library for building robot applications. It provides components |
4 | | -for motion control, sensor access, communication, and indicators. |
| 3 | +A modular Python library for building robot applications. It provides components for motion control, sensors, communications, and indicators over MQTT. |
| 4 | + |
| 5 | +Documentation: the full site is auto-built and deployed to GitHub Pages on pushes to the `dev` branch. Once enabled in the repo settings, it will be available at: `https://pera-swarm.github.io/robot-library-python/`. |
5 | 6 |
|
6 | 7 | ## Installation |
7 | 8 |
|
8 | | -```bash |
9 | | -pip install -e . |
| 9 | +You can install in a few ways: |
| 10 | + |
| 11 | +- Editable (development) install from a local checkout: |
| 12 | + |
| 13 | + ```bash |
| 14 | + pip install -e . |
| 15 | + ``` |
| 16 | + |
| 17 | +- From source via Git (replace org/repo as appropriate): |
| 18 | + |
| 19 | +This library requires `paho-mqtt` (declared in `pyproject.toml`) and Python 3.8+. |
| 20 | + |
| 21 | +## Quick Start |
| 22 | + |
| 23 | +Create a virtual robot by subclassing `robot.VirtualRobot`, configure MQTT, and start the main loop: |
| 24 | + |
| 25 | +```python |
| 26 | +from robot import MQTTSettings, VirtualRobot |
| 27 | +from robot.interfaces import RobotState |
| 28 | + |
| 29 | + |
| 30 | +class MyRobot(VirtualRobot): |
| 31 | + def loop(self): |
| 32 | + if self.state == RobotState.RUN: |
| 33 | + print("running...") |
| 34 | + # do work |
| 35 | + self.delay(1000) |
| 36 | + |
| 37 | + |
| 38 | +# Configure MQTT broker connection |
| 39 | +MQTTSettings.server = "localhost" |
| 40 | +MQTTSettings.port = 1883 |
| 41 | +MQTTSettings.user_name = "" |
| 42 | +MQTTSettings.password = "" |
| 43 | +MQTTSettings.channel = "v1" |
| 44 | + |
| 45 | +r = MyRobot(1, 0, 0, 90) |
| 46 | +r.start() |
| 47 | +r.run() # or run in a separate thread |
| 48 | +``` |
| 49 | + |
| 50 | +See `examples/my_test_robot.py` for a complete runnable example. |
| 51 | + |
| 52 | +## Core Concepts |
| 53 | + |
| 54 | +- Robot: base lifecycle (`start/stop/reset/delay/loop`) and MQTT subscription handling. See `robot.Robot`. |
| 55 | +- VirtualRobot: convenient virtual implementation; subclass and implement `loop`. See `robot.VirtualRobot`. |
| 56 | +- Motion: differential drive style controller for movement and rotation. See `robot.MotionController`. |
| 57 | +- Sensors: distance, color, and proximity, with non-blocking MQTT updates and blocking request/reply helpers. |
| 58 | +- Communication: simple and directed messaging channels to other robots or a controller. |
| 59 | +- Indicators: NeoPixel control via MQTT topics. |
| 60 | + |
| 61 | +## Usage Examples |
| 62 | + |
| 63 | +Motion control: |
| 64 | + |
| 65 | +```python |
| 66 | +from robot import MotionController |
| 67 | + |
| 68 | +motion = MotionController() # standalone stub coordinate for quick tests |
| 69 | +motion.move(100, 100, duration=1000) # forward 1s |
| 70 | +motion.rotate_degree(120, 90) # rotate 90 degrees at speed 120 |
| 71 | +``` |
| 72 | + |
| 73 | +Sensors (within a `Robot` subclass after `setup()` has run): |
| 74 | + |
| 75 | +```python |
| 76 | +dist = self.dist_sensor.get_distance() |
| 77 | +rgb = self.color_sensor.get_color() # RGBColorType |
| 78 | +prox = self.proximity_sensor.get_proximity() # ProximityReadingType |
| 79 | +``` |
| 80 | + |
| 81 | +Communications: |
| 82 | + |
| 83 | +```python |
| 84 | +self.simple_comm.send_message("hello swarm") |
| 85 | +self.directed_comm.send_message_with_distance("to leader", distance=25) |
10 | 86 | ``` |
11 | 87 |
|
12 | | -## Usage |
| 88 | +Indicators: |
13 | 89 |
|
14 | 90 | ```python |
15 | | -from robot.motion import MotionController |
| 91 | +self.neo_pixel.change_color(255, 128, 0) |
| 92 | +``` |
| 93 | + |
| 94 | +## Extending and Customizing |
| 95 | + |
| 96 | +- New behavior: subclass `VirtualRobot` and implement `loop`. Optionally override `sensor_interrupt` or `communication_interrupt`. |
| 97 | +- New sensors/indicators: follow the pattern in `robot.sensors.*` and `robot.indicators.*`, implementing `handle_subscription` and publishing/consuming your desired topics via `RobotMqttClient`. |
| 98 | +- Motion tuning: adjust `robot.configs.robot_settings.RobotSettings` constants (speed bounds, robot geometry) to match your platform. |
| 99 | +- MQTT setup: set fields on `robot.configs.mqtt_settings.MQTTSettings` before constructing robots. |
16 | 100 |
|
17 | | -controller = MotionController() |
| 101 | +## Documentation |
| 102 | + |
| 103 | +Sphinx documentation lives in `docs/` and is auto-generated on pushes to `dev` via GitHub Actions, then deployed to GitHub Pages. To build locally: |
| 104 | + |
| 105 | +```bash |
| 106 | +pip install -r docs/requirements.txt |
| 107 | +sphinx-apidoc -o docs/api src/robot |
| 108 | +python -m sphinx -b html docs docs/_build/html |
| 109 | +open docs/_build/html/index.html |
18 | 110 | ``` |
| 111 | + |
| 112 | +## Contributing |
| 113 | + |
| 114 | +- Run lint and tests: `flake8 src tests` and `PYTHONPATH=src pytest` |
| 115 | +- Example to run: `python examples/my_test_robot.py` |
0 commit comments