setVelocity vs spin vs spinFor during driver control

I was told by a previous team mate that one of the commands for motor spinning was better to use but I forgot, any help would be nice!
Thanks!

You can use setVelocity to calibrate the motor speed with the controller joysticks. I’m sure there are other ways of doing this though. I think spin and spinFor are mostly for autonomous.

1 Like

if you are programming using C++ (or Python for that matter) avoid setVelocity completely.
use spin when just wanting the motor to spin at a given velocity and spinFor/spinTo when asking the motor to spin on its own to a requested position (number of rotations etc.)

vex::controller  Controller;
vex::motor       m1(PORT1);

void usercontrol( void ) {
    while (true) {
      m1.spin( forward, Controller.Axis1.position(), percent );

      this_thread::sleep_for(10);
    }
}
5 Likes

Will this only let the motor go forwards? Or will the fact that it’s using a controller axis let it also rotate backwards.

The motor will be able to go forwards and backwards.
Using m1.spin(forward… with a negative value will run the motor in reverse. Controller.Axis1.position() will give values between +100 and -100, the best way to learn about this is to experiment and see what happens.

5 Likes
  • setVelocity() alone does nothing. All it does is set the default velocity of the motor for when spin is eventually called.
  • spin will spin a motor at a desired speed. For example motor.spin(vex::forward); will spin the motor forwards at whatever speed you set it to using setVelocity beforehand.
    • spin will run the motor continuously until stop is called at a later time.
    • You can also explicitly give spin a velocity to run at, for example motor.spin(vex::forward, 100, vex::percent);
  • spinFor is primarily for autonomous routes. It’ll spin the motor for either a certain time period or certain amount of rotations.
    • Unlike spin, spinFor will automatically stop the motor once it’s reached the target, meaning you don’t have to call stop.
    • spinFor is a blocking function, meaning that you can’t do anything else while the motor is spinning. The motor has to complete the spin for any future code to run.

Example (spins the motor at 50% velocity for one second):

// Option A (using setVelocity)
motor.setVelocity(50, vex::percent);
motor.spin(vex::forward);

// Option B (passing velocity straight to spin)
motor.spin(vex::forward, 50, vex::percent);

// Wait a second
vex::wait(1, vex::seconds);

// Stop the motor after a second
motor.stop();

Equivalent code using a blocking spinFor (notice there is no vex::wait or motor.stop()):

motor.spinFor(1, vex::seconds, 100, vex::percent);
3 Likes

Well almost, if the motor is already spinning then setVelocity will change the motor’s velocity.
more thoughts I have on setVelocity here.

5 Likes

Also quick clarification to add to some of the weirdness. There’s no overloaded version of spinFor that accepts vex::percentUnits, but there IS one that specifically accepts vex::velocityUnits. This is different from spin that accepts both types.

Therefore this:

motor.spinFor(1, vex::seconds, 100, vex::percent);
motor.spinFor(1, vex::seconds, 100, vex::pct);

would throw a compiler error, but this:

motor.spinFor(1, vex::seconds, 100, vex::velocityUnits::pct);

would not. The example from my previous post does not compile

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.