View unanswered posts | View active topics It is currently Tue Sep 23, 2014 11:27 pm






Reply to topic  [ 12 posts ] 
Using my encoders 
Author Message
Rookie
User avatar

Joined: Wed Jan 09, 2013 11:48 pm
Posts: 39
Location: Saint Paul, MN
Post Using my encoders
I recently put some encoders on my drive wheels knowing that wheel rotations are more reliable than timed functions. I had never used them but seemed fairly easy to learn. Thus I edited my autonomous to use the encoders. They work perfectly in the first part to determine which side of the balance lever the robot is on, but then when I try to reset the encoders to have my wheels move a certain distance it gets stuck in a loop. Thus the encoders are not updating or are always getting reset, I put in a variable that was equal to the encoder value in the loop it got stuck on (I used the second loop but it did it in the earlier method that is essentially the same thing too) and the encoder had a constant value of -460, but the wheel was moving forward so they shouldn't have a negative value in the first place. So if someone could just look at my code and maybe see somewhere I went wrong I would be most grateful:
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  none,     none)
#pragma config(Sensor, S2,     IRSeeker,       sensorHiTechnicIRSeeker1200)
#pragma config(Sensor, S3,     RightLight,     sensorCOLORFULL)
#pragma config(Sensor, S4,     LeftLight,      sensorCOLORFULL)
#pragma config(Motor,  motorA,          Block,         tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  mtr_S1_C1_1,     Left,          tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor,  mtr_S1_C1_2,     Right,         tmotorTetrix, openLoop, encoder)
#pragma config(Motor,  mtr_S1_C2_1,     Shoulder,      tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C2_2,     Spinner,       tmotorTetrix, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"

int normSpeed = 30;
int blockSpeed = 50;
int trans = 0;
int encoder = 0;


void placeBlock(const int forward){
   if(SensorValue(IRSeeker) != 5 || SensorValue(IRSeeker) != 4){
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
      motor[Block] = 0;
   }

   if(SensorValue(IRSeeker) == 5 && nMotorEncoder(Right) < forward){
      trans = 11;
   }

   if(SensorValue(IRSeeker) == 4 && nMotorEncoder(Right) >= forward){
      trans = 11;
   }

}


void retractStick(const int time){
   trans = 1;
   motor[Block] = blockSpeed;
   motor[Right] = 0;
   motor[Left] = 0;
   wait1Msec(time);

   motor[Block] = -blockSpeed;
   motor[Right] = 0;
   motor[Left] = 0;
   wait1Msec(time);
}



void outLowGoal(){
    nMotorEncoder(Right) = 0;
    nMotorEncoder(Left) = 0;

   if(SensorValue(RightLight) != 2 && SensorValue(RightLight) != 5){
      motor[Block] = 0;
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
   }

   if(SensorValue(RightLight) == 2 || SensorValue(RightLight) == 5){
      motor[Block] = 0;
      trans = 2;
   }
}



void turn(const int forward, const int turn){
   while(nMotorEncoder(Left) < forward){
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
      motor[Block] = 10;
   }

   while(nMotorEncoder(Left) >= forward && nMotorEncoder(Left) < forward + turn){
      motor[Right] = 0;
      motor[Left] = normSpeed;
   }

   if(nMotorEncoder(Left) >= forward + turn){
      trans = 3;
   }
}



void center(){
   nMotorEncoder(Right) = 0;
   nMotorEncoder(Left) = 0;

   if(SensorValue(RightLight) != 2 && SensorValue(RightLight) != 5){
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
   }

   if(SensorValue(RightLight) == 2 || SensorValue(RightLight) == 5){
      trans = 4;
   }
}



void getOnPlatform(const int turn, const int forward){
   while(nMotorEncoder(Left) < turn){
      motor[Right] = 0;
      motor[Left] = normSpeed;
      encoder = nMotorEncoder(Left);
   }

   while(nMotorEncoder(Left) >= turn && nMotorEncoder(Left) < turn + forward){
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
   }

   while(nMotorEncoder(Left) >= turn + forward){
      motor[Right] = 0;
      motor[Left] = 0;
   }
}


task main(){
   waitForStart();
   ClearTimer(T1);
   nMotorEncoder(Right) = 0;
   nMotorEncoder(Left) = 0;
while(true){
   if(trans == 0) placeBlock(3600);
   if(trans == 11) retractStick(500);
   if(trans == 1) outLowGoal();
   if(trans == 2) turn(720, 3120);
   if(trans == 3) center();
   if(trans == 4) getOnPlatform(3120, 3600);
}
}


I believe the encoders are working perfectly fine, seeing as the beginning of my code works perfectly, using either encoder. What concerns me is if I run this slightly edited sample code:

Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  none,     none)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorNormal, PIDControl, reversed, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//                  TETRIX+NXT Encoder - nMotorEncoder Example

// The program below demonstrates how to use the nMotorEndocer function
// There are three steps to using the function
//    1. Zero out the LEGO encoder using "nMotorEncoder(motorX) = 0"   Lines 17 and 18.
//    2. Place the function into a conditional statement like a while loop.  Line 20
//    3. Turn the motors off. Lines 26 and 27.

task main()
{

  nMotorEncoder[motorC] = 0;  //clear the LEGO encoders in motors B and C
  nMotorEncoder[motorB] = 0;

  while (abs(nMotorEncoder[motorB]) < 1440) //while the encoder wheel turns one revolution
  {
    motor[motorE] = 30; //turn both TETRIX motors on at 30 percent power
    motor[motorD] = 30;
 }

  motor[motorE] = 0; //turn both TETRIX motors off
  motor[motorD] = 0;

  wait1Msec(3000); // wait 3 seconds to see feedback from the debugger screens
                    // open the "NXT Devices" window to see the distance the encoder
                    //spins. The robot will coast a little bit.
}

it doesn't ever stop (I used the abs in case the encoders were some how getting negative values), I didn't have the time to set a variable equal to the encoder value so I'm not exactly sure what it was but I'm guessing it is running into a similar problem. Thanks RobotC community!

p.s. - If anyone knows, how do I view the encoder values directly through a debugger window without having to do the variable thing?

_________________
Head Programmer FTC Team 6699, Tempest

Code:
<Life>
   Fun
</Life>


Sat Jan 18, 2014 6:51 pm
Profile WWW
Rookie

Joined: Thu Nov 07, 2013 12:14 pm
Posts: 5
Post Re: Using my encoders
In your edited sample code you reset the encoders for motorB and motorC and read the encoder of motorB but you are driving motors D and E.


Sat Jan 18, 2014 8:56 pm
Profile
Rookie
User avatar

Joined: Wed Jan 09, 2013 11:48 pm
Posts: 39
Location: Saint Paul, MN
Post Re: Using my encoders
Oh, I did not notice that, that's odd it would be set up like that....

_________________
Head Programmer FTC Team 6699, Tempest

Code:
<Life>
   Fun
</Life>


Sat Jan 18, 2014 9:01 pm
Profile WWW
Rookie
User avatar

Joined: Wed Jan 09, 2013 11:48 pm
Posts: 39
Location: Saint Paul, MN
Post Re: Using my encoders
I switched the problematic code to use nMotorEncoderTarget[], I have not tested it out yet but I really would like to know what I may have done wrong in the code I posted just so I don't make the same mistake again, thanks!

_________________
Head Programmer FTC Team 6699, Tempest

Code:
<Life>
   Fun
</Life>


Mon Jan 20, 2014 10:00 pm
Profile WWW
Expert
User avatar

Joined: Sat Aug 31, 2013 9:15 am
Posts: 106
Post Re: Using my encoders
I believe gadolchik pointed out your problem. You are using the encoders of motorB and motorC, but you are actually driving you robot with motorD and motorE. Changing all references of motorB and motorC to motorD and motorE should fix it.

_________________
FTC Team 6100 Chariots of Fire - Programmer (2012-2013)
FTC Team 7468 Blue Chariots of Fire - Programmer (2013-2014)
FTC Team 7468 Blue Chariots of Fire - Mentor (2014-2015)
Check out our team website at http://cof7468.weebly.com/.


Mon Jan 20, 2014 10:47 pm
Profile
Rookie
User avatar

Joined: Wed Jan 09, 2013 11:48 pm
Posts: 39
Location: Saint Paul, MN
Post Re: Using my encoders
The sample code is not the problem, I do not make that mistake in MY code. I refer to my left drive motor for the encoder value and I am also moving my left drive motor. I merely did not see that the sample code was set up slightly differently.

_________________
Head Programmer FTC Team 6699, Tempest

Code:
<Life>
   Fun
</Life>


Mon Jan 20, 2014 10:52 pm
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Using my encoders
cookthebook wrote:
The sample code is not the problem, I do not make that mistake in MY code. I refer to my left drive motor for the encoder value and I am also moving my left drive motor. I merely did not see that the sample code was set up slightly differently.

If you are asking help to spot problems on your code, you better post YOUR code. It's hard for people to figure out problems on different code.


