ROBOTC.net forumshttp://robotc.net/forums/ Blocked encoderhttp://robotc.net/forums/viewtopic.php?f=1&t=13602 Page 1 of 1

Author:  u50cent [ Fri Mar 11, 2016 6:04 pm ]
Post subject:  Blocked encoder

Hello,

I have written a function for a turn with acceleration at the start of the turn and decceleration at the end.
The code is:
 Code:void Turn(float turn){   int speed;   int tmp=0;   turn = turn*constant;                                    //the turn in degrees is multiplied with a constant to get the number of desired encoder turns   resetMotorEncoder(left);                                       //encoders of both motors are reset   resetMotorEncoder(right);   setMotorSync(left, right, 0, 0);                                 //left(master) and right(slave) motors are synchronised   /*acceleration*/   while(abs(tmp) < (int)abs(turn)/2){                              //motor accelerates until motor encoder reaches half of the desired turn      speed = sqrt(2*a*abs(tmp))+minPower;                           //it accelerates using this formula for speed of the motor      if(speed>maxSpeed){                                       //if the calculated speed is above the maximum         speed=maxSpeed;                                       //it is reset to maximum      }      if(turn<0){                                             //the motors get the speed, depending on the desired direction of the turn         setMotorSync(left, right, 100, speed);      }      else{         setMotorSync(left, right, -100, speed);      }      delay(1);      tmp=getMotorEncoder(left);                                 //the value of the master encoder is read   }   /*deceleration*/   while(abs(tmp) < (int)abs(turn)){                              //the other half it decelerates      speed = sqrt(2*a*(abs(turn)-abs(tmp)))+minPower;                  //it accelerates using this formula for speed of the motor      if(speed>maxSpeed){                                       //if the calculated speed is above the maximum         speed=maxSpeed;                                       //it is reset to maximum      }      if(turn<0){                                             //the motors get the speed, depending on the desired direction of the turn         setMotorSync(left, right, 100, speed);      }      else{         setMotorSync(left, right, -100, speed);      }      delay(1);      tmp=getMotorEncoder(left);                                 //the value of the master encoder is read                  }   setMotorSync(left, right, 0, 0);                                 //for the end of the turn, the motors are turned off}

Most of the time it is working great. But sometimes (more or less randomly) it gets stuck. It appeares as if something was wrong with the condition in while statement or the encoder is malfunctioning.
The master (even if I switch the roles of master and slave) encoder/wheel gets blocked, meaning the encoder value remains the same. The other motor keeps spinning. If I then manually turn the master wheel just a little bit, it goes on without the problem.
I have noticed that the error gets worse the longer the brick is unplugged, but still indicates full battery. Therefore I was wondering if that could be the cause of the problem or did I miss something else.