Hi!
Because using serial communication in the main loop with SimpleFOC changes loop timings, I want to use RT execution for time-critical parts of the code. The manual shows a simple implementation of such loop on Real-time loop | Arduino-FOC , but I can’t get the code to work.
the RT timer loop used by itself works fine blinking an LED, but when SimpleFOC is added to the mix, the board crashes as soon as the timer callback is executed:
rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4744
load:0x40078000,len:15712
load:0x40080400,len:3164
entry 0x4008059c
RT test
Delay
RT timer attach
Guru Meditation Error: Core 1 panic'ed (Interrupt wdt timeout on CPU1).
Core 1 register dump:
PC : 0x400890b7 PS : 0x00060c35 A0 : 0x80089be0 A1 : 0x3ffbf9dc
A2 : 0x3ffb9364 A3 : 0x3ffb81a0 A4 : 0x00000000 A5 : 0x00060c23
A6 : 0x3ffc2c40 A7 : 0x0000abab A8 : 0x3ffb81a0 A9 : 0x00000018
A10 : 0x00000018 A11 : 0x0000008e A12 : 0xb33fffff A13 : 0x3f40f36c
A14 : 0x00000001 A15 : 0x0000abab SAR : 0x00000000 EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x40084df8 LEND : 0x40084e03 LCOUNT : 0xffffffff
Core 1 was running in ISR context:
EPC1 : 0x400dfd13 EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x00000000
Backtrace: 0x400890b4:0x3ffbf9dc |<-CORRUPTED
Core 0 register dump:
PC : 0x400858e9 PS : 0x00060035 A0 : 0x80088778 A1 : 0x3ffbf45c
A2 : 0x00000000 A3 : 0xb33f5454 A4 : 0x0000abab A5 : 0x3f40f2e8
A6 : 0x3ffbc30c A7 : 0x3ffb656c A8 : 0x007bdf00 A9 : 0x3ffbdf00
A10 : 0x003fffff A11 : 0x00060b23 A12 : 0x00060b20 A13 : 0x3f40f2e8
A14 : 0x00000000 A15 : 0x0000cdcd SAR : 0x0000001c EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000
Backtrace: 0x400858e6:0x3ffbf45c |<-CORRUPTED
ELF file SHA256: 32ecd9e7f
@P�@?ON@?T�@?�N@? ets Jul 29 2019 12:21:46&�?8��?j<@?z<@?�<@?T}). @?*J@?9J@?��@? K@?�H@?��
I tried changing timings of the loop RT, but even setting execution to once per second does not eliminate the crash. Just to be sure that it’s not an overrun I tried adding a mutex, but it did not help.
Here’s the code I use for testing:
#include <SimpleFOC.h>
MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
TwoWire I2Cone = TwoWire(0);
BLDCMotor motor = BLDCMotor(11);
BLDCDriver3PWM driver = BLDCDriver3PWM(32,33,25,22);
hw_timer_t *timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void ARDUINO_ISR_ATTR /*IRAM_ATTR*/ foc_loop() {
// portENTER_CRITICAL_ISR(&timerMux);
motor.loopFOC();
motor.move();
// portEXIT_CRITICAL_ISR(&timerMux);
}
void setup() {
Serial.begin(115200);
Serial.println("RT test");
I2Cone.begin(19, 18, 400000);
sensor.init(&I2Cone);
motor.linkSensor(&sensor);
driver.voltage_power_supply = 12;
driver.init();
motor.linkDriver(&driver);
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
motor.controller = MotionControlType::velocity;
motor.init();
motor.initFOC();
Serial.println("Delay");
_delay(1000);
Serial.println("RT timer attach");
timer = timerBegin(1000000);
timerAttachInterrupt(timer, &foc_loop);
timerAlarm(timer, 100, true, 0);
Serial.println("Go!");
}
void loop() {
}
Library version:
SimpleFOC: 2.3.5
IDF: v5.5.1-710-g8410210c9a
plarform-espresif32: 55.03.34