294 lines
11 KiB
Markdown
294 lines
11 KiB
Markdown
# JackBot — Hexapod Control, Simulation & RL Framework
|
|
|
|
JackBot is a modular Python framework for controlling a six-legged robot in simulation or on hardware. The current workspace reflects a matured control stack with a PyBullet-based simulator, a GUI-driven manual runtime, and a reinforcement-learning pipeline for PPO training and evaluation.
|
|
|
|
---
|
|
|
|
## What JackBot Is
|
|
|
|
JackBot is a Python-based hexapod project that combines:
|
|
|
|
* a robot control stack for a six-legged walking robot,
|
|
* a physics simulator (`simulation.py`) for testing in software before using real hardware,
|
|
* a reinforcement learning pipeline that trains locomotion policies with PPO and behavioral cloning,
|
|
* and a GUI/gamepad input layer for manual operation.
|
|
|
|
The project can currently:
|
|
|
|
* run the robot in a PyBullet simulation,
|
|
* accept commands from a GUI or gamepad,
|
|
* stream target joint positions to physical hardware (ESP32 / Arduino),
|
|
* train PPO models and evaluate saved checkpoints,
|
|
* generate kinematics-based teacher data for behavioral cloning.
|
|
|
|
---
|
|
|
|
## Why the Project Exists
|
|
|
|
A hexapod is hard to control manually because each leg has multiple joints and the robot must maintain balance while moving. The project uses a robot model and physics simulation as a testbed for learning motion strategies, refining kinematic control, and validating behavior before applying commands to real hardware.
|
|
|
|
In the current codebase, the practical workflow is:
|
|
|
|
1. Start the robot in either simulation or hardware mode.
|
|
2. Feed motion commands from GUI/gamepad or a training environment.
|
|
3. Convert target foot positions into joint targets using inverse kinematics.
|
|
4. Apply these targets to the active backend.
|
|
5. Train or evaluate PPO policies against the simulated robot.
|
|
|
|
---
|
|
|
|
## Key Components
|
|
|
|
* **Robot abstraction (`Robot.py`)**: Central handler for the active backend, current joint state, IK/FK helpers, gait handling, and RL action application.
|
|
* **Kinematics (`kinematics.py`)**: Converts target foot positions into joint angles using IKPy chains for each leg.
|
|
* **Simulation (`simulation.py`)**: Manages PyBullet scene loading, stepping, joint actuation, base pose queries, and physics telemetry.
|
|
* **Inputs (`inputs/`)**: Receives commands from GUI sliders, Pygame gamepads, and random command generation.
|
|
* **ML Subsystem (`ml/`)**: Gymnasium/PyBullet environment and learning workflows; see the [ML README](ml/README.md).
|
|
* **Board firmware (`BoardCode/`)**: Arduino and ESP32 sketches with a shared ESP32 library; see the [BoardCode README](BoardCode/README.md).
|
|
* **States (`states/`)**: Contains additional state-machine classes such as `IdleState`, `WalkingState`, and `WaveState`. These exist in the repository, but the current active runtime in `main.py` does not currently drive them through `Robot.tick()`.
|
|
|
|
---
|
|
|
|
## Project Architecture
|
|
|
|
```text
|
|
JackBot/
|
|
├── main.py # Manual runtime entry point
|
|
├── Robot.py # Unified robot wrapper + backend selection
|
|
├── config.py # Central runtime configuration
|
|
├── kinematics.py # IK/FK helpers
|
|
├── simulation.py # PyBullet scene / physics manager
|
|
├── robot_init.py # Initial joint definitions and center points
|
|
├── DataTypes.py # Typed arrays for positions / angles
|
|
├── JackBotUrdf.urdf # Robot URDF
|
|
│
|
|
├── states/ # State classes present in the project
|
|
│ ├── State.py
|
|
│ ├── IdleState.py
|
|
│ ├── WalkingState.py
|
|
│ ├── WaveState.py
|
|
│ ├── ml_walking.py
|
|
│ └── __init__.py
|
|
│
|
|
├── inputs/ # Command input providers
|
|
│ ├── InputProvider.py
|
|
│ ├── PygameController.py
|
|
│ ├── RandomeInputProvider.py
|
|
│ └── RandomInputProvider.py
|
|
│
|
|
├── gui/ # GUI/dashboard control layer
|
|
│ └── MainWindow.py
|
|
├── BoardCode/ # Arduino and ESP32 firmware
|
|
│ ├── JackBotArd/
|
|
│ ├── JackBotEsp32/
|
|
│ ├── JackBotEsp32C3/
|
|
│ └── libaries/JackBot/ # Shared ESP32 Arduino library
|
|
│
|
|
├── ml/ # RL environment and workflows; see ml/README.md
|
|
│
|
|
├── EspCommunication.py # ESP32 UDP communication layer
|
|
├── ArduinoCommunication.py # Arduino serial communication layer
|
|
├── Helper Scripts/ # Utility scripts
|
|
│ ├── FindCenterPoints.py
|
|
│ └── torqueCalc.py
|
|
│
|
|
├── requirements.txt
|
|
├── README.md
|
|
└── ...
|
|
```
|
|
|
|
### Important current-state note
|
|
|
|
The repository contains a few pieces that are still present but not fully connected to the current runtime:
|
|
|
|
* `states/WalkingState.py`, `states/WaveState.py`, and `states/ml_walking.py` exist, but `main.py` currently drives `Robot.tick()` directly instead of routing through the active `STATE_REGISTRY`.
|
|
* `inputs/RandomeInputProvider.py` exists, but the active runtime does not currently use it directly. The GUI resolves random walking behavior in `gui/MainWindow.py`.
|
|
* `WaveEmoteState` / `LaolaWaveEmoteState` are defined in `states/WaveState.py`, but they are not currently registered in `states/__init__.py`.
|
|
|
|
---
|
|
|
|
## System Requirements
|
|
|
|
* **Python:** 3.12 recommended
|
|
* **OS:** Windows 10/11 or Linux
|
|
* **Dependencies:** `pybullet`, `gymnasium`, `stable-baselines3`, `torch`, `numpy`, `pygame`, `ikpy`, `pyserial`, `matplotlib`, `dearpygui`, `xacro`
|
|
|
|
## Robot model
|
|
|
|
Edit [`JackBotUrdf.xacro`](JackBotUrdf.xacro), which defines shared leg dimensions, coxa starting-position properties, and a reusable leg macro. Regenerate the checked-in URDF after changing the model:
|
|
|
|
```bash
|
|
xacro JackBotUrdf.xacro -o JackBotUrdf.urdf
|
|
```
|
|
|
|
The `xacro` dependency is included in `requirements.txt`. Keep `JackBotUrdf.urdf` as generated output; the simulator and kinematics continue loading that file.
|
|
|
|
---
|
|
|
|
## Installation & Setup
|
|
|
|
### Linux (Bash)
|
|
|
|
```bash
|
|
python3.12 -m venv .venv
|
|
source .venv/bin/activate
|
|
python -m pip install --upgrade pip setuptools wheel
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
### Windows (PowerShell)
|
|
|
|
```powershell
|
|
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
|
|
python -m venv .venv
|
|
.\.venv\Scripts\Activate.ps1
|
|
python -m pip install --upgrade pip setuptools wheel
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
---
|
|
|
|
## Usage Guide
|
|
|
|
## Manual Control & Hardware Streaming (`main.py`)
|
|
|
|
`main.py` is the main operational entry point for driving the robot manually through the GUI or a connected gamepad.
|
|
|
|
To launch:
|
|
|
|
```bash
|
|
python main.py
|
|
```
|
|
|
|
### Configuration (`config.py`)
|
|
|
|
Before launching `main.py`, edit `config.py` to select the backend and connection targets.
|
|
|
|
* **Backend Selection (`cfg.backend`)**:
|
|
* `BackendType.SIMULATION`: run inside a PyBullet window
|
|
* `BackendType.ESP32`: stream motion over UDP to an ESP32
|
|
* `BackendType.ARDUINO`: stream motion over serial to an Arduino
|
|
|
|
Supported configuration values in the current code include:
|
|
|
|
* `backend`
|
|
* `urdf_path`
|
|
* `port`, `baudrate`
|
|
* `esp32_ip`, `esp32_port`
|
|
* `tick_rate_hz`, `step_duration`
|
|
* `step_height`, `step_length`
|
|
|
|
In ESP32 mode, the GUI uses UDP heartbeat replies to show the robot connection
|
|
state. If the connection is lost, it retries for up to 30 seconds and then
|
|
shows **Robot Disconnected**; press **STOP ROBOT** to end the active session.
|
|
The ESP32 must run firmware containing the heartbeat reply support.
|
|
|
|
### GUI input sources
|
|
|
|
The current GUI (`gui/MainWindow.py`) exposes these input sources:
|
|
|
|
* `Gamepad`
|
|
* `GUI Sliders`
|
|
* `Random Walk`
|
|
|
|
The active `main.py` runtime resolves commands through `resolve_active_command(...)` and then passes the resulting `vector_dirmov` directly to `Robot.tick()`.
|
|
|
|
---
|
|
|
|
## Machine Learning Pipeline (`ml/`)
|
|
|
|
The ML project has its own guide covering environment behavior, reward and curriculum
|
|
design, setup, training, pretraining, evaluation, and generated outputs:
|
|
[ml/README.md](ml/README.md).
|
|
|
|
---
|
|
|
|
## How the Control Loop Works in Practice
|
|
|
|
A simplified view of the current runtime is:
|
|
|
|
1. `main.py` starts the joystick controller process and opens the GUI.
|
|
2. The GUI resolves active commands (`Gamepad`, slider, or random walk).
|
|
3. `Robot.tick()` receives the current motion vector and applies it through the active backend.
|
|
4. In simulation mode, `Simulation.step()` advances the PyBullet world.
|
|
5. In training/evaluation mode, `JackBotEnv.step()` computes reward, updates curriculum, and returns observations.
|
|
6. PPO uses the observation/action loop to improve the policy.
|
|
|
|
---
|
|
|
|
## What Makes this Project Useful
|
|
|
|
This repository is useful because it combines several layers that are often separate:
|
|
|
|
* robot control and kinematics,
|
|
* physics simulation,
|
|
* command input sources,
|
|
* RL environment construction,
|
|
* PPO training and evaluation,
|
|
* hardware communication layers.
|
|
|
|
For a newcomer, the easiest way to think about the project is:
|
|
|
|
- `main.py` is the manual control entry point,
|
|
- `Robot.py` is the core robot wrapper,
|
|
- `simulation.py` is the physics layer,
|
|
- `ml/env.py` is the environment interface for RL,
|
|
- `ml/run_train.py`, `ml/run_eval.py`, and `ml/pretrain_bc.py` are the main ML workflows.
|
|
|
|
---
|
|
|
|
## Current runtime notes and caveats
|
|
|
|
### State system status
|
|
|
|
The state classes are present in the repository, but the current runtime path is not using them as the main control loop:
|
|
|
|
* `Robot.tick()` currently updates the robot directly from `vector_dirmov`
|
|
* the `STATE_REGISTRY` exists, but it is not the path used by the current `main.py` execution flow
|
|
* the state subsystem remains partially implemented and should be treated as a legacy or optional extension
|
|
|
|
### Input source status
|
|
|
|
The repository currently includes both:
|
|
|
|
* `inputs/PygameController.py` for gamepad input
|
|
* `gui/MainWindow.py` for selecting `Gamepad`, `GUI Sliders`, and `Random Walk`
|
|
|
|
The standalone random input provider file is present, but it is not the path currently used by `main.py`.
|
|
|
|
### Hardware communication status
|
|
|
|
The hardware communication classes still exist:
|
|
|
|
* `EspCommunication.py` for ESP32 UDP
|
|
* `ArduinoCommunication.py` for Arduino serial
|
|
|
|
They are available via `cfg.backend`, but they are not the primary path in the current example workflows shown here.
|
|
|
|
---
|
|
|
|
## Helper Scripts
|
|
|
|
### `Helper Scripts/FindCenterPoints.py`
|
|
|
|
This script appears to be a utility for analyzing kinematic center point data and related foot-placement experiments. It is not part of the active runtime path.
|
|
|
|
### `Helper Scripts/torqueCalc.py`
|
|
|
|
This is a stand-alone Tkinter utility for estimating servo torque requirements based on robot mass, leg dimensions, and safety factor. It is a design/support script rather than part of the main robot runtime.
|
|
|
|
---
|
|
|
|
## Bottom Line
|
|
|
|
The current codebase is a working hybrid of:
|
|
|
|
* real-time robot control,
|
|
* PyBullet simulation,
|
|
* GUI/manual input,
|
|
* PPO-based RL training and evaluation,
|
|
* partial state-machine scaffolding,
|
|
* hardware communication support.
|
|
|
|
The information in this README has been updated to match the current files in the workspace, especially around the true ML entry points, active runtime flow, and the fact that some older state-helper modules are present but not currently wired into the main execution path.
|