Motors

Note

For a full list of functions for interacting with the V5 Motors, see its C API and C++ API.

Initialization

V5 Motors should be configured before use in your code. Configuration options like the gearset and encoder units are important to address first thing in your user program to ensure that functions like motor_move_velocity will work as expected.

When declaring motors in C++, it is not necessary to set the configuration for the motor with its constructor (beyond its port number) more than once for the given port. An example of this is given below.

initialize.cpp
1
2
3
4
5
#define MOTOR_PORT 1

void initialize() {
  pros::Motor drive_left_initializer (MOTOR_PORT, E_MOTOR_GEARSET_18, true, E_MOTOR_ENCODER_DEGREES);
}
opcontrol.cpp
1
2
3
4
5
6
#define MOTOR_PORT 1

void opcontrol() {
  pros::Motor drive_left (MOTOR_PORT);
  // drive_left will have the same configuration as drive_left_initializer
}
initialize.c
1
2
3
4
5
6
7
#define MOTOR_PORT 1

void initialize() {
  motor_set_gearing(MOTOR_PORT, E_MOTOR_GEARSET_18);
  motor_set_reversed(MOTOR_PORT, true);
  motor_set_encoder_units(MOTOR_PORT, E_MOTOR_ENCODER_DEGREES);
}

Simple Usage

The easiest way to interact with the motors is through the motor_move function. This is analogous to the motorSet function from PROS 2.

opcontrol.cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#define MOTOR_PORT 1

void opcontrol() {
  pros::Motor drive_left (MOTOR_PORT);
  pros::Controller master (E_CONTROLLER_MASTER);
  while (true) {
    drive_left.move(master.get_analog(E_CONTROLLER_ANALOG_LEFT_Y));
    pros::delay(2);
  }
}
opcontrol.c
1
2
3
4
5
6
7
8
#define MOTOR_PORT 1

void opcontrol() {
  while (true) {
    motor_move(MOTOR_PORT, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    delay(2);
  }
}

Autonomous Movement

The V5 Motors can move in a number of different ways that are better suited towards autonomous movement than the simple motor_move() example shown above.

Profile Movements

Profile movements are movements to a given position that are executed by the motor’s firmware. There are two functions that achieve this, motor_move_absolute() and motor_move_relative(). These two functions are practically similar, but motor_move_relative() takes into account the zero position of the motor’s encoder.

These functions are very well suited to movement in autonomous.

autonomous.cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#define MOTOR_PORT 1
#define MOTOR_MAX_SPEED 100 // The motor has the 36 Gearset

void autonomous() {
  pros::Motor drive_left (MOTOR_PORT);
  drive_left.move_relative(1000, MOTOR_MAX_SPEED);
  // This will move 1000 ticks forward
  drive_left.move_relative(1000, MOTOR_MAX_SPEED);
  // This moves an additional 1000 ticks forward
  drive_left.move_absolute(1000, MOTOR_MAX_SPEED);
  // This moves 1000 ticks backwards to the 1000 tick position
}
autonomous.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#define MOTOR_PORT 1
#define MOTOR_MAX_SPEED 100 // The motor has the 36 Gearset

void autonomous() {
  motor_move_relative(MOTOR_PORT, 1000, MOTOR_MAX_SPEED);
  // This will move 1000 ticks forward
  motor_move_relative(MOTOR_PORT, 1000, MOTOR_MAX_SPEED);
  // This moves an additional 1000 ticks forward
  motor_move_absolute(MOTOR_PORT, 1000, MOTOR_MAX_SPEED);
  // This moves 1000 ticks backwards to the 1000 tick position
}

For further reading material on the algorithms that create these profiled movement, see Mathematics of Motion Control Profiles for the Feedforward control, and George Gillard’s PID Explanation for the feedback control.

Velocity Controller Movement

The final move function available with the PROS Motor API is motor_move_velocity(). This ensures consistent velocity output from the motor through the use of PID.

autonomous.cpp
1
2
3
4
5
6
7
8
#define MOTOR_PORT 1
#define MOTOR_MAX_SPEED 100 // The motor has the 36 Gearset

void autonomous() {
  pros::Motor drive_left (MOTOR_PORT);
  drive_left.move_velocity(MOTOR_MAX_SPEED);
  pros::delay(1000); // Move at full speed for 1 second
}
autonomous.c
1
2
3
4
5
6
7
#define MOTOR_PORT 1
#define MOTOR_MAX_SPEED 100 // The motor has the 36 Gearset

void autonomous() {
  motor_move_velocity(MOTOR_PORT, MOTOR_MAX_SPEED);
  delay(1000); // Move at full speed for 1 second
}