scottbez1 / smartknob

Haptic input knob with software-defined endstops and virtual detents
https://www.youtube.com/watch?v=ip641WmY4pA
Other
19.41k stars 1.11k forks source link

about the Calibration calculation principle of absolute zero electric Angle, why " motor.zero_electric_angle = avg_offset_angle + _3PI_2; " #157

Open left-win opened 8 months ago

left-win commented 8 months ago
    // Measure mechanical angle at every electrical zero for several revolutions
    motor.voltage_limit = 5;
    motor.move(a);
    float offset_x = 0;
    float offset_y = 0;
    float destination1 = (floor(a / _2PI) + measured_pole_pairs / 2.) * _2PI;
    float destination2 = (floor(a / _2PI)) * _2PI;
    for (; a < destination1; a += 0.4) {
        motor.move(a);
        delay(100);
        for (uint8_t i = 0; i < 100; i++) {
            encoder.update();
            delay(1);
        }
        float real_electrical_angle = _normalizeAngle(a);
        float measured_electrical_angle = _normalizeAngle( (float)(motor.sensor_direction * measured_pole_pairs) * encoder.getMechanicalAngle()  - 0);

        float offset_angle = measured_electrical_angle - real_electrical_angle;
        offset_x += cosf(offset_angle);
        offset_y += sinf(offset_angle);

        Serial.print(degrees(real_electrical_angle));
        Serial.print(", ");
        Serial.print(degrees(measured_electrical_angle));
        Serial.print(", ");
        Serial.println(degrees(_normalizeAngle(offset_angle)));
    }
    for (; a > destination2; a -= 0.4) {
        motor.move(a);
        delay(100);
        for (uint8_t i = 0; i < 100; i++) {
            encoder.update();
            delay(1);
        }
        float real_electrical_angle = _normalizeAngle(a);
        float measured_electrical_angle = _normalizeAngle( (float)(motor.sensor_direction * measured_pole_pairs) * encoder.getMechanicalAngle()  - 0);

        float offset_angle = measured_electrical_angle - real_electrical_angle;
        offset_x += cosf(offset_angle);
        offset_y += sinf(offset_angle);

        Serial.print(degrees(real_electrical_angle));
        Serial.print(", ");
        Serial.print(degrees(measured_electrical_angle));
        Serial.print(", ");
        Serial.println(degrees(_normalizeAngle(offset_angle)));
    }
    motor.voltage_limit = 0;
    motor.move(a);

    float avg_offset_angle = atan2f(offset_y, offset_x);

    // Apply settings
    motor.pole_pairs = measured_pole_pairs;
    motor.zero_electric_angle = avg_offset_angle + _3PI_2;

    The above code is about the calibration of the zero electric Angle
    I want to know why  " motor.zero_electric_angle = avg_offset_angle + _3PI_2; "