elektor dvd 1990-1999 - cdn.eleinfo.ircdn.eleinfo.ir/el1990-1999/pdf/1999/e994054.pdf · elektor...

6
Elektor Electronics 4/99 Cybernetic models (electronically pow- ered systems simu- lating living organ- isms) were pub- lished on several occasions in Elektor Electronics. How- ever, in retrospect these systems were complex, slow and bulky. The availability of advanced microcontroller has made it much eas- ier to build an small vehicle doted with a basic amount of artificial intel- ligence (AI). The LightFinder described in this article is a fine edu- cational project, with a V.1 BASIC Stamp from Parallax acting as the ‘brains’. The project also comprises a compact printed circuit board con- taining all I/O components, two Lego motors with built-in reduction gear, and some Lego building blocks. Throw it all together and you are look- ing at a miniature robot vehicle capa- ble of finding its way to a light source, or (by reprogramming), avoid a light source, despite obstacles in its path. Thanks to the use of the BASIC Stamp as the heart of the system, the behaviour of the LightFinder is readily changed to meet your own require- ments. No knowledge of source code programming is required, the Stamp module is programmed in BASIC using your PC. A compact and pro- grammable little robot vehicle may be built from ingredi- ents like the famous BASIC Stamp, assorted electronic parts, and Lego bricks and motors. The LightFinder described in this arti- cle proves that intel- ligence is the key- word in elementary cybernetics. 54 Design by G. Nöcker LightFinder a Stamp-powered Lego vehicle Specification 9-V battery powered Two motors Two front/side contact switches Two light sensors Two infrared light sensors Programmable in BASIC 256 bytes program code Program written on PC GENERAL INTEREST

Upload: dangnga

Post on 27-Feb-2018

327 views

Category:

Documents


13 download

TRANSCRIPT

