Difference between revisions of "ARDUINO 328 Functions Motors and Servos"

From ROBOTC API Guide
Jump to: navigation, search
(Created page with "<yambe:breadcrumb self="Motors and Sensors">ARDUINO_Functions_and_Variables|Functions and Variables</yambe:breadcrumb> <br /> {| |- |''For more examples and explanations, hea...")
 
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<yambe:breadcrumb self="Motors and Sensors">ARDUINO_Functions_and_Variables|Functions and Variables</yambe:breadcrumb>
+
{{DISPLAYTITLE:328 Motor and Servo Functions}}
 +
<yambe:breadcrumb self="328 Motor and Servo">ARDUINO_328_Functions_and_Variables|Functions and Variables</yambe:breadcrumb>
 
<br />
 
<br />
  
{|
+
The 328P-Based Arduinos have 6 ports available for PWM generation. These ports can be used control motors and servos to extend the capabilities of your Arduino system. Users can use the "Motors and Sensor Setup" utility (found under the Robot menu) to configure their ports for the various types of motors:
|-
+
|''For more examples and explanations, head over to [[VEX2_Motors_and_Servos_Overview|Motors and Servos Overview]]!''
+
|-
+
|Up to 8 motors and/or servos can be connected to the VEX controller. Motor speeds range from -127 (full reverse) through zero (stopped) up to +127 (full forward). There is a small deadband in the motors from about +15 to -15
+
|-
+
|}
+
<br />
+
  
{{tl|1|1}}
+
'''''Hardware Timing Servo Ports''' - Pin 9 and 10.''
<br />
+
  
== bMotorReflected ==
+
'''''Hardware Timing PWM (H-Bridge Friendly) Ports''' - Pin 4, 5, 6, and 11.''
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
+
|-
+
| class="variableType"| <span class="bigKeywordBI">bool </span><span class="bigKeywordB">bMotorReflected</span>'''
+
|-
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_bool|bool]]) Boolean array with one entry for each motor. Indicates that the direction of a motor should be reflected 180 degrees. Useful when mechanical design results in a logical "reversed" condition of a motor.
+
|-
+
|
+
  {|
+
  |-
+
  |<syntaxhighlight lang="robotc">
+
bMotorReflected[port1] = true;  // motor 1 WILL be reflected 180 degrees
+
bMotorReflected[port1] = false;  // motor 1 will NOT be reflected 180 degrees
+
</syntaxhighlight>
+
  |-
+
  |}
+
|-
+
|}
+
<br />
+
  
== getEncoderForMotor ==
+
[[File:UnoMotors.png]]
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
+
 
 +
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0" class="wikiText"
 
|-
 
|-
| class="functionType"| <span class="bigKeywordBI">tSensors </span><span class="bigKeywordB">getEncoderForMotor</span><span class="bigCodePunc">(</span><span class="bigCodeBasic">tMotor motorIndex</span><span class="bigCodePunc">)</span>
+
|<span class="keywordB" id="dataType_TBaudRate">Available Motor Types</span>
|-
+
  {| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:center; font-size:100%; border-collapse: separate; border-spacing: 0; border-width: 1px; border-style: solid; border-color: #000;" cellpadding="5%"
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_tSensors|tSensors]])  Returns the sensor port of the encoder paired to motor port, <span class="codeBasic">motorIndex</span>.
+
   ! style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|''Motor Type''
<br />
+
   ! style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|Comments
 
+
   ! style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|
{| class="parameterTable" cellpadding="5%" width="100%"
+
   ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
+
   ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
+
   ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
+
 
   |-
 
   |-
   | style="border-style: solid; border-width: 1px 0px 0px 0px"|''motorIndex''  
+
   | style="text-align:left; border-style: solid; border-width: 1px 0px 1px 0px"|'''No Servo/Motor'''
   | style="border-style: solid; border-width: 1px 0px 0px 0px"|A motor port or name
+
   | style="text-align:right; border-style: solid; border-width: 1px 0px 1px 0px"|Generate no PWM from this pin.
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_tMotor|tMotor]]
+
 
   |-
 
   |-
   |}
+
   | style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Audio Speaker'''
<br />
+
   | style="text-align:right; border-style: solid; border-width: 0px 0px 1px 0px"|Use this Pin to generate Audio output (piezo-electric speaker compatible)
 
+
   {|
+
 
   |-
 
   |-
   |<syntaxhighlight lang="robotc">
+
   | style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Servo - Standard'''
int valueOfEncoder;                                       // set 'valueOfEncoder' to the value...
+
  | style="text-align:right; border-style: solid; border-width: 0px 0px 1px 0px"|Generate a 20ms-based Servo Pulse
valueOfEncoder = SensorValue(getEncoderForMotor(port1)); // ...returned by the encoder paired to port1
+
</syntaxhighlight>
+
 
   |-
 
   |-
   |}
+
   | style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Servo - Cont. Rotation'''
|-
+
   | style="text-align:right; border-style: solid; border-width: 0px 0px 1px 0px"|Generate a 20ms-based Continuous Rotation Servo Pulse
|}
+
<br />
+
 
+
== mapEncoderToMotor ==
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
+
|-
+
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">mapEncoderToMotor</span><span class="bigCodePunc">(</span><span class="bigCodeBasic">tMotor motorPort</span><span class="bigCodePunc">, </span><span class="bigCodeBasic">tSensors I2CPort</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]])  Map an I2C sensor port to the motor it belongs to (when using VEX Integrated Encoder Modules).<br />
+
''This is taken care of in the Motors and Sensors Setup window.''
+
<br />
+
 
+
{| class="parameterTable" cellpadding="5%" width="100%"
+
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
+
   ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
+
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
+
 
   |-
 
   |-
   | style="border-style: solid; border-width: 1px 0px 1px 0px"|''motorPort''  
+
   | style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Ext-HBridge - Single PWM'''
   | style="border-style: solid; border-width: 1px 0px 1px 0px"|A motor port or name
+
   | style="text-align:right; border-style: solid; border-width: 0px 0px 1px 0px"|Generates a standard PWM pulse and associates a second digital out pin to control the direction. Used for HBridges that have High/Low direction control.
  | style="border-style: solid; border-width: 1px 0px 1px 0px"|[[Data_Types#dataType_tMotor|tMotor]]
+
 
   |-
 
   |-
   | style="border-style: solid; border-width: 0px 0px 0px 0px"|''I2CPort''  
+
   | style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Ext-HBridge - Dual PWM'''
   | style="border-style: solid; border-width: 0px 0px 0px 0px"|An I2C sensor port or name
+
   | style="text-align:right; border-style: solid; border-width: 0px 0px 1px 0px"|Generates a standard PWM pulse on two pins for speed and direction control. Used for HBridges that have PWM based direction control.
  | style="border-style: solid; border-width: 0px 0px 0px 0px"|[[Data_Types#dataType_tSensors|tSensors]]
+
 
   |-
 
   |-
   |}
+
   | style="text-align:left; border-style: solid; border-width: 0px 0px 1px 0px"|'''Variable Intensity LED'''
<br />
+
   | style="text-align:right; border-style: solid; border-width: 0px 0px 1px 0px"|Generates a standard PWM pulse on a single pin for brightness control of a LED.
 
+
   {|
+
 
   |-
 
   |-
   |<syntaxhighlight lang="robotc">
+
   | style="text-align:left; border-style: solid; border-width: 0px 0px 0px 0px"|'''Analog Write'''
mapEncoderToMotor(port1, I2C_1); // map the encoder in I2C port 'I2C_1' to motor in port 'port1'
+
  | style="text-align:right; border-style: solid; border-width: 0px 0px 0px 0px"|Generates a standard PWM pulse on a single pin for analog output.
</syntaxhighlight>
+
 
   |-
 
   |-
 
   |}
 
   |}
|-
 
 
|}
 
|}
 
<br />
 
<br />
  
== motor ==
+
{{tl|1|1}}
 +
<br />
 +
 
 +
== bMotorReflected ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="variableType"| <span class="bigKeywordBI">int </span><span class="bigKeywordB">motor</span><span class="bigCodePunc">[</span><span class="bigCodeBasic">tMotor motor</span><span class="bigCodePunc">]</span>
+
| class="variableType"| <span class="bigKeywordBI">bool </span><span class="bigKeywordB">bMotorReflected</span>'''
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_int|int]]) An array variable with one element for each of the possible motors. This command is used to set the speed (-127 to +127) for a motor. Negative values are reverse; positive forward. Zero is stopped. To drive the motor on port1 at 50% of full power you use the statement motor[port1] = 63;
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_bool|bool]]) Boolean array with one entry for each motor. Indicates that the direction of a motor should be reflected 180 degrees. Useful when mechanical design results in a logical "reversed" condition of a motor.
|-
+
|
+
  {| class="parameterTable" cellpadding="5%" width="100%"
+
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
+
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
+
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
+
  |-
+
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|''motor''
+
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|A motor port or name
+
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_tMotor|tMotor]]
+
  |-
+
  |}
+
 
|-
 
|-
 
|
 
|
Line 125: Line 61:
 
   |-
 
   |-
 
   |<syntaxhighlight lang="robotc">
 
   |<syntaxhighlight lang="robotc">
motor[port1] = 127;  // motor 1 is given a power level of 127 (forward)
+
bMotorReflected[motor_2] = true;  // motor 1 WILL be reflected 180 degrees
motor[port1] = -127;  // motor 1 is given a power level of -127 (reverse)
+
bMotorReflected[motor_2] = false;  // motor 1 will NOT be reflected 180 degrees
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
Line 134: Line 70:
 
<br />
 
<br />
  
== motorType ==
+
== motor ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
| class="variableType"| <span class="bigKeywordBI">word </span><span class="bigKeywordB">motorType</span><span class="bigCodePunc">[</span><span class="bigCodeBasic">tMotor motor</span><span class="bigCodePunc">]</span>
+
| class="variableType"| <span class="bigKeywordBI">int </span><span class="bigKeywordB">motor</span><span class="bigCodePunc">[</span><span class="bigCodeBasic">tMotor motor</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_word|word]]) An array variable with one element for each of the possible motors. This command is used to read/write the type of the motor in question.
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_int|int]]) An array variable with one element for each of the possible motors.  
 +
 
 +
When used with a motor, this command is used to set the speed (-127 to +127) for a motor. Negative values are reverse; positive forward. Zero is stopped.
 +
 
 +
When used with a servo, this command is used to set the position (-127 to +127) of the servo. Zero is the "center" position of the servo, and -127 to +127 is the range of the servo. Note that your servo may not be able to travel the full -127 to +127 range, so please experiment with smaller values to avoid breaking your servo.
 
|-
 
|-
 
|
 
|
Line 157: Line 97:
 
   |-
 
   |-
 
   |<syntaxhighlight lang="robotc">
 
   |<syntaxhighlight lang="robotc">
motorType[port1] = tmotorVex393HighSpeed;  // set motor in 'port1' to type 'tmotorVex393HighSpeed'
+
motor[motor_2] = 127;  // motor pin 2 is given a power level of 127 (forward)
 +
motor[motor_2] = -127;  // motor pin 2 is given a power level of -127 (reverse)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-

Latest revision as of 21:12, 11 May 2012

ARDUINOFunctions and Variables → 328 Motor and Servo


The 328P-Based Arduinos have 6 ports available for PWM generation. These ports can be used control motors and servos to extend the capabilities of your Arduino system. Users can use the "Motors and Sensor Setup" utility (found under the Robot menu) to configure their ports for the various types of motors:

Hardware Timing Servo Ports - Pin 9 and 10.

Hardware Timing PWM (H-Bridge Friendly) Ports - Pin 4, 5, 6, and 11.

UnoMotors.png

Available Motor Types
Motor Type Comments
No Servo/Motor Generate no PWM from this pin.
Audio Speaker Use this Pin to generate Audio output (piezo-electric speaker compatible)
Servo - Standard Generate a 20ms-based Servo Pulse
Servo - Cont. Rotation Generate a 20ms-based Continuous Rotation Servo Pulse
Ext-HBridge - Single PWM Generates a standard PWM pulse and associates a second digital out pin to control the direction. Used for HBridges that have High/Low direction control.
Ext-HBridge - Dual PWM Generates a standard PWM pulse on two pins for speed and direction control. Used for HBridges that have PWM based direction control.
Variable Intensity LED Generates a standard PWM pulse on a single pin for brightness control of a LED.
Analog Write Generates a standard PWM pulse on a single pin for analog output.


Color Key
Function:
Variable:


bMotorReflected

bool bMotorReflected
(bool) Boolean array with one entry for each motor. Indicates that the direction of a motor should be reflected 180 degrees. Useful when mechanical design results in a logical "reversed" condition of a motor.
bMotorReflected[motor_2] = true;   // motor 1 WILL be reflected 180 degrees
bMotorReflected[motor_2] = false;  // motor 1 will NOT be reflected 180 degrees


motor

int motor[tMotor motor]
(int) An array variable with one element for each of the possible motors.

When used with a motor, this command is used to set the speed (-127 to +127) for a motor. Negative values are reverse; positive forward. Zero is stopped.

When used with a servo, this command is used to set the position (-127 to +127) of the servo. Zero is the "center" position of the servo, and -127 to +127 is the range of the servo. Note that your servo may not be able to travel the full -127 to +127 range, so please experiment with smaller values to avoid breaking your servo.

Parameter Explanation Data Type
motor A motor port or name tMotor
motor[motor_2] = 127;   // motor pin 2 is given a power level of 127 (forward)
motor[motor_2] = -127;  // motor pin 2 is given a power level of -127 (reverse)