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
|
||||
Reference in New Issue
Block a user