Skip to content

Commit 4a091f0

Browse files
committed
Clean up
1 parent 6226e81 commit 4a091f0

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

src/encoders/MXLEMMING_observer/MXLEMMINGObserverSensor.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ void MXLEMMINGObserverSensor::update() {
2424
// Update sensor, with optional downsampling of update rate
2525
if (sensor_cnt++ < sensor_downsample) return;
2626

27-
// Estimate the BEMF and exit if it's below the threshold
28-
float bemf = _motor.voltage.q - _motor.phase_resistance * _motor.current.q;
29-
if (abs(bemf) < bemf_threshold) return;
30-
3127
sensor_cnt = 0;
3228

3329
// read current phase currents
@@ -39,9 +35,9 @@ void MXLEMMINGObserverSensor::update() {
3935
// get current timestamp
4036
long now_us = _micros();
4137
// calculate the sample time from last call
42-
float Ts = (now_us - angle_prev_ts) * 1e-6f;
38+
float dt = (now_us - angle_prev_ts) * 1e-6f;
4339
// quick fix for strange cases (micros overflow + timestamp not defined)
44-
if(Ts <= 0 || Ts > 0.5f) Ts = 1e-3f;
40+
if(dt <= 0 || dt > 0.5f) dt = 1e-3f;
4541

4642
// This work deviates slightly from the BSD 3 clause licence.
4743
// The work here is entirely original to the MESC FOC project, and not based
@@ -53,10 +49,13 @@ void MXLEMMINGObserverSensor::update() {
5349
// to David Molony as the original author must be noted.
5450

5551
// MXLEMMING Flux Observer
56-
flux_alpha = _constrain( flux_alpha + (_motor.Ualpha - _motor.phase_resistance * ABcurrent.alpha) * Ts -
57-
_motor.phase_inductance * (ABcurrent.alpha - i_alpha_prev),-flux_linkage, flux_linkage);
58-
flux_beta = _constrain( flux_beta + (_motor.Ubeta - _motor.phase_resistance * ABcurrent.beta) * Ts -
59-
_motor.phase_inductance * (ABcurrent.beta - i_beta_prev) ,-flux_linkage, flux_linkage);
52+
float resistive_term_a = _motor.phase_resistance * ABcurrent.alpha;
53+
float resistive_term_b = _motor.phase_resistance * ABcurrent.beta;
54+
float inductive_term_a = _motor.phase_inductance * (ABcurrent.alpha - i_alpha_prev);
55+
float inductive_term_b = _motor.phase_inductance * (ABcurrent.beta - i_beta_prev);
56+
57+
flux_alpha = _constrain( flux_alpha + (_motor.Ualpha - resistive_term_a) * dt - inductive_term_a ,-flux_linkage, flux_linkage);
58+
flux_beta = _constrain( flux_beta + (_motor.Ubeta - resistive_term_b) * dt - inductive_term_b ,-flux_linkage, flux_linkage);
6059

6160
// Calculate electrical angle
6261
electrical_angle = _normalizeAngle(_atan2(flux_beta,flux_alpha));

src/encoders/MXLEMMING_observer/MXLEMMINGObserverSensor.h

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class MXLEMMINGObserverSensor : public Sensor
3737
float electrical_angle = 0; // Electrical angle
3838
float electrical_angle_prev = 0; // Previous electrical angle
3939
float angle_track = 0; // Total Electrical angle
40-
float bemf_threshold = 0; // Bemf voltage amplitude when the flux observer should start tracking
4140

4241
protected:
4342
const FOCMotor& _motor;

src/encoders/MXLEMMING_observer/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ It will not track the position if any of those parameters are missing.
1616

1717
The KV rating and pole pairs parameters are used to derive the motor flux linkage which is key for the flux observer to run well.
1818
```
19+
#include <encoders/MXLEMMING_observer/MXLEMMINGObserverSensor.h>
20+
1921
BLDCMotor motor = BLDCMotor(15, 0.1664, 17.0, 0.00036858); // Hoverboard Motor
2022
MXLEMMINGObserverSensor sensor = MXLEMMINGObserverSensor(motor);
2123
```
@@ -31,10 +33,6 @@ motor.sensor_direction= Direction::CW;
3133
motor.zero_electric_angle = 0;
3234
```
3335

34-
### Bemf Threshold
35-
The sensor also has a bemf_threshold parameter (0 by default) that prevents the flux observer from tracking if the estimated bemf is not high enough.
36-
This can help when starting the motor as the flux observer is not good at tracking the position at low speed.
37-
3836
### To do:
3937
- The Clarke transform is running both in the loopFOC and in the sensor update now, it can be remove from the sensor when the Alpha and Beta currents will be persisted as a BLDCMotor member
4038
- The flux observer is calculating the electrical angle directly, but SimpleFOC needs to derive the electrical angle from the sensor angle for the FOC calculation

0 commit comments

Comments
 (0)