Added parameters for motor config.

This commit is contained in:
Holger Weber
2026-02-26 20:27:52 +01:00
parent c765e4b7c2
commit 3d6144bd7f
4 changed files with 116 additions and 54 deletions

View File

@@ -5,6 +5,7 @@
namespace
{
const char *kConfigPath = "/config.json";
const size_t kConfigJsonCapacity = 4096;
WebServer server(80);
bool configStoreReady = false;
@@ -188,7 +189,6 @@ async function pollLogs() {
return;
}
Log(String("Config gespeichert: penUpPos=") + g_iPenUpPos + ", penDownPos=" + g_iPenDownPos);
server.send(200, "application/json", buildConfigJson());
}
@@ -214,8 +214,17 @@ async function pollLogs() {
} // namespace
ConfigParameter configParameters[] = {
// {"penUpPos", &g_iPEN_UP_POS, "Pen Up Position", 5},
// {"penDownPos", &g_iPEN_DOWN_POS, "Pen Down Position", 20},
{"penUpPos", &g_iPenUpPos, "Pen Up Position", 40},
{"penDownPos", &g_iPenDownPos, "Pen Down Position", 10},
{"rotStepPin", &g_iRotStepPin, "Rotational Stepper Step Pin", kDefaultRotStepPin},
{"rotDirPin", &g_iRotDirPin, "Rotational Stepper Direction Pin", kDefaultRotDirPin},
{"rotEnablePin", &g_iRotEnablePin, "Rotational Stepper Enable Pin", kDefaultRotEnablePin},
{"penStepPin", &g_iPenStepPin, "Pen Stepper Step Pin", kDefaultPenStepPin},
{"penDirPin", &g_iPenDirPin, "Pen Stepper Direction Pin", kDefaultPenDirPin},
{"penEnablePin", &g_iPenEnablePin, "Pen Stepper Enable Pin", kDefaultPenEnablePin},
{"rotMicrostep", &g_iRotMicrostep, "Rotational Stepper Microsteps", kDefaultRotMicrostep},
{"penMicrostep", &g_iPenMicrostep, "Pen Stepper Microsteps", kDefaultPenMicrostep},
{"servoPin", &g_iServoPin, "Servo Pin", kDefaultServoPin},
};
const size_t configParameterCount = sizeof(configParameters) / sizeof(configParameters[0]);
@@ -242,12 +251,12 @@ bool loadConfigFromFile()
return saveConfigToFile();
}
StaticJsonDocument<1024> doc;
StaticJsonDocument<kConfigJsonCapacity> doc;
DeserializationError err = deserializeJson(doc, file);
file.close();
if (err)
{
Log("config.json ist ungueltig, defaults werden gespeichert");
Log(String("config.json ist ungueltig (") + err.c_str() + "), defaults werden gespeichert");
return saveConfigToFile();
}
@@ -273,6 +282,7 @@ bool loadConfigFromFile()
param->description = item["description"].as<String>();
}
}
updateStepCorrectionFactors();
Log(String("Config geladen: penUpPos=") + g_iPenUpPos + ", penDownPos=" + g_iPenDownPos);
return true;
@@ -287,7 +297,7 @@ bool saveConfigToFile()
return false;
}
StaticJsonDocument<1024> doc;
StaticJsonDocument<kConfigJsonCapacity> doc;
JsonArray params = doc.createNestedArray("parameters");
for (size_t i = 0; i < configParameterCount; ++i)
{
@@ -296,6 +306,12 @@ bool saveConfigToFile()
item["value"] = *configParameters[i].value;
item["description"] = configParameters[i].description;
}
if (doc.overflowed())
{
Log("Config JSON Dokument zu klein beim Speichern");
file.close();
return false;
}
bool ok = serializeJsonPretty(doc, file) > 0;
file.flush();
@@ -309,7 +325,7 @@ bool saveConfigToFile()
String buildConfigJson()
{
StaticJsonDocument<1024> doc;
StaticJsonDocument<kConfigJsonCapacity> doc;
JsonArray params = doc.createNestedArray("parameters");
for (size_t i = 0; i < configParameterCount; ++i)
{
@@ -318,6 +334,11 @@ String buildConfigJson()
item["value"] = *configParameters[i].value;
item["description"] = configParameters[i].description;
}
if (doc.overflowed())
{
Log("Config JSON Dokument zu klein beim Lesen");
return String("{\"parameters\":[]}");
}
String output;
serializeJson(doc, output);
@@ -326,7 +347,7 @@ String buildConfigJson()
bool applyConfigJson(const String &payload, String &errorMessage)
{
StaticJsonDocument<1024> doc;
StaticJsonDocument<kConfigJsonCapacity> doc;
DeserializationError err = deserializeJson(doc, payload);
if (err)
{
@@ -361,6 +382,7 @@ bool applyConfigJson(const String &payload, String &errorMessage)
param->description = item["description"].as<String>();
}
}
updateStepCorrectionFactors();
return true;
}

