3
3
// CalibratedSensor()
4
4
// sensor - instance of original sensor object
5
5
// 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
+ };
9
9
10
10
CalibratedSensor::~CalibratedSensor () {
11
11
// delete calibrationLut;
12
+ if (allocated) {
13
+ delete [] calibrationLut;
14
+ }
12
15
};
13
16
14
17
// call update of calibrated sensor
@@ -28,6 +31,9 @@ void CalibratedSensor::init()
28
31
// Retrieve the calibrated sensor angle
29
32
float CalibratedSensor::getSensorAngle ()
30
33
{
34
+ if (!calibrationLut) {
35
+ return _wrapped.getMechanicalAngle ();
36
+ }
31
37
// raw encoder position e.g. 0-2PI
32
38
float raw_angle = fmodf (_wrapped.getMechanicalAngle (), _2PI);
33
39
raw_angle += raw_angle < 0 ? _2PI:0 ;
@@ -86,19 +92,15 @@ void CalibratedSensor::filter_error(float* error, float &error_mean, int n_ticks
86
92
87
93
}
88
94
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)
90
96
{
91
97
// 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];
101
102
}
103
+ motor.monitor_port ->println (" Starting Sensor Calibration." );
102
104
103
105
// Calibration variables
104
106
@@ -174,11 +176,11 @@ void CalibratedSensor::calibrate(FOCMotor &motor, float* lut, float zero_electri
174
176
_delay (settle_time_ms);
175
177
_wrapped.update ();
176
178
// calculate the error
177
- theta_actual = motor.sensor_direction * (_wrapped.getAngle () - theta_init);
179
+ theta_actual = ( int ) motor.sensor_direction * (_wrapped.getAngle () - theta_init);
178
180
error[i] = 0.5 * (theta_actual - elec_angle / _NPP);
179
181
180
182
// 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);
182
184
zero_angle = _normalizeAngle (zero_angle);
183
185
// remove the 2PI jumps
184
186
if (zero_angle - zero_angle_prev > _PI){
@@ -214,10 +216,10 @@ void CalibratedSensor::calibrate(FOCMotor &motor, float* lut, float zero_electri
214
216
_delay (settle_time_ms);
215
217
_wrapped.update ();
216
218
// calculate the error
217
- theta_actual = motor.sensor_direction * (_wrapped.getAngle () - theta_init);
219
+ theta_actual = ( int ) motor.sensor_direction * (_wrapped.getAngle () - theta_init);
218
220
error[i] += 0.5 * (theta_actual - elec_angle / _NPP);
219
221
// 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);
221
223
zero_angle = _normalizeAngle (zero_angle);
222
224
// remove the 2PI jumps
223
225
if (zero_angle - zero_angle_prev > _PI){
@@ -272,7 +274,7 @@ void CalibratedSensor::calibrate(FOCMotor &motor, float* lut, float zero_electri
272
274
if (ind < 0 ) ind += n_lut;
273
275
calibrationLut[ind] = (float )(error[(int )(i * dn)] - error_mean);
274
276
// 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];
276
278
}
277
279
motor.monitor_port ->println (" " );
278
280
_delay (1000 );
0 commit comments