Page 1: ELEKTOR DVD 1990-1999 - cdn.eleinfo.ircdn.eleinfo.ir/el1990-1999/pdf/1999/e994054.pdf · Elektor Electronics 4/99 Cybernetic models (electronically pow-ered systems simu-lating living

Elektor Electronics 4/99

Cybernetic models(electronically pow-ered systems simu-lating living organ-isms) were pub-lished on severaloccasions in ElektorElectronics. How-ever, in retrospect

these systems were complex, slow andbulky. The availability of advancedmicrocontroller has made it much eas-ier to build an small vehicle dotedwith a basic amount of artificial intel-ligence (AI). The LightFinderdescribed in this article is a fine edu-cational project, with a V.1 BASICStamp from Parallax acting as the

‘brains’. The project also comprises acompact printed circuit board con-taining all I/O components, two Legomotors with built-in reduction gear,and some Lego building blocks.Throw it all together and you are look-ing at a miniature robot vehicle capa-ble of finding its way to a light source,or (by reprogramming), avoid a lightsource, despite obstacles in its path.

Thanks to the use of the BASICStamp as the heart of the system, thebehaviour of the LightFinder is readilychanged to meet your own require-ments. No knowledge of source codeprogramming is required, the Stampmodule is programmed in BASIC usingyour PC.

A compact and pro-grammable little

robot vehicle may bebuilt from ingredi-

ents like the famousBASIC Stamp,

assorted electronicparts, and Lego

bricks and motors.The LightFinder

described in this arti-cle proves that intel-

ligence is the key-word in elementary

cybernetics.

54

Design by G. Nöcker

LightFindera Stamp-powered Lego vehicle

Specification 9-V battery powered Two motors Two front/side contact switches Two light sensors Two infrared light sensors Programmable in BASIC 256 bytes program code Program written on PC

GENERAL INTEREST

Page 2: ELEKTOR DVD 1990-1999 - cdn.eleinfo.ircdn.eleinfo.ir/el1990-1999/pdf/1999/e994054.pdf · Elektor Electronics 4/99 Cybernetic models (electronically pow-ered systems simu-lating living

S T R U C T U R EThe block diagram shown in Figure 1shows the elementary structure of theLightFinder cybernetic model. Thevehicle is powered by two compact dcmotors with internal reduction gear.The sensors are a pair of LDRs (light-dependent resistors) for light/darkdetection, a pair of infrared sensors fordistance measurement, and a pair ofswitches to detect obstacles in the way.

The LightFinder’s intelligence isvested in a ready-made BASIC StampVersion 1 module from Parallax, Inc.The LightFinder communicates withyou via beeping sounds from its on-board piezo-buzzer. The Lego motorsare driven by an integrated motor dri-ver IC, which is also shown in theblock diagram.

The two infra-redsensitive ‘eyes’ of theLightFinder are prettyadvanced electronic cir-cuits capable of detect-ing objects by illuminat-ing them with a modulated infra-redlight source. Depending on the sensi-tivity set by the user, these ‘eyes’ arecapable of detecting large obstacles ata distance of about 20 cm.

P R A C T I C A L C I R C U I TThe circuit diagram of the LightFinder’scontrol system is shown in Figure 2.The heart of the circuit is formed by the

PIC-based BASICStamp connected toK5. Because theBASIC Stamp is used

as a ready-made module in this project,its operation is not discussed in this arti-cle. All electronic circuits that link theprocessor to the real world is connectedto the pins of K5. Because of the proces-sor’s rather limited I/O capacity,switches S1 and S2 areconnected to an ana-logue input, PC4, viaa voltage divider. The

processor is capable of measuring thevalue of a resistor, potentiometer orLDR by connecting the relevant resis-tance in series with a capacitor, and thenestablish the RC time produced by thecombination. Because the RC time mea-sured on PC4 will change when S1 isclosed (R = 16.8kΩ) or S2 is closed (R =28.8 kΩ), the system software is capableof detecting whether the left-hand or

right-handb u m p e rswitch wasclosed by

55Elektor Electronics 4/99

M

M

IR

LDRLDR

IR 990035 - 11

BASIC Stamp

L293D

IS471F IS471F

Figure 1. Block diagramof the LightFinder cyber-netic model.

1

K5

1

2

1IC1a

L293D

IC2

OUT1

OUT2

OUT3

OUT4

EN2 EN1

IN1

IN2

IN3

IN4

16

12 13

10

15

11

14

19

8

4 5

2

7

3

6

A B

5

6

1IC1c

M1

MM2

M

C9

10µ 25V

C8

10µ 25V

C1

100n

C2

100n

K4

Bz1

PCO

PCI

RES

P0

P1

P2

P3

P4

P5

P6

P7

D2

IR

250Ω

P2

C5

330n

D1

IR

250Ω

P1

C7

330n

RL

RL

R5

LDR

C3

100n

R4

LDR

C4

100n

RL

K6K7

R1

22k

R2

10k

R3

6k

8

K8K9

C6

100n

S1S2

K3

C10

100n

C11

100n

C12

100µ16V

78L05

IC3

RL

9 81

IC1d

11 101

IC1e

13 121

IC1f

3 41

IC1b

IC1

14

7

IC1 = 74HC04

5V9V6

9V6 5V

9V6 5V

5V

9V6

BA

SIC

ST

AM

P

990035 -- 12

5V5V

IS471F

K2

2

3

4

1

IS471F

K1

2

3

4

1

IS471F

12

34

1

2

3

4

1

Figure 2. The circuit diagram ofthe LightFinder’s electronics is nomore than a collection of sensors.All computing power resides inthe BASIC Stamp module.

Page 3: ELEKTOR DVD 1990-1999 - cdn.eleinfo.ircdn.eleinfo.ir/el1990-1999/pdf/1999/e994054.pdf · Elektor Electronics 4/99 Cybernetic models (electronically pow-ered systems simu-lating living

the LightFinder run-ning into an obstacle.When both switches areopen, the Stamp mea-sures a resistance of38.8 kΩ. The two LDRs are ‘read’ in thesame way by connecting them in serieswith a 100-nF capacitor.

I N F R A R E D E Y E SThe LightFinder uses an ingeniousmethod to determine its distance froma large obstacle such as a wall. Infraredlight is employed for this function, inparticular, an IS471F integrated circuitfrom Sharp. The block diagram of thisIC is shown in Figure 3. More techni-cal details, in particular, on the opto-electrical characteristics, may be foundon this month’s Datasheets. The IS471Fcontains a complete modulator,demodulator, voltage regulator and anoscillator. The latter is used to drive anexternal infrared emitting diode (IRED)via a buffer. The light emitted by theIRED is detected again by the on-chipreceiver diode. The resulting signal isfirst amplified and then converted intoa digital pulse-shaped signal by meansof a comparator. The integrated syn-chronisation detector and demodula-tor circuit then compare the receivedsignal with the pulse signal applied tothe sender diode. If thetwo signals are equal,the digital output of the

IC (pin 2) is enabled.By adapting the resis-tance in series withthe IRED, the ‘trans-mit power’ may be

increased or decreased. In this way, theoptical sensitivity may be controlledwithin certain limits. The configurationused in the circuit allows a large objectat a distance of about 20 cm to bedetected without problems.

The circuit may be powered by volt-ages between 4.5 V and 16 V, so thatbattery voltages of 9 V or 12 V are goodoptions. The pulse repeat time is typi-cally 130 (s (approx. 8 kHz), and thepulse on-time is 8 (s, so that the batteryis used economically. The LightFinderhas two infra-red eyes so that objects ateither side of the vehicle may bedetected.

The buzzer is a simple piezo-electricbeeper. It is controlled via port line P2of the BASIC Stamp. The tone fre-quency is programmable over a widerange: approx. 95 Hz to over 10 kHz.

The integrated motor driver, a TypeL293 from SGS-Thomson (and secondsources) allows the hardware to bereduced considerably. As shown by theinternal diagram in Figure 4, the ICcontains four power drivers. These arecapable of supplying a continuous

direct current of600 mA or 1.2 A peak.On-chip flyback

diodes protect the IC outputs againstvoltage surges caused by the switchingof inductive loads (such as the motorcoils). Because the inputs of the L293are TTL compatible, they can bedirectly linked to the microcontrolleroutputs. An on-chip thermal protec-tion circuit disables the buffer if anoverload condition arises.

The two motors of the LightFinder,M1 and M2, are totally controlled byjust three processor control lines. Portlines P0, P1 and P2 form the linkbetween the controller and the motordriver. P0 carries the common enablesignal for the Lego motors. If the enableline is inactive (low), the power driversin the L293 are switched off, or, moreprecisely, their outputs are switched tohigh-impedance. In this state the cur-rent consumption is minimized.

Port lines P1 and P2 convey themotor direction information. Becausethe buffers are set up in a bridge con-figuration, an inverter is includedbetween the two drivers assigned toone motor. This is done to make surethat the two motor terminals arealways polarized or, of course, at 0 V.

The power supply has been kept assimple as possible, with all parts capableof working at 9 V connected directly tothe battery. The motor driver and theBASIC Stamp can only work at 5 V, andthey receive their supply voltage fromregulator IC3. Attentive readers mayhave noticed that the circuit diagramindicates a supply voltage of 9.6 V. Thereason is that this voltage is suppliedby some rechargeable batteries that canbe used as substitutes for 9-V blocks.Rechargeable batteries were success-fully used during the entire develop-ment period of the LightFinder, withexcellent results, and to stay keep ourgovernors happy as far as budgets areconcerned! In use, the circuit drawsmore than 100 mA when the vehicle ison the move.

E L E C T R O N I CC O N S T R U C T I O NThe copper track layout and compo-nent overlay of the PCB designed for

56 Elektor Electronics 4/99

3

Figure 3. Internal struc-ture of the IS471F fromSharp. Two of thesesensors act as theLightFinder’s ‘eyes’.

OUT1

OUT2

OUT3

OUT4

3 11 168

2IN1

1ENABLE 1

1

2

VS VSS

VS

3

47

10

9

15

4,5,12,13 6 14

IN2

IN3

ENABLE 2

IN4

VS

VS

VS

L293D

990035 - 14

4

Figure 4. A special IC,the L293D, is used todrive the motors.

Page 4: ELEKTOR DVD 1990-1999 - cdn.eleinfo.ircdn.eleinfo.ir/el1990-1999/pdf/1999/e994054.pdf · Elektor Electronics 4/99 Cybernetic models (electronically pow-ered systems simu-lating living

the LightFinder may be found in Fig-ure 5. This board is available ready-made through the Publishers’ ReadersServices. It is plain to see that his boardhas to be cut in two, and we recom-mend doing this first. The two bumperswitches S1 and S2 are fitted on theboard with holes only. The photographof the prototype shows the practicalrealization. Two pieces of metal wireare used to make the actual ‘bumpers’that serve to extend the contact rangeof the switches. The wires are cut andbent such that they extend a littlebeyond the wheels. In this way, the

bumpers allow obsta-cles beside the vehicleto be detected also.

Using two pieces ofmassive wire, theboard holding theswitches is fitted atright angles on to the main board.

The BASIC Stamp is inserted in SILsocket K5 — the component sidepoints in the direction of IC2. TheStamp computer may be programmedlater using the Parallax programmingcable hooked up to the PC and 3-waySIL header K4.

Pay attention when mounting thesensors on to K1 and K2. As indicatedby the component overlay, the pro-truding part of the case should point tothe rear of the vehicle. The infraredlight produced by the associated IRED(D1 or D2) then reaches the sensor facevia the reflecting object. The behaviourof the IR sensors may be fine tuned as

follows: with the motorsdisconnected, set theIRED power presets(P1, P2) to maximumresistance. Then mea-sure the voltage at thesensor output (pin 3).

Next, place a fairly large obstacle at thedesired distance from the sensor, andturn the relevant preset (P1, P2), untilthe output goes low. Turn the sensorssideways a little — they should notlook straight ahead, and horizontallythey should remain in line with theassociated IRED.

M E C H A N I C A LC O N S T R U C T I O NThe prototype of the LightFinder wasbuilt mainly from Lego bricks andother parts. The vehicle is powered bytwo compact 9-volt dc. motors withinternal gear reduction boxes. The‘floor plate’, nose wheel and battery

57Elektor Electronics 4/99

Figure 5. Copper tracklayout and componentmounting plan of theLightFinder PCB(board available ready-made).

990035-1

(C) ELEKTOR

BZ1

C1

C2

C3

C4C5

C6C7

C8C9

C10

C11

C12

D1

D2

IC1

IC2

IC3

K1

K2

K3

K4

K5

K6

K7

K8

K9

M1

M2

P1

P2

R1

R2

R3R

4R

5

990035-1

-+

990035-1

(C) E

LEK

TOR

5COMPONENTS LIST

Resistors:R1 = 22kΩR2 = 10kΩR3 = 6kΩ8R4,R5 = LDRP1,P2 = 250Ω preset, vertical

Capacitors:C1-C4,C6,C10,C11 = 100nF, raster

5mmC5,C7 = 330nFC8,C9 = 10µF 25V radialC12 = 100µF 16V radial

Semiconductors:D1,D2 = IRED, e.g. LD271IC1 = 74HC04IC2 = L293DIC3 = 78L05

Miscellaneous:Bz1 = piezo buzzer (passive)K1,K2 = IS471F (Sharp)K3,K6-K9 = 2-way SIL socketK4 = 3-way SIL headerK5 = 14-way SIL socketM1,M2 = motor, see inset on Lego

partsS1,S2 = microswitch, 1 make con-

tactBASIC Stamp, V. 1 (Milford Instru-

ments)PCB, order code 990035-1, see

Readers Services page

Lego bricks &other partsThe version of the LightFinder as shown on this month’sfront cover was built from standard Lego bricks and parts.Because some of the special parts may be difficult to findindividually, an overview is given of the relevant ordercodes. Further information may be found on Lego’s won-derful website at www.lego.com.

Micro motor: 5119Small low-profile tyres: 5268Nose wheel: 5050Hinge: 5388Cross axles: 5226Plates with wires: 5311

9V battery box: 5391

Items 5050, 5388 and 5226 are part of the Plane Acces-sories Set.It should be noted that two motors and two cable sets haveto be ordered. In addition to these components you willalso require a base plate and a few standard bricks. Any-one who has ever worked with Lego should have theseavailable.For a copy of the Lego Service brochure, or informationon Mail Order Service for Lego sets, contact

Consumer ServiceLEGO UK LimitedRuthin RoadWrexham LL13 7TQMain Switchboard Number: (01978) 290900

Page 5: ELEKTOR DVD 1990-1999 - cdn.eleinfo.ircdn.eleinfo.ir/el1990-1999/pdf/1999/e994054.pdf · Elektor Electronics 4/99 Cybernetic models (electronically pow-ered systems simu-lating living

box are also from the Lego ‘Technic’series. This month’s cover photographshows the result of one evening of‘vehicle building for the young’. If youwant to copy our construction, the rel-evant parts descriptions and Legoorder numbers may be found in theinset.

Users of Meccano, Knex or FischerTechnic parts may easily produce simi-lar constructions of the vehicle, thegeneral structure being uncritical. If thevehicle is made much larger or fromdifferent parts (metal), it will probablybecome so heavy that you have toresort to six (rechargeable) AA-size bat-teries instead of the 9-V PP3 (6F22)block. Non-rechargeable AA batterieshave a much larger capacity than a 9-VPP3 (up to 1500 mAh), and they willtypically last longer.

Other motors may be used, as longas their current consumption does notexceed the 600-mA limit imposed bythe power drivers in the L293.

Special connecting cables are avail-able for the Lego motors. If these arecut, you have two connecting cables,which may be soldered in the desig-nated sockets (M1 and M2).

The wires of the LDRs will be longenough to give these devices a far viewwhen positioned high on the vehicle.Be sure to isolate the wires, though,and bend them carefully only if youneed to adjust the viewing direction.The choice of the LDRs is not particu-larly critical. If necessary, the ‘Alt-P’option in the control software(Stamp.exe) may be used to adapt thesensitivity to the LDR characteristics(see the Stamp manual for furtherdetails).

S O F T W A R EThe BASIC control program developedfor our prototype of the LightFinder islisted in Figure 6. This listing isintended to get you started. Becausethe program is not too extensive, it iseasily typed into the Editor utility ofthe Stamp.exe program.

The BASIC Stamp kit comes withprogramming tools, a programmingcable, a manual and a carrier board.This kit may be ordered directly fromParallax, or through authorized dis-tributors. Readers in the UK shouldcontact Milford Instruments, SouthMilford, Leeds, tel. (01977) 683665, fax(01977) 681465, [email protected], web site atwww.milinst.demon.co.uk.

There is a cheaper alternative to thecomplete kit, however: The Parallaxweb site at www.parallaxinc.com allowsyou to download the complete Stampmanual as a ‘pdf ’ (Adobe AcrobatReader) file, as well as the develop-ment software. If you feel you are ableto make your own programming cable,then all you have to order is one BASIC

58 Elektor Electronics 4/99

'-----------------------------------------------------------------' Program to control a LEGO-robot (type NANOBOT)'' NANO_BOT.BAS 08.03.98' Author Gerhard Nöcker' 1998 Copyright Elektor, Beek (L), The Netherlands'-----------------------------------------------------------------

'----- Port Pin Assignment ---------------------------------------'Port Function Type'Pin 0 Motor current 0/1 == on/off DigOut'Pin 1 Right motor 0/1 == back/fw DigOut'Pin 2 Left motor (buzzer) 0/1 == back/fw DigOut (sound)'Pin 7 Switch right/left AnIn'Pin 4 Right IR obstacle det. 0/1 == obstacle/free DigIn'Pin 5 Right LDR AnIn'Pin 6 Left LDR AnIn'Pin 3 Left IR obstacle det. 0/1 == obstacle/free DigIn'----- Remark ----------------------------------------------------'Pin 2 is also used to produce sounds. Both motors will be halted.'The direction bit of the left motor controls the buzzer.'There are four different sounds:'Obstacle on the right side: high/low'Obstacle on the left side: low/high'Too much light: high'Too dark: low

'----- Declaration of constants ----------------------------------symbol sw_pin = 7 'switch input at port pin 7symbol sw_scale = 33 'switch scale factorsymbol ri_ldr = 5symbol ri_ldr_sc = 82symbol le_ldr = 6symbol le_ldr_sc = 82symbol l_thres = 15symbol halt = 0symbol fwd = 7symbol backw = 1symbol Right = 5symbol Left = 3

'----- Declaration of variables ----------------------------------symbol r_sw = bit0 'storage for right switch (on/off)symbol l_sw = bit1 'storage for left switch (on/off)symbol f_flag = bit2 'NOT USEDsymbol b_flag = bit3 'symbol l_flag = bit4symbol sw_val = b4 'storage for analog switch valuesymbol lw_re = b5symbol lw_li = b6symbol duration = w4symbol Rnd = w5

'----- Program start ---------------------------------------------dirs=%00000111 'port bit 7..3 input, bit 2..0 outputr_sw = 0l_sw = 0

'' If a switch is opened/closed at power up the robot will' search/avoid light.'

pot sw_pin, sw_scale, sw_val 'read switchesb_flag = 1If sw_val < 240 Then lbl1goto loop

lbl1: b_flag = 0l_flag = 0

'----- Program main loop -----------------------------------------Loop: pot ri_ldr, ri_ldr_sc, lw_re 'read right LDR

pot le_ldr, le_ldr_sc, lw_li 'read left LDR

6Figure 6. Listing of the BASIC pro-gram that makes the LightFindercome alive.

Page 6: ELEKTOR DVD 1990-1999 - cdn.eleinfo.ircdn.eleinfo.ir/el1990-1999/pdf/1999/e994054.pdf · Elektor Electronics 4/99 Cybernetic models (electronically pow-ered systems simu-lating living

Stamp module Version 1. The BASIC Stamp is ready for pro-

gramming once the control programhas been typed in using Stamp.exe, theprogramming cable is connected atboth ends (LightFinder and PC), andthe LightFinder is switched on. Notethat Stamp.exe is a DOS programwhose Upload function will only workproperly in ‘real DOS’ mode (i.e., not ina Windows DOS box).

At this point give your cyberneticmodel a test spin — see if it can find alight source! If you are not satisfiedwith its behaviour, the control programmay be modified to make the modelrespond differently to the various sig-nals it is capable of detecting. If youhave ideas for improvements, let usknow!

(990035-1)

59Elektor Electronics 4/99

lw_re=lw_re min l_threslw_li=lw_li min l_thresb8 = lw_li - l_thresb9 = lw_re - l_thresIf lw_re < 200 Then clearfl

cont0:If lw_re < b8 And b_flag = 0 Then turn_l 'compare lightIf lw_li < b9 And b_flag = 0 Then turn_r 'andIf lw_re < b8 And b_flag = 1 Then turn_r 'behaviourIf lw_li < b9 And b_flag = 1 Then turn_lIf lw_re = l_thres And lw_li = l_thres Then turn 'too much lightIf lw_re = 255 And lw_li = 255 And l_flag = 0 Then wait 'too dark

forw: pins = fwdGoTo switch

turn_r: pins = Rightgoto loop

turn_l: pins = Leftgoto loop

switch: 'switches and IR obstacle detectionpot sw_pin, sw_scale, sw_val 'read switchesIf sw_val > 180 And sw_val < 240 Or pin3 = 0 Then slIf sw_val < 180 Or pin4 = 0 Then srIf sw_val > 240 Then Loop

sl: r_sw = 0l_sw = 1GoTo cont1

sr: r_sw = 1l_sw = 0

cont1:pins = haltpause 1000pins = backwpause 1500pins = haltIf l_sw = 1 Then cont2sound 2,(123,40,110,40)pins = LeftGoTo cont3

cont2:sound 2,(110,40,123,40)pins = Right

cont3:random Rndduration = 1000 + b10pause durationpins = haltpause 1000goto loop

turn:pins = haltsound 2,(125,30)pins = Rightduration = 2800pause durationpins = haltpins = fwdpause 3000GoTo switch

wait:pins = haltsound 2,(80,30)pause 10000l_flag = 1GoTo switch

clearfl:l_flag = 0GoTo cont0

Figure 7. Photo-graph of our proto-type. The mechani-cal constructionconsists of Legobricks and parts.