SimpleFOC and ESPAsyncWebServer are conflicting?

rightiochap
Posts: 1
Joined: Wed Mar 20, 2024 1:25 am

SimpleFOC and ESPAsyncWebServer are conflicting?

Postby rightiochap » Wed Mar 20, 2024 1:41 am

As my title suggests, I am trying to run a BLDC motor at full speed in loop(), while at the same time I wish to run a server that updates a chart at some set inverval. However every time the chart updates, or a button is pressed, the motor skips/stutters. I've tried swapping the motor to Core 0, or vice versa but no luck. Here are some snippets of the test code, but it seems I am not understanding something happening in the background. Any help?

Code: Select all

#include <SimpleFOC.h>
#include <ESPAsyncWebServer.h>

void setup(){

  // Route for root / web page
   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });
  server.on("/pressure", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", atmoshpere().c_str());
  }); }
  
   xTaskCreatePinnedToCore(
    Task1code, //function name
    "Task1", // name
    10000, // stack size
    NULL, // type
    2, // priority
    &Task1, // task handle
    0 // core number
 ); 
 
 void loop(){
  motor.move(target_velocity);
  motor_i = motor_i +1;

  if (motor_i>= 3000){
    if (target_velocity<50){
      target_velocity = target_velocity + .25;
    }
    motor_i = 0;
  }
  }

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: SimpleFOC and ESPAsyncWebServer are conflicting?

Postby lbernstone » Wed Mar 20, 2024 7:35 pm

There's not enough info here to really see what is going on, but here's a couple comments.
loop() is in a task run at priority 1 on ARDUINO_RUNNING_CORE (typically core 1). If you want to control the priorities and cpu, you should run all your code from tasks that you can control priority and core.
FreeRTOS has a very simple token scheduler. The highest priority task waiting for access on a cpu will get the token, and will not release it until there is a yield (which will only give up to a >= priority task) or a delay (which takes the task out of the scheduler for a set number of ticks).
While you have 2 cpus, there are hardware resources which have a single queue, particularly the flash and radio. Long flash activities can seriously impact your processing, and so care must be taken to atomize large reads and writes and put delays between transfers.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 111 guests