Hello,
I recently got my SimpleFOC board working with a nucleo board and I’m learning that updates rates are going to be really important to my application. I threw some micros() calls in my loop to see what I should optimize, but the code in the loop is only 1/10th the duration of each loop cycle. Here’s a slightly simplified example of my test:
void loop() {
long now = micros();
long loopDelta = now - lastLoopTime;
lastLoopTime = now;
Serial.print("loopDelta: ");
Serial.print(loopDelta);
Serial.print("\n");
long focStart = micros();
// iterative setting FOC phase voltage
motor.loopFOC();
float localAngle = sensor.getAngle();
float delta = (target - localAngle);
// iterative function setting the outter loop target
motor.move(delta);
long focEnd = micros();
Serial.print("foc: ");
Serial.println((focEnd - focStart));
long serialStart = micros();
if (Serial.available()) {
// change the role via the serial monitor
char c = toupper(Serial.read());
if (c == 'S') {
stopped = true;
}
}
long serialEnd = micros();
Serial.print("serial: ");
Serial.println((serialEnd - serialStart));
}
Please forgive the sloppy naming. The gist is that I’m timing the following:
- How long the SimpleFOC calls take
- How long the Serial calls take
- How long it’s been since the last time the loop ran
Surprisingly, SimpleFOC takes about 200us, Serial takes about 1us, but the loop time (or loopDelta) is 1500-6000us.
So stuff that is invisible to me is dropping my update rate from ~5khz to ~266hz. Does anyone have any hints on what might be going on? I’m pretty new to embedded programming so I’m not sure where to start. Is there a way to profile what’s going on in the background? Am I just measuring things incorrectly? Will I need to make the leap to STM32CubeIDE to get good performance? Any help would be appreciated.
Oh and I’m using platformio if that matters.