View unanswered posts | View active topics It is currently Wed Aug 22, 2018 3:18 am






Reply to topic  [ 1 post ] 
Blocked encoder 
Author Message
Rookie

Joined: Fri Aug 07, 2015 11:02 am
Posts: 6
Post 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.
Thank you for all your suggestions and help in advance


Fri Mar 11, 2016 6:04 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.