1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

McGraw-Hill - The Robot Builder''''s Bonanza Part 4 ppsx

35 329 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

COMBINING DATA TYPES IN A VARIABLE Modern high-level languages such as Visual Basic or JavaScript are designed to take the drudgery out of mundane programming chores. Visual Basic and JavaScript both support (at least as an option) weak data typing, which basically means that the language lets you write programs without requiring that you manage the data types stored in variables. In Visual Basic, for example, you can just throw all data into a “variant” data type, which accepts numbers of different sizes, text strings, you name it. Visual Basic takes care of managing the underlying memory requirements for the data you provide. The typical robot control language is designed to be small and fast because it’s meant to be run on a small single-board computer, or an even smaller microcontroller. The Visual Basic programming environment takes up megabytes of hard disk space; the average robot control program is under 1 kilobyte in size. Because of their simplicity, programming languages for robot control require strong data typing, where you—the programmer—do all the data-typing work yourself. It’s not as hard as it seems. But if you’re used to languages such as Visual Basic and JavaScript, learn- ing the requirements of strong data typing may require a period of adjustment. One of the most difficult aspects of strong data typing is that you cannot directly mix two data types together in a single variable. The reason?—the result of the mixing probably won’t fit in the memory space allocated for the variable. If you add a byte and a string together in an integer variable, the memory will only hold the one byte. When you try to mix data types the programming environment you’re using will either display an error or the robot will not function correctly. In fact, it could function erratical- ly, possibly damaging itself, so exercise care! Here’s an example of two data types that can- not be mixed when used in a programming environment that requires strong data typing: Dim X as Byte Dim Y as Word Dim Z as Byte X = 12 Y = 1025 Z = X + Y Both X and Z are byte-length variables, each of which holds eight-bit values (e.g., 0 to 255). Y is a word-length variable, which can hold a 16-bit value (e.g., 0 to 65535). The statement Z ϭ X ϩ Y will fail because Z cannot hold the contents of Y. If the program- ming environment doesn’t catch this error, it’ll create a bug in your robot. (At best, Z will hold the value 13 and not 1037, as you’d otherwise expect. The value 13 is what’s left of 1037 when that number is stored in a space of only eight bits.) Just because strong data typing restricts you from mixing and matching different data types doesn’t mean you can’t do it. The trick is to use the data conversion statements pro- vided by the programming language. The most common data conversion is between inte- ger numbers and text. Assuming the programming language you’re using supports them, use Va l to convert a string to a numeric type and Str to convert a numeric into a string: Dim X as String Dim Y As Integer X = "1" Y = Val (X) VARIABLES, EXPRESSIONS, AND OPERATORS 91 Ch07_McComb 8/29/00 8:38 AM Page 91 You must also exercise care when using numeric types of different sizes. Data conver- sion statements are typically provided in strong data-typing programming languages to convert 8-, 16-, 32-, and (sometimes) 64-bit numbers from one form to another. If the pro- gramming language supports “floating-point” numbers (numbers that have digits to the right of the decimal point), then there will likely be data conversion statements for those as well. CREATING EXPRESSIONS An expression tells the program what you want it to do with information given to it. An expression consists of two parts: ■ One or more values ■ An operator that specifies what you want to do with these values In most programming languages, expressions can be used when you are defining the con- tents of variables, as in the following: Test1 = 1 + 1 Test2 = (15 * 2) + 1 Test3 = "This is" & "a test" The program processes the expression and places the result in the variable. Expressions can also be part of a more elaborate scheme that uses other program commands. Used in this way, expressions provide a way for your programs to think on their own (although they may seem to be more independent than you’d like them to be!). The following sections present the commonly used operators and how they are used to construct expressions. Some operators work with numbers only, and some can also be used with strings. The list is divided into two parts: math operators (which apply to number val- ues) and relational operators (which work with both numbers and in some programming languages strings as well). Depending on the language, you can use literal values or vari- ables for v1 and v2. Math operators 92 PROGRAMMING CONCEPTS: THE FUNDAMENTALS OPERATOR FUNCTION Ϫ value Treats the value as a negative number. v1 ϩ v2 Adds values v1 and v2 together. v1 - v2 Subtracts value v2 from v1. v1 * v2 Multiplies values v1 and v2. v1 / v2 Divides value v1 by v2. Sometimes also expressed as v1 DIV v2. v1 % v2 Divides value v1 by v2. The result is the floating-point remainder of the division. Sometimes also given as v1 MOD v2. Ch07_McComb 8/29/00 8:38 AM Page 92 Relational operators VARIABLES, EXPRESSIONS, AND OPERATORS 93 OPERATOR FUNCTION Not value Evaluates the logical Not of value. The logical Not is the inverse of an expression: True becomes False, and vice versa. v1 And v2 Evaluates the logical And of v1 and v2. v1 Or v2 Evaluates the logical Or of v1 and v2. v1 ϭ v2 Tests that v1 and v2 are equal. v1 <> v2 Tests that v1 and v2 are not equal. v1 > v2 Tests that v1 is greater than v2. v1 >ϭ v2 Tests that v1 is greater than or equal to v2. v1 Ͻ v2 Tests that v1 is less than v2. v1 Ͻϭ v2 Tests that v1 is less than or equal to v2. Relational operators are also known as Boolean or True/False operators. Whatever they test, the answer is either yes (True) or no (False). For example, the expression 2 ϭ 2 would be True, but the expression 2 ϭ 3 would be False. Using And and Or Relational Operators The And and Or operators work with numbers (depending on the language) and expres- sions that result in a True/False condition. The And operator is used to determine if two val- ues in an expression are both True (equal to something other than zero). If both A and B are True, then the result is True. But if A or B is False (equal to 0), then the result of the And is False. The Or operator works in a similar way, except that it tests if either of the values in an expression is True. If at least one of them is True, then the result is True. Only when both values are False is the result of the Or expression False. It’s often helpful to view the action of the And and Or operators by using a “truth table” like the two that follow. The tables show all the possible outcomes given to values in an expression: AND TRUTH TABLE 0 means False, 1 means True Val1 Val2 Result 00 0 01 0 10 0 11 1 Ch07_McComb 8/29/00 8:38 AM Page 93 USING BITWISE OPERATORS Many programming languages for robot control support unique forms of operators that work with numbers only and let you manipulate the individual bits that make up those numbers. These are called bitwise operators. The following list shows the common characters or names used for bitwise operators. Note that some languages (namely Visual Basic) do not distinguish between bitwise and relational (logical) operators. 94 PROGRAMMING CONCEPTS: THE FUNDAMENTALS OR TRUTH TABLE 0 means False, 1 means True Val1 Val2 Result 00 0 01 1 10 1 11 1 Let’s use some bitwise operators in an example. Suppose you assign 9 to variable This and 14 to variable That. If you bitwise And (& symbol) the two together you get 8 as a result. Here are the binary equivalents of the values 9 and 15 (only four bits are shown since 9 and 14 can be expressed in just four bits): OPERATOR SYMBOL NAME FUNCTION Not or ~ Bitwise Not Reverses the bits in a number; 1s become 0s and vice versa And or & Bitwise And Performs And on each bit in a number Or or | Bitwise Or Performs Or on each bit in a number Xor or ^ Bitwise Xor Performs Xor on each bit in a number ϽϽ Shift left Shifts the values of the bits 1 or more bits to the left ϾϾ Shift right Shifts the values of the bits 1 or more bits to the right DECIMAL NUMBER BINARY EQUIVALENT 9 1001 14 1110 Ch07_McComb 8/29/00 8:38 AM Page 94 Using the And truth table given earlier, manually compute what happens when you bit- wise And these two numbers together: 1001 ϭ 9 1110 ϭ 14 & ____ 1000 ϭ 8 Using the same numbers for a bitwise Or computation, we get: 1001 ϭ 9 1110 ϭ 14 | ____ 1111 ϭ 15 Here’s just one example of how this bitwise stuff is handy in robotics: bitwise And and bitwise Or are commonly used to control the output state of a robot’s computer or micro- controller data port. The port typically has eight output pins, each of which can be con- nected to something, like a motor. Suppose the following four bits control the activation of four motors. When “0” the motor is off; when “1” the motor is on. 0000 all motors off 0010 second motor on 1100 fourth and third motor on …and so forth. Note that the rightmost bit is motor 1, and the leftmost is motor 4. Suppose your program has already turned on motors 1 and 4 at some earlier time, and you want them to stay on. You also want to turn motor 3 on (motor 2 stays off). All you have to do is get the current value of the motor port: 1001 motors 1 and 4 on (decimal value equivalent is 9) You then bitwise Or this result with 0100 motor 3 on (decimal value equivalent is 4) Here’s how the bitwise Or expression turns out: 1001 ϭ 9 0100 ϭ 4 | ____ 1101 ϭ 13 In this example, Or’ing 9 and 4 happens to be the same as adding the decimal values of the numbers together. This is not always the case. For example, Or’ing 1 and 1 results in 1, not 2. VARIABLES, EXPRESSIONS, AND OPERATORS 95 Ch07_McComb 8/29/00 8:38 AM Page 95 For your reference, the following table shows the binary equivalents of the first 16 binary digits (counting zero as the first digit and ending with 15). You can count higher by adding an extra 1 or 0 digit on the left. The extra digit increases the count by a power of two—31, 63, 127, 255, 511, 1023, and so on. 96 PROGRAMMING CONCEPTS: THE FUNDAMENTALS DECIMAL NUMBER BINARY EQUIVALENT 00000 10001 20010 30011 40100 50101 60110 70111 81000 91001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111 USING OPERATORS WITH STRINGS Recall that a string is an assortment of text characters. You can’t perform math calculations with text, but you can compare one string of text against another. The most common use of operations involving strings is to test if a string is equal, or not equal, to some other string, as in: If "MyString" = "StringMy" Then This results in False because the two strings are not the same. In a working program, you’d no doubt construct the string comparison to work with variables, as in: If StringVar1 = StringVar2 Then Ch07_McComb 8/29/00 8:38 AM Page 96 While the ϭ (equals) operator is used extensively when comparing strings, in many pro- gramming languages you can use ϽϾ, Ͻ, Ͼ, Ͻϭ, and Ͼϭ as well. MULTIPLE OPERATORS AND ORDER OF PRECEDENCE All but the oldest or very simple programming languages can handle more than one oper- ator in an expression. This allows you to combine three or more numbers, strings, or vari- ables together to make complex expressions, such as 5 ϩ 10 / 2 * 7. This feature of multiple operators comes with a penalty, however. You must be careful of the order of precedence, that is, the order in which the program evaluates an expression. Some languages evaluate expressions using a strict left-to-right process, while others fol- low a specified pattern where certain operators are dealt with first, then others. When the latter approach is used a common order of precedence is as follows: ORDER OPERATOR 1 - (unary minus), ϩ (unary plus), ~ (bitwise Not), Not (logical Not) 2 * (multiply), / (divide), % or MOD (mod), DIV (integer divide) 3 ϩ (add), - (subtract) 4 ϽϽ(shift left)ϾϾ(shift right) 5 Ͻ (less than), Ͻϭ (less than or equal to), Ͼ (greater than), Ͼϭ (greater than or equal to), ϽϾ (not equal), ϭ (equal) 6 & (bitwise And), | (bitwise Or), ^ (bitwise Xor) 7 And (logical And), Xor (logical Xor) 8 Or (logical Or) The programming language usually does not distinguish between operators that are on the same level or precedence. If it encounters a ϩ for addition and a - for subtraction, it VARIABLES, EXPRESSIONS, AND OPERATORS 97 STRING 1 STRING 2 RESULT hello hello Match Hello hello No Match HELLO hello No Match Now the program compares the contents of the two variables and reports True or False accordingly. Note that string comparisons are almost always case-sensitive: Ch07_McComb 8/29/00 8:38 AM Page 97 will evaluate the expression by using the first operator it encounters, going from left to right. You can often specify another calculation order by using parentheses. Values and operators inside the parentheses are evaluated first. From Here To learn more about Read Programming a robot computer or microcontroller Chapter 28, “An Overview of Robot ‘Brains’” Programming a PC parallel port for robot interaction Chapter 30, “Computer Control Via PC Printer Port” Programming popular high-level language Chapters 31-33 microcontroller chips Ideas for working with bitwise values for Chapter 29, “Interfacing with input/output ports Computers and Microcontrollers” Programming using infrared remote control Chapter 34, “Remote Control Systems” 98 PROGRAMMING CONCEPTS: THE FUNDAMENTALS Ch07_McComb 8/29/00 8:38 AM Page 98 PART 2 ROBOT CONSTRUCTION Ch08_McComb 8/21/00 3:12 PM Page 99 Copyright 2001 The McGraw-Hill Companies, Inc. Click Here for Terms of Use. This page intentionally left blank. [...]... 9.1 PARTS LIST FOR WOOD BASE Robot Base: 1 10-inch-by-10-inch plywood (3/ 8- or 1/2-inch thickness) Misc 1-inch-by-10/ 24 stove bolts, 10/ 24 nuts, flat washers Motor/drive: 2 DC gear motors 2 5- to 7-inch rubber wheels 1 1 1 / 4- inch caster 2 2-by -4 - inch lumber (cut to length to fit motor) 1 Four-cell “D” battery holder Misc 3-inch-by-10/ 24 stove bolts, 10/ 24 nuts, flat washers (for motor mount) 1 1 / 4- inch-by-8/32... 1-by-1-by-1/16-inch angle stock 57/ 6 4- by-9/16-by-1/16-inch channel stock 41 / 6 4- by-1/2-by-1/16-inch channel stock Bar stock, in widths from 1 to 3 inches and thicknesses of 1/16 to 1 /4 inch SHELVING STANDARDS You’ve no doubt seen those shelving products where you nail two metal rails on the wall and then attach brackets and shelves to them The rails are referred to as “standards,” and in a pinch they are... airplane wheels 1 3 1/2-inch (approx.) 10/ 24 all-thread rod 1 6-inch-diameter (approx.) clear plastic dome 1 Four-cell AA battery holder Assorted 1/2-inch-by-8/32 bolts, 8/32 nuts, lock washers, 1/2-inch-by-10/ 24 bolts, 10/ 24 nuts, lock washers, capnuts Motor Control Switch: 1 Small electronic project enclosure 2 Double-pole, double-throw (DPDT) momentary switches, with center-off return Misc Hookup... better, safer results If you use wood bits, you should modify them by blunting the tip slightly (otherwise the tip may crack the plastic when it exits the other side) Continue the flute from the cutting lip all the way to the end of the bit (see Fig 8.1) Blunting the tip of the bit isn’t hard to do, but grinding the flute is a difficult proposition The best idea is to invest in a few glass or plastic bits,... volts A four-cell, AA battery holder does the job nicely The wiring in the holder connects the batteries in series, so the output is 6 volts Secure the battery holder to the base with 8/32 nuts and bolts Drill holes to accommodate the hardware Be sure the nuts and bolts don’t extend too far below the base or they may drag when the robot moves Likewise, be sure the hardware doesn’t interfere with the batteries... control switch for this platform, see the parts list given in Table 8.3 of in Chap 8 Figure 9.2 shows the same 10-inch-square piece of 3/ 8- or 1/2-inch plywood cut into a circle Use a scroll saw and circle attachment for cutting As you did for the square platform, make a notch in the center beam of the circle to allow room for the wheels the larger the wheel, the larger the notch Plywood 10" 2" FIGURE 9.1... for your robot, such as an inverted salad bowl Feel free to use colored plastic Attach the top by measuring the distance from the foundation to the top of the shell, taking into consideration the gap that must be present for the motors and other bulky internal components Cut a length of 10/ 24 all-thread rod to size The length of the prototype shaft was 3 1/2 inches Secure the center shaft to the base... one motor while the other continues turning Attach the caster using four 8/32by-1-inch bolts Secure the caster with tooth lock washers and 8/32 nuts Note that the caster must be level with (or a little higher than) the drive motors You may, if necessary, use spacers to increase the distance from the baseplate of the caster to the bottom of the platform If the caster is already lower than the wheels, you’ll... are not) For the prototype, I used 6-volt motors and a four-cell “D” battery holder Secure the battery holder(s) to the base with 8/32 nuts and bolts Drill holes to accommodate the hardware Be sure the nuts and bolts don’t extend too far below the base or they may drag when the robot moves Likewise, be sure the hardware doesn’t interfere with the batteries Wire the batteries and wheels to the DPDT through... DIAGRAM The wiring diagram in Fig 8 .4 allows you to control the movement of the Minibot in all directions This simple two-switch system, which will be used in many other projects in this book, uses double-pole, double-throw (DPDT) switches The switches called for in the circuit are spring-loaded so they return to a center-off position when you let go of them From Here To learn more about Read Wooden robots . 10/ 24 all-thread rod 1 6-inch-diameter (approx.) clear plastic dome 1 Four-cell AA battery holder Assorted 1/2-inch-by-8/32 bolts, 8/32 nuts, lock washers, 1/2-inch-by-10/ 24 bolts, 10/ 24 nuts,. should modify them by blunting the tip slightly (otherwise the tip may crack the plastic when it exits the other side). Continue the flute from the cutting lip all the way to the end of the bit (see. Data conver- sion statements are typically provided in strong data-typing programming languages to convert 8-, 1 6-, 3 2-, and (sometimes) 6 4- bit numbers from one form to another. If the pro- gramming

Ngày đăng: 10/08/2014, 04:22

Xem thêm: McGraw-Hill - The Robot Builder''''s Bonanza Part 4 ppsx

TỪ KHÓA LIÊN QUAN