Tue Jan 21, 2014 2:44 am
Profile
Rookie
User avatar

Joined: Wed Jan 09, 2013 11:48 pm
Posts: 39
Location: Saint Paul, MN
Post Re: Using my encoders
Are people not seeing the whole first section of my post?
Quote:
I recently put some encoders on my drive wheels knowing that wheel rotations are more reliable than timed functions. I had never used them but seemed fairly easy to learn. Thus I edited my autonomous to use the encoders. They work perfectly in the first part to determine which side of the balance lever the robot is on, but then when I try to reset the encoders to have my wheels move a certain distance it gets stuck in a loop. Thus the encoders are not updating or are always getting reset, I put in a variable that was equal to the encoder value in the loop it got stuck on (I used the second loop but it did it in the earlier method that is essentially the same thing too) and the encoder had a constant value of -460, but the wheel was moving forward so they shouldn't have a negative value in the first place. So if someone could just look at my code and maybe see somewhere I went wrong I would be most grateful:
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  none,     none)
#pragma config(Sensor, S2,     IRSeeker,       sensorHiTechnicIRSeeker1200)
#pragma config(Sensor, S3,     RightLight,     sensorCOLORFULL)
#pragma config(Sensor, S4,     LeftLight,      sensorCOLORFULL)
#pragma config(Motor,  motorA,          Block,         tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  mtr_S1_C1_1,     Left,          tmotorTetrix, openLoop, reversed, encoder)
#pragma config(Motor,  mtr_S1_C1_2,     Right,         tmotorTetrix, openLoop, encoder)
#pragma config(Motor,  mtr_S1_C2_1,     Shoulder,      tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C2_2,     Spinner,       tmotorTetrix, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"

int normSpeed = 30;
int blockSpeed = 50;
int trans = 0;
int encoder = 0;


void placeBlock(const int forward){
   if(SensorValue(IRSeeker) != 5 || SensorValue(IRSeeker) != 4){
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
      motor[Block] = 0;
   }

   if(SensorValue(IRSeeker) == 5 && nMotorEncoder(Right) < forward){
      trans = 11;
   }

   if(SensorValue(IRSeeker) == 4 && nMotorEncoder(Right) >= forward){
      trans = 11;
   }

}


void retractStick(const int time){
   trans = 1;
   motor[Block] = blockSpeed;
   motor[Right] = 0;
   motor[Left] = 0;
   wait1Msec(time);

   motor[Block] = -blockSpeed;
   motor[Right] = 0;
   motor[Left] = 0;
   wait1Msec(time);
}



void outLowGoal(){
    nMotorEncoder(Right) = 0;
    nMotorEncoder(Left) = 0;

   if(SensorValue(RightLight) != 2 && SensorValue(RightLight) != 5){
      motor[Block] = 0;
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
   }

   if(SensorValue(RightLight) == 2 || SensorValue(RightLight) == 5){
      motor[Block] = 0;
      trans = 2;
   }
}



void turn(const int forward, const int turn){
   while(nMotorEncoder(Left) < forward){
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
      motor[Block] = 10;
   }

   while(nMotorEncoder(Left) >= forward && nMotorEncoder(Left) < forward + turn){
      motor[Right] = 0;
      motor[Left] = normSpeed;
   }

   if(nMotorEncoder(Left) >= forward + turn){
      trans = 3;
   }
}



void center(){
   nMotorEncoder(Right) = 0;
   nMotorEncoder(Left) = 0;

   if(SensorValue(RightLight) != 2 && SensorValue(RightLight) != 5){
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
   }

   if(SensorValue(RightLight) == 2 || SensorValue(RightLight) == 5){
      trans = 4;
   }
}



void getOnPlatform(const int turn, const int forward){
   while(nMotorEncoder(Left) < turn){
      motor[Right] = 0;
      motor[Left] = normSpeed;
      encoder = nMotorEncoder(Left);
   }

   while(nMotorEncoder(Left) >= turn && nMotorEncoder(Left) < turn + forward){
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
   }

   while(nMotorEncoder(Left) >= turn + forward){
      motor[Right] = 0;
      motor[Left] = 0;
   }
}


task main(){
   waitForStart();
   ClearTimer(T1);
   nMotorEncoder(Right) = 0;
   nMotorEncoder(Left) = 0;
while(true){
   if(trans == 0) placeBlock(3600);
   if(trans == 11) retractStick(500);
   if(trans == 1) outLowGoal();
   if(trans == 2) turn(720, 3120);
   if(trans == 3) center();
   if(trans == 4) getOnPlatform(3120, 3600);
}
}
   
   


