@@ -36,6 +36,13 @@ void MXLEMMINGObserverSensor::update() {
3636 // calculate clarke transform
3737 ABCurrent_s ABcurrent = _motor.current_sense ->getABCurrents (current);
3838
39+ // get current timestamp
40+ long now_us = _micros ();
41+ // calculate the sample time from last call
42+ float Ts = (now_us - angle_prev_ts) * 1e-6f ;
43+ // quick fix for strange cases (micros overflow + timestamp not defined)
44+ if (Ts <= 0 || Ts > 0 .5f ) Ts = 1e-3f ;
45+
3946 // This work deviates slightly from the BSD 3 clause licence.
4047 // The work here is entirely original to the MESC FOC project, and not based
4148 // on any appnotes, or borrowed from another project. This work is free to
@@ -44,10 +51,8 @@ void MXLEMMINGObserverSensor::update() {
4451 // variable names, structures containing variables or other minor
4552 // rearrangements in place of the original names I have chosen, and credit
4653 // to David Molony as the original author must be noted.
47-
48- // Flux linkage observer
49- int32_t now = _micros ();
50- float Ts = ( now - angle_prev_ts) * 1e-6f ;
54+
55+ // MXLEMMING Flux Observer
5156 flux_alpha = _constrain ( flux_alpha + (_motor.Ualpha - _motor.phase_resistance * ABcurrent.alpha ) * Ts -
5257 _motor.phase_inductance * (ABcurrent.alpha - i_alpha_prev),-flux_linkage, flux_linkage);
5358 flux_beta = _constrain ( flux_beta + (_motor.Ubeta - _motor.phase_resistance * ABcurrent.beta ) * Ts -
@@ -89,7 +94,7 @@ void MXLEMMINGObserverSensor::update() {
8994 // Store Previous values
9095 i_alpha_prev = ABcurrent.alpha ;
9196 i_beta_prev = ABcurrent.beta ;
92- angle_prev_ts = now ;
97+ angle_prev_ts = now_us ;
9398 electrical_angle_prev = electrical_angle;
9499
95100}
0 commit comments