Motors C API module

Files

file motors.h

Functions

uint32_t motor_get_faults(int8_t port)
Gets the faults experienced by the motor.
uint32_t motor_get_flags(int8_t port)
Gets the flags set by the motor's operation.
int32_t motor_get_raw_position(int8_t port, uint32_t*const timestamp)
Gets the raw encoder count of the motor at a given timestamp.
double motor_get_position(int8_t port)
Gets the absolute position of the motor in its encoder units.
double motor_get_power(int8_t port)
Gets the power drawn by the motor in Watts.
double motor_get_temperature(int8_t port)
Gets the temperature of the motor in degrees Celsius.
double motor_get_torque(int8_t port)
Gets the torque generated by the motor in Newton Meters (Nm).
int32_t motor_get_voltage(int8_t port)
Gets the voltage delivered to the motor in millivolts.

Motor movement functions

These functions allow programmers to make motors move

int32_t motor_move(int8_t port, int32_t voltage)
Sets the voltage for the motor from -127 to 127.
int32_t motor_brake(int8_t port)
Stops the motor using the currently configured brake mode.
int32_t motor_move_absolute(int8_t port, double position, const int32_t velocity)
Sets the target absolute position for the motor to move to.
int32_t motor_move_relative(int8_t port, double position, const int32_t velocity)
Sets the relative target position for the motor to move to.
int32_t motor_move_velocity(int8_t port, const int32_t velocity)
Sets the velocity for the motor.
int32_t motor_move_voltage(int8_t port, const int32_t voltage)
Sets the output voltage for the motor from -12000 to 12000 in millivolts.
int32_t motor_modify_profiled_velocity(int8_t port, const int32_t velocity)
Changes the output velocity for a profiled movement (motor_move_absolute or motor_move_relative).
double motor_get_target_position(int8_t port)
Gets the target position set for the motor by the user.
int32_t motor_get_target_velocity(int8_t port)
Gets the velocity commanded to the motor by the user.

Motor telemetry functions

These functions allow programmers to collect telemetry from motors

double motor_get_actual_velocity(int8_t port)
Gets the actual velocity of the motor.
int32_t motor_get_current_draw(int8_t port)
Gets the current drawn by the motor in mA.
int32_t motor_get_direction(int8_t port)
Gets the direction of movement for the motor.
double motor_get_efficiency(int8_t port)
Gets the efficiency of the motor in percent.
int32_t motor_is_over_current(int8_t port)
Checks if the motor is drawing over its current limit.
int32_t motor_is_over_temp(int8_t port)
Checks if the motor's temperature is above its limit.

Function documentation

uint32_t motor_get_faults(int8_t port)

Gets the faults experienced by the motor.

Parameters
port The V5 port number from 1-21
Returns A bitfield containing the motor's faults.

Compare this bitfield to the bitmasks in motor_fault_e_t.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Faults: %d\n", motor_get_faults(1));
    delay(2);
  }
}

uint32_t motor_get_flags(int8_t port)

Gets the flags set by the motor's operation.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns A bitfield containing the motor's flags.

Compare this bitfield to the bitmasks in motor_flag_e_t.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Flags: %d\n", motor_get_flags(1));
    delay(2);
  }
}

int32_t motor_get_raw_position(int8_t port, uint32_t*const timestamp)

Gets the raw encoder count of the motor at a given timestamp.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
timestamp in A pointer to a time in milliseconds for which the encoder count will be returned. If NULL, the timestamp at which the encoder count was read will not be supplied
Returns The raw encoder count at the given timestamp or PROS_ERR if the operation failed.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  uint32_t now = millis();
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Encoder Count: %d\n", motor_get_raw_position(1, &now));
    delay(2);
  }
}

double motor_get_position(int8_t port)

Gets the absolute position of the motor in its encoder units.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns The motor's absolute position in its encoder units or PROS_ERR_F if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Position: %lf\n", motor_get_position(1));
    delay(2);
  }
}

double motor_get_power(int8_t port)

