Lesson 15 - Motor Commands
Motor Commands - The Basics
Motors in VEX are perhaps the most important thing on your robot since motors are what enable your robot to, you know, actually do stuff. We have already learned how to define motors, joysticks, and sensors, so how do we incorporate it into programs so we can actually use them in driver control/autonomous?
User Control Commands
Move Command
There is a command for moving the motor, and it's intuitively named “move.” Although this command is very basic, it will get our robot moving on the field, and it is good for testing when you don’t need elaborate amounts of code to test one function of the robot.
This “move” function accepts values between -127 and 127, with 127 being the max speed and 0 is not moving at all. Here is an example of the move function being used with a motor named “left_motor,” with a half-second delay in between.
pros::Motor left_motor(1, MOTOR_GEARSET_36, false, MOTOR_ENCODER_DEGREES);
left_motor.move(127);
pros::delay(500);
left_motor.move(0);
NOTE: This type of move function is great for driver control as you can directly plug in joystick values as the parameter. Since the joystick also outputs values from -127 to 127, it aligns perfectly with the move function, resulting in a simple driver control that looks similar to this:
left_motor.move(master.get_analog(E_CONTROLLER_ANALOG_LEFT_Y));
right_motor.move(master.get_analog(E_CONTROLLER_ANALOG_RIGHT_Y));
This block of code allows the user to control the robot based on the left and right joysticks, also known as tank control. Both of the joystick values directly control the motor speeds, which allows for slower and faster speed control based on how far the joystick is moved.
Motor Velocity Commands
A variant of the regular “move” function is the motor_name.move_velocity(int speed); function. This function ensures a consistent velocity output in rotations per minute instead of a regular speed. To use this, we can simply do something like this:
motor_name.move_velocity(100);
Autonomous Move Commands
For autonomous runs, we can use other types of movements like profile movements, which are basically movements to a given position that are executed by the motor’s firmware using it’s encoders. There are two similar functions that achieve this:
motor_name.move_absolute(int distance, int speed)
motor_name.move_relative(int distance, int speed)
Both functions require two parameters. The first parameter is the distance you want the motor to move. The second is speed. The difference lies in what the motor considers the starting position.
The absolute move function considers the starting position to be where the motor first turned on. For example, let’s say you turn on your robot. Then let’s say you push your robot forward 1000 encoder units. Then, the function move_absolute(500, 127); will actually make your robot go backward.
On the other hand, the relative move function does not do this. In the same example, if I were to push the robot forward 1000 encoder units, and then I called move_relative(500, 127); The robot would move another 500 encoder units forward. The reason for this is because the robot considered the “starting position” to be 1000 encoder units since that is when the function was called.
left_motor.move_absolute (500, 127);
pros::delay(50);
left_motor.move_absolute (1000, 127);
OR
left_motor.move_relative (500, 127);
pros::delay(50);
left_motor.move_relative (500, 127);
Both of these snippets of code do the same thing, but in their own different ways, whether absolute or relative.
Motor Encoder Commands
To get the output values from a motor, we can use the command motor_name.get_position(); This will allow us to use the encoder values of a motor for a plethora of other things, such as PID or even more accurate autonomous runs.
double left_motor_encoder = left_motor.get_position();
Now if you wished to reset the encoder of the motor to zero, you would use the command motor_name.tare_position();