This commit is contained in:
2026-07-29 20:38:30 +02:00
parent c34449aac5
commit bc84f7e8eb
3 changed files with 10 additions and 90 deletions
-80
View File
@@ -1,80 +0,0 @@
import socket
import struct
PACKET_VERSION = 1
FLOAT_COUNT = 30 * 6 * 3
PACKET_FMT = f"<H I B B {FLOAT_COUNT}f"
PACKET_SIZE = struct.calcsize(PACKET_FMT)
import time
import numpy as np
from threading import Thread, Event
from queue import Queue
import config as cfg
import DataTypes as dt
class ESP32Communication(Thread):
def __init__(self, host=cfg.esp32_ip, port=cfg.esp32_port, timeout=cfg.timeout):
super().__init__()
self.daemon = True
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(timeout)
print(f"Connecting to ESP32 at {host}:{port} ...")
self.sock.connect((host, port))
print("Connected to ESP32!")
self.command_queue = Queue()
self.response_queue = Queue()
self.running = Event()
self.running.set()
def run(self):
while self.running.is_set():
# 1. Send queued commands
if not self.command_queue.empty():
command = self.command_queue.get()
self._write(command)
# 2. Read response
self.sock.setblocking(False)
try:
data = self.sock.recv(1024)
if data:
line = data.decode('utf-8', errors='ignore').strip()
print(f"[ESP32] {line}")
self.response_queue.put(line)
except BlockingIOError:
pass
time.sleep(0.01)
def write(self, target):
if isinstance(target, dt.RadArray):
target = dt.DegArray(np.degrees(target.data))
leg_str = '\n'.join([','.join(map(str, row)) for row in target])
formatted_data = f"<{leg_str}>"
print(f"Gesendete Daten:\n{formatted_data}")
self._write(formatted_data)
def wait4esp32(self, expected_message):
while True:
try:
data = self.sock.recv(1024).decode('utf-8').strip()
print(f"Empfangene Daten: {data}")
if data == expected_message:
print("Bewegung abgeschlossen!")
break
except socket.timeout:
time.sleep(0.05)
def _write(self, message):
self.sock.sendall(message.encode('utf-8'))
def stop(self):
self.running.clear()
self.join()
self.sock.close()
+9 -9
View File
@@ -4,15 +4,7 @@
**Purpose** **Purpose**
- **Overview**: JackBot is a Python project for controlling and simulating a six-legged (hexapod) robot. It computes foot trajectories, runs inverse kinematics against a URDF model, and sends joint commands to either a hardware controller (ESP32 / Arduino) or a PyBullet simulation. - **Overview**: JackBot is a Python project for controlling and simulating a six-legged (hexapod) robot. It computes foot trajectories, runs inverse kinematics against a URDF model, and sends joint commands to either a hardware controller (ESP32 / Arduino) or a PyBullet simulation.
**Quick Start** **Setup Dependencies and Virtual Enviroment**
- **Simulation**: enable the simulator in `config.py` by setting `sim = True`, then run:
```bash
python3 main.py
```
- **Hardware**: set `sim = False` in `config.py` and choose `arduinoConnection = True` or provide your ESP32 settings. Verify `port`, `baudrate`, `esp32_ip`, and `esp32_port` in `config.py`.
**Dependencies**
- **Python packages**: At minimum the project uses `numpy`, `pygame`, `ikpy`, `pybullet`, `pyserial`, and `matplotlib`. - **Python packages**: At minimum the project uses `numpy`, `pygame`, `ikpy`, `pybullet`, `pyserial`, and `matplotlib`.
- **Install** (recommended inside a venv): - **Install** (recommended inside a venv):
@@ -22,6 +14,14 @@ source .venv/bin/activate
pip install numpy pygame ikpy pybullet pyserial matplotlib pip install numpy pygame ikpy pybullet pyserial matplotlib
``` ```
**Quick Start**
- **Simulation**: enable the simulator in `config.py` by setting `sim = True`, then run:
```bash
python3 main.py
```
- **Hardware**: set `sim = False` in `config.py` and choose `arduinoConnection = True` or provide your ESP32 settings. Verify `port`, `baudrate`, `esp32_ip`, and `esp32_port` in `config.py`.
**Project structure (key files)** **Project structure (key files)**
- **Entry point**: [main.py](main.py) — starts the controller and state loop. - **Entry point**: [main.py](main.py) — starts the controller and state loop.
- **Controller**: [Controller.py](Controller.py) — joystick input and `ControlIntent` updates. - **Controller**: [Controller.py](Controller.py) — joystick input and `ControlIntent` updates.
+1 -1
View File
@@ -7,7 +7,7 @@ esp32_port = 3323
timeout = 2.0 timeout = 2.0
sim: bool = False sim: bool = True
arduinoConnection: bool = False arduinoConnection: bool = False
urdf_path = "JackBotUrdf.urdf" urdf_path = "JackBotUrdf.urdf"