_________________
Head Programmer FTC Team 6699, Tempest

Code:
<Life>
   Fun
</Life>


Tue Jan 21, 2014 8:58 am
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Using my encoders
The first if statement in placeBlock should be && not ||.


Tue Jan 21, 2014 9:20 am
Profile
Rookie
User avatar

Joined: Wed Jan 09, 2013 11:48 pm
Posts: 39
Location: Saint Paul, MN
Post Re: Using my encoders
No it should be ||, I don't want the robot to stop because the IR value is 4 when I want to deploy the block when it is five, I have to change what value I deploy at based on the side due to the IR beacon not being centered. But the problem comes after I place the block and I've reset the encoders. I believe that I am resetting them wrong but I have no idea how the problem arises (look at void turn, that's where the problem first comes, then again in void getOnPlatform).

_________________
Head Programmer FTC Team 6699, Tempest

Code:
<Life>
   Fun
</Life>


Tue Jan 21, 2014 1:53 pm
Profile WWW
Expert
User avatar

Joined: Sat Aug 31, 2013 9:15 am
Posts: 106
Post Re: Using my encoders
I believe your problems may arise from the fact that you are not resetting your encoders in turn() or getOnPlatform(). If you reset your encoders at the beginning of those methods like you do in your other ones, that may fix some of your problems.

_________________
FTC Team 6100 Chariots of Fire - Programmer (2012-2013)
FTC Team 7468 Blue Chariots of Fire - Programmer (2013-2014)
FTC Team 7468 Blue Chariots of Fire - Mentor (2014-2015)
Check out our team website at http://cof7468.weebly.com/.


Tue Jan 21, 2014 2:47 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Using my encoders
cookthebook wrote:
No it should be ||, I don't want the robot to stop because the IR value is 4 when I want to deploy the block when it is five, I have to change what value I deploy at based on the side due to the IR beacon not being centered. But the problem comes after I place the block and I've reset the encoders. I believe that I am resetting them wrong but I have no idea how the problem arises (look at void turn, that's where the problem first comes, then again in void getOnPlatform).

Since your code has no comment explaining what it was intending to do, I am just looking at it from pure logic stand point.
Code:
void placeBlock(const int forward){
   if(SensorValue(IRSeeker) != 5 || SensorValue(IRSeeker) != 4){
      motor[Right] = normSpeed;
      motor[Left] = normSpeed;
      motor[Block] = 0;
   }

The IR Seeker will give you a value of 1 through 9. For values 1, 2, 3, 6, 7, 8, 9, the condition for the if statement is true. For value 4, since it is not 5, the left condition will be true. For value 5, since it is not 4, the right condition will be true. Since it is || so both will come out true anyway. In other words, the if statement is always TRUE!
If that's not the problem part, please explain what your code intend to do and when the robot starts to deviate from your intention. From my understanding, your main code is implementing a state machine. The variable "trans" is keeping track of the state. The program starts with trans == 0 (state 0). So it will execute placeBlock. Since I already proved that the first if statement is always true, so the code is equivalent to:
Code:
void placeBlock(const int forward){
    motor[Right] = normSpeed;
    motor[Left] = normSpeed;
    motor[Block] = 0;

   if(SensorValue(IRSeeker) == 5 && nMotorEncoder(Right) < forward){
      trans = 11;
   }

   if(SensorValue(IRSeeker) == 4 && nMotorEncoder(Right) >= forward){
      trans = 11;
   }

}

So your robot will move forward until the IR seeker either hit 4 or 5. Since it doesn't explain why it is comparing the encoder value to "forward", I can only guess that you are placing your robot either on the left side or the right side so the robot may be moving forward or backward depending on the side and so the logic is trying to make it agnostic to which way the robot is moving. But it doesn't make sense because normSpeed is +30 meaning that the robot always goes forward. In any case, it will transition to state 11 after hitting the IR target. In state 11, you will call retractStick which will throw the block into the basket and then transition to state 1. In state 1, you called outLowGoal. It looks like the robot will move forward until it finds either a red line or a blue line then it transition to state 2 (turn). In state 2, I don't understand this code. Are you trying to move forward 720 encoder counts? If so, BuringLights is right, you did not reset your encoders. While you are moving forward, you also turn on the "Block" motor. Why? Then you are trying to turn right. When you said "when I try to reset the encoders to have my wheels move a certain distance it gets stuck in a loop. Thus the encoders are not updating or are always getting reset", which part of the code were you referring to? I assume this is the place you have trouble with but I don't know where this place is in your code.


Tue Jan 21, 2014 3:45 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 12 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.