First Git Push
This commit is contained in:
@@ -0,0 +1,90 @@
|
|||||||
|
#include "BotNetwork.h"
|
||||||
|
|
||||||
|
IPAddress local_IP(192, 168, 188, 32);
|
||||||
|
IPAddress gateway(192, 168, 188, 1);
|
||||||
|
IPAddress subnet(255, 255, 255, 0);
|
||||||
|
|
||||||
|
BotNetwork::BotNetwork(const char* ssid, const char* pass, const char* ap_ssid, const char* ap_pass)
|
||||||
|
: _ssid(ssid), _pass(pass), _ap_ssid(ap_ssid), _ap_pass(ap_pass) {}
|
||||||
|
|
||||||
|
bool BotNetwork::begin(uint16_t port) {
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.config(local_IP, gateway, subnet);
|
||||||
|
WiFi.begin(_ssid, _pass);
|
||||||
|
|
||||||
|
unsigned long start = millis();
|
||||||
|
while (WiFi.status() != WL_CONNECTED && millis() - start < 8000) {
|
||||||
|
delay(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isWifi = true;
|
||||||
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
|
WiFi.mode(WIFI_AP);
|
||||||
|
WiFi.softAPConfig(local_IP, local_IP, subnet);
|
||||||
|
WiFi.softAP(_ap_ssid, _ap_pass);
|
||||||
|
isWifi = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
_udp.begin(port);
|
||||||
|
return isWifi;
|
||||||
|
}
|
||||||
|
|
||||||
|
int BotNetwork::readPacket(uint8_t* buffer, size_t maxSize) {
|
||||||
|
int size = _udp.parsePacket();
|
||||||
|
if (size > 0) return _udp.read(buffer, 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;
|
||||||
|
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;
|
||||||
|
|
||||||
|
// --- BRANCH 1: MOTION 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- BRANCH 2: DISPLAY COMMANDS (The Independent Part) ---
|
||||||
|
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 ---
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pos += f_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef BOTMANAGER_H
|
||||||
|
#define BOTMANAGER_H
|
||||||
|
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
#include "Protocol.h"
|
||||||
|
#include "MotionController.h" // Needed for pointers
|
||||||
|
#include "DisplayManager.h" // Needed for pointers
|
||||||
|
|
||||||
|
class BotNetwork {
|
||||||
|
public:
|
||||||
|
BotNetwork(const char* ssid, const char* pass, const char* ap_ssid, const char* ap_pass);
|
||||||
|
bool begin(uint16_t port); // Returns true if WiFi, false if AP
|
||||||
|
int readPacket(uint8_t* buffer, size_t maxSize);
|
||||||
|
|
||||||
|
void parsePacket(uint8_t *buffer, size_t size, MotionController* motion, DisplayManager* display);
|
||||||
|
|
||||||
|
private:
|
||||||
|
const char* _ssid;
|
||||||
|
const char* _pass;
|
||||||
|
const char* _ap_ssid;
|
||||||
|
const char* _ap_pass;
|
||||||
|
WiFiUDP _udp;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#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
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#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
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include <Wire.h>
|
||||||
|
#include "BotNetwork.h"
|
||||||
|
#include "MotionController.h"
|
||||||
|
#include "DisplayManager.h"
|
||||||
|
|
||||||
|
// Initialize Objects
|
||||||
|
BotNetwork botNetwork("PfLANze", "isteinbisschenengandiehals", "JackBot-AP", "robot-control");
|
||||||
|
MotionController motion;
|
||||||
|
DisplayManager display(5, 16, 17); // TFT_CS, TFT_DC, TFT_RST
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
Wire.begin();
|
||||||
|
Wire.setClock(400000);
|
||||||
|
|
||||||
|
display.begin();
|
||||||
|
display.drawCenteredText("Connecting...", ST77XX_YELLOW);
|
||||||
|
if (botNetwork.begin(3323)) {
|
||||||
|
display.drawCenteredText("WiFi: Connected", ST77XX_CYAN);
|
||||||
|
} else {
|
||||||
|
display.drawCenteredText("AP: JackBot", ST77XX_MAGENTA);
|
||||||
|
}
|
||||||
|
|
||||||
|
motion.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
uint8_t buffer[512];
|
||||||
|
int size = botNetwork.readPacket(buffer, 512);
|
||||||
|
|
||||||
|
if (size > 0) {
|
||||||
|
botNetwork.parsePacket(buffer, size, &motion, &display);
|
||||||
|
}
|
||||||
|
|
||||||
|
motion.update();
|
||||||
|
display.update();
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
#include "MotionController.h"
|
||||||
|
|
||||||
|
MotionController::MotionController() : board0(0x40), board1(0x41) {}
|
||||||
|
|
||||||
|
void MotionController::begin() {
|
||||||
|
board0.begin(); board0.setPWMFreq(50);
|
||||||
|
board1.begin(); board1.setPWMFreq(50);
|
||||||
|
// Initialize current position at 90 degrees
|
||||||
|
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 MotionController::update() {
|
||||||
|
if (millis() - lastUpdate < 5) return; // 200Hz internal update
|
||||||
|
lastUpdate = millis();
|
||||||
|
|
||||||
|
// Check if we arrived
|
||||||
|
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 && head != tail) {
|
||||||
|
// Pop from buffer and calculate steps
|
||||||
|
int next[6][3];
|
||||||
|
memcpy(next, buffer[tail].leg, sizeof(next));
|
||||||
|
tail = (tail + 1) % 10;
|
||||||
|
|
||||||
|
int numSteps = _pythonInterval / 5;
|
||||||
|
for(int i=0; i<6; i++) {
|
||||||
|
for(int j=0; j<3; j++) {
|
||||||
|
targetLeg[i][j] = (float)next[i][j];
|
||||||
|
stepSize[i][j] = (targetLeg[i][j] - currentLegF[i][j]) / (float)numSteps;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeServos(currentLegF);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MotionController::pushFrame(int leg[6][3]) {
|
||||||
|
int next = (head + 1) % 10;
|
||||||
|
if (next == tail) return false;
|
||||||
|
memcpy(buffer[head].leg, leg, sizeof(buffer[head].leg));
|
||||||
|
head = next;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MotionController::setConfigs(int minP, int maxP, int interval) {
|
||||||
|
_servoMin = minP;
|
||||||
|
_servoMax = maxP;
|
||||||
|
_pythonInterval = interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MotionController::writeServos(float leg[6][3]) {
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
int pulse = map((int)leg[i][j], 0, 180, _servoMin, _servoMax);
|
||||||
|
if (i < 3) board0.setPWM(i * 3 + j, 0, pulse);
|
||||||
|
else board1.setPWM((i - 3) * 3 + j, 0, pulse);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int MotionController::degToPulse(int deg) {
|
||||||
|
return map(deg, 0, 180, _servoMin, _servoMax);
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#ifndef MOTIONCONTROLLER_H
|
||||||
|
#define MOTIONCONTROLLER_H
|
||||||
|
|
||||||
|
#include <Adafruit_PWMServoDriver.h>
|
||||||
|
#include "Protocol.h"
|
||||||
|
|
||||||
|
struct MotionFrame { int leg[6][3]; };
|
||||||
|
|
||||||
|
class MotionController {
|
||||||
|
public:
|
||||||
|
MotionController();
|
||||||
|
void begin();
|
||||||
|
void update(); // The 5ms interpolation "heartbeat"
|
||||||
|
bool pushFrame(int leg[6][3]);
|
||||||
|
void setConfigs(int minP, int maxP, int interval);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void writeServos(float leg[6][3]);
|
||||||
|
int degToPulse(int deg);
|
||||||
|
|
||||||
|
Adafruit_PWMServoDriver board0;
|
||||||
|
Adafruit_PWMServoDriver board1;
|
||||||
|
|
||||||
|
// Movement state
|
||||||
|
float targetLeg[6][3];
|
||||||
|
float currentLegF[6][3];
|
||||||
|
float stepSize[6][3];
|
||||||
|
|
||||||
|
// Circular Buffer
|
||||||
|
MotionFrame buffer[10];
|
||||||
|
int head = 0;
|
||||||
|
int tail = 0;
|
||||||
|
|
||||||
|
// Configs
|
||||||
|
int _servoMin = 125;
|
||||||
|
int _servoMax = 625;
|
||||||
|
int _pythonInterval = 50;
|
||||||
|
unsigned long lastUpdate = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef PROTOCOL_H
|
||||||
|
#define PROTOCOL_H
|
||||||
|
|
||||||
|
#define PACKET_VERSION 1
|
||||||
|
#define PKT_COMMAND 1
|
||||||
|
#define PKT_CONFIG 2
|
||||||
|
#define PKT_DISPLAY 3
|
||||||
|
|
||||||
|
#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;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
#include <Adafruit_PWMServoDriver.h>
|
||||||
|
#include <HardwareSerial.h>
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
|
||||||
|
// Self Made Lib
|
||||||
|
#include <JackBotEmotes.h>
|
||||||
|
|
||||||
|
// Verbindung
|
||||||
|
Adafruit_PWMServoDriver board0 = Adafruit_PWMServoDriver(0x40);
|
||||||
|
Adafruit_PWMServoDriver board1 = Adafruit_PWMServoDriver(0x41);
|
||||||
|
SoftwareSerial BTSerial(11, 10); // RX | TX
|
||||||
|
|
||||||
|
// Konstanten
|
||||||
|
#define SERVOMIN 125 // this is the 'minimum' pulse length count (out of 4096)
|
||||||
|
#define SERVOMAX 625 // this is the 'maximum' pulse length count (out of 4096)
|
||||||
|
#define BUFFERSIZE 128
|
||||||
|
|
||||||
|
// Variablen
|
||||||
|
const int numRows = 6;
|
||||||
|
const int numCols = 3;
|
||||||
|
int legarray[numRows][numCols];
|
||||||
|
char legBuffer[BUFFERSIZE];
|
||||||
|
int bufIndex = 0;
|
||||||
|
boolean reading = false;
|
||||||
|
|
||||||
|
JackBotEmotes face;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// USB
|
||||||
|
//Serial.begin(9600);
|
||||||
|
// Bluetooth
|
||||||
|
BTSerial.begin(38400);
|
||||||
|
|
||||||
|
board0.begin();
|
||||||
|
board0.setPWMFreq(50);
|
||||||
|
board1.begin();
|
||||||
|
board1.setPWMFreq(50);
|
||||||
|
pinMode(3, OUTPUT); //Define TX Pin as output
|
||||||
|
pinMode(2, INPUT); //Define RX Pin as input
|
||||||
|
|
||||||
|
// Initialiesieren der Start Position
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
legarray[i][j] = 90;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ServoControll(legarray);
|
||||||
|
|
||||||
|
face.begin();
|
||||||
|
face.showText("Hello JackBot!", 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
face.update();
|
||||||
|
|
||||||
|
while (BTSerial.available()) {
|
||||||
|
char c = BTSerial.read();
|
||||||
|
|
||||||
|
if (c == '<') {
|
||||||
|
// reset buffer for new frame
|
||||||
|
bufIndex = 0;
|
||||||
|
reading = true;
|
||||||
|
}
|
||||||
|
else if (c == '>') {
|
||||||
|
// end of frame
|
||||||
|
if (reading) {
|
||||||
|
legBuffer[bufIndex] = '\0'; // null-terminate string
|
||||||
|
if (stringToArray(String(legBuffer))) {
|
||||||
|
ServoControll(legarray);
|
||||||
|
face.smile();
|
||||||
|
face.showText("Moving!", 4, 1);
|
||||||
|
} else {
|
||||||
|
face.showText("Data Err!", 3, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reading = false;
|
||||||
|
}
|
||||||
|
else if (reading) {
|
||||||
|
if (bufIndex < BUFFERSIZE - 1) {
|
||||||
|
legBuffer[bufIndex++] = c;
|
||||||
|
} else {
|
||||||
|
// buffer overflow → discard this frame
|
||||||
|
reading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Funktion: Ansteuern der Servos
|
||||||
|
void ServoControll(int leg[6][3]) {
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
for (int j = 0; j < 3; j++) {
|
||||||
|
if (i < 3) {
|
||||||
|
board0.setPWM(i * 3 + j, 0, angleToPulse(leg[i][j]));
|
||||||
|
} else {
|
||||||
|
board1.setPWM((i - 3) * 3 + j, 0, angleToPulse(leg[i][j]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Funktion: Konvertiert den Winkel in PWM
|
||||||
|
int angleToPulse(int ang) {
|
||||||
|
return map(ang, 0, 180, SERVOMIN, SERVOMAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funktion: Konvertiert den String in das Array
|
||||||
|
bool stringToArray(String input) {
|
||||||
|
input.replace("\n", ",");
|
||||||
|
char buffer[input.length() + 1];
|
||||||
|
input.toCharArray(buffer, sizeof(buffer));
|
||||||
|
|
||||||
|
char *token = strtok(buffer, ",");
|
||||||
|
int row = 0, col = 0;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
while (token != NULL && row < numRows) {
|
||||||
|
legarray[row][col] = atoi(token);
|
||||||
|
count++;
|
||||||
|
col++;
|
||||||
|
if (col >= numCols) {
|
||||||
|
col = 0;
|
||||||
|
row++;
|
||||||
|
}
|
||||||
|
token = strtok(NULL, ",");
|
||||||
|
}
|
||||||
|
|
||||||
|
return count == (numRows * numCols);
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#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
|
||||||
@@ -0,0 +1,251 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user