Using Degrees with Encoders in V5?

I’m currently writing my driver control code for v5, it’s very rough as I just learned it around 30-45 mins ago. I’m trying to make it so my motor locks up at two angles as in it only allows it to sit at either 33 or 45 degrees? is this possible, if so how? (I’m using C++)

current code that doesn’t lock up

               if (Controller1.ButtonR1.pressing())
            Angler.spin(directionType::fwd, 40, velocityUnits::pct)
          else if (Controller1.ButtonR2.pressing())
            Angler.spin(directionType::rev, 40, velocityUnits::pct)

The problem with your code is that it won’t hit those two angles. But it should try to lock up with the code you posted. I’m surprised it doesn’t lock wherever it is when it stops spinning. Does it not lock at all, or just not where you want it?

Change “spin” to “rotateTo” and adjust the parameters, choosing one button for one angle and the other button for the other angle:

rotateTo(double rotation, rotationUnits units, double velocity, velocityUnits units_v, bool waitForCompletion=true)

You will need to watch out where you start, though.

1 Like

it does lock once I let go. the problem is I want it to auto lock at two select angles, rather than when I let go of the button. I know I can accomplish this with task::sleep but prefer not to.

could you explain the section of code you posted in further detail as in how each unit works (bool, the rotation units etc.)
I’m sorry if I’m being v difficult I’m very new to VCS and don’t entirely understand it ive only really coded in RobotC.


if (Controller1.ButtonR1.pressing()) {            
   Angler.rotateTo(45, rotationUnits::deg, 40, velocityUnits::pct, false);
else if (Controller1.ButtonR2.pressing()) {            
   Angler.rotateTo(33, rotationUnits::deg, 40, velocityUnits::pct, false);

This will rotate at the 40% speed you were setting. If you press R1, it will rotate until it gets to 45 (the number) degrees (the rotation unit). If you press R2, it will rotate until it gets to 33 degrees. The “false” lets other things happen while the motor is heading there. I moved the stopping part earlier. First, rotateTo will already try to stop the motor, so this tells it the type of stop to use. Second, if you have it in the “else,” then you may hit circumstances where it brakes because you’ve released the button instead of because it’s gotten to the target.

You still have to make sure 0 is where you think it is.

1 Like

Thank you very much. I can’t wait to test this Monday! Also, thank you for explaining the pct as I didn’t understand that earlier!