merged old Esp Verison with better FileManagement

This commit is contained in:
2026-10-23 23:38:20 +02:00
parent c7b8c89c2d
commit 893dfd419e
14 changed files with 153 additions and 383 deletions
+24 -14
View File
@@ -35,7 +35,6 @@ int BotNetwork::readPacket(uint8_t* buffer, size_t maxSize) {
return 0;
}
// THE FIX: Access motion and display using '->'
void BotNetwork::parsePacket(uint8_t *buffer, size_t size, MotionController* motion, DisplayManager* display) {
if (size < sizeof(PacketHeader)) return;
PacketHeader *header = (PacketHeader *)buffer;
@@ -45,44 +44,55 @@ void BotNetwork::parsePacket(uint8_t *buffer, size_t size, MotionController* mot
size_t pos = 0;
int tempLeg[6][3];
// Temporary trackers for config values so we can update them cleanly
static int currentServoMin = 125;
static int currentServoMax = 625;
static int currentPythonInterval = 50;
while (pos < header->payload_len) {
uint8_t f_type = payload[pos++];
uint16_t f_len = payload[pos] | (payload[pos+1] << 8);
pos += 2;
uint8_t *data = payload + pos;
// --- BRANCH 1: MOTION COMMANDS ---
// --- BRANCH 1: MOTION & DISPLAY COMMANDS ---
if (header->packet_id == PKT_COMMAND) {
if (f_type == FT_ARRAY_F32 && f_len == 18) {
int tempLeg[6][3];
for (int i=0; i<6; i++) for (int j=0; j<3; j++) tempLeg[i][j] = data[i*3+j];
motion->pushFrame(tempLeg);
} else if (f_type == FT_STRING) {
char textBuffer[64];
size_t len = min((size_t)f_len, (size_t)63);
memcpy(textBuffer, data, len);
textBuffer[len] = '\0';
display->drawCenteredText(textBuffer, 0x07E0); // Green text
}
}
// --- BRANCH 2: DISPLAY COMMANDS (The Independent Part) ---
// --- BRANCH 2: DISPLAY PACKET (If your Python sends separate PKT_DISPLAY) ---
else if (header->packet_id == PKT_DISPLAY) {
if (f_type == FT_STRING) {
char textBuffer[64];
size_t len = min((size_t)f_len, (size_t)63);
memcpy(textBuffer, data, len);
textBuffer[len] = '\0';
// Draw to screen immediately
display->drawCenteredText(textBuffer, 0x07E0);
}
}
// --- BRANCH 3: CONFIGS ---
// --- BRANCH 3: CONFIGS (FIXED) ---
else if (header->packet_id == PKT_CONFIG) {
if (f_type == FT_CONFIG_VAL) {
uint8_t id = data[0]; int16_t val = data[1] | (data[2] << 8);
// NOTE: To make this dynamic, we pass it to the motion controller
if (id == 1 || id == 2 || id == 3) {
// Normally you'd read the current configs, update one, and push back.
// For now, I've added a config method to your MotionController below.
Serial.printf("Config Sync received.\n");
}
uint8_t id = data[0];
int16_t val = data[1] | (data[2] << 8);
if (id == 1) currentServoMin = val;
if (id == 2) currentServoMax = val;
if (id == 3) currentPythonInterval = val;
// Push the updated configs down into the MotionController
motion->setConfigs(currentServoMin, currentServoMax, currentPythonInterval);
Serial.printf("Config Sync: ID %d -> %d\n", id, val);
}
}
pos += f_len;
+87
View File
@@ -0,0 +1,87 @@
#include "DisplayManager.h"
DisplayManager::DisplayManager(int8_t cs, int8_t dc, int8_t rst)
: _tft(cs, dc, rst) {}
void DisplayManager::begin() {
_tft.init(240, 320);
_tft.setRotation(1);
clear();
}
void DisplayManager::clear() {
_tft.fillScreen(ST77XX_BLACK);
}
void DisplayManager::setState(DisplayState state) {
_currentState = state;
// Force a redraw when state changes
if (_currentState == STATE_FACE) {
drawFaceScreen();
} else {
drawMenuScreen();
}
}
void DisplayManager::toggleState() {
if (_currentState == STATE_FACE) {
setState(STATE_MENU);
} else {
setState(STATE_FACE);
}
}
void DisplayManager::update() {
// Put any periodic animations here (e.g., blinking face frames)
}
// --- DESIGNED VIA LOPAKA.APP ---
void DisplayManager::drawFaceScreen() {
clear();
// Example layout elements generated/inspired by Lopaka:
_tft.setTextColor(ST77XX_CYAN);
_tft.setTextSize(3);
_tft.setCursor(90, 100);
_tft.print("^ _ ^"); // Simple default face
}
void DisplayManager::drawMenuScreen() {
clear();
// Example Menu/Stats UI layout adapted from Lopaka export
_tft.drawRect(10, 10, 300, 220, ST77XX_BLUE); // Outer box
_tft.setTextColor(ST77XX_WHITE);
_tft.setTextSize(2);
_tft.setCursor(30, 30);
_tft.print("-- JACKBOT CONFIG --");
_tft.setTextSize(1);
_tft.setCursor(30, 80);
_tft.print("Status: Connected (UDP)");
_tft.setCursor(30, 110);
_tft.print("Servo Profile: Standard");
_tft.setCursor(30, 140);
_tft.print("Press button to exit menu");
}
void DisplayManager::drawCenteredText(const char* text, uint16_t color) {
if (_currentState != STATE_FACE) return; // Only override face text if we are on face screen
clear();
_tft.setTextSize(2);
_tft.setTextColor(color);
int16_t x1, y1;
uint16_t w, h;
_tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
int16_t cx = (_tft.width() - w) / 2;
int16_t cy = (_tft.height() - h) / 2;
_tft.setCursor(cx, cy);
_tft.print(text);
}
+34
View File
@@ -0,0 +1,34 @@
#ifndef DISPLAYMANAGER_H
#define DISPLAYMANAGER_H
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
enum DisplayState {
STATE_FACE,
STATE_MENU
};
class DisplayManager {
public:
DisplayManager(int8_t cs, int8_t dc, int8_t rst);
void begin();
void update();
void drawCenteredText(const char* text, uint16_t color);
void setState(DisplayState state);
void toggleState(); // Switches between Face and Menu
// Lopaka-generated screen renderers
void drawFaceScreen();
void drawMenuScreen();
void clear();
private:
Adafruit_ST7789 _tft;
DisplayState _currentState = STATE_FACE;
unsigned long _lastScreenUpdate = 0;
};
#endif
-44
View File
@@ -1,44 +0,0 @@
#include "DisplayManager.h"
DisplayManager::DisplayManager(int8_t cs, int8_t dc, int8_t rst)
: _tft(cs, dc, rst)
{
}
void DisplayManager::begin() {
_tft.init(240, 320); // Initialize your specific ST7789 size
_tft.setRotation(1);
clear();
}
void DisplayManager::clear() {
_tft.fillScreen(ST77XX_BLACK);
}
void DisplayManager::drawCenteredText(const char* text, uint16_t color) {
clear();
_tft.setTextSize(2);
_tft.setTextColor(color);
int16_t x1, y1;
uint16_t w, h;
// Use the actual _tft object logic you had in Emotes.cpp
_tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
int16_t cx = (_tft.width() - w) / 2;
int16_t cy = (_tft.height() - h) / 2;
_tft.setCursor(cx, cy);
_tft.print(text);
}
void DisplayManager::setEmote(uint8_t emoteId) {
_currentEmote = emoteId;
// Logic for drawing different faces based on ID
// switch(emoteId) { case 1: drawHappyFace(); break; ... }
}
void DisplayManager::update() {
// If you add eyes that blink, the logic goes here
}
-25
View File
@@ -1,25 +0,0 @@
#ifndef DISPLAYMANAGER_H
#define DISPLAYMANAGER_H
#include <Adafruit_c.h>
class DisplayManager {
public:
// Pass the pins to the constructor
DisplayManager(int8_t cs, int8_t dc, int8_t rst);
void begin();
void update(); // For future animations (blinking eyes, etc.)
// Your Emotes logic merged here
void drawCenteredText(const char* text, uint16_t color);
void setEmote(uint8_t emoteId);
void clear();
private:
Adafruit_ST7789 _tft;
// We store internal state here
uint8_t _currentEmote = 0;
};
#endif
-30
View File
@@ -1,30 +0,0 @@
#include "Emotes.h"
JackBot::Emotes::Emotes(Adafruit_GFX* display) {
tft = display;
}
void JackBot::Emotes::clearScreen() {
tft->fillScreen(0x0000); // black
}
// Draw centered text
void JackBot::Emotes::drawCenteredText(const char* text, uint16_t color) {
clearScreen();
tft->setTextSize(2);
tft->setTextColor(color);
int16_t x1, y1;
uint16_t w, h;
// Measure the text
tft->getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
// Calculate center
int16_t cx = (tft->width() - w) / 2;
int16_t cy = (tft->height() - h) / 2;
tft->setCursor(cx, cy);
tft->print(text);
}
-19
View File
@@ -1,19 +0,0 @@
#ifndef JACKBOT_EMOTES_H
#define JACKBOT_EMOTES_H
#include <Adafruit_GFX.h>
namespace JackBot
{
class Emotes {
public:
Emotes(Adafruit_GFX* display);
// Draw text in the center of the screen
void drawCenteredText(const char* text, uint16_t color);
// Simple emoji-like faces
private:
Adafruit_GFX* tft;
void clearScreen();
};
}
#endif
-251
View File
@@ -1,251 +0,0 @@
#include <Adafruit_PWMServoDriver.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h> // CHANGED: Added UDP Support
#include <Wire.h>
#include <Emotes.h>
// ============================================================
// Protocol Definitions (MUST MATCH Python config.py)
// ============================================================
#define PACKET_VERSION 1
#define PKT_COMMAND 1
#define PKT_CONFIG 2
#define FT_ARRAY_F32 1
#define FT_STRING 2
#define FT_UINT8 3
#define FT_CONFIG_VAL 4
struct PacketHeader {
uint8_t packet_id;
uint8_t version;
uint16_t payload_len;
uint32_t timestamp;
};
// ============================================================
// Wi-Fi & UDP Setup
// ============================================================
const char *ssid = "PfLANze";
const char *password = "isteinbisschenengandiehals";
const char *ap_ssid = "JackBot-AP";
const char *ap_password = "robot-control";
WiFiUDP udp; // CHANGED: Switched from WiFiServer to WiFiUDP
unsigned int localPort = 3323;
IPAddress local_IP(192, 168, 188, 32);
IPAddress gateway(192, 168, 188, 1);
IPAddress subnet(255, 255, 255, 0);
// ============================================================
// Servo & Motion State
// ============================================================
int servoMin = 125;
int servoMax = 625;
int pythonInterval = 50; // Expected ms between Python frames (20Hz)
// Interpolation variables
float targetLeg[6][3];
float currentLegF[6][3];
float stepSize[6][3];
unsigned long lastInterpolationUpdate = 0;
const int INTERPOLATION_STEP_MS = 5; // 200Hz internal update rate
Adafruit_PWMServoDriver board0 = Adafruit_PWMServoDriver(0x40);
Adafruit_PWMServoDriver board1 = Adafruit_PWMServoDriver(0x41);
// ============================================================
// Display & Buffers
// ============================================================
#define DISPLAY_TEXT_MAX 64
char displayText[DISPLAY_TEXT_MAX] = "";
bool hasDisplayText = false;
#define TFT_CS 5
#define TFT_DC 16
#define TFT_RST 17
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
JackBot::Emotes face(&tft);
#define MOTION_BUFFER_SIZE 10
struct MotionFrame { int leg[6][3]; };
MotionFrame motionBuffer[MOTION_BUFFER_SIZE];
volatile int motionHead = 0;
volatile int motionTail = 0;
bool pushMotion(int leg[6][3]) {
int next = (motionHead + 1) % MOTION_BUFFER_SIZE;
if (next == motionTail) return false;
memcpy(motionBuffer[motionHead].leg, leg, sizeof(motionBuffer[motionHead].leg));
motionHead = next;
return true;
}
bool popMotion(int leg[6][3]) {
if (motionHead == motionTail) return false;
memcpy(leg, motionBuffer[motionTail].leg, sizeof(motionBuffer[motionTail].leg));
motionTail = (motionTail + 1) % MOTION_BUFFER_SIZE;
return true;
}
// ============================================================
// Servo Control Logic
// ============================================================
int degToPulse(int deg) {
return map(deg, 0, 180, servoMin, servoMax);
}
void writeServos(float leg[6][3]) {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 3; j++) {
int pulse = degToPulse((int)leg[i][j]);
if (i < 3) board0.setPWM(i * 3 + j, 0, pulse);
else board1.setPWM((i - 3) * 3 + j, 0, pulse);
}
}
}
void updateMotion() {
// Run this loop every 5ms (INTERPOLATION_STEP_MS)
if (millis() - lastInterpolationUpdate < INTERPOLATION_STEP_MS) return;
lastInterpolationUpdate = millis();
// 1. Check if we reached the target. If yes, try to grab a new frame from buffer.
bool atTarget = true;
for(int i=0; i<6; i++) {
for(int j=0; j<3; j++) {
if (abs(currentLegF[i][j] - targetLeg[i][j]) > 0.5) atTarget = false;
}
}
if (atTarget) {
int nextFrame[6][3];
if (popMotion(nextFrame)) {
for(int i=0; i<6; i++) {
for(int j=0; j<3; j++) {
targetLeg[i][j] = (float)nextFrame[i][j];
// Smooth the transition: Step size = Total Distance / Number of Interpolation Steps
int numSteps = pythonInterval / INTERPOLATION_STEP_MS;
stepSize[i][j] = (targetLeg[i][j] - currentLegF[i][j]) / (float)numSteps;
}
}
}
}
// 2. Incrementally move current position toward target
for(int i=0; i<6; i++) {
for(int j=0; j<3; j++) {
if (abs(currentLegF[i][j] - targetLeg[i][j]) > abs(stepSize[i][j])) {
currentLegF[i][j] += stepSize[i][j];
} else {
currentLegF[i][j] = targetLeg[i][j];
}
}
}
// 3. Update the actual hardware
writeServos(currentLegF);
}
// ============================================================
// TLV Parser (UDP Version)
// ============================================================
void parsePacket(uint8_t *buffer, size_t size) {
if (size < sizeof(PacketHeader)) return;
PacketHeader *header = (PacketHeader *)buffer;
if (header->version != PACKET_VERSION) return;
uint8_t *payload = buffer + sizeof(PacketHeader);
size_t pos = 0;
int tempLeg[6][3];
while (pos < header->payload_len) {
uint8_t f_type = payload[pos++];
uint16_t f_len = payload[pos] | (payload[pos+1] << 8);
pos += 2;
uint8_t *data = payload + pos;
if (header->packet_id == PKT_COMMAND) {
if (f_type == FT_ARRAY_F32 && f_len == 18) {
for (int i=0; i<6; i++) for (int j=0; j<3; j++) tempLeg[i][j] = data[i*3+j];
pushMotion(tempLeg);
} else if (f_type == FT_STRING) {
size_t len = min((size_t)f_len, (size_t)(DISPLAY_TEXT_MAX - 1));
memcpy(displayText, data, len); displayText[len] = '\0';
hasDisplayText = true;
}
} else if (header->packet_id == PKT_CONFIG) {
if (f_type == FT_CONFIG_VAL) {
uint8_t id = data[0]; int16_t val = data[1] | (data[2] << 8);
if (id == 1) servoMin = val;
if (id == 2) servoMax = val;
if (id == 3) pythonInterval = val;
Serial.printf("Config Sync: ID %d -> %d\n", id, val);
}
}
pos += f_len;
}
}
// ============================================================
// Setup & Loop
// ============================================================
void setup() {
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000); // FIX: Overclock I2C to 400kHz for speed
tft.init(240, 320);
// WiFi Connection with Fallback AP
WiFi.mode(WIFI_STA);
WiFi.config(local_IP, gateway, subnet);
WiFi.begin(ssid, password);
unsigned long start = millis();
while (WiFi.status() != WL_CONNECTED && millis() - start < 8000) { delay(200); }
if (WiFi.status() != WL_CONNECTED) {
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(local_IP, local_IP, subnet);
WiFi.softAP(ap_ssid, ap_password);
face.drawCenteredText("AP: JackBot", ST77XX_MAGENTA);
} else {
face.drawCenteredText("WiFi: Connected", ST77XX_CYAN);
}
udp.begin(localPort); // Start UDP Listener
board0.begin(); board0.setPWMFreq(50);
board1.begin(); board1.setPWMFreq(50);
// Set initial pose
for(int i=0; i<6; i++) {
for(int j=0; j<3; j++) {
currentLegF[i][j] = 90.0; targetLeg[i][j] = 90.0;
}
}
}
void loop() {
// 1. Check for incoming UDP packets
int packetSize = udp.parsePacket();
if (packetSize) {
uint8_t packetBuffer[512];
int len = udp.read(packetBuffer, 512);
if (len > 0) parsePacket(packetBuffer, len);
}
// 2. Smoothly update servos
updateMotion();
// 3. Handle Display
if (hasDisplayText) {
face.drawCenteredText(displayText, ST77XX_GREEN);
hasDisplayText = false;
}
}
View File
File diff suppressed because one or more lines are too long