Optical Shaft Encoder Programming

I need some help with programming a optical shaft encoder. This is a picture of my program. The blue circle is what i want to replace.
http://dl.dropbox.com/u/121714390/vex%202.PNG

I am assuming that you have a set of Optical Encoders and that you have looked at the EasyC v2.x Test Code, specifically “ENCODERTEST.ECP”.

Since the Time to raise you Elevator will depend on Battery Voltage and Weight of and in the Elevator, you will need to setup the Optical Encoders, and Display the Encoder Count when the proper height is reached, maybe even run the same test a few times to get an average… Then you will know what value to run your Encoders to, to reach the desired height of your Elevator.

OK. I have looked at the encodertest.ecp file and this is what I have gotten so far. I want motors 7 and 8 to go 185 ticks and then go reverse 185 ticks. What would i put in. http://dl.dropbox.com/u/121714390/vex%203.PNG

I’m not sure if you intended to create an infinite loop where you are checking the encoder value


while ( loop == 1 )
{
      encoder = GetEncoder(2);
}

Since there is nothing to update your variable loop within the loop, the value will never change, and your code will never exit the loop. To do what you want to do, I would suggest something like this …


encoder = GetEncoder( 2 );
while ( encoder < 185 )
{
      // set motors to the desired values here
      SetMotor( 7, 222 );
      SetMotor( 8, 32 ); 
      encoder = GetEncoder( 2 ); // update the variable
}
// stop motors here
SetMotor( 7, 127 );
SetMotor( 8, 127 ); // assuming you're using the older version of EasyC

This looks pretty good to me… I but I would change the SetUp part to:




PresetEncoder( 2, 0 );
StartEncoder( 2 );
while ( encoder < 185 )
{
      // set motors to the desired values here
      SetMotor( 7, 222 );
      SetMotor( 8, 32 ); 
      encoder = GetEncoder( 2 ); // update the variable
}

If you don’t Preset the Encoder, you will most likely have a Random Value, which might be larger than your Test, which would cause the While Loop to be skipped entirely…

Once you have Preset the Encode to a Known Value, like Zero, there is no reason to Read the Encoder, because it would read Zero, if the Preset and Start are called right before the While Loop…

Oh, sorry I forgot to include that. I was just focusing on the part where he wanted it to move to a certain reading. I didn’t re-write the set up since he already had that correct.

Actually, an Improvement, since the SetMotor() values never change, and will stay running until the end of the Match, an even more effective block of code would be:




PresetEncoder( 2, 0 );
StartEncoder( 2 );

// set motors to the desired values here
SetMotor( 7, 222 );
SetMotor( 8, 32 ); 
 
while ( encoder < 185 )
{
      encoder = GetEncoder( 2 ); // update the variable
}

I totally understand that… :wink:

I try to show the Code as it is, and then how it could be changed…

I noticed he copied the Code out of the Test Code, without totally understanding what it was doing…

It is All Good…

PresetEncoder( 2, 0 );
StartEncoder( 2 );

// set motors to the desired values here
SetMotor( 7, 222 );
SetMotor( 8, 32 ); 
 
while ( encoder < 185 )
{
      encoder = GetEncoder( 2 ); // update the variable
}

Ok. I used this code and it works. Now how do I reset it to where it goes more than just one time? I made a change at the end to see if it possibly works and it doesn’t.

PresetEncoder( 2, 0 );
StartEncoder( 2 );

// set motors to the desired values here
SetMotor( 7, 222 );
SetMotor( 8, 32 ); 
 
while ( encoder < 185 )
{
      encoder = GetEncoder( 2 ); // update the variable
}
PresetEncoder( 2, 0 );
StopEncoder( 2 );

If you notice, I added the presetencoder and stopencoder at the end. am i doing something wrong?

That is good to know… Stop the Encoder and Reset Everything… See Below…

You need to Start the Encoder after Stopping it…

I don’t have time to set this up right this minute, but I would try Stopping the Encoder first, and they doing it all over again…



// Round #1

PresetEncoder( 2, 0 );
StartEncoder( 2 );

// set motors to the desired values here
SetMotor( 7, 222 );
SetMotor( 8, 32 ); 
 
while ( encoder < 185 )
{
      encoder = GetEncoder( 2 ); // update the variable
}

StopEncoder( 2 );

// Round #2 

PresetEncoder( 2, 0 );
StartEncoder( 2 );

// set motors to the desired values here
SetMotor( 7, 222 );
SetMotor( 8, 32 ); 
 
while ( encoder < 185 )
{
      encoder = GetEncoder( 2 ); // update the variable
}

StopEncoder( 2 );



ok. This is what i have.

PresetEncoder (2 , 0) ;
StartEncoder (2 , 0) ;
Set Motor (7 , 160) ;
Set Motor (8 , 95 ) ;
while (encoder < 185)
{
    encoder = Getencoder (2)
}
StopEncoder (2)

Would I put the stopencoder in the while loop? I still can’t get it to work. For me to do autonomous a second time, i have to redownload the program to the robot.

With the Compitition Template, Autonomous is a timed Event. The Time is most likely expiring.

Use the Competition Switch Simulator to reset the Autonomous Mode…

Ok. Thank you for the advice. Now I would like to use a second encoder for the drive. I basicly did the same thing as the elevator. the only difference is the interpret port and ticks. The problem is, when i use the encoder for the drive in the programming the encoder for the elevator doesnt work. Am i doing something wrong?
http://dl.dropbox.com/u/121714390/vex%204.PNG

I really don’t see anything wrong…

Are you sure that the Encoder connected to Interrupt #1 is Working Correctly??

Open the Encoder up to see if there is something effecting the operation…

You might switch them to see if the Encode on Interrupt #1 will work on Interrupt #2. Maybe even switch the Physical Locations of the Encoders.

The encoder for the elevator works by itself. when i add the drive encoder, only the drive encoder works. do you think it could possibly be the variables?

Yes, after the drive runs the variable “encoder” will be greater than 325, add another statement, encoder = GetEncoder(2), before the second while loop or change the while loop to a do loop (ie. put the test for completion at the end of the loop rather than the beginning).

Edit:
So I see EasyC does not give you a do…while choice in the flowchart mode, it would have been like this.


do {
    encoder = GetEncoder(2);
} while( encoder < 185);

Sorry about that… jpearman is right… The variable has the old encoder value…This what you get when I was only half thinking of the question…

So, since there is not a do while loop thing in the program flow. How would I add one? can i just add another variable, like:


int encoderdrive =0;

ok I added a new variable


int encoderdrive =0;

and it works.

That will work.

Or…

You could re-use the encoder variable with the Drive While Loop, but you would need to re-set it to Zero before the While…



<< SNIP >>

encoder = GetEncoder(1);
while( encoder < 325);
{

<< SNIP >>