View File

@@ -1,5 +1,19 @@
#include "EggDuino.h"
void updateStepCorrectionFactors()
{
if (g_iRotMicrostep <= 0)
{
g_iRotMicrostep = kDefaultRotMicrostep;
}
if (g_iPenMicrostep <= 0)
{
g_iPenMicrostep = kDefaultPenMicrostep;
}
fROT_STEP_CORRECTION = 16.0f / (float)g_iRotMicrostep;
fPEN_STEP_CORRECTION = 16.0f / (float)g_iPenMicrostep;
}
void initHardware()
{
if (!initConfigStore())
@@ -7,35 +21,36 @@ void initHardware()
g_iPenUpPos = 5;
g_iPenDownPos = 20;
}
updateStepCorrectionFactors();
g_iPenState = g_iPenUpPos;
g_stepEngine.init();
g_pStepperRotate = g_stepEngine.stepperConnectToPin(step1);
g_pStepperRotate = g_stepEngine.stepperConnectToPin(g_iRotStepPin);
if (g_pStepperRotate)
{
// rotMotor.setMaxSpeed(2000.0);
// rotMotor.setAcceleration(10000.0);
g_pStepperRotate->setDirectionPin(dir1);
g_pStepperRotate->setEnablePin(enableRotMotor);
g_pStepperRotate->setDirectionPin(g_iRotDirPin);
g_pStepperRotate->setEnablePin(g_iRotEnablePin);
g_pStepperRotate->setAcceleration(10000);
g_pStepperRotate->setAutoEnable(false);
}
// Stepper pen init
g_pStepperPen = g_stepEngine.stepperConnectToPin(step2);
g_pStepperPen = g_stepEngine.stepperConnectToPin(g_iPenStepPin);
if (g_pStepperPen)
{
// penMotor.setMaxSpeed(2000.0);
// penMotor.setAcceleration(10000.0);
g_pStepperPen->setDirectionPin(dir2);
g_pStepperPen->setEnablePin(enablePenMotor);
g_pStepperPen->setDirectionPin(g_iPenDirPin);
g_pStepperPen->setEnablePin(g_iPenEnablePin);
g_pStepperPen->setAcceleration(10000);
g_pStepperPen->setAutoEnable(false);
}
motorsOff();
penServo.attach(servoPin);
penServo.attach(g_iServoPin);
penServo.write(g_iPenState);
}
@@ -52,18 +67,18 @@ void storePenDownPosInEE()
void sendAck()
{
Log(__FUNCTION__);
protocolWrite("OK\r\n");
protocolWrite("OK\r\n");
}
void sendError()
{
Log(__FUNCTION__);
protocolWrite("unknown CMD\r\n");
protocolWrite("unknown CMD\r\n");
}
void motorsOff()
{
Log(__FUNCTION__);
Log(__FUNCTION__);
g_pStepperPen->disableOutputs();
g_pStepperRotate->disableOutputs();
g_bMotorsEnabled = 0;
@@ -71,7 +86,7 @@ void motorsOff()
void motorsOn()
{
Log(__FUNCTION__);
Log(__FUNCTION__);
g_pStepperPen->enableOutputs();
g_pStepperRotate->enableOutputs();
g_bMotorsEnabled = 1;
@@ -79,7 +94,7 @@ void motorsOn()
void toggleMotors()
{
Log(__FUNCTION__);
Log(__FUNCTION__);
if (g_bMotorsEnabled)
{
motorsOff();
@@ -123,16 +138,15 @@ void prepareMove(uint16_t duration, int penStepsEBB, int rotStepsEBB)
motorsOn();
}
if ((1 == fROT_STEP_CORRECTION) && (1 == fPEN_STEP_CORRECTION))
{ // if coordinatessystems are identical
// set Coordinates and Speed
g_pStepperRotate->setSpeedInTicks(abs((float)rotStepsEBB * (float)1000 / (float)duration));
g_pStepperRotate->move(rotStepsEBB);
if ((1 == fROT_STEP_CORRECTION) && (1 == fPEN_STEP_CORRECTION))
{ // if coordinatessystems are identical
// set Coordinates and Speed
g_pStepperRotate->setSpeedInTicks(abs((float)rotStepsEBB * (float)1000 / (float)duration));
g_pStepperRotate->move(rotStepsEBB);
g_pStepperPen->setSpeedInTicks(abs((float)penStepsEBB * (float)1000 / (float)duration));
g_pStepperPen->move(penStepsEBB);
}
g_pStepperPen->setSpeedInTicks(abs((float)penStepsEBB * (float)1000 / (float)duration));
g_pStepperPen->move(penStepsEBB);
}
else
{
// incoming EBB-Steps will be multiplied by 16, then Integer-maths is done, result will be divided by 16
@@ -150,25 +164,27 @@ void prepareMove(uint16_t duration, int penStepsEBB, int rotStepsEBB)
long temp_penSpeed = ((long)penStepsToGo * (long)1000 / (long)duration);
float rotSpeed = (float)abs(temp_rotSpeed); // type cast
float penSpeed = (float)abs(temp_penSpeed);
float penSpeed = (float)abs(temp_penSpeed);
// set Coordinates and Speed
g_pStepperRotate->setSpeedInTicks(rotSpeed);
g_pStepperRotate->move(rotStepsToGo);
// set Coordinates and Speed
g_pStepperRotate->setSpeedInTicks(rotSpeed);
g_pStepperRotate->move(rotStepsToGo);
g_pStepperPen->setSpeedInTicks(penSpeed);
g_pStepperPen->move(penStepsToGo);
}
g_pStepperPen->setSpeedInTicks(penSpeed);
g_pStepperPen->move(penStepsToGo);
}
}
void moveOneStep()
{
while (g_pStepperPen->isRunning() || g_pStepperRotate->isRunning());
while (g_pStepperPen->isRunning() || g_pStepperRotate->isRunning())
;
}
void moveToDestination()
{
while (g_pStepperPen->isRunning() || g_pStepperRotate->isRunning());
while (g_pStepperPen->isRunning() || g_pStepperRotate->isRunning())
;
}
void setprgButtonState()

View File

@@ -69,9 +69,18 @@ int g_iPenState = g_iPenUpPos;
uint32_t g_uiNodeCount = 0;
unsigned int g_uiLayer = 0;
boolean g_bPrgButtonState = 0;
float fROT_STEP_CORRECTION = 16.0 / rotMicrostep; // devide EBB-Coordinates by this factor to get EGGduino-Steps
float fPEN_STEP_CORRECTION = 16.0 / penMicrostep; // devide EBB-Coordinates by this factor to get EGGduino-Steps
boolean g_bMotorsEnabled = 0;
int g_iRotDirPin = kDefaultRotDirPin;
int g_iRotEnablePin = kDefaultRotEnablePin;
int g_iRotStepPin = kDefaultRotStepPin;
int g_iPenStepPin = kDefaultPenStepPin;
int g_iPenDirPin = kDefaultPenDirPin;
int g_iPenEnablePin = kDefaultPenEnablePin;
int g_iServoPin = kDefaultServoPin;
int g_iRotMicrostep = kDefaultRotMicrostep;
int g_iPenMicrostep = kDefaultPenMicrostep;
float fROT_STEP_CORRECTION = 16.0 / kDefaultRotMicrostep; // devide EBB-Coordinates by this factor to get EGGduino-Steps
float fPEN_STEP_CORRECTION = 16.0 / kDefaultPenMicrostep; // devide EBB-Coordinates by this factor to get EGGduino-Steps
void setActiveProtocolContext(SerialCommand *parser, ProtocolTransport transport)
{
@@ -130,6 +139,7 @@ void setup()
{
Serial.begin(115200);
Log("Starting...");
startWebInterface();
makeComInterface();
initHardware();
startBleInterface();