Gets the power drawn by the motor in Watts.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns The motor's power draw in Watts or PROS_ERR_F if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  uint32_t now = millis();
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Power: %lf\n", motor_get_power(1));
    delay(2);
  }
}

double motor_get_temperature(int8_t port)

Gets the temperature of the motor in degrees Celsius.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns The motor's temperature in degrees Celsius or PROS_ERR_F if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Temperature: %lf\n", motor_get_temperature(1));
    delay(2);
  }
}

double motor_get_torque(int8_t port)

Gets the torque generated by the motor in Newton Meters (Nm).

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns The motor's torque in Nm or PROS_ERR_F if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Torque: %lf\n", motor_get_torque(1));
    delay(2);
  }
}

int32_t motor_get_voltage(int8_t port)

Gets the voltage delivered to the motor in millivolts.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns The motor's voltage in mV or PROS_ERR_F if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports (1-21). ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Voltage: %lf\n", motor_get_voltage(1));
    delay(2);
  }
}

int32_t motor_move(int8_t port, int32_t voltage)

Sets the voltage for the motor from -127 to 127.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
voltage The new motor voltage from -127 to 127
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This is designed to map easily to the input from the controller's analog stick for simple opcontrol use. The actual behavior of the motor is analogous to use of motor_move_voltage().

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

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

int32_t motor_brake(int8_t port)

Stops the motor using the currently configured brake mode.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This function sets motor velocity to zero, which will cause it to act according to the set brake mode. If brake mode is set to MOTOR_BRAKE_HOLD, this function may behave differently than calling motor_move_absolute(port, 0) or motor_move_relative(port, 0).

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

    void autonomous() {
        motor_move(1, 127);
        delay(1000);
        motor_break(1);
}

int32_t motor_move_absolute(int8_t port, double position, const int32_t velocity)

Sets the target absolute position for the motor to move to.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
position The absolute position to move to in the motor's encoder units
velocity The maximum allowable velocity for the movement in RPM
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This movement is relative to the position of the motor when initialized or the position when it was most recently reset with motor_set_zero_position().

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void autonomous() {
  motor_move_absolute(1, 100, 100); // Moves 100 units forward
  while (!((motor_get_position(1) < 105) && (motor_get_position(1) > 95))) {
    // Continue running this loop as long as the motor is not within +-5 units of its goal
    delay(2);
  }
  motor_move_absolute(1, 100, 100); // This will not cause a movement
  while(!((motor_get_position(1) < 105) && (motor_get_position(1) > 95))) {
    delay(2);
  }

  motor_tare_position(1);
  motor_move_absolute(1, 100, 100); // Moves 100 units forward
  while (!((motor_get_position(1) < 105) && (motor_get_position(1) > 95))) {
    delay(2);
  }
}

int32_t motor_move_relative(int8_t port, double position, const int32_t velocity)

Sets the relative target position for the motor to move to.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
position The relative position to move to in the motor's encoder units
velocity The maximum allowable velocity for the movement in RPM
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This movement is relative to the current position of the motor as given in motor_get_position(). Providing 10.0 as the position parameter would result in the motor moving clockwise 10 units, no matter what the current position is.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void autonomous() {
  motor_move_relative(1, 100, 100); // Moves 100 units forward
  while (!((motor_get_position(1) < 105) && (motor_get_position(1) > 95))) {
    // Continue running this loop as long as the motor is not within +-5 units of its goal
    delay(2);
  }

  motor_move_relative(1, 100, 100); // Also moves 100 units forward
  while (!((motor_get_position(1) < 205) && (motor_get_position(1) > 195))) {
    delay(2);
  }
}

int32_t motor_move_velocity(int8_t port, const int32_t velocity)

Sets the velocity for the motor.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
velocity The new motor velocity from +-100, +-200, or +-600 depending on the motor's gearset
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This velocity corresponds to different actual speeds depending on the gearset used for the motor. This results in a range of +-100 for E_MOTOR_GEARSET_36, +-200 for E_MOTOR_GEARSET_18, and +-600 for E_MOTOR_GEARSET_6. The velocity is held with PID to ensure consistent speed, as opposed to setting the motor's voltage.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void autonomous() {
  motor_move_velocity(1, 100);
  delay(1000); // Move at 100 RPM for 1 second
  motor_move_velocity(1, 0);
}

