I know that I can give power to a motor by using the set() method and passing in a value from -1 to 1, but how can I set a velocity to the motor?
What motor are you using (ex NEO, Falcon), as well as the motor controller?
When running velocities, you need to characterize/tune your motor. This allows you to convert a velocity to a voltage that you can run into your motor.
See: this control systems intro, and this characterization guide. Let me know if you have any more questions
NEO with a spark max
Alright then. Well like @Admlvntv said you do need to come up with PID values. This video provides a good intro on what a PID Controller is.
The actual code is pretty simple. Get the PID values using the WPI guide, and set them in using setP
. However, it is much easier to use the USB-C interface on a spark max to set up all of these things without code, then copy them over to your code. It even has a graph to see velocity, etc.
m_motor.restoreFactoryDefaults(); // start fresh
m_pidController = m_motor.getPIDController();
m_pidController.setP(0.001);
m_pidController.setI(0.0);
m_pidController.setD(0.0);
m_pidController.setReference(position, ControlType.kVelocity);
The example (java) has a good example that shows more advanced configurations. The main part is getting the constants tuned just right.