configureInputPins();
delay(500);
Serial.println("Starting UDP...");
Udp.begin(port); // Start UDP
delay(100);
Serial.println("Creating loop task on core 0...");
// Add current task to the watchdog timer
esp_task_wdt_add(NULL);
xTaskCreatePinnedToCore(
loop_Core0, // Task function
"loopTask_Core0", // Name of task
8192, // Increased stack size
NULL, // Parameter of the task
1, // Priority of the task
NULL, // Task handle to keep track of created task
0); // Pin task to core 0
Serial.println("Task created, deleting setup_Core0 task...");
vTaskDelete(NULL);
}
void setup() {
Serial.begin(115200); // Ensure this matches your configuration
pinMode(STEP_0_PIN, OUTPUT);
digitalWrite(STEP_0_PIN, 0);
pinMode(DIR_0_PIN, OUTPUT);
digitalWrite(DIR_0_PIN, 0);
pinMode(STEP_1_PIN, OUTPUT);
digitalWrite(STEP_1_PIN, 0);
pinMode(DIR_1_PIN, OUTPUT);
digitalWrite(DIR_1_PIN, 0);
pinMode(STEP_2_PIN, OUTPUT);
digitalWrite(STEP_2_PIN, 0);
pinMode(DIR_2_PIN, OUTPUT);
digitalWrite(DIR_2_PIN, 0);
// Configure the timers
stepGen_0 = timerBegin(40000000); // Timer 0
stepGen_1 = timerBegin(40000000); // Timer 1
stepGen_2 = timerBegin(40000000); // Timer 2
// Attach the interrupt service routine (ISR) `onTime_0` to Timer 0.
timerAttachInterrupt(stepGen_0, &onTime_0);
// Attach the ISR `onTime_1` to Timer 1.
timerAttachInterrupt(stepGen_1, &onTime_1);
// Attach the ISR `onTime_2` to Timer 2.
timerAttachInterrupt(stepGen_2, &onTime_2);
// Set the alarm value for Timer 0 to 40,000,000 microseconds (40 seconds).
timerAlarm(stepGen_0, 40000000, true, 0);
// Set the alarm value for Timer 1 to 40,000,000 microseconds (40 seconds).
timerAlarm(stepGen_1, 40000000, true, 0);
// Set the alarm value for Timer 2 to 40,000,000 microseconds (40 seconds).
timerAlarm(stepGen_2, 40000000, true, 0);
Serial.println("Initializing Ethernet...");
UIPEthernet.init(SPI_CS_PIN); // Use UIPEthernet.init(pin) to configure the CS pin
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
} else {
Serial.print("DHCP assigned IP: ");
Serial.println(Ethernet.localIP());
}
Serial.print("Server IP address: ");
Serial.println(ip);
// Check if the watchdog timer is already initialized
if (esp_task_wdt_status(NULL) == ESP_ERR_NOT_FOUND) {
// Define the watchdog timer configuration
esp_task_wdt_config_t wdt_config = {
.timeout_ms = 10000, // 10 seconds timeout
.trigger_panic = true
};
// Initialize the watchdog timer
esp_task_wdt_init(&wdt_config);
}
// Add the current task to the watchdog
esp_task_wdt_add(NULL);
xTaskCreatePinnedToCore(
setup_Core0, // Task function.
"setup_Core0Task", // name of task.
8192, // Increase Stack size of task
NULL, // parameter of the task
1, // priority of the task (change from 0 to 1)
NULL, // Task handle to keep track of created task
0); // pin task to core 0
vTaskDelete(NULL);
}
/==================================================================/
void IRAM_ATTR loop() {
// Motor control logic
for (int i = 0; i < 3; i++) {
if (b_math[i]) {
Serial.print("Math processing for axis: ");
Serial.println(i);
b_math[i] = false;
if (!ul_accelStep[i]) { // the axis is stationary
long l_pos_error = cmd.pos[i] - fb.pos[i];
Serial.print("Position error for axis ");
Serial.print(i);
Serial.print(": ");
Serial.println(l_pos_error);
if (l_pos_error) { // if there is a position error
if ((l_pos_error > 0 && b_dirSignal[i] == HIGH) || (l_pos_error < 0 && b_dirSignal[i] == LOW)) // the direction is good
acceleration(i);
else { // need to change direction
(l_pos_error > 0) ? b_dirSignal[i] = HIGH : b_dirSignal[i] = LOW;
b_dirChange[i] = true;
Serial.print("Changing direction for axis ");
Serial.println(i);
}
}
} else { // the axis moves
Serial.print("Axis ");
Serial.print(i);
Serial.println(" is moving");
if ((cmd.vel[i] > 0.0f && b_dirSignal[i] == HIGH) || (cmd.vel[i] < 0.0f && b_dirSignal[i] == LOW)) { // the direction is good
if (ul_T[i] > ul_cmd_T[i]) // the speed is low
acceleration(i);
else if (ul_T[i] < ul_cmd_T[i]) // the speed is high
deceleration(i);
} else // the direction is wrong or the target speed is zero
deceleration(i);
}
}
}
// Ethernet server logic
EthernetClient client = server.available();
if (client) {
Serial.println("Client connected");
client.println("Hello, client!");
delay(1000);
client.stop();
Serial.println("Client disconnected");
}
Hey jzolee, instead of w5500 im using ENC28J60. So I should use UIPEthernet lib. I made the necessary changes but getting some errors. `#include
include
include
include
// MAC address and static IP configuration byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 96, 54); unsigned int port = 58427;
define SPI_CS_PIN 5 // SPI chip select pin for ENC28J60
EthernetServer server(80); // Adding EthernetServer for HTTP EthernetUDP Udp; // Use EthernetUDP provided by UIPEthernet
define STEP_0_PIN 12
define STEP_0_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT12)
define STEP_0_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT12)
define DIR_0_PIN 13
define DIR_0_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT13)
define DIR_0_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT13)
define STEP_1_PIN 16
define STEP_1_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT16)
define STEP_1_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT16)
define DIR_1_PIN 17
define DIR_1_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT17)
define DIR_1_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT17)
define STEP_2_PIN 21
define STEP_2_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT21)
define STEP_2_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT21)
define DIR_2_PIN 22
define DIR_2_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT22)
define DIR_2_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT22)
/==================================================================/
define OUT_00_PIN 2
define OUT_00_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT2)
define OUT_00_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT2)
define OUT_01_PIN 4
define OUT_01_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT4)
define OUT_01_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT4)
define OUT_02_PIN 25
define OUT_02_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT25)
define OUT_02_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT25)
define OUT_03_PIN 1
define OUT_03_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT1)
define OUT_03_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT1)
define OUT_04_PIN 14
define OUT_04_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT14)
define OUT_04_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT14)
define OUT_05_PIN 15
define OUT_05_H REG_WRITE(GPIO_OUT_W1TS_REG, BIT15)
define OUT_05_L REG_WRITE(GPIO_OUT_W1TC_REG, BIT15)
/==================================================================/
define IN_00_PIN 26
define IN_00 REG_READ(GPIO_IN_REG) & BIT26 //26
define IN_01_PIN 27
define IN_01 REG_READ(GPIO_IN_REG) & BIT27 //27
define IN_02_PIN 32
define IN_02 REG_READ(GPIO_IN1_REG) & BIT0 //32 -32
define IN_03_PIN 33
define IN_03 REG_READ(GPIO_IN1_REG) & BIT1 //33 -32
define IN_04_PIN 34
define IN_04 REG_READ(GPIO_IN1_REG) & BIT2 //34 -32
define IN_05_PIN 35
define IN_05 REG_READ(GPIO_IN1_REG) & BIT3 //35 -32
define IN_06_PIN 36
define IN_06 REG_READ(GPIO_IN1_REG) & BIT4 //36 -32
define IN_07_PIN 39
define IN_07 REG_READ(GPIO_IN1_REG) & BIT7 //39 -32
/==================================================================/
define CTRL_DIRSETUP 0b00000001
define CTRL_ACCEL 0b00000010
define CTRL_PWMFREQ 0b00000100
define CTRL_READY 0b01000000
define CTRL_ENABLE 0b10000000
define IO_00 0b00000001
define IO_01 0b00000010
define IO_02 0b00000100
define IO_03 0b00001000
define IO_04 0b00010000
define IO_05 0b00100000
define IO_06 0b01000000
define IO_07 0b10000000
/==================================================================/
struct cmdPacket { uint8_t control; uint8_t io; uint16_t pwm[6]; int32_t pos[3]; float vel[3]; } cmd = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0f, 0.0f, 0.0f };
struct fbPacket { uint8_t control; uint8_t io; int32_t pos[3]; float vel[3]; } fb = { 0, 0, 0, 0, 0, 0.0f, 0.0f, 0.0f };
/==================================================================/
volatile unsigned long ul_dirSetup[3] = { 1000, 1000, 1000 }; // x 25 nanosec volatile float f_accel_x2[3] = { 1000.0, 1000.0, 1000.0 }; // acceleration*2 step/sec2
volatile unsigned long ul_cmd_T[3]; unsigned long ul_accelStep[3] = { 0, 0, 0 };
volatile unsigned long ul_T[3] = { 0, 0, 0 }; volatile unsigned long ul_TH[3] = { 0, 0, 0 }; volatile unsigned long ul_TL[3] = { 0, 0, 0 }; volatile bool b_math[3] = { false, false, false }; volatile bool b_dirSignal[3] = { LOW, LOW, LOW }; volatile bool b_dirChange[3] = { false, false, false };
const uint8_t pwm_pin[6] = { OUT_00_PIN, OUT_01_PIN, OUT_02_PIN, OUT_03_PIN, OUT_04_PIN, OUT_05_PIN }; bool pwm_enable[6] = { false, false, false, false, false, false };
hw_timer_t stepGen_0 = NULL; hw_timer_t stepGen_1 = NULL; hw_timer_t* stepGen_2 = NULL;
/==================================================================/ float IRAM_ATTR fastInvSqrt(const float x) { const float xhalf = x 0.5f; union { float x; uint32_t i; } u = { .x = x }; u.i = 0x5f3759df - (u.i >> 1); return u.x (1.5f - xhalf u.x u.x); }
/==================================================================/
void IRAM_ATTR onTime_0() { static bool b_stepState = LOW; static bool b_dirState = LOW;
}
void IRAM_ATTR onTime_1() { static bool b_stepState = LOW; static bool b_dirState = LOW;
}
void IRAM_ATTR onTime_2() { static bool b_stepState = LOW; static bool b_dirState = LOW;
}
/==================================================================/ void IRAM_ATTR newT(const int i) { ul_T[i] = 40000000.0f fastInvSqrt((float)ul_accelStep[i] f_accel_x2[i]); ul_TH[i] = ul_T[i] >> 1; ul_TL[i] = ul_T[i] - ul_TH[i]; }
/==================================================================/ void IRAM_ATTR deceleration(const int i) { if (ul_accelStep[i]) { ul_accelStep[i]--; if (ul_accelStep[i]) newT(i); else ul_T[i] = 0; } }
void IRAM_ATTR acceleration(const int i) { if (cmd.control & CTRL_ENABLE) { ul_accelStep[i]++; newT(i); } else deceleration(i); }
/==================================================================/ void IRAM_ATTR outputHandler() { static int last_pwm[6] = { 0, 0, 0, 0, 0, 0 };
}
/==================================================================/ void IRAM_ATTR inputHandler() { (IN_00) ? fb.io = IO_00 : fb.io = 0; if (IN_01) fb.io |= IO_01; if (IN_02) fb.io |= IO_02; if (IN_03) fb.io |= IO_03; if (IN_04) fb.io |= IO_04; if (IN_05) fb.io |= IO_05; if (IN_06) fb.io |= IO_06; if (IN_07) fb.io |= IO_07; } /==================================================================/ void IRAM_ATTR commandHandler() { if (cmd.control & CTRL_READY) { Serial.println("Handling CTRL_READY");
} /==================================================================/ void IRAM_ATTR loop_Core0(void* parameter) { delay(500); Udp.parsePacket(); // to empty buffer
}
/==================================================================/
void configureInputPins() { Serial.println("Configuring input pins..."); pinMode(IN_00_PIN, INPUT_PULLUP); pinMode(IN_01_PIN, INPUT_PULLUP); pinMode(IN_02_PIN, INPUT_PULLUP); pinMode(IN_03_PIN, INPUT_PULLUP);
}
void setup_Core0(void* parameter) { Serial.println("Entering setup_Core0");
}
void setup() { Serial.begin(115200); // Ensure this matches your configuration
}
/==================================================================/ void IRAM_ATTR loop() { // Motor control logic for (int i = 0; i < 3; i++) { if (b_math[i]) { Serial.print("Math processing for axis: "); Serial.println(i); b_math[i] = false; if (!ul_accelStep[i]) { // the axis is stationary long l_pos_error = cmd.pos[i] - fb.pos[i]; Serial.print("Position error for axis "); Serial.print(i); Serial.print(": "); Serial.println(l_pos_error); if (l_pos_error) { // if there is a position error if ((l_pos_error > 0 && b_dirSignal[i] == HIGH) || (l_pos_error < 0 && b_dirSignal[i] == LOW)) // the direction is good acceleration(i); else { // need to change direction (l_pos_error > 0) ? b_dirSignal[i] = HIGH : b_dirSignal[i] = LOW; b_dirChange[i] = true; Serial.print("Changing direction for axis "); Serial.println(i); } } } else { // the axis moves Serial.print("Axis "); Serial.print(i); Serial.println(" is moving"); if ((cmd.vel[i] > 0.0f && b_dirSignal[i] == HIGH) || (cmd.vel[i] < 0.0f && b_dirSignal[i] == LOW)) { // the direction is good if (ul_T[i] > ul_cmd_T[i]) // the speed is low acceleration(i); else if (ul_T[i] < ul_cmd_T[i]) // the speed is high deceleration(i); } else // the direction is wrong or the target speed is zero deceleration(i); } } }
}
`
for this i'm getting output
18:42:53.285 -> mode:DIO, clock div:1 18:42:53.317 -> load:0x3fff0030,len:1448 18:42:53.317 -> load:0x40078000,len:14844 18:42:53.317 -> ho 0 tail 12 room 4 18:42:53.317 -> load:0x40080400,len:4 18:42:53.317 -> load:0x40080404,len:3356 18:42:53.317 -> entry 0x4008059c 18:42:53.448 -> Initializing Ethernet... 18:43:53.774 -> Failed to configure Ethernet using DHCP 18:43:53.839 -> Server IP address: 192.168.96.54 18:43:53.839 -> E (60379) task_wdt: esp_task_wdt_init(573): TWDT already initialized 18:43:53.839 -> Entering setup_Core0 18:43:53.839 -> Configuring input pins... 18:43:54.331 -> Starting UDP... 18:43:54.430 -> Creating loop task on core 0... 18:43:54.430 -> Task created, deleting setup_Core0 task... 18:43:54.955 -> Entering loop_Core0 18:43:54.955 -> Packet received 18:43:54.955 -> Received raw packet: 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 80 BB 0 0 0 EE 2 0 80 A9 3 0 0 0 0 0 0 0 0 0 0 0 0 0 B8 18:43:54.955 -> Received cmdPacket: 18:43:54.955 -> control: 2 18:43:54.955 -> io: 0 18:43:54.955 -> pwm[0]: 0 18:43:54.955 -> pwm[1]: 0 18:43:54.955 -> pwm[2]: 0 18:43:54.955 -> pwm[3]: 0 18:43:54.955 -> pwm[4]: 0 18:43:54.955 -> pwm[5]: 0 18:43:54.955 -> pos[0]: 48000 18:43:54.955 -> pos[1]: 192000 18:43:54.985 -> pos[2]: 240000 18:43:54.986 -> vel[0]: 0.00 18:43:54.986 -> vel[1]: 0.00 18:43:54.986 -> vel[2]: 0.00 18:43:54.986 -> fb.control not ready 18:43:54.986 -> CTRL_ACCEL set 18:43:54.986 -> Sending fbPacket: 18:43:54.986 -> control: 2
Can you help me out?