View unanswered posts | View active topics It is currently Tue Nov 12, 2019 7:12 am






Reply to topic  [ 2 posts ] 
IME clearing issue 
Author Message
Rookie

Joined: Wed May 27, 2015 1:59 pm
Posts: 1
Post IME clearing issue
Here is a bit of code from our functions header file.

Code:
//Error margin calculation
int difference = abs(nMotorEncoder[RD]) - abs(nMotorEncoder[LD]);

//Timed braking for movements
void pause()
{
   wait1Msec(200);
}

//Move Forward (Distance)
void forward[code][/code]_enc(int distanceInDegrees)
{
   //Clear encoders
   nMotorEncoder[RD] = 0;
   nMotorEncoder[LD] = 0;
   {
      while(abs(difference) >= 2 && abs(nMotorEncoder[RD]) > abs(nMotorEncoder[LD]) && abs(nMotorEncoder[RD]) < distanceInDegrees)
         //Slow down right side to adjust for error
      {
         motor[RD] = -50;
         motor[LD] = 42;
      }
      while(abs(difference) >= 2 && abs(nMotorEncoder[RD]) < abs(nMotorEncoder[LD]) && abs(nMotorEncoder[RD]) < distanceInDegrees)
         //Slow down left side to adjust for error
      {
         motor[RD] = -50;
         motor[LD] = 38;
      }
      while(abs(difference) < 2 && abs(nMotorEncoder[RD]) < distanceInDegrees)
         //Drive both sides equally
      {
         motor[RD] = -50;
         motor[LD] = 40;
      }
   }
   //Braking motion to minimize drift
   motor[RD] = 40;
   motor[LD] = -30;
   pause();
   motor[RD] = 0;
   motor[LD] = 0;
}


When running the autonomous mode (Forward and turn left) the robot only does the braking motion and does not move forward and turn left the correct distance. The functions clear the encoders before moving, it can be seen in the sensors window as it happens. If I clear the encoders manually using the virtual competition control the autonomous functions as intended. What could I do to fix this so the encoders are zeroed correctly through the code?

_________________
Mentor of team 9571D from Southern Illinois, USA


Wed May 27, 2015 2:11 pm
Profile
Moderator
Moderator

Joined: Tue May 19, 2015 3:07 pm
Posts: 91
Post Re: IME clearing issue
Code:
//Error margin calculation
int difference = abs(nMotorEncoder[RD]) - abs(nMotorEncoder[LD]);
}


So this line only is run once in the program. That means that if you run this program with all encoder values at 0 than the only while loop that will be executed is the third one. What you want to do is update the difference value continuously.

Code:
while(abs(difference) >= 2 && abs(nMotorEncoder[RD]) > abs(nMotorEncoder[LD]) && abs(nMotorEncoder[RD]) < distanceInDegrees)
         //Slow down right side to adjust for error
      {
         motor[RD] = -50;
         motor[LD] = 42;
      }
      while(abs(difference) >= 2 && abs(nMotorEncoder[RD]) < abs(nMotorEncoder[LD]) && abs(nMotorEncoder[RD]) < distanceInDegrees)
         //Slow down left side to adjust for error
      {
         motor[RD] = -50;
         motor[LD] = 38;
      }
      while(abs(difference) < 2 && abs(nMotorEncoder[RD]) < distanceInDegrees)
         //Drive both sides equally
      {
         motor[RD] = -50;
         motor[LD] = 40;
      }


It also appears that you have a bracket that opens right before the first while loop for no reason and then closes after all the while loops. The use of this chain of while loops means that if the robot is on the third while loop it will never go back to the first one. Here I made a few small changes to the code so that the robot will always be able to correct and so that the difference variable updates every cycle.

Code:
//Error margin calculation
int difference = abs(nMotorEncoder[RD]) - abs(nMotorEncoder[LD]);

//Timed braking for movements
void pause()
{
   wait1Msec(200);
}

//Move Forward (Distance)
void forward(int distanceInDegrees)
{
   //Clear encoders
   nMotorEncoder[RD] = 0;
   nMotorEncoder[LD] = 0;
   int threshold=5;//ticks off from goal that accepts as close enough
   while(abs(nMotorEncoder[RD]-distanceInDegrees)<threshold&&(abs(nMotorEncoder[LD]-distanceInDegrees))){//while not there yet
      difference = abs(nMotorEncoder[RD]) - abs(nMotorEncoder[LD]);
      if(abs(difference) >= 2 && abs(nMotorEncoder[RD]) > abs(nMotorEncoder[LD]) && abs(nMotorEncoder[RD]) < distanceInDegrees)
         //Slow down right side to adjust for error
      {
         motor[RD] = -50;
         motor[LD] = 42;
      }
      if(abs(difference) >= 2 && abs(nMotorEncoder[RD]) < abs(nMotorEncoder[LD]) && abs(nMotorEncoder[RD]) < distanceInDegrees)
         //Slow down left side to adjust for error
      {
         motor[RD] = -50;
         motor[LD] = 38;
      }
      if(abs(difference) < 2 && abs(nMotorEncoder[RD]) < distanceInDegrees)
         //Drive both sides equally
      {
         motor[RD] = -50;
         motor[LD] = 40;
      }
   }
   //Braking motion to minimize drift
   motor[RD] = 40;
   motor[LD] = -30;
   pause();
   motor[RD] = 0;
   motor[LD] = 0;
}

Does that make sense or do you want me to explain in more detail.


Wed May 27, 2015 4:54 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

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.