Motors¶
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.
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); } |
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 } |
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.
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); } } |
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.
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 } |
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.
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 } |
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 } |
Telemetry¶
The V5 motors return a plethora of diagnostic information about their performance. The motors return the following parameters:
Parameter | C Function | C++ Function |
---|---|---|
Position | motor_get_position | pros::Motor::get_position |
Velocity | motor_get_actual_velocity | pros::Motor::get_actual_velocity |
Current | motor_get_current_draw | pros::Motor::get_current_draw |
Efficiency | motor_get_efficiency | pros::Motor::get_efficiency |
Power | motor_get_power | pros::Motor::get_power |
Temperature | motor_get_temperature | pros::Motor::get_temperature |
Torque | motor_get_torque | pros::Motor::get_torque |
Voltage | motor_get_voltage | pros::Motor::get_voltage |
Direction | motor_get_direction | pros::Motor::get_direction |