A SPADE-based XMPP agent for controlling an AlphaBot2 robot using the Raspberry Pi Zero.
This project implements a SPADE (Smart Python Agent Development Environment) agent that controls an AlphaBot2 robot. The agent uses XMPP (Extensible Messaging and Presence Protocol) for communication, allowing remote control of the robot through messaging.
- Remote control of AlphaBot2 robot via XMPP messages
- Movement commands: forward, backward, left, right, stop
- Direct motor control with specified speed values
- Containerized deployment with Docker
- Raspberry Pi Zero (or compatible)
- AlphaBot2 robot kit
- GPIO access
- Python 3.9+
- SPADE 3.3.3
- RPi.GPIO
- spidev
- rpi_ws281x
-
Install Docker:
curl -sSL https://get.docker.com | sh sudo usermod -aG docker $USER restart terminal -
Clone this repository:
git clone <repository-url> cd AlphaPiZeroAgent -
Configure the XMPP settings in
docker-compose.yml: (Leave as it is unless configured)environment: XMPP_SERVER: "prosody" XMPP_PORT: 5222 XMPP_DOMAIN: "prosody" XMPP_USERNAME: "alpha-pi-zero-agent" XMPP_PASSWORD: "top_secret"
-
Build and run with Docker Compose:
docker-compose up -d
-
Install the required dependencies:
pip install -r requirements.txt -
Set the required environment variables:
export XMPP_SERVER="prosody" export XMPP_PORT=5222 export XMPP_DOMAIN="prosody" export XMPP_USERNAME="alpha-pi-zero-agent" export XMPP_PASSWORD="top_secret" -
Run the agent:
python -m agent
Send XMPP messages to the agent with the following commands:
forward- Move the robot forward for 2 secondsbackward- Move the robot backward for 2 secondsleft- Turn the robot left for 2 secondsright- Turn the robot right for 2 secondsstop- Stop all motorsmotor <left_speed> <right_speed>- Set specific motor speeds (range: -100 to 100)
agent/__main__.py- Main agent implementationagent/alphabotlib/- Library for controlling the AlphaBot2 hardwareAlphaBot2.py- Core motor control functions- Additional sensor and control modules
The agent uses environment variables for configuration:
XMPP_SERVER- XMPP server hostnameXMPP_PORT- XMPP server port (default: 5222)XMPP_DOMAIN- XMPP domainXMPP_USERNAME- XMPP username for the agentXMPP_PASSWORD- XMPP password for the agent
sudo nano /etc/systemd/system/camera-api.service
[Unit] Description=Camera Flask API After=network.target
[Service] ExecStart=/usr/bin/python3 /home/test/RobotController/agent/camera_api.py WorkingDirectory=/home/test/RobotController/agent Restart=always User=test Environment=PYTHONUNBUFFERED=1
[Install] WantedBy=multi-user.target
sudo systemctl daemon-reexec # Réinitialise systemd (utile après update) sudo systemctl daemon-reload # Recharge les services sudo systemctl enable camera-api # Démarrage auto au boot sudo systemctl start camera-api # Lance maintenant
The CameraHandler class provides an easy-to-use interface for interacting with the Raspberry Pi camera module. Below are the steps to use it:
To use the CameraHandler, you need to initialize the camera and configure it with the desired resolution.
Here’s an example of how to use the CameraHandler class:
from camera_utils import CameraHandler
# Initialize the camera
cam = CameraHandler(resolution=(640, 480))
cam.initialize_camera()
# Capture an image
image = cam.capture_image()
# Save the captured image
cam.save_image(image, output_path="captured_image.jpg")
# Close the camera
cam.close()-
initialize_camera()- Initializes the camera and configures it with the specified resolution.
- Raises an error if no camera is detected.
-
capture_image(convert_rgb=True)- Captures an image from the camera.
- Converts the image to RGB format if
convert_rgbis set toTrue. - Returns the captured image as a NumPy array.
-
save_image(frame, output_path="captured_image.jpg")- Saves the captured image to the specified file path.
- Default file path:
captured_image.jpg.
-
close()- Stops the camera and releases resources.
You can also run the camera_api.py script directly to capture and save an image. The script will:
- Initialize the camera.
- Capture an image.
- Save the image as
captured_image.jpg. - Close the camera.
Run the script using:
python camera_api.pyThe captured image will be saved in the same directory as camera_api.py.
- Ensure the Raspberry Pi camera module is properly connected and enabled in the Raspberry Pi configuration.
- Install the required dependencies (
picamera2,opencv-python) before using the script:pip install picamera2 opencv-python
[Add your license information here]
- This project uses the SPADE framework for agent development
- AlphaBot2 is a product of Waveshare