How to code encoders for auton

Hi there. My team and I are planning on using encoders for our auton this year, but none of us know how to code them. If anyone has any tips, please let me know. Thanks!

Check out the Vex V5 API: https://api.vexcode.cloud/v5/html/

It has class references for the encoders.

6 Likes


Uses internal encoders in the motors within the code example, potato potaato you can replace to read a 3 wire external encoder instead using api mentioned above.

1 Like

This is very helpful thank you. Also, do you know how to declare the encoders in the following format? image
I have it set up in the robot config window right now, but i prefer to have it in the above format. Thanks!

You need to put in “encoder” again and put the “C” into the parentheses something like this:

vex::encoder::encoderLeft = encoder(vex::triport::C);

The “(vex::triport::C)” part might be slightly different, I don’t have access to my computer right now and it’s been a while since I setup devices.

Edit:
Put in the encoder parentheses what @bananavex did below
|
|
v

1 Like

in robot_config.h type extern encoder encoderLeft;
in robot_config.cpp type encoder encoderLeft= encoder(Brain.ThreeWirePort.C);

ya triport::port::C doesnt work but Brain.ThreeWirePort.C); does

also @Pat.Chet.02 your method wont work becuase encoderLeft is not a part of the encoder class you have to declare an encoder object called leftEncoder

3 Likes

Good catch,
I also usually leave out the “vex::” part when declaring a device.

Is this something that I should be doing as good programming practice?

I learned to program in RobotC by looking at the example programs and I figured out what the functions did by tweaking stuff. To transfer over to VexCode I used example programs to look at the Syntax. It never required me to add that part so I didn’t put it in as I thought it made the code easier to read.

I did the exact same thing. I was following an example so I was coping everything word for word.

It is really up to you. The vex:: is known as a namespace and VEXcode automatically defaults to using namespace vex; which pretty much lets you not type it. However doing this can lead to namespace confusion (which is why you should not use namespace std) but with vex is probably ok.

For more info on namespaces: https://www.learncpp.com/cpp-tutorial/user-defined-namespaces/

4 Likes

Is there a specific reason you want it this way? As I said you are trying to declare an object but the way you are doing it is treating like it’s in an encoder namespace which doesn’t exist so it wont work

Ok I played around with it a little and I found a way to avoid the Brain.ThreeWirePort stuff. You can do encoder leftEncoder = encoder(triport(PORT22).C); and it should work. But then again…why?

2 Likes