# NXT Functions Motors

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

 For more examples and explanations, head over to Motors and Servos Overview! There are three motors on the NXT labeled A, B and C. Each motor is equipped with an integrated encoder that can be used to determine the current position of the motor. The encoders have 360 counts per single revolution of the motor. A count of 180 indicates that the motor has traveled half a rotation.

Color Key
Function:
Variable:

## bFloatDuringInactiveMotorPWM

bool bFloatDuringInactiveMotorPWM
(bool) Boolean variable. True "coasts" ("floats") the motor when power is not applied. False brakes the motor. False is best choice.
 ```bFloatDuringInactiveMotorPWM = true; // the motors WILL coast when power is not applied bFloatDuringInactiveMotorPWM = false; // the motors will NOT coast when power is not applied```

## bMotorReflected

bool bMotorReflected
(bool) Boolean array. Used to indicate that the direction of a motor should be reflected 180 degrees. Useful when mechanical design results in a logical "reversed" condition of a motor. This can also be configured in the Motors and Sensor Setup menu in ROBOTC.
 ```bMotorReflected[motorA] = true; // motor A WILL be reflected 180 degrees bMotorReflected[motorA] = false; // motor A will NOT be reflected 180 degrees```

## motor

int motor[tMotor motor]
(int) Contains the motor power or speed level (-100 to +100). Negative values are reverse; positive formward. A power level of 0 (zero) stops the motors.
Parameter Explanation Data Type
motor A motor port or name tMotor
 ```motor[motorA] = 100; // motor A is given a power level of 100 (forward) motor[motorA] = -100; // motor A is given a power level of -100 (reverse)```

## nMaxRegulatedSpeedNXT

int nMaxRegulatedSpeedNXT
(int) Specifies the maximum speed, in encoder counts per second, that should be used for speed regulation on the NXT. (Default is 1000)
 `nMaxRegulatedSpeedNXT = 500; // the PID maximum speed for the NXT motors is scaled down by half`

## nMotorEncoder

long nMotorEncoder
(long) Current value of the motor encoder. Range is -32768 to 32767 so it will "wrap" after about ~90 revolutions. The user's program should reset the value of the encoder often to avoid the value "resetting" itself when the maximum distance is met.
 ```nMotorEncoder[motorB] = 0; // reset the Motor Encoder of Motor B while(nMotorEncoder[motorB] < 360) // while the Motor Encoder of Motor B has not yet reached 360 counts: { motor[motorB] = 75; // motor B is given a power level of 75 motor[motorC] = 75; // motor C is given a power level of 75 } motor[motorB] = 0; // motor B is given a power level of 0 (stop) motor[motorC] = 0; // motor C is given a power level of 0 (stop)```

## nMotorEncoderTarget

long nMotorEncoderTarget
(long) The nMotorEncoderTarget is used to set a target distance that a motor should move before system puts motor back in idle or stopped state. A target value of 0 (zero) means run forever. The first example only looks at one motor's encoder, keeping things simple in order to travel a specific distance. The second example monitors both motor's encoders to ensure that they both reach their target before moving on to the next segment of code.
 ```nMotorEncoder[motorB] = 0; // reset the Motor Encoder of Motor B nMotorEncoderTarget[motorB] = 360; // set the target for Motor Encoder of Motor B to 360 motor[motorB] = 75; // motor B is run at a power level of 75 motor[motorC] = 75; // motor C is run at a power level of 75   while(nMotorRunState[motorB] != runStateIdle) // while Motor B is still running (hasn't reached target yet): { // do not continue } motor[motorB] = 0; // motor B is stopped at a power level of 0 motor[motorC] = 0; // motor C is stopped at a power level of 0``` With 2 motors:```nMotorEncoder[motorB] = 0; // reset the Motor Encoder of Motor B nMotorEncoder[motorC] = 0; // reset the Motor Encoder of Motor C nMotorEncoderTarget[motorB] = 360; // set the target for Motor Encoder of Motor B to 360 nMotorEncoderTarget[motorC] = 360; // set the target for Motor Encoder of Motor C to 360 motor[motorB] = 75; // motor B is run at a power level of 75 motor[motorC] = 75; // motor C is run at a power level of 75   while(nMotorRunState[motorB] != runStateIdle && nMotorRunState[motorC] != runStateIdle) // while Motor B AND Motor C are still running (haven't yet reached their target): { // do not continue } motor[motorB] = 0; // motor B is stopped at a power level of 0 motor[motorC] = 0; // motor C is stopped at a power level of 0```

## nMotorPIDSpeedCtrl

TMotorRegulation nMotorPIDSpeedCtrl
(TMotorRegulation) nMotorPIDSpeedCtrl is used to enable or disable the speed regulation that is used to ensure that motors travel at a consistent speed, regardless of the surface or resistance met. Speed Regulation can be set in the Motors and Sensor Setup menu in ROBOTC. To make things easier, by default PID is enabled for LEGO motors.
 `nMotorPIDSpeedCtrl[motorA] = mtrSpeedReg; // enable PID on Motor A`

## nMotorRunState

TNxtRunState nMotorRunState
(TNxtRunState) Array containing the internal state of a NXT motor. Useful in checking when a motor movement "command" has finished. There are three different states:
• runStateRunning
• runStateHoldPosition
• runStateIdle
 ```nMotorEncoder[motorB] = 0; // reset the Motor Encoder of Motor B nMotorEncoderTarget[motorB] = 360; // set the target for Motor Encoder of Motor B to 360 motor[motorB] = 75; // motor B is run at a power level of 75 motor[motorC] = 75; // motor C is run at a power level of 75   while(nMotorRunState[motorB] != runStateIdle) // while Motor B is still running: { // Do not continue. } motor[motorB] = 0; // motor B is stopped at a power level of 0 motor[motorC] = 0; // motor C is stopped at a power level of 0```

## nPidUpdateInterval

int nPidUpdateInterval
(int) Interval (in milliseconds) to use for motor PID updates. Default is 25. Updating the PID algorithm too often could cause the PID algorithm to be very dynamic and cause a jerky motion. A value too high will not update fast enough and will cause the motors to seem as if they are not regulated.
 `nPidUpdateInterval = 20; // a good interval is 20`

## nSyncedMotors

TSynchedMotors nSyncedMotors
(TSynchedMotors) Used to specify synchronization of two NXT motors so they operate in "lock step" at the same speed turn ratio. The command to sync motors is "synchXY", where X is the "master" motor letter, and Y is the "slave" motor letter.
 ```nSyncedMotors = syncBC; // sync motors B and C so that B is the master and C is the slave nSyncedTurnRatio = 100; // the slave motor will recieve only half of what the master motor does```

## nSyncedTurnRatio

int nSyncedTurnRatio
(int) Turn ratio to use for a motor that is "slaved" to another motor. +100 is same as primary motor. Zero is stopped. -100 is same movement as primary but in inverted directon. Values between 100 and 0 give porportional power levels from the primary motor to the slave motor.
 ```nSyncedMotors = syncBC; // sync motors B and C so that B is the master and C is the slave. nSyncedTurnRatio = 100; // the slave motor will recieve only half of what the master motor does.```