Motor Control ACCEL strange code

bernard

Well-known member
Hi.

Code:
void Robot::setMotorPWM(int pwmLeft, int pwmRight, boolean useAccel){
  unsigned long TaC = millis() - lastSetMotorSpeedTime;    // sampling time in millis
  lastSetMotorSpeedTime = millis();  
  if (TaC > 1000) TaC = 1;

  if (useAccel){  
    // [URL]http://phrogz.net/js/framerate-independent-low-pass-filter.html[/URL] 
    // smoothed += elapsedTime * ( newValue - smoothed ) / smoothing;          
    motorLeftPWMCurr += TaC * (pwmLeft - motorLeftPWMCurr) / motorAccel;
    motorRightPWMCurr += TaC * (pwmRight - motorRightPWMCurr) / motorAccel;   
  }

  motorLeftPWMCurr = pwmLeft;
  motorRightPWMCurr = pwmRight;

  // ---------------------------------
  if (motorLeftSwapDir)  // swap pin polarity?
    setActuator(ACT_MOTOR_LEFT, -motorLeftPWMCurr);
  else


In motorcontrol in the case of no odometry the accel (setMotorPWM( leftSpeed, rightSpeed, true )) is set to true but it can't work because just after the compute of motorLeftPWMCurr it's immediately set to Pwmleftvalue and same think for RIGHT.

But only for mower without ODOMETRY because into the rest of the code the Accel is always set to false.
 
my azurit code is little different :


Code:
void Robot::setMotorPWM(int pwmLeft, int pwmRight, boolean useAccel){
  unsigned long TaC = millis() - lastSetMotorSpeedTime;    // sampling time in millis
  lastSetMotorSpeedTime = millis();  
  if (TaC > 1000) TaC = 1;

  if (useAccel){  
    // [URL]http://phrogz.net/js/framerate-independent-low-pass-filter.html[/URL] 
    // smoothed += elapsedTime * ( newValue - smoothed ) / smoothing;          
    motorLeftPWMCurr += TaC * (pwmLeft - motorLeftPWMCurr) / motorAccel;
    motorRightPWMCurr += TaC * (pwmRight - motorRightPWMCurr) / motorAccel;   
  }

  // ----- driver protection (avoids driver explosion) ----------
  if ( ((pwmLeft < 0) && (motorLeftPWMCurr >= 0)) ||
       ((pwmLeft > 0) && (motorLeftPWMCurr <= 0)) ) { // changing direction should take place?
    if (motorLeftZeroTimeout != 0)
      pwmLeft = motorLeftPWMCurr - motorLeftPWMCurr *  ((float)TaC)/200.0; // reduce speed
  }
  if ( ((pwmRight < 0) && (motorRightPWMCurr >= 0)) ||
       ((pwmRight > 0) && (motorRightPWMCurr <= 0)) ) { // changing direction should take place?    
    if (motorRightZeroTimeout != 0) // reduce motor rotation? (will reduce EMF)      
      pwmRight = motorRightPWMCurr - motorRightPWMCurr *   ((float)TaC)/200.0;  // reduce speed
  }

  motorLeftPWMCurr = pwmLeft;
  motorRightPWMCurr = pwmRight;
 
Oben