Hi, I did some more tests:
It seems that a simple delay long enough is not a fix. The timing is quite sensitive, with a repeating pattern.
To get reliable results, I had to compile without any optimization (-O0).
I have serial debug enabled at 1Mbps.
I’m adding a delay in microseconds between driver.init(); and motor.linkDriver(&driver);
Here is the pattern
/*
0 fail
1 fail
15 fail
20 fail
25 fail
30 pass
40 pass
50 pass
55 pass
60 fail
70 fail
80 fail
90 fail
95 pass
100 pass
110 pass
120 fail
130 fail
140 fail
145 pass
150 pass
160 pass
170 fail
180 fail
190 fail
195 pass
200 pass
210 pass
220 fail
230 fail
240 fail
250 fail
255 fail
260 pass
270 pass
280 pass
290 fail
*/
Here is the setup code:
void setup() {
_delay(2000);
#ifdef USE_HW_ENC
encoder._pinA = PB_6;
encoder._pinB = PB_7_ALT1;
#else
encoder.enableInterrupts(doA, doB);
#endif
// use monitoring with serial
Serial.begin(1000000);
// enable more verbose output for debugging
// comment out if not needed
SimpleFOCDebug::enable(&Serial);
encoder.init();
motor.linkSensor(&encoder);
driver.voltage_power_supply = 16; // power supply voltage [V]
driver.init();
delayMicroseconds(255); // Depending on this delay, the current sensor alignment will pass ot fail (see list below)
motor.linkDriver(&driver);
currentSense.linkDriver(&driver);
currentSense.init();
motor.linkCurrentSense(¤tSense);
motor.voltage_sensor_align = 1;
motor.torque_controller = TorqueControlType::foc_current;
motor.controller = MotionControlType::torque;
motor.PID_current_q.P = 2.;
motor.PID_current_q.I = 800.0;
motor.PID_current_d.P = 2.;
motor.PID_current_d.I = 1000.0;
motor.LPF_current_q.Tf = 0.003;
motor.LPF_current_d.Tf = 0.003;
motor.current_limit = 3.0;//A
motor.voltage_limit = 4.0;//V
//motor.useMonitoring(Serial);
// initialize motor
motor.init();
// align encoder and start FOC
motor.initFOC();
while(1);