33// CalibratedSensor()
44// sensor - instance of original sensor object
55// n_lut - number of samples in the LUT
6- CalibratedSensor::CalibratedSensor (Sensor &wrapped, int n_lut) : _wrapped(wrapped) {
7- this -> n_lut = n_lut;
8- };
6+ CalibratedSensor::CalibratedSensor (Sensor &wrapped, int n_lut, float *lut)
7+ : _wrapped(wrapped), n_lut(n_lut), allocated( false ), calibrationLut(lut) {
8+ };
99
1010CalibratedSensor::~CalibratedSensor () {
1111 // delete calibrationLut;
12+ if (allocated) {
13+ delete [] calibrationLut;
14+ }
1215};
1316
1417// call update of calibrated sensor
@@ -28,6 +31,9 @@ void CalibratedSensor::init()
2831// Retrieve the calibrated sensor angle
2932float CalibratedSensor::getSensorAngle ()
3033{
34+ if (!calibrationLut) {
35+ return _wrapped.getMechanicalAngle ();
36+ }
3137 // raw encoder position e.g. 0-2PI
3238 float raw_angle = fmodf (_wrapped.getMechanicalAngle (), _2PI);
3339 raw_angle += raw_angle < 0 ? _2PI:0 ;
@@ -86,19 +92,15 @@ void CalibratedSensor::filter_error(float* error, float &error_mean, int n_ticks
8692
8793}
8894
89- void CalibratedSensor::calibrate (FOCMotor &motor, float * lut, float zero_electric_angle, Direction senor_direction, int settle_time_ms)
95+ void CalibratedSensor::calibrate (FOCMotor &motor, int settle_time_ms)
9096{
9197 // if the LUT is already defined, skip the calibration
92- if (lut != nullptr ){
93- motor.monitor_port ->println (" Using the provided LUT." );
94- motor.zero_electric_angle = zero_electric_angle;
95- motor.sensor_direction = senor_direction;
96- this ->calibrationLut = lut;
97- return ;
98- }else {
99- this ->calibrationLut = new float [n_lut]();
100- motor.monitor_port ->println (" Starting Sensor Calibration." );
98+
99+ if (calibrationLut == NULL ) {
100+ allocated = true ;
101+ calibrationLut = new float [n_lut];
101102 }
103+ motor.monitor_port ->println (" Starting Sensor Calibration." );
102104
103105 // Calibration variables
104106
@@ -174,11 +176,11 @@ void CalibratedSensor::calibrate(FOCMotor &motor, float* lut, float zero_electri
174176 _delay (settle_time_ms);
175177 _wrapped.update ();
176178 // calculate the error
177- theta_actual = motor.sensor_direction * (_wrapped.getAngle () - theta_init);
179+ theta_actual = ( int ) motor.sensor_direction * (_wrapped.getAngle () - theta_init);
178180 error[i] = 0.5 * (theta_actual - elec_angle / _NPP);
179181
180182 // calculate the current electrical zero angle
181- float zero_angle = (motor.sensor_direction * _wrapped.getMechanicalAngle () * _NPP ) - (elec_angle + _PI_2);
183+ float zero_angle = (( int ) motor.sensor_direction * _wrapped.getMechanicalAngle () * _NPP ) - (elec_angle + _PI_2);
182184 zero_angle = _normalizeAngle (zero_angle);
183185 // remove the 2PI jumps
184186 if (zero_angle - zero_angle_prev > _PI){
@@ -214,10 +216,10 @@ void CalibratedSensor::calibrate(FOCMotor &motor, float* lut, float zero_electri
214216 _delay (settle_time_ms);
215217 _wrapped.update ();
216218 // calculate the error
217- theta_actual = motor.sensor_direction * (_wrapped.getAngle () - theta_init);
219+ theta_actual = ( int ) motor.sensor_direction * (_wrapped.getAngle () - theta_init);
218220 error[i] += 0.5 * (theta_actual - elec_angle / _NPP);
219221 // calculate the current electrical zero angle
220- float zero_angle = (motor.sensor_direction * _wrapped.getMechanicalAngle () * _NPP ) - (elec_angle + _PI_2);
222+ float zero_angle = (( int ) motor.sensor_direction * _wrapped.getMechanicalAngle () * _NPP ) - (elec_angle + _PI_2);
221223 zero_angle = _normalizeAngle (zero_angle);
222224 // remove the 2PI jumps
223225 if (zero_angle - zero_angle_prev > _PI){
@@ -272,7 +274,7 @@ void CalibratedSensor::calibrate(FOCMotor &motor, float* lut, float zero_electri
272274 if (ind < 0 ) ind += n_lut;
273275 calibrationLut[ind] = (float )(error[(int )(i * dn)] - error_mean);
274276 // negate the error if the sensor is in the opposite direction
275- calibrationLut[ind] = motor.sensor_direction * calibrationLut[ind];
277+ calibrationLut[ind] = ( int ) motor.sensor_direction * calibrationLut[ind];
276278 }
277279 motor.monitor_port ->println (" " );
278280 _delay (1000 );
0 commit comments