VEX PID Control Functions

From ROBOTC API Guide
Jump to: navigation, search
(mtrPID_ErrorP)
(Variables)
 
(22 intermediate revisions by one user not shown)
Line 86: Line 86:
 
   |-
 
   |-
 
   |<syntaxhighlight lang="ROBOTC">mtrPid_kD[port1] = 75; // Sets the Derivative Gain Parameter in the PID loop to 0.75 (75/100)
 
   |<syntaxhighlight lang="ROBOTC">mtrPid_kD[port1] = 75; // Sets the Derivative Gain Parameter in the PID loop to 0.75 (75/100)
</syntaxhighlight>
 
  |-
 
  |}
 
|-
 
|}
 
<br />
 
 
=== mtrPid_Deadband (coming soon) ===
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
| class="variableType"| <span class="bigKeywordBI">short </span><span class="bigKeywordB">mtrPid_Deadband[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|short]]) Sets the deadband for a motor or servo to prevent movement at very low speeds. This is on a per-motor basis.
 
|-
 
|
 
  {|
 
  |-
 
  |<syntaxhighlight lang="ROBOTC">mtrPid_Deadband[port1] = 5; // Motor values between -5 and +5 will be treated as 0 (zero).
 
</syntaxhighlight>
 
  |-
 
  |}
 
|-
 
|}
 
<br />
 
 
=== mtrPID_PowerLimit (coming soon) ===
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
| class="variableType"| <span class="bigKeywordBI">short </span><span class="bigKeywordB">mtrPid_PowerLimit[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|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.
 
|-
 
|
 
  {|
 
  |-
 
  |<syntaxhighlight lang="ROBOTC">mtrPid_PowerLimit[port1] = 100; // Sets a limit on Motor Port #1 to not go above power level 100.
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 170: Line 134:
 
   |-
 
   |-
 
   |<syntaxhighlight lang="ROBOTC">mtrPid_SlewDown[port1] = 10; //Set the "Slew Down" rate to use increments of 10 when ramping down the motor speed with PID.
 
   |<syntaxhighlight lang="ROBOTC">mtrPid_SlewDown[port1] = 10; //Set the "Slew Down" rate to use increments of 10 when ramping down the motor speed with PID.
</syntaxhighlight>
 
  |-
 
  |}
 
|-
 
|}
 
<br />
 
 
=== mtrPID_Slop (coming soon) ===
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
| class="variableType"| <span class="bigKeywordBI">short </span><span class="bigKeywordB">mtrPid_Slop[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|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)
 
|-
 
|
 
  {|
 
  |-
 
  |<syntaxhighlight lang="ROBOTC">mtrPid_Slop[port1] = 10; // Sets the slop tolerance to 10 encoder counts for motor port #1
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 220: Line 165:
 
| class="variableType"| <span class="bigKeywordBI">const short </span><span class="bigKeywordB">mtrPid_ErrorP[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
| class="variableType"| <span class="bigKeywordBI">const short </span><span class="bigKeywordB">mtrPid_ErrorP[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_const|const]]) Used to read the Proportional (P) Error Value from the PID algorithm.
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_const|const]]) Used to read the Proportional (P) Error Value returned from the PID algorithm.
 
|-
 
|-
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">int x = 0;                // Creates a variable to store the Proportional error value
+
   |<syntaxhighlight lang="ROBOTC">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.
 
x = mtrPid_ErrorP[port1]; // Read the "Proportional" error value from Motor Port #1 and stores it in a variable.
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 238: Line 183:
 
| class="variableType"| <span class="bigKeywordBI">const short </span><span class="bigKeywordB">mtrPid_ErrorI[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
| class="variableType"| <span class="bigKeywordBI">const short </span><span class="bigKeywordB">mtrPid_ErrorI[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_const|const]]) Used to read the Integral (I) error difference of the specified motor
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_const|const]]) Used to read the Integral (I) Error Value returned from the PID algorithm.
 
|-
 
|-
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">int y = 0;                // Creates a variable to store the Integral error value
+
   |<syntaxhighlight lang="ROBOTC">int y = 0;                // Creates a variable to store the "Integral" error value
