VEX PID Control Functions
From ROBOTC API Guide
Variables
motorPWMLevel
| short motorPWMLevel[motor_name] | |
| (short) A variable that reports the current PWM value being sent to the motor from the PID Algorithm. This value allows the user to see if the PID algorithm is sending a slower or faster speed to the motor due to external forces. This variable is read only and on a per-motor basis. | |
|
mtrPid_Period
| short mtrPid_Period[motor_name] | |
| (short) This variable will set the time period that the PID algorithm loop will process new data from the encoders. The assigned value is in milliseconds. We do not recommend setting this value below 10ms or above 200ms to maintain PID efficiency. The default value is 50ms. This value can be assigned on a per-motor basis. | |
|
mtrPid_kP
| short mtrPid_kP[motor_name] | |
| (short) Sets the Proportional gain parameter (P) for PID algorithm for the specified motor port. Because this value is an integer, it is divided by 100 before being applied to the PID algorithm. This value is set on a per-motor basis. | |
|
mtrPid_kI
| short mtrPid_kI[motor_name] | |
| (short) Sets the Integral gain parameter (I) for PID algorithm for the specified motor port. Because this value is an integer, it is divided by 100 before being applied to the PID algorithm. This value is set on a per-motor basis. | |
|
mtrPid_kD
| short mtrPid_kD[motor_name] | |
| (short) Sets the Derivative gain parameter (D) for PID algorithm for the specified motor port. Because this value is an integer, it is divided by 100 before being applied to the PID algorithm. This value is set on a per-motor basis. | |
|
mtrPid_Deadband (coming soon)
| short mtrPid_Deadband[motor_name] | |
| (short) Sets the deadband for a motor or servo to prevent movement at very low speeds. This is on a per-motor basis. | |
|
mtrPID_PowerLimit (coming soon)
| short mtrPid_PowerLimit[motor_name] | |
| (short) This variable will set an absolute (both negative and positive) limit for the generated PWM power level from the PID Algorithm. This command is used to prevent motors in a stall/high torque situation from tripping the internal fuses on the motors. This can be set on a per-motor basis. | |
|
mtrPID_SlewUp
| short mtrPid_SlewUp[motor_name] | |
| (short) The "Slew Up" variable controls the rate in which the ramping functionality of the PID algorithm will set the motor power level. The frequency of the rate change is based on the PID Update Internal (set by the mtrPid_Period variable).
Example: With a motor stopped, we want to set the desired power level to 100.
Users can disable this functionality by setting the "Slew Rate" to 255. This setting is on a per-motor basis. | |
|
mtrPID_SlewDown
| short mtrPid_SlewDown[motor_name] | |
| (short) The "Slew Down" variable controls the rate in which the ramping functionality of the PID algorithm will set the motor power level. The frequency of the rate change is based on the PID Update Internal (set by the mtrPid_Period variable).
Example: With a motor at power level 100, we want to set the desired power level to 60.
Users can disable this functionality by setting the "Slew Down" rate to 255. This setting is on a per-motor basis. Note: Setting a motor to speed 0 (zero) will take instant effect and will not utilize the "slew" command. | |
|
mtrPID_Slop (coming soon)
| short mtrPid_Slop[motor_name] | |
| (short) This variable will set the tolerance (or slop) for the "Move to Position" commands in which the command will report back success. This value is in number of encoder counts and set on a per-motor basis.
Example: A motor set to move to position with a "slop" of 10 will report success when the encoders reach anywhere between 990-1010 (+10 or -10 of the desired movement) | |
|
mtrPID_PowerSlew
| short mtrPid_PowerSlew[motor_name] | |
| (short) The "Power Slew" command will allow the user to read what speed the ramping functionality is currently sending to the PID algorithm. This can be used to check if the current "slew" movement has completed. This variable is read-only and can be read on a per-motor basis. | |
|
mtrPID_ErrorP
| const short mtrPid_ErrorP[motor_name] |
| (const) Used to read the Proportional (P) Error Value returned from the PID algorithm. |
int x = 0; // Creates a variable to store the Proportional error value x = mtrPid_ErrorP[port1]; // Read the "Proportional" error value from Motor Port #1 and stores it in a variable. |
mtrPID_ErrorI
| const short mtrPid_ErrorI[motor_name] |
| (const) Used to read the Integral (I) Error Value returned from the PID algorithm. |
int y = 0; // Creates a variable to store the "Integral" error value y = mtrPid_ErrorI[port1]; // Read the "Integral" error value from Motor Port #1 and stores it in a variable. |
mtrPID_ErrorD
| const short mtrPid_ErrorD[motor_name] |
| (const) Used to read the Derivative (D) error difference of the specified motor |
int z = 0; // Creates a variable to store the Derivative error value z = mtrPid_ErrorD[port1]; // Reads the Derivative error value from motor port 1 and sets the integer z equal to it |
mtrPID_SyncErr
| const short mtrPid_SyncErr[motor_name] |
| (const) Used to read the Synchronization error difference of the specified motor; used with the driveSynchronized() and driveSynchronizedToPosition() functions |
int x = 0; // Creates a variable to store the Synchronization error value z = mtrPid_SyncErr[port1]; // Reads the Synchronization error value from motor port 1 and sets the integer x equal to it |
Functions
moveMotorToPosition
| void moveMotorToPosition(tMotor nMotor, int nMaxSpeedToUse, long nEncoderTargetPosition) |
| (void) Used to read the Drive Straight error difference of the specified motor; used with the driveStraight() and driveStraightToPosition() functions |
moveMotorToPosition(port1, 75, 1000); // Rotates the motor on port one forward at 75 power level until 1000 encoder counts are reached |
driveStraight
| void driveStraight(tMotor nLeftMotor, tmotor nRightMotor, int nSpeed) |
| (void) Used to drive the robot straight using the specified motors and power levels |
driveStraight(port1, port10, 50); // Drives the robot straight forward by moving the motors on ports 1 and 10 at 50 power level. |
driveSynchronized
| void driveSynchronized(tMotor nLeftMotor, tmotor nRightMotor, int nLeftSpeed, int nRightSpeed) |
| (void) Used to synchronize two motors to move at the same speed and direction |
driveStraight(port1, port10, 50); // Drives the robot straight forward by moving the motors on ports 1 and 10 at 50 power level. |
stopDriveSynchronized
| void driveSynchronized(tMotor nMotor) |
| (void) Used to stop motor synchronization on the specified motor |
stopDriveSynchronized(port1); // Stops motor synchronization on on port 1 |
slaveMotor
| void slaveMotor(tMotor nSlaveMotor, tMotor nMasterMotor) |
| (void) Used to specify a master and slave motor when pairing motors |
slaveMotor(port1, port2); // Sets the motor on port1 as the slave motor and the motor on port 2 as the master motor |
driveStraightToPosition (Coming Soon!)
| void driveStraightToPosition(tMotor nLeftMotor, tmotor nRightMotor, int nMaxSpeedToUse, long nEncoderTargetPosition) |
| (void) Used to drive the robot straight for a specified amount of encoder counts |
driveStraightToPosition(port1, port10, 75, 1200); // Drives the robot straight forward by moving the motors on ports 1 and 10 at a maximum of 75 power level for 1200 encoder counts. |
driveSynchronizedToPosition (Coming Soon!)
| void driveStraightToPosition(tMotor nLeftMotor, tmotor nRightMotor, int nMaxSpeedToUse, long nEncoderTargetPosition) |
| (void) Used to drive the robot straight for a specified amount of encoder counts using synchronized motors |
driveStraightToPosition(port1, port10, 75, 1200); // Drives the robot straight forward by synchronizing the motors on ports 1 and 10 at a maximum of 75 power level for 1200 encoder counts. |