int32_t motor_move_voltage(int8_t port, const int32_t voltage)

Sets the output voltage for the motor from -12000 to 12000 in millivolts.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
voltage The new voltage value from -12000 to 12000
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void autonomous() {
  motor_move_voltage(1, 12000);
  delay(1000); // Move at max voltage for 1 second
  motor_move_voltage(1, 0);
}

int32_t motor_modify_profiled_velocity(int8_t port, const int32_t velocity)

Changes the output velocity for a profiled movement (motor_move_absolute or motor_move_relative).

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
velocity The new motor velocity from +-100, +-200, or +-600 depending on the motor's gearset
Returns 1 if the operation was successful or PROS_ERR if the operation failed, setting errno.

This will have no effect if the motor is not following a profiled movement.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void autonomous() {
  motor_move_absolute(1, 100, 100);
  delay(100);
  motor_modify_profiled_velocity(1, 0); // Stop the motor early
}

double motor_get_target_position(int8_t port)

Gets the target position set for the motor by the user.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns The target position in its encoder units or PROS_ERR_F if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void autonomous() {
  motor_move_absolute(1, 100, 100);
  printf("Motor Target: %d\n", motor_get_target_position(1));
  // Prints 100
}

int32_t motor_get_target_velocity(int8_t port)

Gets the velocity commanded to the motor by the user.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns The commanded motor velocity from +-100, +-200, or +-600, or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move_velocity(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Commanded Velocity: %d\n", motor_get_target_velocity(1));
    delay(2);
  }
}

double motor_get_actual_velocity(int8_t port)

Gets the actual velocity of the motor.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns The motor's actual velocity in RPM or PROS_ERR_F if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Actual velocity: %lf\n", motor_get_actual_velocity(1));
    delay(2);
  }
}

int32_t motor_get_current_draw(int8_t port)

Gets the current drawn by the motor in mA.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns The motor's current in mA or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Current Draw: %d\n", motor_get_current_draw(1));
    delay(2);
  }
}

int32_t motor_get_direction(int8_t port)

Gets the direction of movement for the motor.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns 1 for moving in the positive direction, -1 for moving in the negative direction, or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Direction: %d\n", motor_get_direction(1));
    delay(2);
  }
}

double motor_get_efficiency(int8_t port)

Gets the efficiency of the motor in percent.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns The motor's efficiency in percent or PROS_ERR_F if the operation failed, setting errno.

An efficiency of 100% means that the motor is moving electrically while drawing no electrical power, and an efficiency of 0% means that the motor is drawing power but not moving.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Efficiency: %d\n", motor_get_efficiency(1));
    delay(2);
  }
}

int32_t motor_is_over_current(int8_t port)

Checks if the motor is drawing over its current limit.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns 1 if the motor's current limit is being exceeded and 0 if the current limit is not exceeded, or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Current Limit Hit?: %d\n", motor_is_over_current(1));
    delay(2);
  }
}

int32_t motor_is_over_temp(int8_t port)

Checks if the motor's temperature is above its limit.

Parameters
port The V5 port number from 1 to 21, or from -21 to -1 for reversed motors
Returns 1 if the temperature limit is exceeded and 0 if the the temperature is below the limit, or PROS_ERR if the operation failed, setting errno.

This function uses the following values of errno when an error state is reached: ENXIO - The given value is not within the range of V5 ports |1-21|. ENODEV - The port cannot be configured as a motor

Example

void opcontrol() {
  while (true) {
    motor_move(1, controller_get_analog(E_CONTROLLER_MASTER, E_CONTROLLER_ANALOG_LEFT_Y));
    printf("Motor Temp Limit: %d\n", motor_is_over_temp(1));
    delay(2);
  }
}