y = mtrPid_ErrorI[port1]; // Reads the Integral error value from motor port 1 and sets the integer y equal to it
+
y = mtrPid_ErrorI[port1]; // Read the "Integral" error value from Motor Port #1 and stores it in a variable.
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 256: Line 201:
 
| class="variableType"| <span class="bigKeywordBI">const short </span><span class="bigKeywordB">mtrPid_ErrorD[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
| class="variableType"| <span class="bigKeywordBI">const short </span><span class="bigKeywordB">mtrPid_ErrorD[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_const|const]]) Used to read the Derivative (D) error difference of the specified motor
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_const|const]]) Used to read the "Derivative"(D) Error Value returned from the PID algorithm.
 
|-
 
|-
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">int z = 0;                // Creates a variable to store the Derivative error value
+
   |<syntaxhighlight lang="ROBOTC">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
+
z = mtrPid_ErrorD[port1]; // Read the "Derivative" error value from Motor Port #1 and stores it in a variable.
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 274: Line 219:
 
| class="variableType"| <span class="bigKeywordBI">const short </span><span class="bigKeywordB">mtrPid_SyncErr[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
| class="variableType"| <span class="bigKeywordBI">const short </span><span class="bigKeywordB">mtrPid_SyncErr[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_const|const]]) Used to read the Synchronization error difference of the specified motor; used with the driveSynchronized() and driveSynchronizedToPosition() functions
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_const|const]]) When two motors are synchronized (using the driveSynchronized function), the user can read the encoder difference (error) between the two motors. This allows the user to see if one motor is ahead/behind compared to the other motor (to determine drift or additional resistant)
 +
 
 +
'''Note:''' This value is normalized when the two motors are driving at different speeds (i.e one motor running at power level 100 and another motor at power level at 50, the values will be normalized for the difference in speeds/counts per second.)
 
|-
 
|-
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">int x = 0;                 // Creates a variable to store the Synchronization error value
+
   |<syntaxhighlight lang="ROBOTC">task main()
z = mtrPid_SyncErr[port1]; // Reads the Synchronization error value from motor port 1 and sets the integer x equal to it
+
{
 +
driveSynchronized(port1, port10, 50, 50);  //Synchronize Motor Ports #1 and #10 together at power level 50.
 +
static int synchroError1 = 0;
 +
static int synchroError10 = 0;
 +
 +
while(true)
 +
{
 +
synchroError1 = mtrPid_SyncErr[port1]; //Read the "Synchronization Error" on Motor Port 1
 +
synchroError10 = mtrPid_SyncErr[port10];
 +
wait1Msec(5);
 +
}
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|}
 +
<br />
 +
=== mtrPid_Deadband (coming soon) ===
 +
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="variableType"| <span class="bigKeywordBI">short </span><span class="bigKeywordB">mtrPid_Deadband[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|short]]) Sets the deadband for a motor or servo to prevent movement at very low speeds. This is on a per-motor basis.
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">mtrPid_Deadband[port1] = 5; // Motor values between -5 and +5 will be treated as 0 (zero).
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 287: Line 262:
 
<br />
 
<br />
  
== Functions ==
+
=== mtrPID_PowerLimit (coming soon) ===
 
+
=== moveMotorToPosition ===
+
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">moveMotorToPosition</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">tMotor </span><span class="bigCodeBasic">nMotor</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">int </span><span class="bigCodeBasic">nMaxSpeedToUse</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">long </span><span class="bigCodeBasic">nEncoderTargetPosition</span><span class="bigCodePunc">)</span>
+
| class="variableType"| <span class="bigKeywordBI">short </span><span class="bigKeywordB">mtrPid_PowerLimit[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 
|-
 
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|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.
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Used to read the Drive Straight error difference of the specified motor; used with the driveStraight() and driveStraightToPosition() functions
+
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">mtrPid_PowerLimit[port1] = 100; // Sets a limit on Motor Port #1 to not go above power level 100.
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 
|-
 
|-
 +
|}
 +
<br />
 +
=== mtrPID_Slop (coming soon) ===
 +
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="variableType"| <span class="bigKeywordBI">short </span><span class="bigKeywordB">mtrPid_Slop[</span><span class="bigCodeStringsNums">motor_name</span><span class="bigKeywordB">]</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_short|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)
 +
|-
 +
