teemuatlut / TMC2130Stepper

Arduino library for Trinamic TMC2130 Stepper driver
MIT License
159 stars 50 forks source link

Calculations in rms_current() #52

Open qx1147 opened 5 years ago

qx1147 commented 5 years ago

This might be nitpicking, but wouldn't it be better to use round() or ceil() when converting from float to integer in TMC2130Stepper::rms_current((), and do the conversion as late as possible for the ihold() argument? It currently is always floor() (implicitly), and the argument of ihold() gets floor()'ed even twice. Along that line, is it useful to have getCurrent() return the value requested by the user rather than the value currently realized by the driver (as TMC2130Stepper::rms_current() does when called without argument)?

void TMC2130Stepper::rms_current(uint16_t mA, float multiplier, float RS) {
    Rsense = RS;
    uint8_t CS = 32.0*1.41421*mA/1000.0*(Rsense+0.02)/0.325 - 1;
    // If Current Scale is too low, turn on high sensitivity R_sense and calculate again
    if (CS < 16) {
        vsense(true);
        CS = 32.0*1.41421*mA/1000.0*(Rsense+0.02)/0.180 - 1;
    } else if(vsense()) { // If CS >= 16, turn off high_sense_r if it's currently ON
        vsense(false);
    }
    irun(CS);
    ihold(CS*multiplier);
    val_mA = mA;
}