So i have this project which require me to move a CNC machine using webserver. but when i start pressing on a button from the webserver, the program will run for a while but suddenly gets rebooted in the middle of a process.
For example i want to move motor X to 500 with the speed of 100. so i input 100 for the speed of my motor (feedrate) on the webserver and then i press the button which should move the motor to X500. but the ESP32 got rebooted when it reached 450.
This is the error i got :
E (78495) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (78495) task_wdt: - async_tcp (CPU 1)
E (78495) task_wdt: Tasks currently running:
E (78495) task_wdt: CPU 0: IDLE
E (78495) task_wdt: CPU 1: loopTask
E (78495) task_wdt: Aborting.
abort() was called at PC 0x400e730d on core 0
Backtrace: 0x40083695:0x3ffbec0c |<-CORRUPTED
ELF file SHA256: 4b9153f163730f6d
Rebooting...
Here is my esp32 code :
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include "SPIFFS.h"
#include "LITTLEFS.h"
const char *ssid = "WIFI SSID";
const char *password = "PASSWORD";
AsyncWebServer server(80);
//------------------------------------------------------------------------------
// GLOBALS
//------------------------------------------------------------------------------
byte directionPinX = 14;
byte stepPinX = 12;
byte enableX = 13;
#define MIN_STEP_DELAY (50.0)
#define MAX_FEEDRATE (1000000.0 / MIN_STEP_DELAY)
#define MIN_FEEDRATE (0.01)
float px, py, pz, pe; // CURRENT LOC
// speeds
float fr = 0; // human version
long step_delay; // machine version
unsigned long lastReportTime = 0;
const long reportInterval = 500;
//------------------------------------------------------------------------------
// METHODS
//------------------------------------------------------------------------------
void m1step(int dir)
{
digitalWrite(enableX, LOW);
digitalWrite(directionPinX, dir == 1 ? HIGH : LOW);
digitalWrite(stepPinX, HIGH);
digitalWrite(stepPinX, LOW);
}
void pause(long ms)
{
delay(ms / 1000);
delayMicroseconds(ms % 1000); // delayMicroseconds tidak berfungsi untuk nilai > ~16k.
}
void feedrate(float nfr)
{
if (fr == nfr)
return;
if (nfr > MAX_FEEDRATE || nfr < MIN_FEEDRATE)
{
Serial.print(F("New feedrate must be greater than "));
Serial.print(MIN_FEEDRATE);
Serial.print(F("steps/s and less than "));
Serial.print(MAX_FEEDRATE);
Serial.println(F("steps/s."));
return;
}
step_delay = 1000000.0 / nfr;
fr = nfr;
}
void position(float npx, float npy, float npz, float npe)
{
px = npx;
py = npy;
pz = npz;
pe = npe;
}
void line(float newx, float newy, float newz, float newe)
{
long dx = newx - px;
long dy = newy - py;
long dz = newz - pz;
long de = newe - pe;
int dirx = dx > 0 ? 1 : -1;
int diry = dy > 0 ? 1 : -1;
int dirz = dz > 0 ? 1 : -1;
int dire = de > 0 ? 1 : -1;
dx = abs(dx);
dy = abs(dy);
dz = abs(dz);
de = abs(de);
long overx = 0, overy = 0, overz = 0, overe = 0;
long max_distance = max(dx, max(dy, dz));
for (long i = 0; i < max_distance; ++i)
{
// X
if (dx > 0)
{
overx += dx;
if (overx >= max_distance)
{
overx -= max_distance;
m1step(dirx);
}
}
if (millis() - lastReportTime >= reportInterval)
{
Serial.print("Current Position: X=");
Serial.print(px);
Serial.print(", Y=");
Serial.print(py);
Serial.print(", Z=");
Serial.print(pz);
Serial.print(", E=");
Serial.println(pe);
lastReportTime = millis();
}
pause(step_delay);
// Update motor position
px += dirx * (1.0 / max_distance) * dx;
py += diry * (1.0 / max_distance) * dy;
pz += dirz * (1.0 / max_distance) * dz;
pe += dire * (1.0 / max_distance) * de;
}
px = newx;
py = newy;
pz = newz;
pe = newe;
Serial.println("Motor has reached the target:");
Serial.print("end position: X=");
Serial.print(px);
Serial.print(", Y=");
Serial.print(py);
Serial.print(", Z=");
Serial.print(pz);
Serial.print(", E=");
Serial.println(pe);
}
void setup()
{
Serial.begin(115200);
if (!SPIFFS.begin(true))
{
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html", String(), false); });
server.on("/moveXPlus", HTTP_GET, [](AsyncWebServerRequest *request){
line(px + 500, py, pz, pe);
request->send(200, "text/plain", "Moving X Plus"); });
server.on("/setFeedrate", HTTP_GET, [](AsyncWebServerRequest *request){
if (request->hasParam("value")) {
String feedrateValue = request->getParam("value")->value();
feedrate(feedrateValue.toFloat());
request->send(200, "text/plain", "Feedrate set to: " + feedrateValue);
} else {
request->send(400, "text/plain", "Invalid request");
}});
server.begin();
// x
pinMode(directionPinX, OUTPUT);
pinMode(stepPinX, OUTPUT);
pinMode(enableX, OUTPUT);
position(0, 0, 0, 0); // set starting position for X, Y, Z
feedrate((MAX_FEEDRATE + MIN_FEEDRATE) / 2); // set default speed
}
void loop()
{
}
and here is my index.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="globals.css" />
</head>
<body>
<div class="frame">
<div class="overlap-wrapper">
<div class="overlap">
<div class="section-2">
<div class="other-control">
<div class="overlap-3">
<form class="feed-rate">
<div class="text-wrapper-10">FeedRate :</div>
<input type="text" class="text-wrapper-9 overlap-4" />
<button type="submit" class="overlap-group-2">
<div class="text-wrapper-11">Submit</div>
</button>
</form>
</div>
</div>
<div class="position-control">
<div class="XYZ-controller">
<div class="XY-controller">
<button class="triangle triangle-x-plus" id="x-plus">Move X plus </button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
document.getElementById("x-plus").addEventListener("click", function () {
fetch("/moveXPlus")
.then((response) => response.text())
.then((data) => console.log(data));
});
document
.querySelector(".feed-rate")
.addEventListener("submit", function (event) {
event.preventDefault();
var feedrateValue = document.querySelector(".text-wrapper-9").value;
fetch(`/setFeedrate?value=${feedrateValue}`)
.then((response) => response.text())
.then((data) => console.log(data));
});
</script>
</body>
</html>
i'm new to working projects on ESP32, i've searched everywhere regarding my problems but couldn't find a solution. can someone help me fix my code?
note: this is not the full version of my code, i have the full version which i can input G-code through serial monitor which works perfectly fine, for example i want to move X to 500 i can just say 'G01 X500 F100' and the watch dog timer wouldn't get triggered. it just that when i control through webserver which supposed to have the same function, the watchdog get triggered.
And i've seen people saying it's because of delay or something like that, well it might be true but then if i remove my delay which i use the function pause() i couldn't controll the speed of my stepper motor. can someone help me find a solution for my problem please