Home
TSL1401-DB (#28317): Linescan Camera Module
Contents
1. ao PIN 0 TSL1401R s analog output threhsolded by Stamp si PIN 1 GESEIS ONR SI qam alike PIN 2 MIS LACIIRY SS CINK Ostia i SSeS io gorns bars MEE EE EERS EE EES EE EE EE EER EES EE EE DRK CON 0 Value assignd to which for dark pixels BRT CON 1 Value assigned to which for bright pixels FWD CON 0 Value assigned to dir for left to right search BKWD CON Value assigned to dir for right to left search E Veriasles J e SS REDE SS SS SS SS SS SSS VariableName VAR Byte What is variable for pdata VAR Word 8 Pixel data as acquired LSB first from sensor pixels VAR pdata BITO 128 bit pixel array mapped onto pdata exp VAR Word Exposure integration time in 2uSec units lip er VAR Byte Left pixel pointer for count and find operations rptr VAR Byte Right pixel pointer for count and find operations al VAR Byte General purpose index eins VAR Byte Result of pixel count routine which VAR Bit Indicates seeking DRK or BRT pixels edges lake VAR Bit Indicates direction of search FWD or BKWD found VAR Bit Indicates pixels found 1 or not found 0 ig A OABEOSEANMESAS s c ER SE ee ae oe ee CUT NOTE This code assumes that DEBUG will wrap after 128 characters regardless of the DEBUG window width Later versions of DEBUG may not do this and you will have to add CRs where needed exp 8333 Set exp
2. IF dir FWD THEN Yes Search left to right NOIR Iboicre pis WO ioc Yes Loop forward IF pixels lptr which THEN RETURN Return on match NEXT ELSE HORSE ap ere IE er No Loop backward IF pixels rptr which THEN RETURN Return on match NEXT ENDIF ENDIF found 0 MDatcla Vic Look oue Me said de me RETURN Return I ue GEGEE RSS SS SSS SS EE SS SS SS SS SSE Count pixels within the range lptr through rptr of the type indicated by U wmm 0 ghe dL Xm s CountPix cnt 0 metale COMME s IF lptr lt rptr AND rptr lt 127 THEN Valid range IMO a Mjeitie INO par Yes Loop over desired rang We Gorse aL hle Wels me ume ap od vU Add to count when pixel matches NEXT ENDIF RETURN Pseudo analog Pixel Acquisition Even though pixels acquired by the BASIC Stamp are thresholded and converted to single bits it s still possible to obtain a picture of each pixel s analog value at least for static subjects that don t move The voltage output from any given pixel in the TSL1401R can be expressed as follows Output voltage k x LightIntensity x IntegrationTime Where k is a constant At the two volt threshold level this can be rewritten 2 k x LightIntensity x IntegrationTime What we want this formula to answer is this For a given light intensity to produce an output at the two volt threshold how long does the integration time have to be Solving the ab
3. The location information ranges from O to 127 This is because we re dealing with grayscale pixels here and there are only 128 of them whose locations are zero based Binary pixels by contrast number from 1 to 255 The information provided by these stats can be used for example to adjust the exposure time to maintain a constant maximum pixel intensity An example of this technique is shown in a later section In addition to buffering pixels and stats the acquire commands initialize the internal Left and Right pointers for counting a searching to 1 and 255 respectively Counting Pixels and Edges One of the simplest methods of analyzing a binary image is to determine how many pixels are bright and how many are dark This information alone can tell you an object s width for example a conveyor s degree of coverage or whether a bottle cap is present The commands that performs this task are CNTNEW and CNTNXT Bueno Systems Inc e TSL1401 DB 2009 10 01 Page 33 of 52 CNTNEW Modifiers Begin End CNTNXT Modifiers CNTNEW is a constant defined in the template as C8 The CNTNEW command counts pixels or edges between pixels Begin and End inclusive Begin and End can range from 1 to 255 and Begin should be less than or equal to End Remember that binary pixel locations are one based not zero based The first pixel is pixel 1 CNTNXT CO counts pixels or edges between the current internal Left and R
4. 20 And back for another scan So that s it a complete inspection and reporting program in 20 lines of code But let s take it one step further One technique where buffering really shines is when it s combined with externally triggered exposures This makes it possible to buffer an entire acquisition and analysis sequence and to begin execution immediately But nothing will happen until a falling edge on P3 is detected whereupon the driver firmware springs into action by itself snaps the picture and does the analysis All the PBASIC program has to do is call Ready when t ready to see if new results are available In fact if further processing of the results read from the firmware is required we can even arm the firmware ahead of that processing in order to overlap processing one image with acquiring the next To demonstrate this technique we re going to change the mission slightly Instead of looking for each bagel s maximum width we re going to measure each one to compute the overall area covered by the bagel including the hole For measurement consistency we ve installed an encoder on our hypothetical conveyor that issues a pulse every quarter inch of travel So if we measure the overall width of each bagel every quarter of an inch of travel and accumulate the sum of those widths we will have measured its area once it has passed Bueno Systems Inc e TSL1401 DB 2009 10 01 Page 41 of 52 Here s one way to
5. Bueno Systems Inc TSL1401 DB 2009 10 01 Page 15 of 52 Cosine Effect You may have noticed in the images above that the center of the image is brighter than the edges This is an optical property that s present in nearly all imaging systems It s known as the cosine effect and it makes images appear brighter near their centers than at the edges This happens because a light emitter such as a diffuse backlight is brighter on axis than off axis Since the edges of a flat light source are captured more off axis than the center they appear darker Compounding the effect is the fact that behind the lens light striking the sensor at the edges comes in at a more oblique angle than light striking the center This effect becomes more pronounced as the imaging lens s focal length decreases i e becomes more wide angle Here s a scan of just the backlight without anything in front of it Even though the backlight itself is very evenly illuminated it appears to have a cosine shaped brightness contour when imaged with the camera mmm O Image from Backlight Only Showing the Cosine Effect Different ways of dealing with this effect are discussed later in the section Image Analysis and Measurment Note This also illustrates the importance of keeping everything clean Do you see that little divot in the trace about two thirds of the way across It was caused by a tiny lint fiber clinging to the sensor chip If you see somet
6. Lighting No discussion of machine vision would be complete without an entire chapter on lighting The fact is that any vision application will succeed or fail based on how well you are able to control the lighting If you try to use ambient lighting you will almost surely fail in your endeavor It really is that simple Fortunately there are ample resources on the internet that cover this important topic A few of the better ones can be found at these URLs e Melles Griot http www mellesgriot com products machinevision lif 1 htm e Advanced Illumination http advill com uploads downloads A 20Practical 20Guide 20to 20Machine 20Vision 20 Lighting pdf e Edmund Scientific http www edmundoptics com techSupport DisplayArticle cfm articleid 264 e Vision amp Sensors Magazine http www visionsensorsmag com CDA Articles Cover Story BNP GUID 9 5 2006 A 10000000000000097315 Schematic 5V Vdd NH Vdd U1 ZN TSL1401R LF SI 8 CLK Vss AO vss 5 1 2 3 v4 zR2 Vdd 7 330R T4LVC1G14 Q1 5V rv N N RI gt 4 7K J2 Bueno Systems Inc TSL1401 DB 2009 10 01 Page 50 of 52 Index RE EE 39 49 AE EE N 39 40 49 A ACOAND ese ite ES Ee Ee EE 30 33 47 49 ACQANDNOT eH 30 47 ACQBIN 24 30 31 33 37 38 39
7. usueuss 4 27 P PBASIC 6 7 8 13 15 17 20 24 25 26 30 31 36 41 42 46 Pixel drop erre 31 PIXELS eit dietetic aie ens 33 Pseudo analog pixel acquisition 11 R Ready busy polling ees 24 Reset 25 26 27 30 34 35 36 38 39 41 44 47 48 49 RESULTS 25 27 34 35 37 38 39 40 41 42 43 47 SchematiE se VS eres cte e cuv sees ee vga 50 SETBIN 28 29 31 37 38 39 40 42 43 46 49 Bueno Systems Inc TSL1401 DB 2009 10 01 SETEXP 27 31 37 38 39 40 42 43 45 47 49 SETLED neuere Dee gies ies 29 SI niti Re AD Ee 3 4 6 9 10 StrobeLED DBM ees ee ee EE 14 29 T ES ol EE aaan aa aIian 18 19 29 Threshold 5 11 15 17 18 19 21 28 29 31 41 44 46 TSL1401 monitor exe 13 TSL1401 template bpe 46 TSL1401 DB Monitor Program 13 28 33 TSL1401DBO1 hex EE EG 13 TSL1401R 1 2 3 8 9 11 16 24 31 45 Ww Window thresholding 17 28 X XTRIG diit hee eee iu Ee e 30 31 42 47 49 Page 52 of 52
8. 40 41 42 43 45 47 49 ACQGRAY 26 30 31 33 39 47 49 ACQOR eos 30 33 47 49 ACQORNOT eee 30 47 ACQXOR ne rne ees 30 47 49 ACQXORNOT eese 30 47 49 GR AE ED 3 5 6 Automatic exposure ee ees ee ee RR Re ER Ke RR RR ee EE 45 AVGPIX oe EE WEE ER ge EE 27 33 47 B BADCMD eiii aae exe 26 47 BASIC Stamp 1 3 5 11 20 25 30 31 Binary acquisition coefficients 27 Binary image 20 21 24 27 30 31 33 36 47 BKWD 8 9 36 38 39 40 42 43 47 49 Board of Education eeeeeeeere 5 BRTEDG 34 35 36 37 40 42 43 47 49 BRTPIX xin 34 36 47 49 Buffered mode eee 24 Buffering commands ssssssss 39 C CANTBUF iei Ke PER GERED tee 26 47 el EE 3 6 9 CMDOVE nr Ek Ee bi SE edd 26 47 CNTNEW 33 34 35 36 47 49 CNTNXT e 33 34 35 36 47 49 Continuous imaging ees ee RR KAR RR Re Ke 3 Cosine effect eene se ee RR AE EE EE RR ee ee ee 16 Counting pixels and edges 33 D DATOVWEF ciere cre irren nei 26 47 DB Expander ees ee ee EE EE RE ee ee 1 3 5 Driver fimWar esse see ee EE EE RE ee ee ER EE EG 13 DRKEDG 34 35 36 37 38 39 43 47 49 DRKPIX rine 34 35 36 47 49 Bu
9. Bd Compare ES SI between ISI an Capped Bottle RS PEN 255 equals E Code Uncapped Bottle Bueno Systems Inc TSL1401 DB 2009 10 01 Page 23 of 52 By setting allowable ranges for these two measurements we can tell if any bottle presented to the camera passes inspection In the next section we will see how to write a PBASIC program to perform the various measurements required by applications similar to this one Programming with the TSL1401R Driver using PBASIC Programs written in PBASIC for the MoBoStamp pe can interact directly with the AVR coprocessor which handles all the TSL1401R interface details This interaction consists of sending commands to the AVR and waiting for results which can then be read out to the user s PBASIC program for further action There are commands for setting parameters acquiring images counting and finding pixels and dumping results Nearly all of the binary pixels processing can be done in the AVR itself at machine language speed so it s seldom necessary to read the actual binary pixels into your PBASIC program But they re available anyway if you need to examine them Immediate and Buffered Modes Commands are handled by the AVR in two modes immediate mode and buffered deferred mode In immediate mode you send the AVR a command and it is executed right away In buffered mode you can send as many as eleven bytes of commands but they are not executed until the end of buff
10. Search from left to right BKWD 04 Search from right to left DRKPIX 00 Locate dark pixels BRTPIX 02 Locate bright pixels DRKEDG 03 Locate dark high to low edges BRTEDG 01 Locate bright low to high edges These are the same modifiers used with CNTNEW and CNTNXT but with two additions FWD and BKWD With these modifiers you can select which end to start the search from Searching forward starts from the Left limit and scans towards the right until either the desired feature is found or the Right limit is reached Searching backwards starts at the Right limit and scans to the left until either the desired feature is found or the Left limit is reached Each invocation of FNDNEW or FNDNXT appends one byte to the results buffer If the sought after pixel or edge was found this byte will be its position 1 to 255 in the binary image If it wasn t found the result will be 0 Another side effect of the find commands is that the Left or Right limit whichever one you started from is replaced by the result of the find So for example if the first dark pixel scanning from the left were found at position 45 then the new value of Left would become 45 That way the next time you use FNDNXT to scan from the left the search picks up where the last one ended at position 45 This makes it possible to chain multiple finds to locate say the third occurrence of a certain feature rather than jus
11. 5V is needed for the optional LED strobe attachment Applications Measure height width diameter thickness Locate objects lines edges gaps holes Count items measure conveyor coverage Determine volume shape orientation Read simple barcodes Learn the principles of machine vision What s Included TSL1401 DB with lens TSL1401 DB What You Need to Provide e Parallax motherboard or DB Expander with BASIC Stamp and carrier board such as the BOE Bueno Systems Inc TSL1401 DB 2009 10 01 Page 1 of 52 Introduction What the Module Sees The TSL1401R chip is a linear array linescan sensor It consists of a single row of 128 photodetectors The TSL1401 DB includes a lens to form images on the sensor array What results is somewhat like peering through the narrow crack of a partially opened door to see a thin slice of what lies behind it The illustration below helps to explain the concept First Pixel D E n i S d The output from each observed pixel is an analog voltage proportional to light intensity The analog intensity curve corresponding to the image above would look something like this Bueno Systems Inc TSL1401 DB 2009 10 01 Page 2 of 52 Here you can see not only the edges of the bagel and the hole in the middle but also the intensity variations caused by the seeds and herbs on its surface The overall width field of view seen by the TSL1401 DB using the included 7 9mm
12. TO 31 2 OWIN owio 0 pix 22 DEBUG BIN8 pix REV 8 23 IF i amp 7 7 THEN DEBUG CR 24 NEXT 25 OWOUT owio 1 DUMPADR RESULTS 5 26 OWEN Onse 2 aep otm lic ce Cag Cow Ceo low 27 Cab siz Qe oj Bag owm 28 lieg iwi Lie tee r lag orm gt gt i 29 Ie Cap siz SS AND eys toD lt 206 AND lie lyi gt TLO AND Lie lyi lt 190 aN 30 DEBUG CR Pass 31 ELSE 32 DEB GSC EM Wise 33 ENDIF 34 DEBUG Vg Cap Size V DEC cep Siz V Cap op DEC Cap ios 35 Dawe eene je A JC lag wl 36 LOOP Unlike the prior program which relies on a falling edge on P3 to trigger a new scan this one operates more in demo mode in that the trigger comes from tapping the spacebar in the DEBUG window Here s a blow by blow description Bueno Systems Inc TSL1401 DB 2009 10 01 Page 43 of 52 Line Description 1 8 Some variables that aren t included in the template are defined here 10 Exposure time is set to 60 11 Binary acquisition parameters are set Floating threshold 0 Hysteresis 1 Filter 5 12 Beginning of the main program loop 13 Wait for the spacebar 14 16 Cue up and execute the commands that snap the picture and find the first dark object edges liquid meniscus and the last dark object edges cap 17 Wait for everything that s been cued up to finish 18 Start dumping the binary pixels 19 24 Display the binary pixels in four lines beginning
13. TSL1401 DB 2009 10 01 Page 13 of 52 mezzanine connector The forthcoming LightSYNC DBM plugs into this connector and provides a signal synchronized to the 50 60Hz variations in light level from fluorescent lamps This makes the camera immune to these variations by starting all exposures at the same point on the 50 60Hz cycle When syncing is used in conjunction with triggering or gating the trigger gate condition must be met first then the sync pulse must be received e Acquisition Control You can start and stop scanning with these controls or obtain scans one at a time lt TSL1401 DB Monitor Free Triggered Gated Ext Sync EN Run Single Exposure Time ES ES IEEE o NEE MEE ra Threshold SENS Hysteresis Fite BS Compare IS f i i between ME anc ERI couai e Exposure Controls These set the exposure type and time Exposure times can be either fixed or automatic When set to fixed you can set the actual time using the numerical control When set to automatic exposure time is constantly adjusted to maintain the peak pixel value between certain bounds In this case the numerical boxes show the actual exposure time for any given exposure e Lighting Controls When used with the forthcoming StrobeLED DBM which plugs into the 6 pin mezzanine socket these controls adjust the type and timing of the light output from the LED Bueno Systems Inc TSL1401 DB 2009 10 01 Page 14 of 52 Types are Off
14. do it dice edhe WAR Byte rgt edge VAR Byte area VAR Word OWOUT owio 1 SETEXP 30 OWOUT owio 0 SETBIN 128 10 FIXED LEVEL i 2 3 4 5 6 7 8 9 DO 10 OWOUT owio 0 ACQBIN XTRIG FNDNXT FWD BRTEDG 11 OWOUT owio 0 FNDNXT BKWD BRTEDG 2 3 4 5 6 7 8 9 0 I Jiu ege Sele NEIDIN alee moda cM clc dit cle a MNT ELSEIF area THEN DEBUG Bagel area DEC area CR area 0 ENDIF GOSUB Ready OWOUT owio 0 DUMPADR RESULTS 5 OWIN ovg 2 i eee rge cele LOOP NN This is very similar to the previous example except that the statement order has been changed We ve buffered the acquire and finds in the loop first but then look what happens we start computing with data that hasn t yet been read In PBASIC all variables are initialized to zero So the first time through the loop neither the IF nor the ELSEIF conditions will be true and that section between lines 12 and 17 will simply be skipped Then comes the call to Ready because right then we want to read some new data which we do on lines 19 and 20 Now when we loop back we can prime the next exposure right away and then get on with our area calculations and possibly output This has the effect of overlapping the coprocessor s work with the PBASIC program s in the most efficient manner possible Of course in any real application you also have t
15. edge value The routine to do it is FindEdge which 1 which Look for opposite kind of pixel first GOSUB FindPix which 1 which THEN look for desired pixel GOSUB FindPix RETURN Locating pixels and edges is handy for finding objects in a field of view and measuring their extents An object s extent includes its outside boundaries and everything in between regardless of pixel intensity For example in the bagel illustration the bagel s extent would include the hole while its area obtained by counting bright pixels would not Here is a complete program which incorporates all the routines described above and then some It acquires images and locates bright objects computing both their extents and areas You can also use it as a template for your own programs Wiese 6060 0 TSL1401 scan bs2 Purpose Image capture and processing demo using the TSL1401 DB io DR EI Pike BUENO Systems Jie Beds 5 5 6 Updated SSTAMP BS2 SEarteds ss AI July 2007 i SPBASIC 2 5 This program demonstrates image capture and processing using the TSL1401 DB Parallax p n 28317 It continuously acquires and displays images from the TSL1401R sensor chip It then locates both left and right Bueno Systems Inc TSL1401 DB 2009 10 01 Page 8 of 52 bright edges computes both th displaying them graphically using DEBUG area and extent of the object found Finally it
16. edges between Begin CNTNEW C8 new limits FWD 00 End Count pixels edges between BKWD 04 SNTNXT CO current limits Yes ve DRKPIX 00 None A FNDNEW F8 Find pixels edges between BRTPIX 02 Begin new limits DRKEDG 00 End FNDNXT F0 Find pixels edges between BRTEDG 01 None current limits 3C Begin buffering commands No No None None 0 un End buffering execute buffer 7 0 Z SE then enter immediate mode 1s TE 9E Rone 168 Notes 1 The Buffer column indicates whether the command can be buffered 2 The Busy column indicates whether the busy bit needs to be read as zero after the command is sent and before further interaction with the firmware can take place Applies only to immediate mode 3 Modifiers are ORed to the command byte except where noted Modifiers with a value of zero 00 may be omitted however including them can make a program more readable ONAN A what the individual commands produce Bueno Systems Inc TSL1401 DB 2009 10 01 The Results column indicates how many bytes are appended by the command to the results buffer INTEN and TIME modify the Value parameter not the command itself FWD and BKWD apply to FNDNEW and FNDNXT only not to the count routines The gt command can on y be buffered It cannot be used in immediate mode The number of results produced by a buffered sequence depends on the commands in the buffer and will be the sum of Page 49 of 52
17. i D e E bel wpe 1 oE FB Ee Ba Be it PB Wi VER IIT ULLE Da B p Paes Ee GESIG RS Ee T A So EA Sas pee SE A RTL iS SEER EER UR eR SEDES EE See SME isla WS PERE ESE Ui dd ai SE EE Pa PS fa e p oe sig We f B BS bi E T TE WS We Ta a ss WU Fe ME fes a UF I SS REELE Ma BW 2D BERGER SG REED EE Di GE GED die ALLII cc BEER EEELDEEOEEE EE OOR DEER KO GE EE GEE RE DEE soe LERERLEEEEERLREUE SEGE RERERER BEE EEE GEE EE OE REK ER ERGE LEREEEEBEREREE LE UERER SERE GED KEER E RE BOE EE BERE n P n En EDE ND e wv LLU LIL EULELILUL LULL LILLE LIL DER ER SOLE DE OER ORE DEE REDE REEN wb a a Ag BD Wg DIS VEL ELE BERD SE DIE DSS UGDELU ID LISE II LEIERS LES do fe a DELE tc th a Ba DEEL DLE GLS EI LEUDDEELDEELDI EER DIE LAG DUS DLT HELE ESE BEIDE Le a a Ae DIG EGTER DE DEU GE DS IE EE BEIDE ID EGOLI ER DODE L USE DERDE RE R DER USE RE DI DRR D HUGE N SERE be ECS fa ie el be Rig Me lob eb Dig Ua UAB ge Be EE ER EE YL DERE RE GR DE DE IE GE Eg Di Bb Up ve Ed SG ol VIE dd pi be GR GE Ds DiE EDE Ibid br Tel bi ae be Tol is Ea ESE TE LA Up SA G BEER ER DOE 3 a Be Up EP EDE EED REDE EED SU DEU EE HOER BEDE WL Wh Of EER DOE EK DIG GR DEE GOEDERE BERGE ORDE DEERE RUE ga eh Sa TT EE EL e TA TA FA Ge A LILLTALELLLLLLLLILU LLL REEE EYE SG EOEER EE ESEG ERGER GEE GE BEEK EER EE GESE EER EL DEERE REEL DOER DEE GEOES OER GESE ORE RAOR DODE EE EE EE EE EG EG EEN Es ES ET ME pae You can see where the hole is as well as the outside edges However this also illustrates a couple pitfalls of
18. lens is approximately equal to the subject distance So for example if the module is 1 meter away from the subject it will see a linear slice of the subject that s 1 meter wide and 1 128 of a meter high Focusing the TSL1401 DB s lens is accomplished by screwing it in or out When screwed almost all the way in distant subjects will be in focus To focus on closer subjects the lens needs to be screwed out a bit Once proper focus is achieved it may be necessary to secure the lens from vibration by wrapping tape around the lens bezel and lens holder barrel If the lens is screwed in far enough a small O ring snapped into the crevice between the lens bezel and lens holder barrel will serve the same purpose Note The use of a thread locker e g Loc Tite or any cyanoacrylic adhesive e g Super Glue is not recommended near lens elements as the fumes can destroy any optical coatings that may be present If you are using the TSL1401 DB with a Parallax MoBoStamp pe you can use the PC hosted monitor program described later in this document as an aid to focusing Interface and Basic Operation Refer to the schematic on the last page of this document for the TSL1401 DB s pinout and to TAOS s TSL1401R LF datasheet available from www taosinc com for the sensor chip s particulars For normal operation i e without external strobing or syncing there are only three signals that need to be considered SI digital output to the sens
19. to parlay this into an application that inspects bagels moving past on a conveyor and that finds their actual diameters Bueno Systems Inc TSL1401 DB 2009 10 01 Page 38 of 52 Buffering Commands In the prior section we saw how commands can be chained in immediate execution mode and how a call to Ready must be performed after some of them Here we shall see how to eliminate these extraneous calls by buffering a whole sequence of commands When this is done one call to Ready has to be performed after the commands are executed regardless of which commands were buffered But that s it just a single call to Ready The buffering commands are as follows commands being buffered gt The command can only be executed in immediate mode and it puts the TSL1401R driver in buffered mode Commands entered after that are buffered in the driver s memory beginning at location RESULTS 5 but are not executed When the command gt is encountered it is buffered too and execution begins from the beginning of the buffer Each buffered command is executed in turn until the gt is reached Because commands are buffered in what will become the results area of memory they are likely to get clobbered as results get appended there However execution will always be at least one step ahead of the results so the only commands that get clobbered will be ones that have already executed Here are some additi
20. two lines down from the previous output 25 Start dumping the results of the FNDNXTs Do you notice the 1 in the OWOUT statement This sends the reset that terminates the pixel dumping from line 21 26 Read the results of the FNDNXTs 27 Compute the cap size as the difference between its edge locations 28 Compute the liquid level as the average of the two edge locations 29 Test to make sure all the observed locations and sizes meet the specs 30 32 Print pass fail 34 35 Print the observed data Here is what the output looks like Y Debug Terminal 1 DI xl Com Port Baud Rate pa z Data Bits Flow Control TX DIR RTS None z a v COM8 5600 7 or z AX DSR CTS Make SEER HE Ha ED Bp ee a EER DE US BES Uh Bg Pp Bp EF ERG de LE LEE CLL LeeLee teehee koe ALLIIIIIILIIXIILIIIIEILIIIIILILllllllllOOUUOCUUNH 0000000000011111111111111111111111111111110 40 Cap Top 3 Liquid Level 0011111111111111111111111111111111111111111111111111111111111111 LELELLLLLT TELL LEE EEU ES EER SEER GEREED REEL ELT LLL ELLA dae oos EE Ed PLLELLULILTLULELILTEL LLLP AAI n m n e ee P P e e n n nc nis in RP B 1111111111111111111110000000000000000000000000000000000000000000 Fail Cap Size ll Cap Top 184 Liquid Level 119 inp m Ee Eee Wa Bea a Wh 1 Ob Ua Epa WEB Ep Bec gt Bf ff Op eB Ep VA Te Ep OE ba DT LR Wh Bp Bp Tt 11111111111111111111111111111111100000000000 00 000000000000111111111111111111111111111
21. 11100 Cap Top 204 Liquid Level 92 Macros Pause Clear Close Echo Off Bueno Systems Inc TSL1401 DB 2009 10 01 Page 44 of 52 Automatic Exposure For accurate measurements the only thing that should ever change is the size or position of what we are trying to measure If a subject isn t in perfect focus or isn t lit with perfect evenness it can seem to grow and shrink with variations in lighting And despite our best efforts lighting isn t always as controlled as we d like it to be So we often need to compensate by adjusting the exposure time to changing light levels In cases where a bright object is always within the field of view and or light intensity changes very slowly this is pretty easy The image acquisition commands all record the intensity of the brightest pixel This information can be used to maintain a constant maximum brightness under varying light conditions The simplest rule is this 1 Ifthe maximum brightness is greater than 220 we decrease the exposure time by one 2 If the maximum brightness is less than 200 we increase the exposure time by one 3 Ifthe maximum brightness is between 200 and 220 we leave the exposure time alone We want to keep the brightness high for maximum analog resolution but we don t want it to saturate When brightness levels reach 255 that means the limit of the TSL1401R s voltage output has been reached so there s no way to tell if the actual brightness mi
22. 7 SS 4 TEXAS lt ADVANCED LLA AO OPTOELECTRONIC SOLUTIONS Web Site www parallax com Office 916 624 8333 Forums forums parallax com Fax 916 624 8003 Web Site www taosinc com Office 972 673 0759 Sales sales parallax com Sales 888 512 1024 Sales sales taosinc com Fax 972 943 0610 Technical support parallax com Support 888 997 8267 TSL1401 DB 28317 Linescan Camera Module Product Overview General Description The TSL1401 DB is a daughterboard that provides a TAOS TSL1401R 128 pixel linear array sensor and a lens It is designed to plug into a motherboard e g MoBoStamp pe p n 28300 MoBoProp p n 28303 in development Propeller Backpack p n 28327 or the DB Expander p n 28325 This module will allow its host system to see in one dimension Two dimensional vision can also be achieved by moving either the subject or the sensor in a direction perpendicular to the sensor axis Features Provides vision in one dimension with 128 pixel resolution Three line serial interface with analog intensity output for each pixel Included 7 9mm lens provides a field of view equal to subject distance Plug compatible with Parallax motherboards Coprocessor driver firmware for the MoBoStamp pe available for download Can be interfaced directly to a BASIC Stamp for some functions e Onboard accessory socket for strobe output or 50 60Hz fluorescent light sync input e Runs from 3 3V or 5V supplies
23. NO ACQORNOT ACOXORNOT ACODIFF ACOSAME XTRIG CNTNEW CNTNXT FNDNEW FNDNXT Modifiers NXT BKWD FWD DRKPIX BRTPIX BRTEDG DUMPADR Address constan PIXELS INPIX INLOC AXPIX AXLOC AVGPIX DUMPID RESULTS DUMPFLAGS CON CON CON CON CON CON CON CON CON CON CON CON CON CON CON CON SEF SAO SA4 SA1 A2 A3 A5 A6 SAT A3 SAT 08 C8 CO SF8 SFO ORed to CNTNEW CON CON CON CON CON CON CON CON CON CON CON CON CON CON CON CON CON Bit positions in BADCMD CON 80 CANTBUF CON 40 CMDOVF CON 20 DATOVFE CON 10 y cc Weuexeledes flags VAR Byte busy VAR Base T m Iarteializarion PAUSE 10 00 04 00 00 02 03 01 DA 00 20 20 21 22 23 24 SDD SDF US ede osi caso vase i 255 tollikowames 0 27 isis Acquire and dump a grayscale image Acquire a binary image Acquire binary image ANDed w previous Acquire binary image ORed w previous Acquire binary image XORed w previous Acquire binary image ANDed w NOT prev Acquire binary image ORed w NOT prev Acquire binary image XORed w NOT prev Idiom for ACQXOR Idiom for ACQXORNOT External trigger flag ORed to ACQ commands Count pixels edges between new bounds Co
24. Normal and Strobed Normal mode means that the LED is on for the duration of each exposure at a brightness level that can be set in the numerical window In strobed mode the brightness level is fixed at maximum brightness with the duration being the adjustable factor Scope Window This window shows the analog output of each pixel in real time The horizontal magenta lines represent high and low thresholds for binary pixel acquisition similar to the BS2 acquisition discussed in the previous section but controllable In normal comparator style acquisition the separation between these lines is the comparator hysteresis These lines can be moved up and down by positioning the cursor between them and dragging them with the left mouse button held down The separation hysteresis can also be adjusted by dragging with the right mouse button held down The vertical cyan colored lines determine the area of interest for image analysis They can be dragged left and right individually with the left mouse button held down The yellow line and any yellow feature for that matter represents the result of an image analysis measurement Image Brightness Window The narrow window below the scope window is divided into three slices The bottom slice shows the instantaneous image brightness for each pixel as a gray level The next slice up shows which pixels register as light pixels after thresholding The top slice is used to show image analysis resu
25. QBIN our results will be found sequentially beginning at location RESULTS 5 Bueno Systems Inc TSL1401 DB 2009 10 01 Page 37 of 52 e Third there s a chance that the first edge won t have been found But we didn t check for that we just plowed ahead looking for the second edge But remember the way find works if it doesn t find something once it won t find anything on subsequent FNDNXTs either until the Left and Right limits are reset to new values So we re safe there e Fourth we re only checking for the presence of the right edge in the IF statement to see if the entire object is present Again that s because if the left edge wasn t found the right edge is automatically not found either So that s all we need to check e And finally what s with the 1 in the first DEBUG statement Well here are the two edges we would have found with the above image 0000111000 1111111100000000 f1 1111000111111 1111100000 31 32 64 65 The second edge is located one pixel beyond what we consider to be the right edge of the object so to point to the right edn of the object we need to subtract one On the other hand if we re interested in the size of the object we can just subtract Ift edge from rgt edge Now suppose the central portion of above image represents a backlit bagel In this situation the bagel consists of two dark silhouetted areas separated by a bright hole What we re after here is the dia
26. TEDG 32 64 GOSUB Ready We use BRTEDG here because that s the kind of edge low to high FNDNEW has to find But for there to exist such an edge within the given range there has to be a dark pixel first followed by a light one So given the same image as above we find our edge at location 48 as 0000111000 1111111100000000 f111110000111111 1111100000 31 32 64 65 Now suppose we want to find the first bright object in the same region A bright object is one that begins with a low to high transition and ends with a high to low transition Here is where chaining two finds comes in handy lft edge VAR Byte rgt edge VAR Byte OWOUT owio O0 SETENE OPES TRETEN PET CO COP MEC M OW ACO GOSUB Ready OWOUT owio O0 FNDNEW FWD BRTEDG 32 64 GOSUB Ready OWOUT owio O0 FNDNXT FWD DRKEDG GOSUB Ready Chained finds OWOUT owio O0 DUMPADR RESULTS 5 OWIN owio 2 lft edge rgt edge Win Gee Erle Wallan DEBUG ob oe us eN Ar EG at WC litt edge amc encling ae DEC woe cele 1 ELSE DEBUG No object found ENDIF There are several things to talk about in the above code e First once we ve located the first edge we want to continue from where we left off to find the second one so we use FNDNXT for the second leaving off the Begin and End locations e Second since we ve chained two commands in a row after the AC
27. ad in LSB first Here s how the word and bit arrays are declared pdata VAR Word 8 pixels VAR pdata BITO Counting light or dark pixels within a given range is simple Here s the code that does it EEUE cnt 0 mie LIES GOUME s IF lptr lt rptr AND rptr lt 127 THEN Valid range FOR i lptr TO rptr Yes Loop over desired range IF pixels i which THEN cnt cnt 1 Add to count when pixel matches NEXT ENDIF RETURN cnt can be declared as a byte since it will never exceed 128 Iptr and rptr are also bytes that can range from 0 to 127 inclusive They indicate the range over which the counting occurs which is a bit variable that indicates whether to count dark pixels 0 or light pixels 1 Counting pixels is handy for computing an object s area either in one scan or cumulatively over multiple scans for two dimensional objects passing under the camera on a conveyor Locating the first occurrence of a dark or light pixel within a given range isn t much harder FindPix IF found 1 AND lptr lt rptr AND rptr lt 127 THEN Still looking amp within bounds IF dir FWD THEN Yes Search left to right MOR Ios pier ANC de Yes Loop forward IF pixels lptr which THEN RETURN Return on match NEXT ELSE HORE AA pr IMO Ifo d No Loop backward IF pixels rptr which THEN RETURN Return on match NEXT ENDIF ENDIF Bueno Syst
28. at looks for the first dark to bright edge Notice that we just use FNDNXT here without the Begin and End limits This is because we re scanning the entire field of view When a new image is acquired these limits are automatically reset to 1 and 255 respectively Continuing with the buffering we include the command that looks for the same kind of edge but 10 coming in from the right We also end the buffering which starts the whole chain of commands executing Here we wait for all the commands to finish executing When they do the image will have been acquired and both edges will have been located all with only four bytes in the buffer Now we re ready to read the results so start reading from location RESULTS 5 This is 12 because the ACQBIN is buffered along with the finds and it always adds five bytes to the results buffer thus pushing the find results five bytes higher 13 Read the positions of the left and right edges 14 If we found the right edge the left edge is there too 15 The current diameter is rgt edge Ift edge 1 If that s greater than the maximum diameter seen so far on this bagel make it the maximum diameter using the MIN operator 16 If we didn t see an edge we re between bagels and if max dia is non zero one has just gone past that we havent recorded yet 17 So tell the world what we just saw 18 Reset max dia to zero so it s ready for the next bagel
29. ata acquired from a previous scan can be used to see what changes or stays the same between acquisitions With the possible exception of ACQAND and ACQOR they are typically used second in a pair of commands the first being an ACQBIN In other words you first obtain a scan that simply records pixels in the usual way then wait awhile then obtain another scan modifies the first one In addition to the 32 bytes of binary pixel data each acquire command also collects statistics from the image it has acquired These are the intensity and location of the dimmest pixel the intensity and location of the brightest pixel and the average pixel intensity over the entire image The following chart shows the readable AVR memory locations including those affected by the acquisition commands Name Addr Description PIXELS 00 Binary subpixel data Pixels are packed LSB first Since there are only 255 to 1F pixels the last pixel MSB of location 1F is always O MINPIX 20 Intensity 0 255 of the dimmest pixel MINLOC 21 Location 0 127 of the dimmest pixel If multiple pixels share the lowest intensity it will be the location of the last one MAXPIX 22 Intensity 0 255 of the brightest pixel MAXLOC 23 Location 0 127 of the brightest pixel If multiple pixels share the brightest intensity it will be the location of the last one AVGPIX 24 Average pixel intensity 0 255 of all 128 pixels
30. ch represents the length of time to output the PWM signal For the BS2 this is nominally 1mS Actually it s more like 1 2mS and consists of about 150 cycles PULSOUM Su eso SS 1 MON 10126 i06 This statement sets the exposure time exp can be either a constant or a variable and represents the length of the exposure in microseconds The reason for using PULSOUT instead of PAUSE say is that the timing resolution is so much finer And the reason for sending the pulse on SI is that it s not used for anything else durng this time and so long as we don t clock the pulse with CLK the sensor chip is unaffected The value subtracted from the pulse width 1016 represents the timing overhead from the PBASIC program minus the start of integration delay to the 18 clock in the PWM statement This means that the minimum exposure time will be about 2 032mS Note Timings for BASIC Stamps other than the BS2 will vary and the value subtracted will need to be adjusted accordingly Sinne VS GR Oy MINL This clocks out the SI pulse again to end the exposure and begin actual data readout SHIFTIN AO CLK LSBPRE pdata 0 N16 pdata 1 16 pdata 2 16 pdata 3 16 SHIFTIN AO CLK LSBPRE pdata 4 16 pdata 5 N16 pdata 6 16 pdata 7 N16 These two statements read 128 bits of thresholded pixel data from the AO pin into an eight position word array declared pdata word 8 least signficant bits first Doing it
31. d it from the Parallax website to use with these examples and with your own programs i Tol me e e tsl1401 template bpe Y Purpose Code template for the TSL1401 DB driver firmware 7 Author Parallax Ine i Bice ee eee epit elles Go i Statseol 5 20 aI 2007 Updated SSTAMP BS2pe SEBAS TCR 2 5 This is a blank template used for interacting with the TSL1401R driver firmware in the MoBoStamp pe s AVR coprocessor D scc I O Deisusdiigume c c eoe owio PIN 6 Pin for OWIN and OWOUT to AVR coprocessor 55 HAS esse io EE ea ae Se a D a eae eS aS ee Commands SETLED CON SEB Set LED strobe and brightness time from next byte Flag to OR to brightness time 0 127 value TIME CON 80 Set strobe value 0 127 is 0 3 4mS at 100 on INTEN CON 00 Seu d3 eeexewssbEws velus 0 127 as 49 6 Gu SETBIN CON Sige Set threshold hysteresis and filter 3 bytes Filter flags ORed with filter value NOT with SETBIN FLOAT CON 80 Threshold is floating per filter value 0 7 FIXED CON 00 Threshold is fixed WINDOW CON 40 Threshold is a window outside of hysteresis band LEVEL CON 00 Threshold is a level with hysteresis Bueno Systems Inc TSL1401 DB 2009 10 01 Page 46 of 52 SETEXP ACOGRAY ACOBIN ACOAND ACQOR ACOXOR ACOAND
32. ds as a hysteresis band In order to transition from dark to light a pixel must attain a light level above the upper threshold To transition from light to dark a pixel s level must sink below the lower threshold Excursions into and out of the hysteresis band without crossing it completely will not result in a dark to light or light to dark transition This helps to eliminate hair trigger transitions when the level is near threshold Of course if this is what you want you can always set the hysteresis value to zero This kind of comparison is illustrated in the above diagram by the color of the dots The firmware also supports window comparisons in which values inside the hysteresis band evaluate to zero those above or below to one This is useful for determining how much a subject s light intensity deviates from an acceptable range of levels for example Thresholds can also be either fixed as the example above illustrates or floating A floating threshold follows the contour of the pixel response as a kind of moving average whose filter constant is programmable This can be handy for thresholding subjects whose illumination is uneven It also allows the detection of extreme edges while ignoring gently rising or falling light levels The screen shots below illustrate this Bueno Systems Inc TSL1401 DB 2009 10 01 Page 17 of 52 Threshold EE ES Hysteresis EI Fiter d Compare ES Here a floating threshold wit
33. e Analysis and Measurement The monitor program is capable of performing feature measurement on binary images A feature can be an edge location the centroid of an object the brightness of the brightest pixel etc The feature to be measured is selected on the measurement control bar at the bottom of the screen re f re f between ME ano ER o ERN Code Possible values for the various options are Feature Between Not all combinations of these values will make sense or be realistic for the BASIC Stamp to compute In such cases the result will be shown as n a If a measurement can be computed though the numerical value will be shown in the equals box and a graphical indicator also in yellow will be displayed at the appropriate place on the scope In a subsequent version of the program the Code button will produce the PBASIC program necessary to acquire an image and make the desired measurement Now let s define some terms First Beginning at the left hand side the first feature to match the conditions Last Beginning at the left hand side the last feature to match the conditions Extreme Dark Pixel Least bright pixel Extreme Light Pixel Brightest pixel Average Mean value of the feature s matching the conditions Dark Edge Light to dark transition reading from left to right Light Edge Dark to light transition reading from left to right Object The span between an edge of the object
34. e band between the highest and lowest pixels increases to 48 or 27 of the maximum Of course two and a half seconds to read out the pixels would be quite extreme But it illustrates quite graphically what happens if they re read out to slowly Also bear in mind that this effect can only occur with ACQGRAY and not with any of the binary acquisition commands Bueno Systems Inc TSL1401 DB 2009 10 01 Page 31 of 52 oO Threshold AA NES Hysteresis ad Fite Id Compare ES Intensity plot resulting from even illumination and a fast readout The little blips at the ends are due to a lensing effect from the edge of the clear chip package They do not appear when an imaging lens is in place Threshold EE NEEF Hysteresis EL Fite Id Compare EES Intensity plot resulting from even illumination and a very slow readout Bueno Systems Inc TSL1401 DB 2009 10 01 Page 32 of 52 Here s a program snippet that uses ACQGRAY and outputs the pixel data at 38400 baud to the DEBUG port It s identical to that used by the TSL1401 DB Monitor Program pixno VAR Byte char VAR Byte 16 OWOUT owio 0 ACQGRAY GOSUB Ready FOR pixno 0 TO 7 OWIN owio 8 STR char 16 SEROUT 16 6 STR char 16 NEXT In the above code Ready is a subroutine defined in the code template which polls the AVR until it s no longer busy The binary acquisition commands that perform Boolean operations on the d
35. e next available byte location in the results buffer Assuming that this CNTNEW was the first command after an image acquisition that would be location RESULTS 5 25 so we could read the result into the Byte variable dark count with the following code OWOUT owio 0 DUMPADR RESULTS 5 OWIN owio 2 dark count Once this sequence of code has executed remember the results buffer pointer is reset to RESULTS 20 and the next result computed will be buffered there This is due to the reset sent at the end of the OWIN statement Bueno Systems Inc TSL1401 DB 2009 10 01 Page 34 of 52 When DRKEDG or BRTEDG is selected the command looks for the first pixel that does not match the selected intensity i e DRK or BRT starting from the Left limit It then looks for the next pixel that does match This transition is an edge and is counted as such This process continues accumulating the edge count until the Right limit is reached Here is an example OWOUT owio 0 CNTNEW DRKEDG 32 64 GOSUB Ready Here are the same pixels but with the counted edges IMMISIT 0000111000 11111111 0000000111111 000111111 1111100000 31 32 64 65 There are two such edges within the region of interest so the result of this command is 2 which is then buffered at the next available buffer location Assuming again that this is just a continuation of the code that went before we could then read this
36. e now have to read our results from address RESULTS instead of RESULTS 5 This is because the lt reset the results pointer back to that point after the ACQBIN executed How many bytes did we actually buffer anyway In the first line there are three the lt doesn t get buffered In the second line there Bueno Systems Inc TSL1401 DB 2009 10 01 Page 39 of 52 are two the gt does get buffered That s five altogether so we re well within the eleven limit As we shall see in the next section we can even include an acquisition command in the buffer Bagels and Bottles Putting it All Together Let s integrate everything we ve learned now into a real application In this application we ve got bagels passing by single file atop a black conveyor belt They are being lighted from above so they will look bright against a dark background We want to record the outer diameter of each one For this app we assume that the bagels are round and not oval We also assume that there is a wide enough gap between each pair of bagels that we will see it at least once Finally we assume that the conveyor spans the entire field of view and that there are no crumbs on it to confuse the camera In real life we would have to question every one of these assumptions Here is the meat of the code that will do the work As with all previous examples we must wrap it in the template given at the end of this chapter to be complete lur ce
37. ed in the code template as the subroutine GetError Finally is the DUMPADR command which is used to read results from the driver s memory Its format is DUMPADR Address DUMPADR is a constant from the code template equal to DA Following it is an address byte which can range from O to 47 2F Once these two bytes are sent the firmware expects your program to begin reading data using OWIN It will continue sending data until a reset is received at which point the internal address pointer is reset to RESULTS 20 Here s an example for reading the average pixel value from the last scan OWOUT owio 0 DUMPADR AVGPIX OWIN owio 2 average Here AVGPIX is a constant from the template equal to 24 and average is a byte variable used to hold the result Setting Exposure Time The TSL1401R driver acquires all images using one shot imaging as described above in the Interface and Basic Operation section It handles the exposure integration time details for you All you have to do is tell it how long you want each exposure to be This is done with the Set Exposure command SETEXP ExpTime SETEXP is a constant defined in the code template that follows whose value is EE mnemonic for enter exposure SETEXP requires one argument ExpTime the actual exposure time which can range from 1 to 255 and represents a time span of 267uS to 68mS Note Because exposure timing is based on the AVR s internal RC cloc
38. ems Inc TSL1401 DB 2009 10 01 Page 7 of 52 found 0 paca ic look ue Ee aie de He RETURN Return found is a bit variable that should be initialized to 1 for the search to commence and indicates when the subroutine returns whether the desired pixel has been found 0 no 1 yes Iptr and rptr have the same meaning as in the counting routine except that one or the other can get moved to the location of the found pixel Combined with the cumulative effect that found has this make it easier to perform a whole string of searches dir is a bit variable that indicates which end of the Iptr rptr range to start the search from You can predefine the constants FWD 0 left_to_right and BKWD 1 right to left to assign to dir to make your programs more readable which as with the counting routine indicates what kind of pixel to look for You can predefine constants for which as well DRK 0 BRT 1 for readability When FindPix returns found will indicate whether the desired pixel was located and either Iptr if dir FWD or rptr if dir BKWD will point to the found pixel location Sometimes it s necessary to locate an edge instead of just a pixel A ight edge for example is one that begins with a dark pixel then transisitions to a light one The FindPix routine can be used to find edges too by looking for the first pixel opposite of the edge you re seeking then the next pixel after that that matches the
39. eno Systems Inc TSL1401 DB 2009 10 01 DUMPADR 25 26 27 31 34 35 37 38 39 40 42 43 45 47 49 DUMPFLAGS 26 27 39 47 48 49 DUMPID eee 26 39 47 49 Exposure time 3 4 6 9 10 12 14 27 31 33 45 46 49 F Field of view 1 3 8 40 41 45 Filter sie ote totns 28 29 44 46 FIXED ee ee ee 28 29 31 40 42 46 FLOAT RE EE TN 29 43 46 FNDNEW 34 35 36 37 38 39 47 49 FNDNXT 34 35 36 37 38 39 40 41 42 43 47 49 oe S 3 15 FWD 7 8 9 11 36 37 38 39 40 42 43 47 49 H Hysteresis 15 17 19 28 29 31 41 44 46 I Image acquisition 5 16 20 25 30 31 34 45 Image analysis 7 15 16 17 20 25 31 Immediate mode 24 39 49 Integration time See Exposure time nai ee eee ee ttai eret nae 3 27 L lupe E 1 14 29 45 46 49 LENS oie trei 1 2 3 15 16 21 31 32 Lighting 12 14 21 29 45 46 50 LoadAVR ex nencen 13 Locating pixels and edges 35 M MAXLOG anne etes 33 47 MAXPIX idee 33 45 46 47 Memory MAD NE ect trn ena rhone 25 MINLQC 2 OR 33 47 MINPIX ianiai 33 47 MOBOPPOD RE EE EE N 1 MoBoStamp pe 1 3 5 13 16 24 30 45 46 Page 51 of 52 One shot imaging
40. er command is received This makes it possible to queue up commands to acquire and analyze an image ahead of time and then execute them all in rapid sequence when the proper moment arrives Not all commands can be buffered however The ones that cannot are the ones that send data directly to the PBASIC program as they execute Sending Commands Commands are sent to the AVR using PBASIC s OWOUT statement For example to begin acquisition of a simple binary image you would write OWOUT owio 0 ACOBIN The pin designator owio is either 6 for socket B preferred or 10 for socket A The designator ACQBIN is simply a constant defined in the code template at the end of this chapter It s value is A4 In all the examples that follow we shall use these predefined constants instead of their numerical equivalents just to keep things as readable as possible You will also want to use the code template downloadable from Parallax s TSL1401 DB product page to make writing and reading your programs easier Ready Busy Polling Some commands such as the ACQBIN command mentioned above require a finite amount of time to execute before their results can be read out or another command is sent When these commands execute the AVR needs to be polled until the not busy condition is detected This applies only to immediate mode though In buffered mode the not busy bit is sent only when all the commands in the buffer have finished executin
41. etermined by Hysteresis Set Threshold to O Here s a statement that sets Threshold to 30 Hysteresis to 10 and uses a floating threshold with level comparison and a filter value of 5 OWOUT owio 0 SETBIN 30 10 FLOAT LEVEL 5 The constants SETBIN FLOAT and LEVEL are defined for you in the code template near the end of this section Instead of FLOAT you could also choose FIXED and instead of LEVEL WINDOW which are also predefined Setting the LED If you are using the StrobeLED DBM mezzanine board forthcoming you can set the duration and or brightness of the LED flashes as shown here The basic format for this command is SETLED Amount Where SETLED is a constant equal to EB mnemonic enter brightness and Amount has the following format Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0 Intensity LED brightness value 0 127 equivalent to 0 50 for duration of exposure 1 Time LED on strobe time 0 127 equivalent to 0 3 4mS at 100 brightness Once the SETLED command has been issued it stays in effect until reissued When the lower seven bits of Amount are zero the LED is effectively turned off Otherwise it is turned on at the beginning of each exposure If bit 7 of Amount is zero it remains on for the entire integration time at a level selected by the lower seven bits If bit 7 is one it strobes on at 100 brightness for
42. ffer more than 11 bytes of commands in the command data buffer e DATOVF data overflow is defined as 10 When ANDed with the flags byte it shows whether an attempt was made to buffer too many results in the command data buffer Whenever an error condition occurs the driver firmware will not permit further operations to be performed until a reset is received Under these conditions if you execute a DUMPADR before sending a reset you will read a result equal to FF Since the last four bits of the result are supposed to be zero you will know something is wrong and can then send a reset followed by another DUMPFLAGS command to see what the error was Here s the code that performs the aforementioned tasks Bueno Systems Inc TSL1401 DB 2009 10 01 Page 26 of 52 OWOUT owio 0 DUMPFLAGS OWIN owio 0 flags IF flags SFF THEN OWOUT owio 1 DUMPFLAGS OWIN owio 0 flags ENDIF The first DUMPFLAGS is sent without a prepended reset Then the byte variable flags is read and compared with FF If it s equal that means the firmware is waiting for a reset so DUMPFLAGS is sent again this time with a prepended reset the 1 in the OWOUT command At the end of this entire sequence the variable flags will contain either the error flag which can be ANDed with one or more of the error constants defined above to determine which error occurred or zero indicating that no error occurred The above sequence is includ
43. four variables have been read out which tells the AVR to quit transmitting data Any time such a reset pulse is received the internal pointer that determines where the next result is deposited in memory is set to 20 the beginning of the RESULTS buffer So when you re reading results make sure to read everything you need before sending a command that adds data to the buffer Otherwise any remaining data that you need to read might be overwritten There are two instances when a reset pulse will not be recognized by the driver e When the driver is busy e When a triggered acquisition is awaiting the trigger pulse In the former case just complete the not busy polling before resetting the firmware The latter case is discussed in the section Acquiring an Image Bueno Systems Inc e TSL1401 DB 2009 10 01 Page 25 of 52 Reading Data Data can be read from the AVR using various forms of the dump command The first is the DUMPID command which reads the firmware ID and version number Its format is DUMPID DUMPID is a constant defined in the code template at the end of this section that has the value DD In all code examples that follow we shall use the defined constants since they make the code so much more readable It also makes the code more adaptable in case the firmware gets upgraded and the hex commands change After sending it to the AVR using OWOUT you can read three bytes of data two of them are the
44. front lighting specular reflection glare from the black background surface i e black not being all that black and rounded off edges Proper lighting techniques encompass an entire subject area of their own The last chapter of this document links to various resources on the internet that cover this important topic Bueno Systems Inc TSL1401 DB 2009 10 01 Page 12 of 52 Operation with the MoBoStamp pe The TSL1401 DB is designed to plug into the MoBoStamp pe Though it will work in either socket it is recommended that socket B be used in order to make socket A available for interface type daughterboards requiring access to Vin In all the examples included here socket B is assumed and sometimes required Here s a photo of the TSL1401 DB plugged into the MoBoStamp pe socket B Loading the TSL1401R Driver Firmware Before plugging in the TSL1401 DB you will first want to load the firmware driver for it into the coprocessor associated with socket B Be sure you ve downloaded the program LoadAVR exe from the Parallax website as well as the hex file for the TSL1401R driver TSL1401DBO1 hex Connect your MoBoStamp pe to the PC s USB port and then run LoadAVR exe Select TSL1401DBO1 hex as the file to upload and socket B as the destination Then click Upload Once the file has uploaded successfully you can plug the TSL1401 DB into its socket TSL1401 DB Monitor Program There is a Windows PC host program
45. g And it does this regardless of whether any of the commands would require it in immediate mode Here s a snippet of PBASIC code that does the read busy polling DO OWIN owio 4 busy LOOP WHILE busy This reads a single bit variable busy and loops until it reads as a zero It is most convenient to perform the busy checking in its own subroutine since it may be required more than once in your program The code template provides such a subroutine named Ready Bueno Systems Inc TSL1401 DB 2009 10 01 Page 24 of 52 Memory Map Your PBASIC program has read only access to 48 bytes of the TSL1401R driver s internal memory This memory is used for storing binary pixel values image acquisition stats and the results of various image processing functions It is laid out as follows Name Addr Description PIXELS 00 Binary pixel data Pixels are packed LSB first Since there are only 255 pixels to 1F the last pixel bit 7 of location 1F is always O RESULTS 20 Beginning of the results buffer The internal result pointer is set here after a to 24 reset and after state changes between immediate and buffered modes 25 Beginning of the command buffer 11 bytes and continuation of the results to 2F buffer The named constants shown above are predefined in the code template near the end of this chapter The 32 byte PIXELS area will always contain the results of the latest binary scan Followi
46. ght have been higher than that So we try to keep the maximum brightness between 200 and 220 Here s a snippet of code that illustrates the rule in action It uses the LEDs on the MoBoStamp pe to indicate the current light level Red is too high green is too low yellow red and green together is just right red PIN 13 green PIN 14 Pin for red LED on MoBoStamp pe Pin for green LED on MoBoStamp pe max brt VAR Byte aximum brightness read from driver exp time VAR Byte Current exposure time So time 30 Establish initial exposure time OWOUT owio 1 SETEXP exp time rd set ie DO Do repeatedly OWOUT owio 0 ACQBIN Acquire an image GOSUB Ready Ao Mene JO EA DAS SALE OWOUT owio 0 DUMPADR MAXPIX Read the maximum pixel value OWIN owio 2 max brt IF max brt 200 THEN GO ume ex9 ime de L MAX 255 OWOUT owio 0 SETEXP exp time LOW green HIGH red ELSEIF max brt 220 THEN jo tine Gyo time d onn d OWOUT owio 0 SETEXP exp time Is it less than 200 Yes Incremen xposure time to 255 max and set it Indicate as green too low Is it greater than 220 Yes Decrement exposure time to 1 min and set it LOW red HIGH green Indicate as red too high ELSE Is it between these values LOW green LOW red Yes Indicate as yellow just right ENDIF LOOP Keeping the maximum brightness a
47. h a filter factor of 6 has been selected Now see what happens when the filter factor is changed to 2 Threshold ER ES Hysteresis ar Fitter BA Compare ES Only the most extreme rising edges are detected in this case as indicated by the green area in the narrow strip below the scope window In fact one of the most important applications of floating thresholds is edge detection Another application of floating thresholds is in texture detection Texture is a characteristic associated with rapidly alternating pixel values The seeds and herbs on a bagel represent texture for example In Bueno Systems Inc TSL1401 DB 2009 10 01 Page 18 of 52 fact a possible application might be inspecting bagels to see if they have enough stuff sprinkled on them In this case we would set the floating threshold level to zero the filter to zero and the hysteresis to a level consistent with how much texture we want to call good Also we ll use window comparison so that rising and falling pixel values get treated equally Here is an example using a bagel with stuff on it top compared to a plain bagel bottom Hysteresis Threshold EE EES Hysteresis om Fite Id Compare K Notice how the green one pixels capture the texture of the coated bagel and even the fact that the right side of that bagel has more stuff on it than the left side Bueno Systems Inc TSL1401 DB 2009 10 01 Page 19 of 52 Imag
48. hing like this unscrew the lens housing from the board and use dry compressed air or a soft cloth to remove whatever is causing the problem Binary Image Acquisition The AVR firmware that you uploaded to the MoBoStamp pe enables a wealth of image acquisition options particularly in the conversion of grayscale pixel values to binary light dark values suitable for image analysis Binary pixels are acquired using sub pixel resolution This enables the acquisition of 255 binary pixels from the 128 grayscale pixels output from the TSL1401R The firmware accomplishes sub pixel resolution during image acquisition time by interpolating a virtual pixel between every pair of actual pixels as shown below Bueno Systems Inc TSL1401 DB 2009 10 01 Page 16 of 52 Binary Pixel Numbers Gray level Pixel Numbers All light dark thresholding takes place using the values of the 255 real and interpolated pixels yielding 255 bits of data whose positions are numbered 1 through 255 Note This one based numbering is different from that of the direct BS2 numbering shown earlier That was zero based to conform with PBASIC s zero based subscript conventions Binary pixel O is non existent in this system and is used in the context of image analysis to indicate feature not found Conversion from gray level to binary pixels always uses two thresholds for each pixel Normal compare to level thresholding treats the area between the two threshol
49. his can be used in successive pairs to see which pixels change between them useful for motion detection Acquire a binary image ORing the binary pixels with the NOT of the previous ACQORNOT A5 image This reveals only those pixels that become dark betweens pairs of scans Acquire a binary image ANDing the binary pixels with the NOT of the ACQANDNOT A6 previous image This reveals only those pixels that become bright between pairs of scans Acquire a binary image XORing the binary pixels with the NOT of the ACQXORNOT A7 previous image This can be used in successive pairs to see which pixels remain the same between them Two additional constants are defined in the code template ACQDIFF which is a pseudonym for ACQXOR and ACQSAME which is a pseudonym for ACQXORNOT These pseudonyms reflect the use of these two commands which is to find pixels which are either different from or the same as pixels from the previous acquisition Although the acquire commands do not take an argument there is one optional modifier that can be ORed to it This is the external trigger constant XTRIG which has a value of 08 When XTRIG is ORed to any acquire command the command when executed will wait for a falling edge on BASIC Stamp pin P3 then begin its exposure Since P3 is common to both daughterboard sockets this same pin triggers the TSL1401 DB in either one This enables exposures to be synchronized precise
50. holds the hysteresis band is twice the value entered for Hysteresis The Mode byte includes flags that determine the binary acquisition mode along with the floating threshold Filter value It s format is Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0 Fixed 0 Level Filter value for floating threshold 1 Floating 1 Window ed eee eens 0 no filtering to 7 maximum When FIXED thresholding is selected each pixel is compared to the same constant upper and lower thresholds defined by Threshold and Hysteresis The first pixel is compared to Threshold alone to determine the initial state O or 1 After that each pixel is compared with either Threshold Hysteresis if the last comparison yielded a 0 or Threshold Hysteresis if the last comparison yeilded a 1 Both Threshold and Hysteresis can range from 0 to 255 When their sum is greater than 255 255 is used as the upper threshold When their difference is less than 0 0 is used as the lower threshold When FLOATing thresholding is selected the value given for Threshold is assumed to be a signed byte that ranges from 128 80 to 127 7F It is treated as an offset which is added to an internal floating parameter Float that changes depending on the values of the pixels that preceded it For the first pixel Float is simply assigned the value of that pixel From there on out Float is modified after each pixel is processed according
51. ight limits whatever they might be These limits are set by the following actions whichever occurred most recently e Any acquire command which resets Left to 1 and Right to 255 e Any CNTNEW command Left ends up at Begin Right at End e A FNDNEW command Left is initialized to Begin Right to End e Any FNDNEW or FNDNXT command wherein Left is moved to the found pixel or edge searching forward or Right is moved to the found pixel or edge searching backward If the desired feature is not found Left will equal Right 1 causing further counts and finds to return zero The modifiers that can be used with i e ORed to CNTNEW or CNTNXT are Name Value Description DRKPIX 00 Count dark pixels BRTPIX 02 Count bright pixels DRKEDG 03 Count dark high to low edges BRTEDG 01 Count bright low to high edges When DRKPIX or BRTPIX is selected the command counts either dark pixels or bright pixels depending on which modifier is used Here s an example OWOUT owio 0 CNTNEW DRKPIX 32 64 GOSUB Ready This will count all the dark pixels between locations 32 and 64 inclusive Suppose the binary pixel array looked like this where vertical bars represent the location 32 and 64 boundaries 0000111000 11111111000000001111110000111111 1111100000 31 32 64 65 In this case there would be twelve dark pixels counted and a 12 would be written at th
52. inline like this is faster than doing it in a loop Here are the waveforms from the above acquisition routine with the various sections annotated IDLE SHIFTOUT PWMOUT PULSOUT LI SHIFTIN16 x 8 IDLE SI CLK 1 129150 1 2345678910 129 Tri Tri Tri Stated Stated Stated A0 2V Input Threshold Bueno Systems Inc TSL1401 DB 2009 10 01 Page 6 of 52 You can use DEBUG to display the acquired pixels as in the following program fragment A somewhat fancier version is given in the complete program later in this section FOR i 0 TO 7 DEBUG BIN16 pdata i REV 16 NEXT The variable i can be declared as a NIBble The reason for the REV is because the data were read in LSB first but DEBUG s BIN formatter displays data MSB first So we need to reverse the order of the bits to get an accurate picture of the pixel order One might well ask why we didn t just read the data in MSB first to begin with After all SHIFTIN can do that just as easily The answer lies in the image analysis routines to follow Image Analysis Analyzing a linescan image to extract useful information from it involves two major operations pixel counting and pixel and edge location The PBASIC subroutines that perform these operations treat the original array of eight words as an array of 128 bits each bit corresponding to a single pixel Bit 0 is the first pixel read bit 127 the last This mapping is why the pixels needed to be re
53. k these times are approximate and can vary with temperature Here s a statement that sets the exposure time to 30 about 8mS OWOUT owio 0 SETEXP 30 Once the exposure time is set it stays set until changed by another SETEXP If you never set the exposure time explicitly it defaults to a value of 128 Setting Binary Acquisition Coefficients When a binary image is acquired each pixel is first read from the AVR s A D converter as a value between 0 and 255 Then it s converted to a O or a 1 depending on the values of the three coefficients Bueno Systems Inc TSL1401 DB 2009 10 01 Page 27 of 52 provided by this command These are the Threshold the Hysteresis and the Mode These coefficients are the same as described above in the TSL1401 DB Monitor Program section The sequence of bytes required by the Set Binary Coefficients command is SETBIN Threshold Hysteresis Mode Where SETBIN is a constant having the value EC mnemonic enter coefficients Threshold is the value which determines whether an acquired pixel is a one light or a zero dark Hysteresis is the width of the band above and below Threshold which in effect creates two thresholds an upper and a lower A pixel must be at least as high as the upper threshold to cause a transistion from dark to light and it must be lower than the lower threshold to cause a transistion from light to dark The separation between the two thres
54. le WAR Bye rgt edge VAR Byte max dia VAR Byte OWOUT owio 1 SETEXP 30 OWOUT owio 0 SETBIN 128 10 FIXED LEVEIL Fl DO 1 2 3 4 5 6 7 8 9 OWOUT owio 0 lt ACOBIN FNDNXT FWD BRTEDG 10 OWOUT owio 0 FNDNXT BKWD BRTEDG 1 2 3 4 5 6 7 8 9 0 GOSUB Ready OWOUT owio 0 DUMPADR RESULTS 5 OWIN wio 2 lit edge rgt eel DE ieee GicleS HEN mez cla rge edge lie edge 1L MIN mes cHe ELSEIF max dia THEN DAE Hagel biewsewtes W DEC mas Clic CR mes hie 0 ENDIF LOOP 2 Line numbers have been added to the left of the actual program so we can discuss each line here Bueno Systems Inc TSL1401 DB 2009 10 01 Page 40 of 52 Line Description Some variables that aren t included in the template are defined here You re already familiar with 1 3 Ift edge and rgt edge The variable max dia is used to keep track of the largest diameter seen so far on a particular bagel 5 Exposure time is set to 30 Binary acquisition parameters are set Threshold 128 Hysteresis 10 Threshold is fixed instead of floating and comparisons are done on the level instead of a window Once the basic parameters have been established they don t change The real work can now commence within a DO loop Here we buffer the plain binary acquire command and the command th
55. lete by calling the Ready subroutine defined in the code template that polls the AVR for a not busy condition OWOUT owio 0 SETEXP 30 SETBIN 64 10 FIXED LEVEL ACQBIN XTRIG GOSUB Ready This also demonstrates how commands can be chained in a single OWOUT statement The ACQGRAY command is unique in that it gives you access to the gray level pixel values as it reads them out from the TSL1401R Since there isn t enough memory in the AVR to store all these values they have to be sent to the BASIC Stamp on the fly For this reason any PBASIC program that uses ACQGRAY should read these 128 bytes of data and do something with them as quickly as possible Slowing down acquisition from the TSL1401R chip could result in pixel droop as the internal charge storage capacitors self discharge The two plots below illustrate this effect For this test the lens and lens housing were removed in order to illuminate the TSL1401R chip as evenly as possible In the first the pixels are read out at and transmitted to the host PC at maximum speed about 1 4 mS pixel The pixel intensities are all within 14 of each other 8 of the maximum as indicated by the threshold cursors In the second an additional 20mS was added to each pixel time 2 56 seconds overall to read out the scan You can see the droop near the end that results from the internal caps discharging very unevenly as they await their turn to be read And th
56. letters L and S for linescan the last is just a byte the version number which for this version is 1 Here s a snippet of code that reads and displays the firmware ID OWOUT owio 0 DUMPID OMNEM ewe Ol sed Muere Ver DEBUG Firmware version Ltri Ltr2 DEC Ver Ltri Ltr2 and Ver are Byte variables When executed the DEBUG screen should display Firmware version LS1 The next dump command is DUMPFLAGS whose format is simply DUMPFLAGS This command allows the PBASIC program to read one byte which contains various error flag bits that can be used for debugging The format of the flag byte is as follows Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 BADCMD CANTBUF CMDOVF DATOVF 0 0 0 0 The four most significant bits are the error flags A flag bit is set to 1 if its associated error has occurred since the last time DUMPFLAGS was executed When DUMPFLAGS is executed all the error flags are cleared internally e BADCMD is defined in the code template as 80 It can be ANDed with the flags byte to test whether the AVR has received an unrecognized command e CANTBUF is defined as 40 It can be ANDed with the flags byte to test whether an attempt was made to buffer a command ACQGRAY or any of the dump commands that can t be buffered e CMDOVF command overflow is defined as 20 When ANDed with the flags byte it will tell you if an attempt was made to bu
57. lts in yellow when those results include location or count information Binary Acquisition Controls These adjust the threshold types and values and are discussed in their own section below Suffice it to say here that changes to these controls are reflected in the magenta lines displayed in the scope window Image Analysis Controls These controls make it easy to measure various features in an acquired image to test what might work or not work in your application They are discussed in detail in their own section to follow The value in the window after equals in yellow is the numerical result to which the yellow graphics coincide In the example above this is the value of the first pixel in the region defined by between 1 and 255 The Code button will be used in a future rev of this program to write a PBASIC program for you that performs the scan acquisitions and performs the image analysis that you ve selected The monitor program enables almost instant feedback for focusing the lens Below are two screenshots one of a backlit comb that s in focus and one that s not Notice the sharper edges and more pronounced detail in the in focus image This is what you need to strive for By screwing the lens in and out while watching the scope window you will be able to maximize the sharpness of your image to obtain the best and most repeatable results ETES REEKSE EE rrr Backlit Comb in Focus Backlit Comb Out of Focus
58. ly with an external event such as an encoder pulse or optosensor output Note When the driver firmware is waiting for an external pulse on P3 it is not possible to reset it using the OWOUT statement s reset pulse However you can force an exposure by pulling P3 low momentarily with the following PBASIC code sequence LOW O INPUT 0 Note that P3 like P2 should not be driven high The MoBoStamp pe includes pull ups for this purpose allowing bussed open collector drivers to actuate it by pulling it down Bueno Systems Inc TSL1401 DB 2009 10 01 Page 30 of 52 All of the acquire commands send out a not busy bit in immediate execution mode when they have completed their work The exception is the ACQGRAY command which sends the not busy bit ahead of outputting the 128 bytes of grayscale pixel data In any case it is necessary to poll for this bit after sending any acquire command to the AVR The image acquisition commands all result in 255 bits of subpixel resolution image data in the AVR s internal data buffer You can access this data using the DUMPADR command described later However this is seldom necessary given the firmware s internal image analysis functions also described later Here s a code snippet that sets the exposure time to 30 the threshold and hysteresis to a fixed 64 and 10 respectively causes the AVR to wait for P3 to transition low then acquires a binary image It then waits for the image acquisition to comp
59. meter of the bagel we don t care about the hole This is where a backward search comes into play First we locate the first bright to dark edge scanning forward Next we locate the first bright to dark edge scanning backward These will be the extreme left and right edges of the bagel from which we can compute its diameter Here s the code lft edge VAR Byte rge edge VAR BYTS OWOUT owio O0 SEINE 60 SEIN LOO S95 O ACORN GOSUB Ready OWOUT owio O0 FNDNEW FWD DRKEDG 32 64 GOSUB Ready OWOUT owio O0 FNDNXT BKWD DRKEDG GOSUB Ready OWOUT owio O0 DUMPADR RESULTS 5 OGNIN owi 2 lze edge rge xls Ji zot Giles MaN DEBUG eee Ee AE sk ame e DEC oee oe Jit cele s i ELSE DEBUG No bagel found ENDIF Okay everything looks as expected except for that 1 What s up with that Here are the edges that the program found 0000111000 11111111 0000000111111000f111111 1111100000 31 32 64 65 When scanning backward for an edge FNDNXT looks for the first bright pixel which is at location 64 then moving right to left the first dark pixel after that which is the one highlighted above and which is part of the bagel itself Hence the necessary addition to get the diameter Of course all this assumes that we re looking at the largest part of the bagel which would be a real coincidence In a subsequent section we ll see how
60. ng that is the 16 byte RESULTS buffer Immediately after an image is acquired five bytes of the results buffer will contain statistics from the acquisition These are discussed in detail in the image acquisition section Unless the firmware gets an OWIN OWOUT reset pulse or changes state further results from image analysis are appended to these five bytes The area they get appended to just happens to be the 11 byte command buffer where buffered commands are stored For this reason the command buffer is only temporary storage and a command sequence saved there must be reloaded each time it is used Resetting the Driver PBASIC s OWOUT and OWIN commands include a provision for sending a reset pulse to the AVR The TSL1401R driver will accept this pulse anytime it s expecting I O from the BASIC Stamp and only then and will use it as a signal to reset its communication state and buffer pointers to their initial conditions The reset pulse is most often used to terminate a sequence of result data being read from the AVR as the following example shows OWOUT owio 0 DUMPADR 32 Begin dumping from buffer address 32 OWIN owio 2 minpix minloc maxpix maxloc Read 4 byte variables Here DUMPADR is a command to begin dumping data from the address given by the next byte It is followed immediately by a read using OWIN The OWIN parameter 2 indicates that a reset pulse should be sent after the statement is finished executing i e after the
61. o make sure there s enough time between encoder pulses to get everything done The illustration below shows the timeline of events Primed Triggered Not Busy Primed Triggered n e g Driver Acquire Image Find Edges Acquire Image Find Edges re ey Encoder PO In the monitor program section we looked at bottles some full some not so full some with caps and some without What we want to do is pass the bottle if all of the following conditions prevail 1 The liquid level is detected between pixels 111 and 129 and 2 The cap is detected and it has a size of at least 36 pixels 3 The top of the cap is no higher than pixel 205 i e it s pushed on all the way Bueno Systems Inc e TSL1401 DB 2009 10 01 Page 42 of 52 Here is the program that sorts this all out and gives a pass fail grade to each bottle it sees 1 liq btm VAR Byte 2 icu Op MARIE HE 3 cap btm VAR Byte 4 cap top VAR Byte 5 cU 6 ease VARE ES 7 B VAR Byte 8 pix VAR Byte 9 10 OWOUT owio 1 SETEXP 60 11 OWOUT owio 0 SETBIN 0 1 FLOAT LEVEL 5 12 DO 13 SERIN 16 84 WAIT 14 OWOUT owio 0 lt ACQBIN FNDNXT FWD DRKEDG 15 OWOUT owio 0 FNDNXT FWD BRTEDG FNDNXT BKWD DRKEDG 16 OWOUT owio 0 FNDNXT BKWD BRTEDG 17 GOSUB Ready 18 OWOUT owio 0 DUMPADR PIXELS 19 DEBUG CLS 20 FOR i 0
62. onal important points e When lt is sent in immediate mode and when gt is encountered when executing from the buffer the internal results pointer is reset to RESULTS 20 That means further results will be appended to the buffer beginning at that point e Do not buffer the ACQGRAY DUMPID DUMPFLAGS or DUMPADR and do not buffer more than eleven bytes including the gt Doing so will raise an error condition and you will need to run the error recovery procedure outlined above or else reload your program e Since nothing gets executed until the gt is received you can send everything up to that point but defer sending the gt until the time is right to begin execution Now let s see how to rewrite our code from the previous example to use buffering lit eche WAR Byte rgt_edge VAR Byte OWOUT owio 0 SETEXP 60 SETBIN 100 3 0 ACQBIN GOSUB Ready OWOUT owio 0 lt FNDNEW FWD DRKEDG 32 64 Buffered commands OWOUT owio 0 FNDNXT BKWD DRKEDG y GOSUB Ready OWOUT owio 0 DUMPADR RESULTS OWIN owio 2 lft edge rgt edge Win ee edge Ezi DASU Base Hemde emo DEC rge edge Mitt eee i ELSE DEBUG No bagel found ENDIF By buffering the finds we eliminate one call to Ready since the driver firmware waits until af the buffered commands have executed before signaling that it is no longer busy Notice too that w
63. or begins a scan exposure CLK digital output to the sensor latches SI and clocks the pixels out and AO analog pixel input from the sensor 0 Vdd or tri stated if beyond pixel 128 The TSL1401 datasheet describes these signals in detail so that description won t be repeated here except as it relates to the BASIC Stamp If you are using the TSL1401 DB with Parallax s DB Expander p n 28325 the pin correspondences are as follows TSL1401R Pin DB Expander Pin AO A SI B CLK C The TSL1401R is a ight integrating device It s a bit like photographic film in that regard the longer you expose it the brighter the resulting image Also like film it can saturate such that if exposed too long everything even the darkest subjects will look completely white The exposure time also called integration time is the time interval between SI pulses Well actually the exposure doesn t really begin until 18 clocks after SI but it s often convenient to ignore that detail if those clocks occur quickly enough During each exposure all the pixels need to be clocked out of the device to prepare it for the next exposure However the exposure interval for each pixel begins and ends with the SI pulse not with the moment it s clocked out as with some other sensors Therefore all the pixels get exposed simultaneously and the acquired image represents the same interval in time for each of them There are two wa
64. osure time to 8333uSec 1 120th sec DEBUG HOME Go to home position on DEBUG screen GOSUB DispHdr Display the pixel location header DO Begin the scan and process loop GOSUB GetPix Obtain a pixel scan DEBUGMCR Ss RXV O Move to column 0 row 2 GOSUB DispPix Display the pixels here lptr 0 rptr 127 which BRT dir FWD found 1 Initialize parameters for find GOSUB FindEdge Find first dark to light edge going L gt R dir BKWD Switch directions GOSUB FindEdge Find first dark to light edge going L lt R DEBUG CLREOL Clear the next lin TE found THEN Both edges found DEBUG CER liste 1 w jw yes Display left edge DING CNSR iqotie W W y Display right edge GOSUB CountPix Jj Compute area light pixel count DEBUCTOR as Chi MUT C NE DES GUT io Display area y Extent Wo DECIT pA pti p dir CEREO Voss and extent ne objek ELSE No Display failure message DEBUG CG Ri as Re ye OD ECCE reisel V CEREO ENDIF Bueno Systems Inc TSL1401 DB 2009 10 01 Page 9 of 52 Acquire 128 thresholded pixels from sensor chip ede sus Teo xposure time in microseconds GetPix SKUNO Gi el O Jw elek outs Clove Sic EISE PHIM c 129 1 Rapidly send 150 or so CLKs PULSOUT si exp gt gt 1 MIN 1016 1016 Wait for remaining integration time SDEEWQOUR si el O Jw Clock out another SI pulse Read 8 words 128 bits of data SHIFTIN ao clk LSBPRE pdata 0 N16 pdata 1 N16
65. ove equation gives us our answer IntegrationTime 2 k x LightIntensity This means that if we want all the pixels whose light intensity is at or above a certain level to read as ones all we have to do is integrate for a time nverse y proportional to that intensity level and those pixels will read as ones after thresholding By iterating over a range of intensity levels and displaying each line of pixels one above the other we can obtain an analog bar graph of intensity levels Here s the code snippet that does it It can be plugged into the main code template shown above Bueno Systems Inc TSL1401 DB 2009 10 01 Page 11 of 52 inten VAR cnt DEBUG HOME Go to home position on DEBUG screen GOSUB DispHdr Display the pixel location header FOR inten 32 TO 8 Cycle through intensities in reverse exp 32000 inten 8 Compute exposure time as inverse of intensity GOSUB GetPix Acquire the binary pixels GOSUB DispPix Display them NEXT Continue with next lower intensity level END Here s what the output looks like when viewing a bagel lighted from the front iB xi Com Port Baud Rate Parity pes Bits Flow Control TX DIR RTS None RX DS COMI 9600 fe 7 Of v RUE EE BR BEG GREEP ERG ole EE ie s 12lIIIILIIITIILIILlIll IIIIIIIILLIIIlllllllill 111 l11111111111111111111111111 11l eg Ea a Te e Fo e te Lacs i En e Po e Hi e hl i i ec ee cle eria is en Re Ee et n a e Dr Eee a a
66. pdata 2 16 pdata 3 16 SHNS aO Clk ESBPERE AI Astra Wil oara Me paartie Ho piaca yio RETURN Display a header to aid in identifying pixel positions DispHdr BOR we TA Display tens digits DEBUG DEC i DIG 0 ii a 302 spem DEE GE vAN GESENDEB AE ER NEXT BORS st ON Wo 127 Display ones digits DEBUG DEC i 10 NEXT RETURN 2 MI rc LLL e IN ENNINI LELL LIL L LL LLLO CUNLLGGGLGG GGGCGOZGIESOLLIBC G C COLULULUOLGOILISCICOLCELEC LCLEELL LLEG0O Display 328 pixeles ligne pixels as 1 carck eg W DispPix FOR i 0 TO 127 TEEPE n nsus Joisis UKG ENE Hen EEBUS V Us NEXT RETURN Find the first edge within the range lptr through rptr in the direction given by dir and of the type indicated by which 0 light to dark y db eleuslk ito 1 aigilaue a FindEdge which 1 which GOSUB FindPix which 1 which Then look for desired pixel Joy telilims lS io elei Look for opposite kind of pixel first Bueno Systems Inc TSL1401 DB 2009 10 01 Page 10 of 52 Find the first pixel within the range lptr through rptr in the direction given by dir and of the type indicated by which 0 dark 1 light FindPix IF found 1 AND lptr lt rptr AND rptr lt 127 THEN Still looking amp within bounds
67. plete scan then would require 128 bits of data i e 16 bytes or 8 words which the BASIC Stamp can accommodate handily Once these bits have been read in it s possible to analyze features of the scene by looking for groups of light and dark pixels For example if you wanted to measure the width of a light object against a dark background you could read in the image then count the number of 1 bits in the data Likewise if you wanted sense the edge of a web e g paper in a paper mill to keep the web on track you would look for the first occurrence of a light or dark pixel in each scan Bueno Systems Inc TSL1401 DB 2009 10 01 Page 5 of 52 The following PBASIC code fragment taken from the complete program template shown later in this section can be used to read a single scan from the TSL1401 DB It consists of five lines of code which are shown and discussed individually Si aMOUT Sx CIE O DHT SI and CLK are defined in the larger program s preamble as PINs and connect to like named ports on the TSL1401 DB This statement clocks out SI as a single bit of synchronous serial data which starts a new exposure interval Exgs Cu J 285 Ib We want to clock through all 128 pixels as fast as possible since we re just timing an exposure here not reading data The PWM statement fits the bill perfectly The 128 in this statement is the duty cycle 5096 not the number of pulses The number of pulses is given by the 1 whi
68. result into the Byte variable edge count from location 20 RESULTS OWOUT owio 0 DUMPADR RESULTS OWIN owio 2 edge count But if we re interested in both the pixel count and the edge count it s much more efficient to compute them both then read the results This is what the code would look like in that case OWOUT owio 0 CNTNEW DRKPIX 32 64 GOSUB Ready OWOUT owio 0 CNTNXT DRKEDG GOSUB Ready OWOUT owio 0 DUMPADR RESULTS 5 OWIN owio 2 dark count edge count Note that because these commands are executed immediately we need to wait for each one to complete by calling Ready before sending another Also note that edge count is now being read from location RESULTS 6 26 instead of RESULTS 20 This is because there is no reset this time between reading dark count and edge count Also note that CNTNEW leaves the internal pointers set to Begin and End and CNTNXT leaves the internal pointers where they were when it was invoked For that reason the second count can use CNTNXT since the 32 and 64 are already established In the section that discusses buffered commands we shall see how this whole sequence can be made yet more efficient by chaining the commands in the command buffer and executing them as a single command Locating Pixels and Edges In addition to counting pixels and edges it s also useful to know where certain pixels and edges are located within the image We may want to loca
69. scus and the size of the cap shadow we can determine if the bottle is filled and capped properly Once again you can see evidence of the cosine effect discussed earlier There are multiple possible ways of correcting for this effect These include e Storing a brightness contour and dividing each captured pixel level by its contour value not possible with the AVR firmware due to lack of memory e Using a telephoto lens and backing off from the subject to narrow the capture angle possible with the TSL1401 DB but requires a different lens e Using lighting that s brighter near the image edges than at the center possible but sometimes difficult e Using a floating threshold that approximates the contour when capturing binary images easiest where practical In this example application we will use the floating threshold method Bueno Systems Inc TSL1401 DB 2009 10 01 Page 21 of 52 In the next pair of images you can see the difference between a full bottle and one that s not so full To find the liquid level we use the First Dark Object Location measurement Full Bottle Not so full Bottle Bueno Systems Inc TSL1401 DB 2009 10 01 Page 22 of 52 In the next pair of images you can see the difference between a bottle that s capped and one that s not Here we use the Last Dark Object Area measurement AOUUOUUOONOOUUOONO ONOONO ONONO ONIONIONI E E Threshold EE NN Hysteresis Fitter
70. t the first one If the result of the search is 0 the internal Left and Right limits will have crossed forcing all subsequent searches to result in O as well until these limits are reset to new values When the DRKPIX or BRTPIX modifier is selected FNDNEW and FNDNXT will look for the first dark or light pixel in the selected direction depending on the modifier used For example suppose we want to find the first dark pixel between locations 32 and 64 scanning from left to right Here s the code that does it OWOUT owio 0 FNDNEW FWD DRKPIX 32 64 GOSUB Ready Bueno Systems Inc TSL1401 DB 2009 10 01 Page 36 of 52 As with the count commands we need to call Ready before issuing further commands Now suppose the binary pixel array looked like this where vertical bars represent the location 32 and 64 boundaries 0000111000 11111111000000001111110000111111 1111100000 31 32 64 65 The result of the FNDNEW would thus be 40 the location of the first dark pixel starting from location 32 and moving right That value would be appended to the results buffer as with the count commands and we can read it into the Byte variable location like this assuming this is the first command after the last acquisition OWOUT owio 0 DUMPADR RESULTS 5 OWIN owio 2 location Now suppose we wanted to locate the first dark to light edge in the same region Here s the code OWOUT owio 0 FNDNEW FWD BR
71. t a constant level is a this code does by the way One thing to note is that brightness is being measured continuously here In applications where images are acquired sporadically this approach may not work unless acquisitions are performed between them just to measure image brightness If you run this program you will notice that it responds to changes in brightness rather slowly In situations where brightness can vary faster than simple incrementing or decrementing can compensate for it may be necessary to adjust by an amount proportional to the Bueno Systems Inc TSL1401 DB 2009 10 01 Page 45 of 52 difference between the desired and actual levels to get a faster response For example the auto exposure method used in the TSL1401 Monitor program is ex SOOO mede Qu d 1 medo Exo maz 255 mim di where exp is the exposure time and maxbrt is the maximum brightness read from location MAXPIX after each acquisition This method responds instantly to changes in maximum brightness which may not always be a good thing especially when those changes occur because of changes in the subject and not changes in the lighting But this just illustrates that there are many possible approaches and that you have to pick one appropriate to your individual application PBASIC Code Template Here is the code template TSL1401 template bpe which defines all the constants and subroutines used by the examples above You can also downloa
72. te the edge of a web on a paper machine for example to make sure it s tracking right or follow a seam for welding two pipe sections together or determine the level of liquid in a clear bottle The commands that do all this are FNDNEW and FNDNXT FNDNEW Modifiers Begin End FNDNXT Modifiers Bueno Systems Inc TSL1401 DB 2009 10 01 Page 35 of 52 FNDNEW is a constant defined in the PBASIC code template as F8 and finds pixels or edges between pixels Begin and End inclusive Begin and End can range from 1 to 255 and Begin should be less than or equal to End Remember that binary pixel locations are one based not zero based The first pixel is pixel 1 FNDNXT finds pixels or edges between the current internal Left and Right limits whatever they might be These limits are set by the following actions whichever occurred most recently e Any acquire command which resets Left to 1 and Right to 255 e Any CNTNEW command Left ends up at Begin Right at End e AFNDNEW command Left is initialized to Begin Right to End e Any FNDNEW or FNDNXT command wherein Left is moved to the found pixel or edge searching forward or Right is moved to the found pixel or edge searching backward If the desired feature is not found Left will equal Right 1 causing further counts and finds to return zero The modifiers that can be used with i e ORed to FNDNEW and FNDNXT are Name Value Description FWD 00
73. that will let you see what the TSL1401 DB sees in real time It s called TSL1401 monitor exe and it can be downloaded for free from the Parallax website Just copy it to the directory of your choice With the MoBoStamp pe TSL1401 DB the camera connected to your PC s USB port start up the monitor program After it makes a connection it will upload its own PBASIC code which replaces any program currently in memory Then you should see something like what appears on the next page Starting from the top here are some points of interest in the display some of which will be discussed in more detail later e Scan Window Every scan from the camera gets appended to this image from right to left circulating back to the left edge when the screen is filled By slowly rotating the camera on the motherboard s short axis you can obtain a two dimensional scene The bottom pixels in this window correspond to the leftmost pixels obtained in each scan e Trigger Sync Control Scans can be free running triggered or gated Triggering and gating are controlled by Pin 3 which is a BS2pe port common to both daughterboards When triggering is set a high to low edge on Pin 3 will cause a single scan to be acquired When gating is set scans will be obtained continuously while Pin 3 is low Using the Ext Sync button scans can also be synchronized to an external source The signal for this is obtained from the onboard 6 pin Bueno Systems Inc
74. the duration specified by the lower seven bits This time is approximate and is governed by the AVR s internal RC clock So it will vary somewhat with temperature Here s some sample code that causes the LED to strobe for about 1mS at the beginning of each exposure OWOUT owio 0 SETLED TIME 75 TIME is a constant defined in the code template that equals 80 In its place the constant INTEN may be used It s equal to zero and does nothing but it makes the code more readable Bueno Systems Inc TSL1401 DB 2009 10 01 Page 29 of 52 Acquiring an Image Image acquisition is done using the various acquire commands each having the same general format Acquire Acquire XTRIG They all take no arguments and come in eight flavors which are described in the table below Command Value Description ACQGRAY AO0 Acquire a binary image while dumping all 128 grayscale pixel values ACQBIN A4 Acquire a binary image replacing the previously acquired image Acquire a binary image ANDing the binary pixels with the previous image ACQAND A1 This can be used to track which bright pixels are common to all successive scans Acquire a binary image ORing the binary pixels with the previous image ACQOR A2 This can be used to track which bright pixels appear in one or more successive scans Acquire a binary image XORing the binary pixels with the previous image ACQXOR A3 T
75. the error flags OWIN owio 0 flags IF flags SFF THEN If SFF driver is waiting for a reset OWOUT owio 1 DUMPFLAGS So reset and try again OWIN owio 0 flags NDIF ETURN Zu Bueno Systems Inc TSL1401 DB 2009 10 01 SEE SUD ROUGH Cie SESSE IE EE EE EE ee TEL SE Page 48 of 52 Command Summary XM Buf 2 ms 3 Re Name Val Description Fer Busy Modifiers Args sults DUMPID epp Dumps two letter ID and No No None None 0 version byte DUMPFLAGS DF Dumps single error byte No No None None 0 Dumps memory beginning at DUMPADR DA Addr until reset No No None Addr 0 Set exposure time to SETEXP EE ExpTime Yes No None Exp 0 wee Thld SETBIN EC nd UE Yes No None Hyst 0 j Mode Set LED brightness 5 SETLED EB INTEN Value or strobe Yes No TIME a Value 0 time TIME Value ACQGRAY A0 Acquire binary image dump No gray values ACQBIN A4 Acquire binary image ACQAND A1 Acquire and AND new binary image to old image Acquire and OR new binary ACQOR A2 image to old image ACQXOR A3 Acquire and XOR new binary Yes Yes XTRIG 08 None 5 ACQDIFF image to old image Acquire and AND new binary RERNOTAND See image to NOT old image Acquire and OR new binary ACQNOTOR PAP image to NOT old image ACOXORNOT A7 Acquire and XOR new binary ACQSAME image to NOT old image Count pixels
76. to the formula Float Float Pixel Float 1 gt gt Filter Therefore when Filter equals 0 Float takes on the value of the pixel itself When Filter is a larger number Float becomes a moving average whose time constant increases as Filter increases The new value of Float is used to process the next pixel Each pixel is then compared with Float Threshold Hysteresis if the previous pixel evaluated to a 0 or with Float Threshold Hysteresis if the previous pixel evaluated to a 1 The preceding discussion assumes that Level thresholding is in effect If Window thresholding is selected instead the upper and lower thresholds are computed as described above but each pixel is assigned a O if it s value lies between the two thresholds and a 1 if it s value lies above the upper threshold or below the lower one i e outside the window formed by the hysteresis band Bueno Systems Inc TSL1401 DB 2009 10 01 Page 28 of 52 Here is a chart that shows how each binary acquisition mode might be used Threshold Comparison Filter Application Fixed Level Looking for objects and edges when lighting is even Fixed Window Looking for intensity excursions outside a certain band Floating Level 5 7 Looking for objects and edges when lighting is uneven Floating Level 2 4 Edge location among widely varying pixel values Floating Window 0 1 Looking for texture whose minimum intensity is d
77. type dark light and an edge of the opposite type Value The intensity of a pixel or collection of pixels Location The pixel index 1 255 of the selected feature or zero if the feature wasn t found Count The number of features meeting the specified conditions Area The number of dark or light pixels encompassed by the selected feature Extent The number of total pixels encompassed by the selected feature When experimenting with the various measurement options it s often handy to freeze image acquisition using the Stop or Single button That way you can adjust the measurement parameters with an image that s not itself changing Now let s explore image analysis using a real life example bottling juice Before the bottles are cased the bottler wants to know two things a is the bottle full and b is the cap on In this example bottles will be passing between a backlight and the camera So the camera will be looking through the bottle towards the backlight This is what it will see To the right of this image is a rotated scope display showing what the TSL1401 DB sees Lighter is to the left darker to the right Bueno Systems Inc TSL1401 DB 2009 10 01 Page 20 of 52 Clearly visible are the shadows created by both the cap and the meniscus at the liquid level Although the juice in this example is colored such a sharp meniscus will be present even with water clear liquids By measuring both the position of the meni
78. unt pixels edges between current bounds Find first pixel edge between new bounds Find first pixel edge between current bounds CNTNXT FNDNEW and FNDNXT Continue from where last CNT or FND left off Search backward Search forward Target is a dark pixel Target is a bright pixel Target is a bright to dark edge Target is a dark to bright edge Dump data beginning at addr and until reset ts for single byte arg following DUMPADR Beginning of binary pixel buffer 32 bytes Beginning of results buffer Value of darkest pixel 0 255 Location of darkest pixel 0 127 Value of brightest pixel 0 255 Location of brightest pixel 0 127 Average pixel value 0 255 Dump the firmware ID returns 3 bytes Dump error flags returns 1 byte returned byte Unrecognized command Attempt to buffer unbufferable command Command buffer overflow Result data buffer overflow Wait for AVR to finish reset Bueno Systems Inc TSL1401 DB 2009 10 01 Page 47 of 52 sesse Program Code Your program code goes here MNS ARG CVG EES SEE EE EES ESE OE Kp a ea EE EE Ke Wait for the driver to become not busy Ready DO OWIN owio 4 busy Read busy bit LOOP WHILE busy Keep checking until it goes low RETURN Ue ita iit GC ets Oe eee E Read the error flags from the driver GetError OWOUT owio 0 DUMPFLAGS Read
79. you have the MoBoStamp pe BASIC Stamp 2pe motherboard you can skip this section and proceed to the section titled Operation with the MoBoStamp pe Connection The following illustration shows how to connect the TSL1401 DB to a BASIC Stamp using Parallax s Board of Education and a DB Expander board 00000 mp imir E 00000 LI OUUULU OO O m 00000 OO 00000 O O L1 LI O OOOO LI OOOO LI OOO ad OO OO OO OOO Uo OOO OOOOO O m m0000 OOOOO OOOO The signal pinouts and port usage shown above are consistent with the examples to follow in this section You can also use the DB Extension Cable p n 500 28301 to separate the TSL1401 DB from the DB Expander board if you need to Image Acquisition The output of the TSL1401R is an analog signal but the BASIC Stamp does not have analog input capability except via RCTIME which isn t fast enough to read 128 pixels How then is it possible to use this device with a BASIC Stamp We do it by connecting the AO signal directly to one of the Stamp s digital inputs When done this way the BASIC Stamp will threshold the analog input Anything over about 2 volts will read as a 1 anything under as a O By treating the signal this way it s possible to input a string of 1s and Os that represent light and dark portions of the scene being recorded A com
80. ys to acquire images with the TSL1401R continuous and one shot In continuous imaging the SI pulses occur in a steady stream with 129 or more pixel clocks in between during each exposure interval To acquire an image you need to wait for the next SI pulse time before clocking out Bueno Systems Inc TSL1401 DB 2009 10 01 Page 3 of 52 the pixels that constitute the image These pixels will represent the light received during the previous exposure interval Waveforms illustrating this method are shown below Clock out scan n 1 Clock out scan n Integrate scan n Integrate Scan n 1 SI CLK 1 2345678910 129 1 2345678910 M 129 Tri Tri Tri A0 Stated Stated Stated In one shot imaging the TSL1401R is left idle until it s time to snap a picture Then SI clocked in and 128 pixels are rapidly clocked out and discarded Then you simply wait until the desired exposure time since the SI pulse has elapsed and pulse SI again At this point you can clock out the pixels resulting from the timed exposure Here is a sample waveform Clock out garbage si IDLE Integrate scan Clock out scan IDLE CLK 1 129 1 2345678910 vis 129 Tri Stated Tri Stated Tri Stated A0 In all the discussion that follows we will be using one shot imaging Bueno Systems Inc e TSL1401 DB 2009 10 01 Page 4 of 52 Operation with the BASIC Stamp This section explains how to use the TSL1401 DB directly with a BASIC Stamp If
Download Pdf Manuals
Related Search
Related Contents
取扱説明書 - 三菱電機 Samsung YP-T9BAB Manual de Usuario USER MANUAL EdU Click HTS Manual 桜島火山における各種微動の伝ぱ速度 IVT Bluetooth Wireless Solution for WinCE TLS-4000 Technical Service Manual 2007 Hawkeye 2x4 and 4x4 Owner`s Manual Folex IP TOP COLOR Ceci - Plonger net Copyright © All rights reserved.
Failed to retrieve file