Encoders

I need help programming the quad encoders. I cant seem to figure out how to work them. :confused:

Do you have the copy of my code which I gave to your teammate?

When we are back in school I can help you out. Also remember F1 when you need help in any program like Easy C.

i dont understand why You guys are using 8 ports

Going on limited context here, VEX shaft encoders (the large red ones) need two ports per encoder. If you have 4 encoders, you need 8 ports.

never mind we figured out the problem. We accidentally changed some of the ports luckily we did not save the changes.

But thanks for all the help.

I’ve tried but it didn’t stop after 360 clicks

You are not giving us much to go on here, post some code and we can help you.

Is this EasyC? Can you screenshot your code or something?

http://file:///C:/Users/kiran/Desktop/Encoders.png

[attach]7331[/attach]

.
Encoders.jpg

[ATTACH]7333[/ATTACH]

This might be better
better.jpg

Get rid of the Wait inside of the while loop, and did you stop your motors after the while loop? (because after the encoders go past whatever you want them to go to, the statement is no longer true the program will skip to the next part, but if you didn’t stop the motors, they will just continue to keep going)

[attach]7334[/attach]


better.jpg

Not much, however, I can almost make out part of what you are doing. Things to check, are the encoders counting forwards, increase the delay at the end of the loop to 200 and then add a PrintToScreen statement so you can see the encoder values, if one (or both) are counting down instead of up then swap the two digital input cables, that will make it count the other way. When you have finished delete the PrintToScreen block and set the delay back to perhaps 10.

How to post EasyC code.

  1. Find the function you want to post in the “Project Explorer” window under the “Block Diagram” folder, expand the folder if necessary.
  2. Right mouse click on the function and choose “Open C File”, this will open the source in another tab.
  3. Select all the code using the mouse or right click and “select all” fom the contextual menu.
  4. Copy all the selected code, right click and select “copy” or use C.
  5. On the forum “paste” the copied code and surround with the “CODE” tags.

Maybe that was the Problem. Also If the motor has done one revolution or 360 ticks and before we start a new while loop that we program it to go back to 0 ticks or back to the starting position, do we put anything in between both of them?

#include "Main.h"

void Autonomous ( unsigned long ulTime )
{
      long RightEncoder; 
      long LeftEncoder; 

      StartQuadEncoder ( 1 , 2 , 0 ) ;
      StartQuadEncoder ( 3 , 4 , 0 ) ;
      PresetQuadEncoder ( 1 , 2 , 0) ;
      PresetQuadEncoder ( 3 , 4 , 0) ;
      LeftEncoder = GetQuadEncoder ( 1 , 2 ) ;
      RightEncoder = GetQuadEncoder ( 3 , 4 ) ;
      while ( LeftEncoder <= 360 && RightEncoder <= 360 ) // Drive forward
      {
            LeftEncoder = GetQuadEncoder ( 1 , 2 ) ;
            RightEncoder = GetQuadEncoder ( 3 , 4 ) ;
            SetMotor ( 6 , 127 ) ;
            SetMotor ( 7 , 127 ) ;
            SetMotor ( 8 , -127 ) ;
            SetMotor ( 9 , -127 ) ;
      }
      while ( LeftEncoder <= 0 && RightEncoder >= 0 )
      {
            LeftEncoder = GetQuadEncoder ( 1 , 2 ) ;
            RightEncoder = GetQuadEncoder ( 3 , 4 ) ;
            SetMotor ( 6 , -127 ) ;
            SetMotor ( 7 , -127 ) ;
            SetMotor ( 8 , 127 ) ;
            SetMotor ( 9 , 127 ) ;
      }
      while ( LeftEncoder >= 360 && RightEncoder <= 360 )
      {
            LeftEncoder = GetQuadEncoder ( 1 , 2 ) ;
            RightEncoder = GetQuadEncoder ( 3 , 4 ) ;
            SetMotor ( 6 , 127 ) ;
            SetMotor ( 7 , 127 ) ;
            SetMotor ( 8 , -127 ) ;SetMotor ( 8 , -127 ) ;
            SetMotor ( 9 , -127 ) ;
      }
      SetMotor ( 6 , 0 ) ;
      SetMotor ( 7 , 0 ) ;
      SetMotor ( 8 , 0 ) ;
      SetMotor ( 9 , 0 ) ;
      Wait ( 500 ) ;
}

Try removing the 2nd and 3rd while loops.

Ill do that, but I can’t try it out until tomorrow because the robot is at the builders house

So it looks like you want to drive forward, drive backwards, drive forwards again and then stop.

A couple of things I notice.


while ( LeftEncoder <= 0 && RightEncoder >= 0 )

Assuming both encoders have reached 360 then this will not be true. Perhaps you wanted.


while ( LeftEncoder >= 0 && RightEncoder >= 0 )

Same here


while ( LeftEncoder >= 360 && RightEncoder <= 360 )

If both have reached 0 again (or really less than 0) it can not be true.

I would also stop the motors between each each step and have a small delay to allow the robot to come to rest. It’s very hard on the motors to send full forward speed and then full backwards. Did you check they are both counting up when you drive forwards?