McGraw-Hill - The Robot Builder''''s Bonanza Episode 2 Part 6 ppt

35 229 0
McGraw-Hill - The Robot Builder''''s Bonanza Episode 2 Part 6 ppt

Đ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

twice in this example, changes the state of a specified I/O line. Note the use of the con- stants. The syntax for PutPin is as follows: PutPin (PinNumber; Value) where PinNumber is the number of the pin you want to use (e.g., pin 25 for the red LED), and Value is either 1 for on (or logical HIGH) or 0 for off (or logical LOW). The Delay function causes the BX-24 to pause a brief while, in this case 70 millisec- onds. Delay is called twice, so there is a period of time between the on/off flashing of each LED: ' Green pulse. Call PutPin(GreenLED, LEDon) Call Delay(0.07) Call PutPin(GreenLED, LEDoff) Call Delay(0.07) The process is repeated for the green LED. Controlling RC Servos with the BX-24 You can easily control RC servos with the BX-24 using a few simple statements. While there is no built-in “servo command” as there is with the OOPic microcontroller (see Chapter 33), the procedure is nevertheless very easy to do in the BX-24. Here’s a basic pro- gram that places a servo connected to pin 20 of the BX-24 at its approximate mid-point position. (I say “approximate” because the mechanics of RC servos can differ between makes, models, and even individual units): Sub Main Do Call PulseOut(20, 1.5E-3, 1) Call Delay(0.02) Loop End Sub The program continuously runs because it’s within an infinite Do loop. The PulseOut statement sends a short 1.5-millisecond (ms) HIGH pulse to pin 20. The Delay statement causes the BX-24 to wait 20 milliseconds before the loop is repeated all over again. With a delay of 20 milliseconds, the loop will repeat 50 times a second (50 * 20 milliseconds ϭ 1000 milliseconds, or one second). Note the optional use of scientific notation for the second parameter of PulseOut. Using the value 0.0015 would yield the same result. You should be aware that the BX-24 supports two versions of the PulseOut statement: a float version and an integer version: ■ The float version is used with floating-point numbers, that is, numbers that have a decimal point. ■ The integer version is used with integers, that is, whole numbers only. CONTROLLING RC SERVOS WITH THE BX-24 511 Ch32_McComb 8/29/00 8:33 AM Page 511 The BX-24 compiler automatically determines which version to use based on the data format of the second parameter of the PulseOut statement. If you use Call PulseOut(20, 20, 1) it tells the BX-24 you want to send a pulse of 20 “units.” A unit is 1.085 microseconds long; 20 units would produce a very short pulse of only 21.7 microseconds. To continue working in more convenient milliseconds, be sure to use the decimal point: Call PulseOut(20, 0.020, 1) This creates a pulse of 20 milliseconds in length. Listing 32.2 shows a more elaborate servo control program and is based on an appli- cation note provided on the BasicX Web site. This program allows you to specify the position of the servo shaft as a value from 0 to 100, which makes it easier for you to use. LISTING 32.2 Const ServoPin As Byte = 20 Const RefreshPeriod As Single = 0.02 Const NSteps As Integer = 100 Dim SetPosition As Byte Dim Position As Single, PulseWidth As Single Sub Main () ' Moves a servo by sending a single pulse. ' Insert position as a value from 0 to 100 SetPosition = 50 ' move to mid-point Position = CSng(SetPosition) / CSng(NSteps) Do ' Translate position to pulse width, from 1.0 to 2.0 ms PulseWidth = 0.001 + (0.001 * Position) ' Generate a high-going pulse on the servo pin Call PulseOut(ServoPin, PulseWidth, 1) Call Delay(RefreshPeriod) Loop End Sub The five lines at the beginning of the program set up all the variables that are used. The line Const ServoPin As Byte = 20 creates a byte-sized constant and also defines the value of the constant as pin 20. Because it is a constant, the value assigned to ServoPin cannot be changed elsewhere in the pro- gram. Similarly, the lines Const RefreshPeriod As Single = 0.02 Const NSteps As Integer = 100 create the constants RefreshPeriod and NSteps. RefreshPeriod is a single-precision float- ing-point number, meaning that it can accept numbers to the right of the decimal point. Nsteps is an integer and can accept values from Ϫ32768 to ϩ32767. 512 USING THE BASICX MICROCONTROLLER Ch32_McComb 8/29/00 8:33 AM Page 512 The main body of the program begins with Sub Main. The statement SetPosition = 50 sets the desired position of the servo relative to the total number of steps defined in NSteps (in the case of our example, 100). Therefore, a SetPosition of 50 will move the servo to its approximate midpoint. The line Position = CSng(SetPosition) / CSng(NSteps) produces a value from 0.0 to 1.0, depending on the number you used for SetPosition. With a value of 50, the Position variable will contain 0.5. The Position variable is then used with- in the Do loop that follows. Within this loop are the following statements: PulseWidth = 0.001 + (0.001 * Position) Call PulseOut(ServoPin, PulseWidth, 1) Call Delay(RefreshPeriod) The first statement sets the pulse width, which is between 1.0 and 2.0 milliseconds. The PulseOut statement sends the pulse through the indicated servo pin (the third parameter, 1, specifies that the pulse is positive-going, or HIGH). Finally, the Delay statement delays the BX-24 for the RefreshPeriod, in this case 20 milliseconds (0.02 seconds). Reading Button Inputs and Controlling Outputs A common robotics application is reading an input, such as a button, and controlling an output, such as an LED, motor, or other real-world device. Listing 32.3 shows some sim- ple code that reads the value of a momentary push button switch connected to I/O pin 20. The switch is connected in a circuit, which is shown in Fig. 32.5, so when the switch is open, the BX-24 will register a 0 (LOW), and when it’s closed the BX-24 will regis- ter a 1 (HIGH). The instantaneous value of the switch is indicated in the LED. The LED will be off when the switch is open and on when it is closed. LISTING 32.3 Sub Main() Const InputPin As Byte = 20 Const LED As Byte = 26 Dim State as Byte Sub Main() Do ' Read I/O pin 20 State = GetPin(InputPin) ' Copy it to the LED Call PutPin(LED, State) READING BUTTON INPUTS AND CONTROLLING OUTPUTS 513 Ch32_McComb 8/29/00 8:33 AM Page 513 Loop End Sub Now let’s see how the program works. The lines, Const InputPin As Byte = 20 Const LED As Byte = 26 Dim State as Byte set the constant InputPin as I/O pin 20, and the constant LED as I/O pin 26. (Recall that one of the BX-24’s on-board LEDs—the green one, by the way—is connected to I/O pin 26.) Finally, the variable State is defined as type Byte: Do ' Read I/O pin 20 State = GetPin(InputPin) ' Copy it to the LED Call PutPin(LED, State) Loop The Do loop repeats the program over and over. The GetPin statement gets the current value of pin 20, which will either be LOW (0) or HIGH (1). The companion PutPin state- ment merely copies the state of the input pin to the LED. If the switch is open, the LED is off; if it’s closed, the LED is on. Additional BX-24 Examples So far we’ve just scratched the surface of the BX-24’s capabilities. But fear not: throughout this book are several real-world examples of BX-24 being using in robotic applications. For instance, in Chapter 41 you’ll learn how to use the BX-24 to interface to a sophisticated accelerometer sensor. In addition, you can find several application notes for the BX-24 (and its “sister” microcontrollers, such as the BX-01) on the BasicX Web page (www.basicx.com). 514 USING THE BASICX MICROCONTROLLER 10K To BasicX-24 I/O pin 20 +5 vdc (from BX-24 carrier board) (ground from BX-24 carrier board) FIGURE 32.5 Wire the switch so it con- nects to the Vϩ (pin 21, not pin 24) of the BX-24. The resistors are added for safety. Ch32_McComb 8/29/00 8:33 AM Page 514 From Here To learn more about… Read Stepper motors Chapter 19, “Working with Stepper Motors” How servo motors work Chapter 20, “Working with Servo Motors” Different approaches Chapter 28, “An Overview of Robot ‘Brains’” for adding brains to your robot Connecting the OOPic Chapter 29, “Interfacing with Computers and microcontroller to sensors Microcontrollers” and other electronics FROM HERE 515 Ch32_McComb 8/29/00 8:33 AM Page 515 This page intentionally left blank. While the Basic Stamp described in Chapter 31 is a favorite among robot enthusiasts, it is not the only game in town. Hardware designers who know how to program their own microcontrollers can create a customized robot brain using state-of-the-art devices such as the PIC16CXXX family or the Atmel AVR family of eight-bit RISC-based controllers. The reality, however, is that the average robot hobbyist lacks the programming skill and devel- opment time to invest in custom microcontroller design. Recognizing the large market for PIC alternatives, a number of companies have come out with Basic Stamp work-alikes. Some are pin-for-pin equivalents, and many cost less than the Stamp or offer incremental improvements. And a few have attempted to break the Basic Stamp mold completely by offering new and unique forms of programmable micro- controllers. One fresh face in the crowd is the OOPic (pronounced “OO-pick”). The OOPic uses object-oriented programming rather than the “procedural” PBasic programming found in the Basic Stamp. The OOPic—which is an acronym for Object-Oriented Programmable Integrated Circuit—is said to be the first programmable microcontroller that uses an object- oriented language. The language used by the OOPic is modeled after Microsoft’s popular Visual Basic. And, no, you don’t need Visual Basic on your computer to use the OOPic; the OOPic programming environment is completely stand-alone and available at no cost. The OOPic, shown in Fig. 33.1, has built-in support for 31 input/output (I/O) lines. With few exceptions, any of the lines can serve as any kind of hardware interface. What enables them to do this is what the OOPic documentation calls “hardware objects,” digital I/O lines 33 USING THE OOPIC MICROCONTROLLER 517 Ch33_McComb 9/7/00 1:43 PM Page 517 Copyright 2001 The McGraw-Hill Companies, Inc. Click Here for Terms of Use. that can be addressed individually or by nibble (4 bits), by byte (8 bits), or by word (16 bits). The OOPic also supports predefined objects that serve as analog-to-digital conver- sion inputs, serial inputs/outputs, pulse width modulation outputs, timers-counters, radio- controlled (R/C) servo controllers, and 4x4-matrix keypad inputs. The device can even be networked with other OOPics as well as with other components that support the Philips I2C network interface. The OOPic comes with a 4K EEPROM for storing programs, but memory can be expanded to 32K, which will hold some 32,000 instructions. The EEPROM is “hot swap- pable,” meaning that you can change EEPRPOM chips even while the OOPic is on and running. When a new EEPROM is inserted into the socket, the program stored in it is immediately started. Additional connectors are provided on the OOPic for add-ins such as floating- point math; precision data acquisition; a combination DTMF, modem, musical-tone generator; a digital thermometer; and even a voice synthesizer (currently under development). The OOPic’s hardware interface is an open system. The I2C interface specification, published by Philips, allows any IC that uses the I2C interface to “talk” to the OOPic. While the hardware capabilities of the OOPic are attractive, its main benefit is what it offers robot hackers: Much of the core functionality required for robot control is already embedded in the chip. This feature will save you time writing and testing your robot con- 518 USING THE OOPIC MICROCONTROLLER FIGURE 33.1 The OOPic supports 31 I/O lines and runs on 6–12 vdc power. Connectors are provided for the I/O lines, programming cable, memory sockets, and Philips I2C network. Ch33_McComb 9/7/00 1:43 PM Page 518 trol programs. Instead of needing several dozen lines of code to set up and operate an RC servo, you need only about four lines when programming the OOPic. A second important benefit of the OOPic is that its various hardware objects are mul- titasking, which means they run independently and concurrently of one another. For example, you might command a servo in your robot to go to a particular location. Just give the command in a single statement; your program is then free to activate other func- tions of your robot—such as move another servo, start the main drive motors, and so forth. Once started by your program, all of these functions are carried out autonomously by the objects embedded within the OOPic. This simplifies the task of programming and makes the OOPic capable of coordinating many hardware connections at the same time. Fig. 33.2 shows a fire-fighting robot that uses several networked OOPics as its main processor. This two-wheeled robot hunts down small fires and literally snuffs them out with a high-powered propeller fan. OBJECTS AND THE OOPIC 519 FIGURE 33.2 This fire-fighting robot, built by OOPic developer Scott Savage, uses three OOPics wired together in a network to control the machine’s central command, sensors, and locomotion. Ch33_McComb 9/7/00 1:43 PM Page 519 Objects and the OOPic Mention the term object-oriented programming to most folks and they freeze in terror. Okay, maybe that’s an exaggeration, but object-oriented programming seems like a black art to many, full of confusing words and complicated coding. Fortunately, the OOPic avoids the typical pit- falls of object-oriented programming. The OOPic chip supports an easy-to-use programming language modeled directly after Microsoft Visual Basic, so if you already know VB, you’ll be right at home with the OOPic. Future versions of the OOPic software development platform will support C and Java syntax for those programmers who prefer these languages. The OOPic VB-like language offers some 41 programming commands. That’s not many commands actually, but it’s important to remember that the OOPic doesn’t derive its flex- ibility from the Basic commands. Rather, the bulk of the chip’s functionality comes from its built-in 31 objects. Each of these objects has multiple properties, methods, and events. You manipulate the OOPic’s hardware objects by working with these properties, methods, and events. The Basic commands are used for program flow. Here’s a sample OOPic program written in the chip’s Basic language. I’ll review what each line does after the code sample. This short program flashes a red LED on and off once a second. Fig. 33.3 shows how to connect the LED and a current-limiting resistor to I/O line 1 (pin 7 on the I/O connector) of the OOPic. Dim RedLED As New oDio1 Sub Main() RedLED.IOLine = 1 RedLED.Direction = cvOutput Do RedLED.Value = OOPic.Hz1 Loop End Sub These lines comprise a complete, working program. Here’s the program broken down: Dim RedLED As New oDio1 The Dim statement creates a new instance of a particular kind of digital I/O object. This I/O object, referred to as oDio1, has already been defined within the OOPic. All of the behaviors of this object have been preprogrammed; your job is to select the behavior you want and activate the object. Note that all of the OOPic’s object names start with a lower- case letter O, such as oDio1, oServo,and oPWM. Sub Main() End Sub The main body of every OOPic program resides within a subroutine called Main. OOPic Basic permits you to add additional subroutines to your program, but every program must have a Main subroutine. As with Microsoft’s Visual Basic, you refer to subroutines by name. RedLED.IOLine = 1 RedLED.Direction = cvOutput 520 USING THE OOPIC MICROCONTROLLER Ch33_McComb 9/7/00 1:43 PM Page 520 [...]... 1-bit, 4-bit, 8-bit, or 1 6- bit blocks In the case of the 1-bit I/O object (named oDio1), the Value property of the object represents the electrical state of a single I/O line In the case of the remaining digital I/O objects, the Value property presents the binary value of all the lines of the group (4, 8, or 16, depending on the object used) There are 31 physical 1-bit I/O lines implemented within the. .. use the joystick teaching pendant software, Joystick.bas You’ll want to read Chapter 32 to learn more about the BasicX -2 4 chip and how it’s programmed Follow these steps: Ch34_McComb 8 /21 /00 3:31 PM Page 5 42 5 42 REMOTE CONTROL SYSTEMS 1 Insert the BasicX -2 4 chip into a suitable carrier 2 Attach the serial programming cable between your PC and the BasicX -2 4 carrier 3 Connect a joystick to the BasicX -2 4 ,... the other direction when their Value property is 63 Note that in Listing 33 .2 the “normal” direction of travel for servo 2 (S2) is reversed from S1, with the following statement: S2.InvertOut = cvTrue This is handy because in the two-wheeled robot the servos are mounted on opposite sides, and therefore one motor must operate in mirror image to the other That is, one must turn clockwise while the other... property of the oA2D object initiates the conversion, and the Value property is updated with the result of the conversion When the Operate value of the oA2D object is 1, the analog-to-digital conversion, along with the Value update, occurs repeatedly Conversion ceases when the Operate property is changed to 0 There are four physical analog-to-digital circuits implemented within the OOPic They are available... Therefore, it is only necessary to specify the name of the object and the value you want for it: S1 = 0 This sets the servo all the way in one direction, and the following expression, S1 = 63 sets the servo all the way in the other direction Because the Value property is the default for the oServo object, the statement S1 ϭ 63 is the same as writing S1.Value ϭ 63 Exercise care when playing around with servos... ribbon 4 5 6 7 8 cables that have the appropriate header connectors I used a ribbon cable originally designed for use in PCs It is outfitted with the proper DB-15 connector for the joystick on one end and a 1 6- pin dual-row male header on the other Apply power to the BX -2 4 Create a new project (Joystick2.Bxp) and be sure to add the SerialPort.Bas file as one of its files Write the Joystick2.Bas program... interface for the infrared receiver-demodulator Note the electrolytic capacitor and pull-up resistor These are required for proper operation You’ll find the circuit will work better if you solder the ground lead to the metal case of the receiverdemodulator (if it is so equipped) Keep lead lengths short PROGRAMMING THE BX -2 4 Listing 34 .2 shows the demonstration program to be used with the BX -2 4 microcontroller... acting as the drive motors for a two-wheeled robot You can easily construct a demonstrator robot using LEGO parts, like the prototype shown in Fig 33 .6 I cemented two light- FIGURE 33 .6 You can construct a demonstrator for the OOPic two-wheel robot using LEGO bricks The servos are glued to small LEGO parts to aid in mounting Ch33_McComb 9/7/00 1:43 PM Page 529 OPERATING MODIFIED SERVOS 529 weight... Hz Hz60 1-bit value that cycles every 60 Hz Node Used when two or more OOPics “talk” to each other via the I2C network A Node value of more than 0 is the OOPic’s I2C network address Operate Specifies the power mode of the OOPic Pause Specifies if the program flow is suspended PullUp Specifies the state of the internal pull-up resisters on I/O lines 8 –15 Reset Resets the OOPic StartStat Indicates the. .. One is to increase the number of steps per second This is done by decreasing the value as follows: mdDelay As Integer = 25 6 The value 25 6 is approximately one half of a second, so 128 would be a quarter second, 64 would be an eighth of a second, and so forth Be aware that the smaller the number is, the more steps are recorded per second, so the 60 -element array will fill up faster Another enhancement . to the right of the decimal point. Nsteps is an integer and can accept values from Ϫ 327 68 to ϩ 327 67 . 5 12 USING THE BASICX MICROCONTROLLER Ch 32_ McComb 8 /29 /00 8:33 AM Page 5 12 The main body of the. USING THE BASICX MICROCONTROLLER 10K To BasicX -2 4 I/O pin 20 +5 vdc (from BX -2 4 carrier board) (ground from BX -2 4 carrier board) FIGURE 32. 5 Wire the switch so it con- nects to the Vϩ (pin 21 , not. InputPin as I/O pin 20 , and the constant LED as I/O pin 26 . (Recall that one of the BX -2 4 ’s on-board LEDs the green one, by the way—is connected to I/O pin 26 .) Finally, the variable State is defined

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

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan