Robot App Store Developer Program

robots-app-store-developer-program

Knowledge Base

How to Control Lego NXT Motors

  By Anna Sandler

Make sure you've read the "What is a Lego NXT Bluetooth Telegram" tutorial before you continue reading this chapter.

Motors command types

Motor commands are part of the "Direct commands".
There are two relevant command types for a direct command:
  • 0x00 for a command with reply message, and
  • 0x80 for a command without the reply message.



SetOutputState command

This command is used to control the motors of NXT that are connected to the motor output port.
Each motor (port) can be configured separately with different configuration by sending the SetOutputState command several times, each time for a different port.
In case all the connected motors should be affected the same way, instead of sending three/two commands for each port with identical values, just send the command once by using 0xFF as a motor output port byte.

Syntax

[0x0C] [0x00] [0x00 or 0x80] [0x04] [0x00-0x02 or 0xFF Motor output port] [Motor power set point] [Mode byte] [Regulation mode] [Turn ratio] [Run state] [Tacho limit LSB] […] […] [Tach limit MSB]

Explanation:

  • Byte 0-1: Command length LSB first.
  • Byte 2: Command type- direct command. For direct command with response message use 0x00, otherwise, for direct command without the reply message, use 0x80.
  • Byte 3: Command- set motor output state.
  • Byte 4: Motor output port. See explanation for motor output port below.
  • Byte 5: Motor power set point. See explanation for motor power set point below.
  • Byte 6: Motor mode byte (bit field). See explanation for motor byte below.
  • Byte 7: Regulation mode. It is valid only when the motor mode is regulated, otherwise use 0x00 value. See explanation for regulation mode below.
  • Byte 8: Turn ratio. It is valid only when using a motors synchronization regulation mode, otherwise use 0x00 value. See explanation for a turn ratio below.
  • Byte 9: Run state. See explanation for Run state below.
  • Byte 10-13: Tacho limit LSB first. Valid only when using a ramp-up or ramp-down as a Run stae, otherwise use 0x00 value. See explanation for tacho limit below.

The reply message that is coming back (if requested) looks like:

[0x03] [0x00] [0x02] [0x04] [0x00 or error]

Explanation:

  • Byte 0-1: Message length LSB first.
  • Byte 2: Command type- reply message
  • Byte 3: Command that has requested for this reply.
  • Byte 4: Command status- 0x00 for success or error number for failure. Check the NXT File Handling over Bluetooth- Introduction chapter for error numbers.

Example:

Let’s drive a 2-wheels vehicle forward when its motors are synchronized.
Preparations- build a basic vehicle according to the quick start. Connect the left motor to the B port and the right motor to the C port.

The command that is sent to NXT is:
0x0C 0x00 0x00 0x04 0x01 0x32 0x05 0x02 0x00 0x20 0x00 0x00 0x00 0x00
0x0C 0x00 0x00 0x04 0x02 0x32 0x05 0x02 0x00 0x20 0x00 0x00 0x00 0x00

The commands that are received from NXT as a reply both look like:
0x03 0x00 0x02 0x04 0x00

SetOutputState parameter's explanation

Motor output port

Motor output port byte tells the NXT, which port will be effected by the command.
Make sure that there is actually a motor that is connected to the specified port.
If all the three motors should be affected the same way,
instead of sending three commands for each port with identical values,
just use 0xFF as a motor output port byte.
The following are port mapper bytes:
  • 0x00- Port A;
  • 0x01- Port B;
  • 0x02- Port C;
  • 0xFF- All ports;

Motor power set point

Power set point byte configures the power (speed) in percentage and the rotation direction of the motor that is connected to the specified motor output port (or all of them if specified).

The direction is determined by the sign of the number:
  • Negative decimal number tells the NXT to rotate the motor backwards;
  • Positive decimal number rotates the motor forward;
The power percentage set the speed rotation of the specified motor/s. The higher the value, the faster the motor rotates;

The minimum value is -100% (full power backward) or FF9C in Hex when converting the sbyte to byte we get 0x9C.
The maximum value is 100% (full power forward) or 0x64 in Hex.

The following values are mostly used:
  • 0x64: Power speed of 100% for forward direction;
  • 0x4B: Power speed of 75% for forward direction;
  • 0x32: Power speed of 50% for forward direction;
  • 0x19: Power speed of 25% for forward direction;
  • 0x00: No power. Stop moving.
  • 0xE7: Power speed of 25% for backward direction;
  • 0xCE: Power speed of 50% for backward direction;
  • 0xB5: Power speed of 75% for backward direction;
  • 0x9C: Power speed of 100% for backward direction;

Motor mode byte

Motor mode byte is a bit-field that configures the working mode of the motors.
You can choose more than one mode. In this case, the motor mode byte is summarized.
For example, the Motor ON mode (0x01) with Break mode (0x02) will result 0x03 as a motor mode byte (0x01 + 0x02).

The following values are all the valid combinations and their influence on the motors working mode:
  • 0x00- Coast mode. Motor that is connected to the specified port will rotate freely.
    Use the coast mode to release the motor locking for letting the motor to rotate from external source alone (manually).
    The Motor power set point value will be ignored.
  • 0x01- Motor ON mode. Motor that is connected to the specified port will rotate according to the value specified in the motor power set point value.
    Note- the actual speed can be effected by external physical load on the motor and can be different from the motor power set point value.
  • 0x02- Break mode alone. Nothing will happen. Don’t use this value.
  • 0x03- Motor ON with Break mode. Use this mode to improve the accuracy of motor output.
    Note- this mode uses more battery power, and the actual speed can be affected by external physical load on the motor and can be different from the motor power set point value.
  • 0x04- Regulated mode alone. Nothing will happen. Don’t use this value.
  • 0x05- Motor ON with Regulated mode. Turn on the regulation mode that will be specified in the Regulation mode byte.
    This mode gives NXT firmware the ability to adjust the speed (add more power to the motors) according to the external physical load on the motors.
    This mode is used to synchronize two motors when it is important the vehicle to drive straight no matter the surface it is driving on, or to synchronize the specified speed (motor power set point) to be constant no matter the physical overload on the motor.
    The regulation synchronize mode will be configured in Regulation Mode byte.
    In case of using the regulation mode to synchronize two motors with separate SetOutputState commands, make sure to use either the 0x05 or 0x07 mode on the both commands.
  • 0x07- Motor ON with Break and Regulated mode- This mode is used for regulated mode with accuracy of motor output.
    The actual speed will not be affected by external physical load on the motor.
    The regulation synchronize mode will be configured in Regulation Mode byte.
    In case of using the regulation mode to synchronize two motors with separate SetOutputState commands, make sure to use either the 0x05 or 0x07 mode on the both commands.
    Note- this mode uses more battery power.

Regulation mode byte

Regulation mode byte configures the way the NXT firmware automatically adjusts the speed, by adding more power to the motors, according to the external physical load on the motors.
Regulation mode is valid only when setting regulated (0x05 or 0x07) as a motor mode, otherwise, use the 0x00 (disable regulation) as a regulation mode.

The following are regulation mode values:
  • 0x00- No regulation will be enabled. Use this value if the Motor mode value is 0x00 (Coast mode), 0x01 (Motor ON mode) or 0x03 (Motor ON with Break mode);
  • 0x01- Speed regulation. Use this regulation mode if the Motor mode value is 0x05 (Motor ON and Regulated mode) or 0x07 (Motor ON with Break and Regulated mode), and you would like the NXT firmware automatically adjust the speed to the specified motor power set point value no matter the physical overload on the motor.
  • 0x02- Two motors synchronization regulation. Use this regulation mode if the Motor mode value is 0x05 (Motor ON and Regulated mode) or 0x07 (Motor ON with Break and Regulated mode), and you would like the NXT firmware automatically keep the two motors in sync regardless of varying physical loads.
    Synchronization of the two motors is highly used when the NXT vehicle should drive straight.
    In case of using separate SetOutputState commands for each motor, make sure to configure both motors to 0x02 as a regulation mode.
    The synchronization regulation (0x02) of the third motor will be ignored.
    The minimum Motor power set point for motor synchronization regulation is 75% or higher or -75% or lower.

Turn ratio

Turn ratio byte specifies the proportional turning for synchronized turning using two motors.
Turn ratio is valid only when using a motor synchronize regulation mode (0x02) as a regulation mode and Run state byte that is set to a non-idle value (see the Run set description below), otherwise use the 0x00 as turn ration value.

When NXT is built as a vehicle, both motors (motor for the right wheel and a motor for the left wheel), should rotate in different directions in order to make a turn.
The backward rotation direction determines the turning direction of the vehicle.
The rotation direction of the motor is set in the motor power set point.
Negative value for backward rotation and positive for a forward direction as described in the Motor power set point section.

