Using Encoder values In drive code

Hello everyone. I am here asking a question about using the encoder values for our lift. We are trying to code a macro for towering with the press of a button, and I was wondering how to set the lift to stop at a certain encoder value so all you had to do was drop the cube into the tower(if that makes sense).

This is an idea that I had on how to do this:
start the lift at the original point
print out the encoder value of the lift motor

find the encoder value when the lift is at the top

If(button pressed and liftStartValue< endpointValue
keep going
else if(button pressed and liftStartValue>= endpointValue
Stop

Not sure if above code is correct though.
Help with this would be greatly appreciated. Thanks,

only thing I’d add to avl 's post.

V5 is awesome and you can simply use the encoder ‘ticks’ for positioning

‘assuming’ your lift has a home (starting) position reset the encoder so this position is always ‘0’:

Lift.resetRotation();

there are three ‘spin’ methods:

Lift.spin() //- just turns the motor
LiftSpinTo() //- turn a specific position from home (absolute positioning’
Lift.spinFor() //- turns a specific about based on current position (relative positioning)

so on your button.isPressing() you can Lift.spinTo(#tower_ticks)

As far as figuring out how many ‘ticks’ you can try different values or get fancy and echo the encoder position to the screen…

Could you perhaps give me an example of spinTo being used? I can’t find the documentation for it.

sure - same as rotateTo()

Lift.spinTo(360, vex::rotationUnits::deg, 100, vex::velocityUnits::pct, true);

when ever you’re using a spinTo/For you have to keep in mind the command is either blocking (true) or not (false) . Blocking means it will wait until it reaches the completion before letting the next command execute. This can really bite you if you’re not careful/mindful!

https://api.vexcode.cloud/v5/html/classvex_1_1motor.html

3 Likes

rule of thumb 360 deg (1rev) is ~12’" w/4" wheel