@@ -28,10 +28,11 @@ void CalibratedSensor::init()
28
28
// Retrieve the calibrated sensor angle
29
29
float CalibratedSensor::getSensorAngle ()
30
30
{
31
- // raw encoder position e.g. 0-2PI
32
- float raw_angle = _wrapped.getMechanicalAngle ();
31
+ // raw encoder position e.g. 0-2PI
32
+ float raw_angle = fmodf (_wrapped.getMechanicalAngle (), _2PI);
33
+ raw_angle += raw_angle < 0 ? _2PI:0 ;
33
34
34
- // Calculate the resolution of the LUT in radians
35
+ // Calculate the resolution of the LUT in radians
35
36
float lut_resolution = _2PI / n_lut;
36
37
// Calculate LUT index
37
38
int lut_index = raw_angle / lut_resolution;
@@ -41,7 +42,7 @@ float CalibratedSensor::getSensorAngle()
41
42
float y1 = calibrationLut[(lut_index + 1 ) % n_lut];
42
43
43
44
// Linearly interpolate between the y0 and y1 values
44
- // Calculate the relative distance from the y0 (raw_angle has to be between y0 and y1)
45
+ // Calculate the relative distance from the y0 (raw_angle has to be between y0 and y1)
45
46
// If distance = 0, interpolated offset = y0
46
47
// If distance = 1, interpolated offset = y1
47
48
float distance = (raw_angle - lut_index * lut_resolution) / lut_resolution;
@@ -85,7 +86,7 @@ void CalibratedSensor::filter_error(float* error, float &error_mean, int n_ticks
85
86
86
87
}
87
88
88
- void CalibratedSensor::calibrate (FOCMotor &motor, float * lut, float zero_electric_angle, Direction senor_direction)
89
+ void CalibratedSensor::calibrate (FOCMotor &motor, float * lut, float zero_electric_angle, Direction senor_direction, int settle_time_ms )
89
90
{
90
91
// if the LUT is already defined, skip the calibration
91
92
if (lut != nullptr ){
@@ -170,7 +171,7 @@ void CalibratedSensor::calibrate(FOCMotor &motor, float* lut, float zero_electri
170
171
}
171
172
172
173
// delay to settle in position before taking a position sample
173
- _delay (30 );
174
+ _delay (settle_time_ms );
174
175
_wrapped.update ();
175
176
// calculate the error
176
177
theta_actual = motor.sensor_direction *(_wrapped.getAngle () - theta_init);
@@ -210,7 +211,7 @@ void CalibratedSensor::calibrate(FOCMotor &motor, float* lut, float zero_electri
210
211
}
211
212
212
213
// delay to settle in position before taking a position sample
213
- _delay (30 );
214
+ _delay (settle_time_ms );
214
215
_wrapped.update ();
215
216
// calculate the error
216
217
theta_actual = motor.sensor_direction *(_wrapped.getAngle () - theta_init);
0 commit comments