When using a motor synchronize regulation mode, the direction of one motor will be automatically affected by the other motor regardless the value that is set in the motor power set point.
In order to accomplish the turning action in the motor synchronized regulation mode, use the turn ratio value on the both motors for NXT firmware to relatively adjust power settings for the left and right motors automatically.

The Turn ratio value determines the speed percentage from the Motor power set point value and the direction of the rotation- which motor will be rotated backwards.
The higher the Turn ratio value, the faster the robot will make the turn.
According to Lego NXT’s documentation, negative Turn ratio value (for both motors) will shift power towards the left motor, whereas a positive value will shift power towards the right motor.
The turning direction is based on the motor that is faster.
If the left motor uses a more power than the right motor, the robot will turn to the right, and vise verse.
In the real world, this behavior is different and depends on the first motor that got the SetOutputState command.

In case the Turn ratio is positive (for both motors)- the first motor will rotate forward while the second motor will rotate the opposite way.
This behavior will make the robot turn to the second motor direction.
If the Turn ratio is negative (for both motors)- the first motor will rotate backward while the second motor will rotate forward.
This behavior will make the robot turn to the first motor direction.

The motor synchronize regulation algorithm is a bit buggy when it comes to the Turn ratio.
We recommend using this regulation for a straight driving (0x00 Turn ratio value) and in order to make a turn, use the non-regulated option by turning OFF the regulation mechanism and using the Motor power set point as negative for the turning direction.

If you insist on making the turns using the motor synchronized regulation,
make sure that both motors:
  • Motor mode is regulated: 0x05 or 0x07;
  • Regulation mode is a motor synchronized regulation mode- 0x02;
  • Run state is ramp up (0x10), running (0x20) or ramp down (0x40);
  • Motor power set point is 75% or higher, and both motors are set to positive power set point or both are set to a negative power set point, otherwise motors will go back and forth in opposite directions.
  • When facing the display screen, the LEFT motor is connected to either A or B ports;
  • When facing the display screen, the RIGHT motor is connected to either B or C ports;
The following values are mostly used (make sure to set the value on both motors):
  • 0x64: 100% of the power set point percentage for turning purposes;
  • 0x4B: 75% of the power set point percentage for turning purposes;
  • 0x32: 50% of the power set point percentage for turning purposes;
  • 0x19: 25% of the power set point percentage for turning purposes;
  • 0x00: No turning. Drive straight when regulation is active otherwise- ignore.
  • 0xE7: 25% of the power set point percentage for turning purposes to the opposite direction;
  • 0xCE: 50% of the power set point percentage for turning purposes to the opposite direction;
  • 0xB5: 75% of the power set point percentage for turning purposes to the opposite direction;
  • 0x9C: 100% of the power set point percentage for turning purposes to the opposite direction;
  • Run state

    Run state enables a smooth change to the motor’s speed.
    The ramping states are not valid when the rotation direction is changed as well.

    The following are Turn state values:
    • 0x00- Idle. It disables power to specified port;
    • 0x10- RampUp. It is valid only when using a motor synchronization regulation mode. This value enables an automatic ramp-up from current speed to the value specified in the Motor power set point over the number of degrees specified by Tach limit bytes. Motor power set point should be greater than a current speed;
    • 0x20- Running. Enable power to the motor that is connected to the specified port;
    • 0x40- RampDown. It is valid only when using a motor synchronization regulation mode. It enables automatically ramp-down from current speed to the value specified in the Motor power set point over the number of degrees specified by Tach limit bytes. Motor power set point should be less than the current speed;

    Tacho limit

    Tachometer limit is valid only when using a ramp-up or ramp-down as a Run state.
    Tachometer limit specifies the rotational distance in degrees for turning the motors.
    The lower the value, the smoother the speed will be changed.

    Tacho limit important values:
    • 0x00 0x00 0x00 0x00- Use this value when the Run state is not one of the ramping states;
    • 0x00 0x00 0x19 0xFF- Use this value (change the order to LSB first) as a minimum value;
    • 0x4B 0xFF 0xFF 0xFF- Use this value (change the order to LSB first) as a maximum value;

    will my wife cheat again click here how do i know if my wife has cheated
    my wife cheated on me now what online why are women unfaithful
    read link click
    sumatriptan succ sumatriptan succ sumatriptan succ
    canada drug pharmacy coupon internet drug coupons



    Comments