Appendix A. Math Basics for NXT

When programming your robots, you may find many times that you need to perform some basic math in order to properly direct them. While I don't have the space to cover every possible mathematical calculation you may need, I do want to focus on three very simple subjects that you might find useful. The first is converting between degrees and rotations; the second is calculating travel distance based on the number of degrees or rotations you configure; and the third is a very short discussion of how the X/Y coordinate system works on your Brick.

Converting Between Degrees and Rotations

When it comes to the MOVE block's Duration setting on its configuration panel, I've found that most people generally have a preference when configuring their robots to move a specified distance: some will use degrees, and others will use rotations. A rare few will rely on seconds (time-based movement), but if your robot relies at all on accurate movements, you simply cannot program your robot to, say, move forward for five seconds and know for a fact that it will move the exact same distance every time (the issue really comes down to batteries—as the batteries become weaker, the motor power is reduced, and those five seconds result in a shorter distance than in previous runs).

Whether you prefer degrees or rotations, you may find a time you need to use the other method; a book or an article might contain a robot you are duplicating, and the MOVE block settings might be in degrees, though you usually work in rotations.

Well, you'll be happy to know that the math for converting back and forth between degrees and rotations is very simple. Take a look at Figure A-1, and I'll give you a couple of examples.

Equations for converting between degrees and rotations

Figure A-1. Equations for converting between degrees and rotations

Figure A-1 contains two simple formulas. The top formula is for converting rotations to degrees, and the bottom formula is for converting degrees to rotations. Let me give you an example of each.

Let's say you want SPOT to roll forward 9.5 (nine and a half) rotations—easy enough. But your friend asks you to share your program with her and wants all the MOVE blocks configured in degrees. That's easy enough, too. You simply take a look at Figure A-1 and see that to obtain the value in degrees you need to multiply the number of rotations by 360. Using a calculator or doing it by hand, you'll find that the value in degrees equals 3,420. You go to the configuration panel on the MOVE block, change the Duration setting to degrees, enter the value of 3420, and then share your program with your friend.

OK, a week goes by and your friend e-mails you a copy of her version of the program with some modifications. You open the .rbt file and find that she's configured all the MOVE blocks using degrees, but you would really prefer the values to be in rotations. Again, it's easy to change. The bottom formula in Figure A-1 shows that all you need to do is divide the number of degrees by 360. In her first MOVE block, she's configured motor B to spin for 7,543 degrees. If you've done your calculations correctly, you'll find that the value of 7,543 divided by 360 is equal to 20.95277777! Will that work?

Well, the answer is that the MOVE block will allow you to enter only up to three decimal places. It would be safe to enter the value of 20.953 for number of rotations, but you'll find that the NXT motors are accurate only to the first decimal place. It's your call, and you'll want to experiment with accuracy, but in most cases, you'd be safe entering 20.9 or 21 as the value for rotations. Again, if accuracy is needed, you'll want to do a lot of testing to tweak that value. You might start by using a value of 21 and then reducing it by .1 for each experiment until you get the correct behavior from the motor.

Converting Degrees and Rotations into Distances

OK, you've programmed SPOT to move forward 720 degrees. But how far will that actually move him? What you need is the ability to convert rotations or degrees into inches or centimeters. Well, take a look at Figure A-2, and you'll see some more formulas.

Before I give you an example, remember that to calculate distance you need to convert the Duration value to rotations. So, if your MOVE block has the Duration set in degrees, use the first formula in Figure A-1 to convert it to rotations.

Now, let me walk you through how to use these simple formulas. In our example, we want to determine how far SPOT will travel if the Duration is set to 1080 degrees. We first need to convert that value to rotations, so we simply divide the number of degrees by 360. We end up with three rotations.

Next, according to Figure A-2, we need to determine one other value—the wheel circumference. This is easy, and I've given you a small picture to help you. You first measure the diameter of the wheel. The diameter is actually the distance between the two farthest points on the wheel (which happens to be the midpoint of the wheel as well). It doesn't matter if you measure the diameter in inches or centimeters, as long as you remember that the final distance you're going to calculate will also be in those units.

Calculating distance traveled using rotations

Figure A-2. Calculating distance traveled using rotations

If you take one of the NXT 2.0 wheels and measure it, you'll find that the diameter of the wheel is approximately 1.75 inches (4.445 centimeters). (NXT 2.0 wheels are approximately 2.25 inches or 5.715cm.) Figure A-2 tells us to find the circumference by multiplying the wheel diameter by 3.14 (also known as pi; pi is a much longer number, but for our calculations, 3.14 is a safe enough approximation). If we've done our math correctly, we obtain a wheel circumference value of 5.495 inches (or 13.9573 centimeters).

Next, Figure A-2 tells us that we can calculate the total distance moved by multiplying the wheel circumference by the number of rotations. Once again, if we've done our calculations correctly, multiplying 5.495 inches by three rotations gives us 16.485 inches (41.8719 centimeters).

And that's it! You can now convert back and forth between rotations and degrees as well as calculate the distances your motors will spin (and that your robots will travel). There's one more small calculation I'll leave you with, but it will involve a quick visit to the Internet.

Would you like to know how to calculate the number of rotations or degrees required to turn your robots left or right? If you want your robot to turn in place without moving forward or backward, how do you calculate the proper number of rotations or degrees to spin one of the motors so that the robot turns left or right?

For the answer (and an example), point your web browser to the following URL:

http://thenxtstep.blogspot.com/2006/10/reader-question-submission-2.html

The X/Y Coordinate System in NXT

The final bit of information I want to provide is how to interpret the coordinate system used on your NXT Brick's LCD screen. The LCD screen has a horizontal resolution of 100 pixels and a vertical resolution of 64 pixels. This simply means you could place 100 small dots across the screen and 64 small dots down the screen. Values actually start with a zero, so the range of coordinates for X is 0–99, and for Y, the range is 0–63.

When using the DISPLAY block (or any block where you must specify a location on the LCD screen), you specify a location by using its horizontal value (X coordinate) and its vertical value (Y coordinate). So, to place a pixel (or dot) directly in the center of the screen, you would have a DISPLAY block place a Point (see Chapter 3 for details on using the DISPLAY block) with an X coordinate of 50 and a Y coordinate of 32.

Some people think there's a trick to using the coordinate system, but it's actually quite simple: coordinates start at 0, 0 in the lower-left corner of the LCD screen. Values for X increase as you move to the right, and values for Y increase as you move up the screen.

To summarize, the X/Y value of a pixel in the lower-left corner is 0/0. For the upper-right corner, the value would be 99/63. Remember, the Y value increases only as you move up the LCD screen, so the value of a pixel in the upper-left corner is 0/63, and a pixel in the lower-right corner is 99/0 (see Figure A-3).

The Brick's LCD screen's X/Y coordinate system

Figure A-3. The Brick's LCD screen's X/Y coordinate system

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset