NXT Motors and Servos Overview
(Created page with "<yambe:breadcrumb self="Motors and Servos Overview">NXT|NXT</yambe:breadcrumb> {| class="wikiText" |- |''For ROBOTC NXT Motor and Servo ''functions'', check out [[NXT:_Funct...") |
|||
| Line 4: | Line 4: | ||
{| class="wikiText" | {| class="wikiText" | ||
|- | |- | ||
| − | |''For ROBOTC NXT Motor and Servo ''functions'', check out [[ | + | |''For ROBOTC NXT Motor and Servo ''functions'', check out [[NXT_Functions_Motors|Motor Functions]] and [[TETRIX_Functions_Servos|Servo Functions]]!'' |
|- | |- | ||
|} | |} | ||
Latest revision as of 13:19, 18 April 2012
| For ROBOTC NXT Motor and Servo functions, check out Motor Functions and Servo Functions! |
|
Motor Speed / Power Control
| Motors are controlled by specifying a power level to apply to the motor. Power levels range from -100 to +100. Negative values indicate reverse direction and positive values indicate forward direction. To move motor ‘A’ forward at 50% of full power, you would use the following statement: | |
| |
| motor[] is the ROBOTC array variable that controls the motor power and motorA is an enum variable for motor A.
| |
| |
| With brand new alkaline batteries a motor can move at almost 1,000 encoder counts per second. But with a partially discharged motor and a motor that has had some wear and tear the maximum speed may reduce to 750 counts. This has an impact on speed regulation.
|
Encoder Value
| nMotorEncoder[] is an array that provides access to the current value of the motor encoder. To wait until the motor moves to a specific encoder position, you might use the following: | |
| |
| nMotorEncoder[] is a 16-bit variable. The maximum value before it overflows is 32,767 which allows for about 95 motor rotations. In long running applications, you may want to periodically reset the value in your application program to avoid overflow situations. |
Moving to a Specific Encoder Value
| You can move a motor a specific amount with the variable nMotorEncoderTarget[]. This indicates the number of encoder ticks to move. If you specify a positive value the motor will slow to a stop at this position. A negative value will leave the motor in coast / float mode when the encoder position is reached.
| |
|
Synchronizing Two Motors
| A very common application is two motors used to power the ‘left’ and ‘right’ motors on a robot. The robot will travel a straight line if both motors are rotating an exactly the same speed. The variable nSyncedMotors is used to synchronize two motors so that the second motor’s speed is slaved to the first motor. | |
| |
| The synchronization “mode” is specified with the variable nSyncedTurnRatio. This ranges in value from -100% to +100%. The magnitude (0 to 100) represents the ratio of the slave speed to the primary motor speed. The sign indicates same (positive) or opposite (negative) direction of travel for the slave vs. the primary. A value of less than 100 will cause the robot to turn. | |
The following example will drive robot in a straight line and then turn the robot:
| |
| A value of 100 indicates that the slave will travel in the same direction and at exactly the same speed as the primary motor. Sync ratio -100 indicates that slave will travel at same speed but in opposite direction. |
Brake vs. Coast/Float Mode
| Motor speed is controlled via a technique called pulse width modulation (PWM). PWM applies “pulses” of power to the motor thousands of times a second. Each pulse is a square wave containing a period of on or powered time followed by a period of off time. The ratio of on-time to off-time adjusts the power applied to the motor. Full battery voltage is applied during the on-time; this is a better technique for speed adjustment than systems that applies partial battery voltage to regulate the speed.
During the “off” time, power is not applied to the motors. During off-time the motors can be either “open-circuited” ore “short-circuited”. Open circuit results in a “floating” voltage where the motor is coasted. Short circuit results in a braking action. ROBOTC has a variable that controls which mode is used. | |
| |
| Brake mode works best for the NXT motors. This is the default mode. You can change the default mode within the ROBOTC “preferences” setup when under the "Expert" menu level. The ROBOTC compiler will automatically generate code at the start of your program to setup the brake or float mode. |
Reflected or Mirrored Motors
| Forward and reverse direction for a motor can be somewhat arbitrary. You may build a Lego model where the forward / reverse direction of the motor is the opposite of what seems logical. This can be easily handled in ROBOTC with the bMotorReflected[] variable which flips or mirrors the motor direction. | |
|