|
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">moveMotorToPosition(port1, 75, 1000); // Rotates the motor on port one forward at 75 power level until 1000 encoder counts are reached
+
   |<syntaxhighlight lang="ROBOTC">mtrPid_Slop[port1] = 10; // Sets the slop tolerance to 10 encoder counts for motor port #1
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 306: Line 297:
 
|}
 
|}
 
<br />
 
<br />
 +
 +
== Functions ==
  
 
=== driveStraight ===
 
=== driveStraight ===
Line 313: Line 306:
 
|-
 
|-
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Used to drive the robot straight using the specified motors and power levels
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) This function will be used to cause two motors to drive together as close as possible to maintain the robot traveling straight.
 
|-
 
|-
 
   {|
 
   {|
Line 331: Line 324:
 
|-
 
|-
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Used to synchronize two motors to move at the same speed and direction
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) When two motors are "Synchronized" their movements and PID calculations are based off of each other. This allows the PID algorithm to dynamically adjust the motors speed should one motor get too far ahead of behind. This command is similar to the "DriveStraight" command, but also allows the user to specific different motors speeds for the two "synchronized" motors. In this scenario, the different between the two motors is normalized to ensure that each motors travels the requested speed, but will still make adjustments should one motor become out of synchronization with the other motor.
 +
 
 +
Note: Manually setting a synchronized motor's power level will cancel the synchronization and instantly change that motor's speed to the newly requested value. The other synchronized motor will continue to travel at the previously requested speed un-synchronized.
 
|-
 
|-
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">driveStraight(port1, port10, 50); // Drives the robot straight forward by moving the motors on ports 1 and 10 at 50 power level.
+
   |<syntaxhighlight lang="ROBOTC">driveSynchronized(port1, port10, 50, 50); //Synchronizes Motor Ports #1 and #10 together at power level 50.
 +
</syntaxhighlight>
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">driveSynchronized(port2, port3, 50, 75); //Synchronizes Motor Ports #2 (at power level 50) and #3 (at power level 75) together.
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 346: Line 344:
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
|  class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">driveSynchronized</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">tMotor </span><span class="bigCodeBasic">nMotor</span><span class="bigCodePunc">)</span>
+
|  class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">stopDriveSynchronized</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">tMotor </span><span class="bigCodeBasic">nMotor</span><span class="bigCodePunc">)</span>
 
|-
 
|-
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Used to stop motor synchronization on the specified motor
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) This command is used to stop the synchronization on a motor without adjust the motor speed. Manually changing the speed of a synchronized motor will have the same effect as this command.
 
|-
 
|-
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">stopDriveSynchronized(port1); // Stops motor synchronization on on port 1
+
   |<syntaxhighlight lang="ROBOTC">stopDriveSynchronized(port1); // Stops the motor synchronization for the motor connected to Motor Port #1
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 367: Line 365:
 
|-
 
|-
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Used to specify a master and slave motor when pairing motors
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Slaving two motors together is important for robots that use multiple motors in the same gear train. The slave motor will run as the same PID regulated speed as the master motor.
 +
 
 +
The "slave" command allows you to only need one motor with an integrated encoder (with PID enabled) in a gear train to have the same PID effect on the entire gear-train.
 
|-
 
|-
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">slaveMotor(port1, port2); // Sets the motor on port1 as the slave motor and the motor on port 2 as the master motor
+
   |<syntaxhighlight lang="ROBOTC">task main()
 +
{
 +
  slaveMotor(port10, port1); //Set Motor Port #10 as the "Slave" motor and Motor Port #1 as the "Master" motor
 +
  motor[port1] = 50;  //Turn on Motor Port #1 (which will also turn on Motor Port #10 as the same speed)
 +
 
 +
  wait1Msec(5000);
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 385: Line 391:
 
|-
 
|-
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Used to drive the robot straight for a specified amount of encoder counts
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Description coming soon!
 
|-
 
|-
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">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.
+
   |<syntaxhighlight lang="ROBOTC">driveStraightToPosition(port1, port10, 75, 1200);
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 403: Line 409:
 
|-
 
|-
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Used to drive the robot straight for a specified amount of encoder counts using synchronized motors
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) Description coming soon!
 +
|-
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">driveStraightToPosition(port1, port10, 75, 1200);
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|}
 +
<br />
 +
 
 +
