@@ -28,10 +28,11 @@ void CalibratedSensor::init()
2828// Retrieve the calibrated sensor angle
2929float CalibratedSensor::getSensorAngle ()
3030{
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 ;
3334
34- // Calculate the resolution of the LUT in radians
35+ // Calculate the resolution of the LUT in radians
3536 float lut_resolution = _2PI / n_lut;
3637 // Calculate LUT index
3738 int lut_index = raw_angle / lut_resolution;
@@ -41,7 +42,7 @@ float CalibratedSensor::getSensorAngle()
4142 float y1 = calibrationLut[(lut_index + 1 ) % n_lut];
4243
4344 // 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)
4546 // If distance = 0, interpolated offset = y0
4647 // If distance = 1, interpolated offset = y1
4748 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
8586
8687}
8788
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 )
8990{
9091 // if the LUT is already defined, skip the calibration
9192 if (lut != nullptr ){
@@ -170,7 +171,7 @@ void CalibratedSensor::calibrate(FOCMotor &motor, float* lut, float zero_electri
170171 }
171172
172173 // delay to settle in position before taking a position sample
173- _delay (30 );
174+ _delay (settle_time_ms );
174175 _wrapped.update ();
175176 // calculate the error
176177 theta_actual = motor.sensor_direction *(_wrapped.getAngle () - theta_init);
@@ -210,7 +211,7 @@ void CalibratedSensor::calibrate(FOCMotor &motor, float* lut, float zero_electri
210211 }
211212
212213 // delay to settle in position before taking a position sample
213- _delay (30 );
214+ _delay (settle_time_ms );
214215 _wrapped.update ();
215216 // calculate the error
216217 theta_actual = motor.sensor_direction *(_wrapped.getAngle () - theta_init);
0 commit comments