Skip to content

Commit 0010b2c

Browse files
committed
Cleanup
1 parent 9045e6c commit 0010b2c

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

src/encoders/MXLEMMING_observer/MXLEMMINGObserverSensor.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,29 @@ void MXLEMMINGObserverSensor::update() {
5858
flux_beta = _constrain( flux_beta + (_motor.Ubeta - _motor.phase_resistance * ABcurrent.beta) * Ts -
5959
_motor.phase_inductance * (ABcurrent.beta - i_beta_prev) ,-flux_linkage, flux_linkage);
6060

61-
// Calculate angle
62-
float electrical_angle = _normalizeAngle(_atan2(flux_beta,flux_alpha));
61+
// Calculate electrical angle
62+
electrical_angle = _normalizeAngle(_atan2(flux_beta,flux_alpha));
6363

6464
// Electrical angle difference
65-
float d_electrical_angle = 0;
66-
if (first){
67-
// Skip angle difference calculation the first time
68-
first = 0;
69-
d_electrical_angle = electrical_angle;
70-
}else{
71-
d_electrical_angle = electrical_angle - electrical_angle_prev;
72-
if(abs(d_electrical_angle) > _2PI * 0.8 ){ //change the factor based on sample rate can also just use _PI for simplicity
73-
if (d_electrical_angle > 0){
74-
d_electrical_angle -= _2PI;
75-
}else{
76-
d_electrical_angle += _2PI;
77-
}
65+
float d_electrical_angle = electrical_angle - electrical_angle_prev;
66+
if(abs(d_electrical_angle) > _2PI * 0.8 ){ //change the factor based on sample rate can also just use _PI for simplicity
67+
if (d_electrical_angle > 0){
68+
d_electrical_angle -= _2PI;
69+
}else{
70+
d_electrical_angle += _2PI;
7871
}
7972
}
8073
angle_track += d_electrical_angle;
8174

8275
// Mechanical angle and full_rotations
83-
if(abs(angle_track) > _2PI * _motor.pole_pairs){
76+
float full_rotation = _2PI * _motor.pole_pairs;
77+
if(abs(angle_track) > full_rotation){
8478
if (angle_track>0){
8579
full_rotations += 1;
86-
angle_track -= _2PI * _motor.pole_pairs;
80+
angle_track -= full_rotation;
8781
}else{
8882
full_rotations -= 1;
89-
angle_track += _2PI * _motor.pole_pairs;
83+
angle_track += full_rotation;
9084
}
9185
}
9286
angle_prev = angle_track /_motor.pole_pairs;
@@ -108,4 +102,4 @@ void MXLEMMINGObserverSensor::init(){
108102
*/
109103
float MXLEMMINGObserverSensor::getSensorAngle(){
110104
return 0;
111-
}
105+
}

src/encoders/MXLEMMING_observer/MXLEMMINGObserverSensor.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ class MXLEMMINGObserverSensor : public Sensor
2929
// For sensors with slow communication, use these to poll less often
3030
unsigned int sensor_downsample = 0; // parameter defining the ratio of downsampling for sensor update
3131
unsigned int sensor_cnt = 0; // counting variable for downsampling
32-
float flux_alpha = 0; // Flux Alpha
33-
float flux_beta = 0; // Flux Beta
32+
float flux_alpha = 0; // Flux Alpha
33+
float flux_beta = 0; // Flux Beta
3434
float flux_linkage = 0; // Flux linkage, calculated based on KV and pole number
3535
float i_alpha_prev = 0; // Previous Alpha current
36-
float i_beta_prev = 0; // Previous Beta current
36+
float i_beta_prev = 0; // Previous Beta current
37+
float electrical_angle = 0; // Electrical angle
3738
float electrical_angle_prev = 0; // Previous electrical angle
3839
float angle_track = 0; // Total Electrical angle
3940
float bemf_threshold = 0; // Bemf voltage amplitude when the flux observer should start tracking
40-
int8_t first = 1; // To skip angle difference calculation the first time
41-
41+
4242
protected:
4343
const FOCMotor& _motor;
4444

4545
};
4646

47-
#endif
47+
#endif

src/encoders/MXLEMMING_observer/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ The sensor also has a bemf_threshold parameter (0 by default) that prevents the
3636
This can help when starting the motor as the flux observer is not good at tracking the position at low speed.
3737

3838
### To do:
39-
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.
39+
- 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
40+
- 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
4041

0 commit comments

Comments
 (0)