Sunday, January 17, 2010

Abstracting NXT Robot Rotation

One of the first questions asked by a Lego NXT user/owner is "How do I make my robot rotate by 90 degrees?" Indeed a simple question, but one that leads to great confusion. Of course after understanding more about the programming blocks we discover that rotation of the robot gets translated to rotation of the robot's motor. Furthermore we discover that 90 degrees rotation of a motor does not yield to 90 degrees rotation of the robot. Could there be an easier way to rotate the robot? Then answer of course is, yes. The purpose of this blog entry is to provide one solution to abstract motor rotation to robot rotation for the NXT robot.

For simplicity we assume that your robot is using a left and right hand side motors, and rotation is on the robots center axis. By this I mean the left and righ hand motors will opperate on opposite directions to obtain rotation.

First Abstraction
The first and most basic approach is to run both motors at the same time (in parallel) and rotate the motors in opposite directions by the same number of degrees as illustrated in the sequence image below. The number of degrees of motor rotation is obtained by trial and error.




Once the correct angle is found create a My Block containing the two blocks, name this new block ROTATION_v1.rbt. Watch out the NXT Software sometimes rearranges the blocks. Rearrange so that the look as shown in the image above. With these steps you have successfully abstracted rotation of the robot by 90 degrees.

Second Abstraction
Now that we have a way of rotating the robot by 90 degrees it would be nice to rotate it by any angle. Writing the motor rotation angle value for each motor might lead to errors or mistakes. This next step deals with preventing this error.
  1. For this we create a variable, we'll called it degrees.
  2. On the sequence panel add the writing and reading blocks for this variable as shown in the image to the right.
  3. Connect the wires as shown so that the variable value is feed into the rotation of both motors.
  4. Select only motors C and B and create a new My Block. This will allow for an input into the new My Block.
  5. Name the new block ROTATION_V2.rbt
The finished block sequence should look as shown in the image below. Notice this block provides for an input of the motor rotation angle.




Third Abstraction
Lets build on the last abstraction ROTATION_v2.rbt to be able to complete the rotation abstraction.
  1. To do this we must first calculate the Rotation Constant. This is done by dividing the rotation angle obtained in the first abstraction (Motor Rotation angle to produce 90 degrees robot rotation) by 90. In our case this Rotation Constant would be



    455/90 = 5.06


  2. Use a Constant block and enter the Rotation Constant just calculated.
  3. Use a Math block and multiply the Rotation Constant by the input number of degrees. The result will be the motor angle of rotation.
  4. Feed the result of the Math block to the rotation angle of each of the motors.
  5. Save the file as ROTATION_v3.rbt
  6. Test the Rotation block to make sure the rotation is correct. Adjust the Rotation Constant as necessary.
The resulting sequence should look as follows:




Final Abstraction
The previous abstraction will only allow right rotations. A negative number will not yield a left rotation. To deal with both left and right rotation we must identify if the input angle is either positive or negative to make the motors rotate in opposite directions.
  1. Use a Comparison block to find out if the number is greater than zero.
  2. Take the output of the Comparison block and feed it into the input of a Logical block set to NOT operation.
  3. The output of the Comparison block gets feed into the direction of the C motor while the output of the Logic block gets feed into the direction of motor B.
The resulting sequence should look as follow:




With this we complete the robot rotation abstraction.

Remember every new robot you create will require a new Rotation Constant.

1 comment: