Skip to content

Commit e96c5b8

Browse files
committed
CalibratedSensor: settle time as parameter
1 parent 05f458e commit e96c5b8

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/encoders/calibrated/CalibratedSensor.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ void CalibratedSensor::init()
2828
// Retrieve the calibrated sensor angle
2929
float 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);

src/encoders/calibrated/CalibratedSensor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CalibratedSensor: public Sensor{
2323
/**
2424
* Calibrate method computes the LUT for the correction
2525
*/
26-
virtual void calibrate(FOCMotor& motor, float* lut = nullptr, float zero_electric_angle = 0.0, Direction senor_direction= Direction::CW);
26+
virtual void calibrate(FOCMotor& motor, float* lut = nullptr, float zero_electric_angle = 0.0, Direction senor_direction= Direction::CW, int settle_time_ms = 30);
2727

2828
// voltage to run the calibration: user input
2929
float voltage_calibration = 1;
@@ -55,4 +55,4 @@ class CalibratedSensor: public Sensor{
5555
float* calibrationLut;
5656
};
5757

58-
#endif
58+
#endif

0 commit comments

Comments
 (0)