=== moveMotorToPosition (coming soon) ===
 +
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
|  class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">moveMotorToPosition</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">tMotor </span><span class="bigCodeBasic">nMotor</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">int </span><span class="bigCodeBasic">nMaxSpeedToUse</span><span class="bigCodePunc">, </span><span class="bigKeywordBI">long </span><span class="bigCodeBasic">nEncoderTargetPosition</span><span class="bigCodePunc">)</span>
 +
|-
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) This function will allow the specified motor to travel a specific distance and come to a stop after it has completed its movement. This function will cause the motor to ramp its speed downward to ensure the motor stops within a specific margin of error of the specified target. This margin-of-error (slop) distance is defined by the mtrPid_Slop[] variable.
 
|-
 
|-
 
   {|
 
   {|
 
   |-
 
   |-
   |<syntaxhighlight lang="ROBOTC">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.
+
   |<syntaxhighlight lang="ROBOTC">moveMotorToPosition(port1, 75, 1000); // Rotates the motor on port one forward at 75 power level until 1000 encoder counts (within the defined "slop" margin-of-error)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-

Latest revision as of 08:11, 7 December 2012

Contents

Color Key
Function:
Variable:

[edit] Variables

[edit] 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.
int currentPWMSpeed = 0;
currentPWMSpeed = motorPWMLevel[port1]; // Reads the current PWM level on Motor Port #1.


[edit] 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_Period[port1] = 50; // Set the PID refresh rate to 50ms for Motor Port #1


[edit] 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_kP[port1] = 75; // Sets the Proportional Gain Parameter in the PID loop to 0.75 (75/100)


[edit] 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_kI[port1] = 75; // Sets the Integral Gain Parameter in the PID loop to 0.75 (75/100)


[edit] 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_kD[port1] = 75; // Sets the Derivative Gain Parameter in the PID loop to 0.75 (75/100)


[edit] 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.

  • At a "Slew Up" rate of 25, and a PID update interval of 50ms. In the first 50ms, the PID Algorithm will be set to get to speed 25. The second interval will use speed 50, the 3rd interval will use speed 75 and the 4th PID interval will set to the final speed of power level 100. In this example, the ramp up will take 200ms to reach target desired speed. This prevents excessive accumulation of PID error integral and also should prevent wheel spin.

Users can disable this functionality by setting the "Slew Rate" to 255. This setting is on a per-motor basis.

mtrPid_SlewUp[port1] = 25; //Set the "Slew Up" rate to use increments of 25 when ramping up the motor speed with PID.


[edit] 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.

  • At a "Slew Down" rate of 10, and a PID update interval of 50ms. In the first 50ms, the PID Algorithm will be set to get to speed 90. The second interval will use speed 80, the 3rd interval will use speed 70 and the 4th PID interval will set to the final speed of power level 60. In this example, the ramp down will take 200ms to reach the new target desired speed. This prevents excessive accumulation of PID error integral and also should prevent wheel spin.

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_SlewDown[port1] = 10; //Set the "Slew Down" rate to use increments of 10 when ramping down the motor speed with PID.


[edit] 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.
int currentSlew = 0;
currentSlew = mtrPid_Slop[port1]; //Read the current "slew" value from the ramping function in ROBOTC.


[edit] 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.


[edit] 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.


[edit] mtrPID_ErrorD

const short mtrPid_ErrorD[motor_name]
(const) Used to read the "Derivative"(D) Error Value returned from the PID algorithm.
int z = 0;                 // Creates a variable to store the "Derivative" error value
z = mtrPid_ErrorD[port1]; // Read the "Derivative" error value from Motor Port #1 and stores it in a variable.


[edit] mtrPID_SyncErr

const short mtrPid_SyncErr[motor_name]
(const) When two motors are synchronized (using the driveSynchronized function), the user can read the encoder difference (error) between the two motors. This allows the user to see if one motor is ahead/behind compared to the other motor (to determine drift or additional resistant)

Note: This value is normalized when the two motors are driving at different speeds (i.e one motor running at power level 100 and another motor at power level at 50, the values will be normalized for the difference in speeds/counts per second.)

task main()
{
	driveSynchronized(port1, port10, 50, 50);  //Synchronize Motor Ports #1 and #10 together at power level 50.
	static int synchroError1 = 0;
	static int synchroError10 = 0;
 
	while(true)
	{
		synchroError1 = mtrPid_SyncErr[port1];	//Read the "Synchronization Error" on Motor Port 1
		synchroError10 = mtrPid_SyncErr[port10];
		wait1Msec(5);
	}
}


[edit] 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_Deadband[port1] = 5; // Motor values between -5 and +5 will be treated as 0 (zero).


[edit] 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_PowerLimit[port1] = 100; // Sets a limit on Motor Port #1 to not go above power level 100.


[edit] 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_Slop[port1] = 10; // Sets the slop tolerance to 10 encoder counts for motor port #1


[edit] Functions

[edit] driveStraight

void driveStraight(tMotor nLeftMotor, tmotor nRightMotor, int nSpeed)
(void) This function will be used to cause two motors to drive together as close as possible to maintain the robot traveling straight.
driveStraight(port1, port10, 50); // Drives the robot straight forward by moving the motors on ports 1 and 10 at 50 power level.


[edit] driveSynchronized

void driveSynchronized(tMotor nLeftMotor, tmotor nRightMotor, int nLeftSpeed, int nRightSpeed)
(void) When two motors are "Synchronized" their movements and PID calculations are based off of each other. This allows the PID algorithm to dynamically adjust the motors speed should one motor get too far ahead of behind. This command is similar to the "DriveStraight" command, but also allows the user to specific different motors speeds for the two "synchronized" motors. In this scenario, the different between the two motors is normalized to ensure that each motors travels the requested speed, but will still make adjustments should one motor become out of synchronization with the other motor.

Note: Manually setting a synchronized motor's power level will cancel the synchronization and instantly change that motor's speed to the newly requested value. The other synchronized motor will continue to travel at the previously requested speed un-synchronized.

driveSynchronized(port1, port10, 50, 50); //Synchronizes Motor Ports #1 and #10 together at power level 50.
driveSynchronized(port2, port3, 50, 75); //Synchronizes Motor Ports #2 (at power level 50) and #3 (at power level 75) together.


[edit] stopDriveSynchronized

void stopDriveSynchronized(tMotor nMotor)
(void) This command is used to stop the synchronization on a motor without adjust the motor speed. Manually changing the speed of a synchronized motor will have the same effect as this command.
stopDriveSynchronized(port1); // Stops the motor synchronization for the motor connected to Motor Port #1


[edit] slaveMotor

void slaveMotor(tMotor nSlaveMotor, tMotor nMasterMotor)
(void) Slaving two motors together is important for robots that use multiple motors in the same gear train. The slave motor will run as the same PID regulated speed as the master motor.

The "slave" command allows you to only need one motor with an integrated encoder (with PID enabled) in a gear train to have the same PID effect on the entire gear-train.

task main()
{
  slaveMotor(port10, port1);  //Set Motor Port #10 as the "Slave" motor and Motor Port #1 as the "Master" motor
  motor[port1] = 50;  //Turn on Motor Port #1 (which will also turn on Motor Port #10 as the same speed)
 
  wait1Msec(5000);
}


[edit] driveStraightToPosition (Coming Soon!)

void driveStraightToPosition(tMotor nLeftMotor, tmotor nRightMotor, int nMaxSpeedToUse, long nEncoderTargetPosition)
(void) Description coming soon!
driveStraightToPosition(port1, port10, 75, 1200);


[edit] driveSynchronizedToPosition (Coming Soon!)

void driveStraightToPosition(tMotor nLeftMotor, tmotor nRightMotor, int nMaxSpeedToUse, long nEncoderTargetPosition)
(void) Description coming soon!
driveStraightToPosition(port1, port10, 75, 1200);


[edit] moveMotorToPosition (coming soon)

void moveMotorToPosition(tMotor nMotor, int nMaxSpeedToUse, long nEncoderTargetPosition)
(void) This function will allow the specified motor to travel a specific distance and come to a stop after it has completed its movement. This function will cause the motor to ramp its speed downward to ensure the motor stops within a specific margin of error of the specified target. This margin-of-error (slop) distance is defined by the mtrPid_Slop[] variable.
moveMotorToPosition(port1, 75, 1000); // Rotates the motor on port one forward at 75 power level until 1000 encoder counts (within the defined "slop" margin-of-error)


Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox