university of idaho ece senior design project final ... · university of idaho ece senior design...

86
University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 Sponsor: Synthetic Industries – Genesee Division Advisors – Jim Frenzel, David Egolf Dani Cook Mark Hutchinson Brandon Prescott [email protected] [email protected] [email protected]

Upload: vannguyet

Post on 01-Jul-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

University of Idaho ECE Senior Design Project Final Documentation

for

Defect Detection System10 December 2004

Sponsor: Synthetic Industries – Genesee Division

Advisors – Jim Frenzel, David Egolf

Dani Cook Mark Hutchinson Brandon Prescott [email protected] [email protected] [email protected]

Page 2: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

i

TABLE OF CONTENTS

I. INTRODUCTION ………………………………………………………………1

II. PROJECT DESCRIPTION……………………………………………………... 1

A.Problem Statement………………………………………………………...1

B.Objectives………………………………………………………………… 1

C.Functions and Specifications……………………………………………... 1

1.Detection……………………………………………………………...1

2.Alert………………………………………………………………….. 2

3.Halting the Machine…………………………………………………. 2

III. PROJECT STATUS…………………………………………………………….. 2

IV. METHOD OF SOLUTION……………………………………………………...2

V. MANUFACUTURING AND SUPPORT……………………………………….3

A.Product Life Cycle Plan…………………………………………………...3

B.Research and Development………………………………………………. 3

1.Software Development………………………………………………..3

2.Lighting Design……………………………………………………… 4

3.Communications……………………………………………………... 4

4.Image Treatment……………………………………………………... 4

C.Active Phase……………………………………………………………… 4

D.Functionally Stable Phase…………………………………………………4

E.Maturity……………………………………………………………………5

F.Retirement…………………………………………………………………5

VI. FAILURE MODES, EFFECTS AND CRITICALITY ANALYSIS……………5

A.Failure Rate Calculations………………………………………………… 5

B.Failure Modes and Effect Analysis………………………………………..6

1.Potential Failure Modes……………………………………………… 6

2.Assign a Severity Rating for Each Effect……………………………. 6

3.Assign an Occurrence Rating for Each Effect……………………….. 6

4.Assign a Detection Rating for Each Effect…………………………...6

5.Calculate the Risk Priority Number for Each Effect………………… 6

6.Prioritize Failure Modes for Action…………………………………. 6

Page 3: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

ii

VII. SOCITIAL CONCERNS…………………………………………………… 7

VIII. CONCLUSION……………………………………………………………... 7

IX. ACKNOWLEDGEMENTS…………………………………………………….. 7

X. REFERENCES…………………………………………………………………..7

XI. APPECENDICIES……………………………………………………………… 7

A. Specifications……………………………………………………………. A-1

B. Bill of Materials, Data Sheets…………………………………………… B-1

C. Test Data …………………………………………………………………C-1

Section 1. Lighting and Filtration Testing ………………………….. C-1

Section 2. Software Testing………………………………………….. C-2

Section 3. PCB Waveforms………………………………………….. C-4

D. Users Manual……………………………………………………………. D-1

E. Software………………………………………………………………….. E-1

Section 1. Program Control Structure……………………………..… E-1

Section 2. Quality Checker.cpp . ………………………………..….. E-2

Section 3. Checker.h.………………………………………………… E-21

Section 4. Resource.h.……………………………………………….. E-24

F. Installation Instructions.…………………………………………………..F-1

Section 1. Lighting …………………………………………………...F-1

Section 2. Camera …………………………………………………… F-1

Section 3. Software …………………………………………………. F-1

Section 4. PCB………………………………………………………..F-2

Section 5. Relay ……………………………………………………... F-2

Page 4: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

1

Abstract—This is a comprehensive report covering the Defeect

Detection System, designed for Synthetic Industries, by a seniordesign team at the University of Idaho, to be implemented attheir Genesee Division. Included in the report are thedevelopment of the project, specifications, test and technical dataand a users manual for the system. Additionally, information isincluded that will facilitate the continuation of this project in thefuture.

I. INTRODUCTION

HIS report details all aspects of the development andimplementation of the Defect Detection System. The

initial need for the system came from Synthetic Industries inlooking at methods for adding an additional level of qualitycontrol in their manufacturing process. Because of theextream environment in the production facility, caution wastaken with the design of the system so as to minimize the riskof environmental interaction thereby posing a threat to thestaff and property.

The design underwent many changes before the finalimplementation scheme was chosen. The basic sub systems ofthe project include software analysis, lighting and imagefiltration and component communication/interface. Currentlythe system has been fully constructed and implemented at theproduction facility. The life cycle analysis section of thisreport demonstrates how this project would be suited for massmarketing and potential uses in other manufacturing arearequiring image analysis and defect detection.

In its current developmental state, the system is capable ofdetecting various sizes of defects both in straw and coconutbased blankets. Several latter desires expressed from SI wereaddressed in the design of the system. This version of thesystem does not interface with the production machinery tohalt manufacturing when a defect is found. Instead, a visualalert was installed that responded to the system’s detection ofa defect. Instructions and wiring diagrams are provided in theappendices to facilitate SI to eventually implement the

Manuscript received December 10, 2004. This work was supportedbySynthetic Industries, Genesee Division.

D.M. Cook is with the University of Idaho Senior Design Team. Moscow,ID 83843 USA (phone: 208-301-4135; e-mail: cook0135@ uidaho.edu).

M.D. Hutchinson is with the University of Idaho Senior Design Team.Moscow, ID 83843 USA (e-mail: hutc8408@ uidaho.edu).

B.J. Prescott is with the University of Idaho Senior Design Team. Moscow,ID 83843 USA (e-mail: pres0293@ uidaho.edu).

machine halting capabilities of the system. To promote thecontinuation of this project, all relevant information has beencollected and organized to allow for a secondary student teamto resume work from where this project is currently.

II. PROJECT DESCRIPTION

A. Problem StatementOperating on-site, the system must be automated in that is

electronically detects individual and patterned flaws in theblankets before they reach the stitching bar and become apermanent defect. The detection system must be able tooperate independently of the machine’s normal functions andnot impede the manufacturing process in any way. A visualalert must be activated in the production facility to notify thestaff of the defect in the blanket. With the dusty environmentthat exists within the production facility, the detection systemmust minimize any dangers that would arrive from theinteraction with the dust, including electrical and fire hazard.

B. ObjectivesOperating on-site, the system must be automated in that is

electronically detects individual and patterned flaws in theblankets before they reach the stitching bar and become apermanent defect. The detection system must be able tooperate independently of the machine’s normal functions andnot impede the manufacturing process in any way. A visualalert must be activated in the production facility to notify thestaff of the defect in the blanket. With the dusty environmentthat exists within the production facility, the detection systemmust minimize any dangers that would arrive from theinteraction with the dust, including electrical and fire hazard.

C. Functions and Specifications1) Detection: The lighting system is to provide more

intensive lighting that the ambient lighting to enhancement ofthe image for better quality analysis. The light fixtures musthave low heat emission, be dust proof and be mounted in amanner that allows them to be easily removed for machinemaintenance. Filtration of the image will be done with a red-tinted lens mounted on the camera. The lens is used to createa color differential between the product and the belt to providethe most contrasted image for analysis. The use of the digitalcamera is to provide a steady stream of data for real time

Defect Detection System Final Report(December 2004)

Dani M. Cook, Mark D. Hutchinson, Brandon J. Prescott

T

Page 5: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

2

analysis. The last component in the detection phase of thesystem is the PC and detection software. This system is tocapture information from the video camera and conduct imageanalysis of the blankets.

2) Alert: One a defect has been found it is the PC’s job tooutput a signal to the communication circuit. Additionally,the PC must real-time on screen feedback of the blanket imagefor visual interpretation of the defect. The communicationscircuit serves as an intermediary between the PC and the relay.It accepts the defect signal from the PC and accordingly sendsthe proper signal to the relay for alert activation. The visualalert in the production facility is a rotating amber-colored lightthat is bright enough to gain the attention of the staff.

3) Halting the Machine: The communications circuit willagain be the intermediary for the PC and the relay. Here therelay will be wired to accept the signal from thecommunication circuit and the to activate in such a manner asto shut down the power to the machine, in turn stoppingproduction.

III. PROJECT STATUS

All of the systems in the project have been completed anddemonstrated to work properly (Fig. 1 Functional BlockDiagram of Project Status). By instruction from SI, theoriginal objective of interfacing our system so as to halt themachines was delayed. It was SI’s wish that our system onlytriggered a visual alert when a defect is found rather thanhalting the machine. This was to allow the staff in theproduction facility to become acclimated to the functioning ofour system. Information has been included in this report thatdetails the necessary procedures for updating our system toallow it to meet its original objective of halting the machine.

IV. METHOD OF SOLUTION

Initially, we thought about using photo-optics to sense thinareas in the blanket, but soon realized that it would onlyprovide linear information. Since the defects are 2-dimensional, we would need to store the data for a certainamount of time. This would develop an array of data in 2-dimensions that would be a crude picture. This was the pointwhere we realized that we could just take a picture with adigital camera and analyze the data from that.

Next we needed to figure out how to emphasize thedefective areas. This involved researching lighting, filtration,and camera options. Halos from light reflecting off of dustparticles became a major concern. If a halo appeared above adefect, the defect could potentially be missed. The lightingand filtration would have to eliminate the possibility of this.

Another item of concern was how to get the data from thecamera into the computer. Initially we considered using a

wireless transmission, but later decided on using coaxialcable. Along with this was the decision to take the imageusing a digital camera that sent the image as an analog signalthrough the coaxial cable. This was due to the reluctance ofdigital camera manufacturers to divulge the “proprietary” datatransfer protocol information. By using an image capture cardinstead, we bypassed this issue. Matrox makes several capturecards, and they provide a library of functions to control thecards.

Fig. 1. Functional Block Diagram of Project Status

Once an image was captured, we needed to have the systemanalyze it. We decided that a simple approach would be thebest bet for our first attempt. The analysis would consist ofcounting all of the dark pixels in a square area of the imageand comparing it to a “threshold” value to see if it was goodor not. As it turns out, this approach works very well. It isvery costly in terms of having to repeatedly check all of theimage data, but it will find all areas in which the thresholdvalue is exceeded. It typically finds a single defect multipletimes, but once is enough to trigger the system to stop.

After the image is processed, it must send a signal to stopthe machines if a defect was detected. We initially weren’tsure how we would implement this, but finally decided onserial communications through an RS232 cable. When adefect is found, the PC sends a repeated pulse through theserial port to a relay via an amplifier circuit to stop the system.

Designed and working

Designed but notworking

Designed but not tested

Project Status

Software Analysis

Lighting andFiltration Scheme

PCBCommunications

Relay Interface

Production AlertLight

System Interfacefor haltingmachine

None

Hardwiring fromproductin tolead's office

Overheadcamera image

capture

DDS systemoverride switchin production

Second systemimplementation

on BT1

Page 6: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

3

The pulse is sent 35 times in order to have adequate current tothe relay that shuts off the machinery. After this, the PC stopssending the signal until the defect condition clears and a newdefect appears. This prevents overheating the transistor in theamplifier circuit.

Many changes both large and small were made to thedesign during the development process. Most of the changeswere simplifications to original plans. (Using an RS232 cablestraight to an amplifier circuit versus using a microcontrollerbefore the amplifier circuit.) Some of the changes addedcomplexity but increased flexibility of the system. Theseincluded all of the functions added to change variables intuning the sensitivity and search areas of the scanningprogram. They weren’t in the original plans, but were addedto give greater flexibility to the operators and make thesoftware more useful.

We also discovered that the polarizing filters weren’tneeded to remove halos after the lighting system was put inplace. Since the lights were mounted at 65 degrees from theblanket, dust particles don’t reflect light directly back to thecamera. We did discover that the overhead lights cause thefluorescent lights to cast slight shadows on the blanket. Thisis an area that will need to be improved in the future. Itwasn’t very noticeable to the human eye, but you can see itvery plainly through the image from the camera. In order toget a good quality image, the lighting must be uniform acrossthe entire search area. Software could also be designed tocompensate for this, but would take several months toimplement properly.

V. MANUFACTURING AND SUPPORT

A. Product Life Cycle PlanThe product life cycle plan illustrates the stages of the

Defect Detection System (DDS) from inception to retirement.Within each stage of the life cycle the product can experiencechanges in concept, user, unit cost and revenue and goesthrough the phases of planning, research and development,active phase, stable phase, mature phase and retirement. Theplanning section encapsulates the initial ideas and desires ofthe customer, research and development expresses the thoughtprocess that the designers went through to achieve the desiredresults and the active phase shows the expected outcome forthe finalized product. At this point, all of the mass-productioncomponents are assumption based, as this product does notcurrently have any future plans for a world market debut.These assumptions carry the life of the product through thestable phase at which the product has proven itself ready formass-production and the planning for such a sales volume iscontemplated. From there, the product moves into a maturestage in which it has been in the market for a number of yearsand is in need of revision to accommodate changes intechnology and possible application. After that, the product

enters retirement and the effects of this on the user and vendorare considered.

B. Research and DevelopmentInitial design solution paths included: pneumatics,

photocells, rollers and digital cameras. A design solutioninvolving digital video cameras was chosen based on a designselection matrix. Ease of design, reliability, cost, complexityand impact on manufacturing process were elements that weretaken into consideration in the selection matrix. With thesolution path chosen, the project branched into three areas:Software Development, Lighting Design, Communicationsand Image Treatment.

1) Software Development: The design of the software isintended to use the least complex algorithm possible to detectdefects in the blankets. The basic technique is to look atsquare “boxes” in the image, and determine whether or not thebackground color value exceeds a predetermined percentage.This is much easier than trying to locate specific shapes andanalyze an image according to patterns, especially since thereis no limit to the possible number of shapes a critical defectcan take. If the algorithm is fairly simple, it can be moreeasily modified in the future. This may be necessary if themanufacturing process changes to use different materials or adifferent setup.

In order to make the software more flexible, the designneeds to take into account the fact that the manufacturingprocess may speed up over time. If this occurs, the softwareshould still be capable of detecting defects without modifyingthe code. This means that planning is required to figure out,in advance, what the highest production speed may be. Achange in materials could also cause problems, but it would bedifficult to quantify every possible material that may be usedin the future. The likelihood of a material composition changeis low enough that we will not worry about adding extrafeatures for changing to new materials. The design will stillallow using straw, coconut fiber, and a combination of thetwo. It just won’t have a feature to allow for using wet leavesor any other material they may think up in the future.

Maintenance of the software should be relatively easy afterit is fully developed. The brunt of the testing is going onduring development to ensure proper operation and design ofthe software. A few modifications have already been made toensure more stable operation of the software, includingseparate data arrays for images from the different cameras.This prevents overwriting data before it gets a chance to beanalyzed. The software will also be easy to install on a newcomputer if necessary. The most complex part is installing theimage capture card and driver software. The defect analysisprogram can just be copied onto a new computer as a regular“.exe” file.

If the software requires modification in the future, there is acopy of Visual Studio with Visual C++ 6.0 installed on thecomputer. It would require a programmer familiar with the Cprogramming language, and the Windows API. This may be a

Page 7: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

4

potential pitfall, but it shouldn’t be a serious problem. Sincemost of the code is already written, it may only require somemodifications to the code. That is usually easier thanrewriting an entire application. This is especially true if thecode is written in modules. It is much easier to rewrite a smallmodule.

The highest cost of the software is in the initial design andtesting if it is developed correctly. If not, maintenance andrewriting will become a high drain on profitability. Thismeans it will be extremely important to make the softwarework right by the time the design is completed.

2) Lighting Design: Primary research focused on color andspectral qualities for various types of lighting. Fluorescentlighting was chosen because of its range of availability, lowcost and sustainability. The commonplace nature offluorescent lighting allows for ease of replacement for bulbsor fixtures. Fixture mounting brackets were then designed tofulfill the design requirements. The brackets were designedusing preexisting component too allow for ease orreplacement or reproduction.

3) Communications: Image data needs to be transferredfrom the cameras to the computer. Initially we consideredusing a wireless setup, but it would be very complicated toimplement, and might become cost prohibitive. We found thatdigital cameras that transmit an analog signal through acoaxial cable are a much simpler solution to this problem.The analog signal is then converted to a digital image by animage capture card in the computer. The only problem left toovercome is how to control the capture card from the analysisprogram.

Once the images are analyzed, communication needs to takeplace between the PC and the outside control system. It lookslike this can be accomplished with a serial cable and somesimple logic or a microcontroller. We are still looking at whatto do for this part, but we would like to find a cheap, simple,elegant solution. The basic signal will be a 2-bit signal. Eachbit will represent an On/Off signal to relays for the BT1 andBT2 controllers. We just need to find a way to implement thisreliably, since the serial cable signal won’t hold a constantvalue for us. It sends a byte in pulses, but then drops low afterthe transmission is complete. We need to latch the datasomehow, or as we initially proposed, read it with amicrocontroller and set output pins high or low. We are nowresearching possible RS232 transmission options.

4) Image Treatment: Capturing an image for analysisrequired two visual manipulations, the primary being thecreation of a black and white color differential between theviewed items and the second, a reduction of the halos createdby reflections from the dust particles in the air. In order toestablish a variance between the conveyor belting and theproduct, an RGB color analysis was done on the two products(coconut and straw)1. It was found that both products hadonly a similarity in red light reflection values, therefore a red

1 Note: An RGB analysis was not preformed on the belt because it is blackand therefore does not reflect light.

filter was used. The second issue of halos was corrected byadding a polarizing lens in front of the red filter. Now thecamera sees the translated image that is first polarized thenfiltered and read in as a black and white image resulting in acontrast enhanced image which is ready to be analyzed.

In the event that this system sees mass production, many ofthe design choices lend themselves to revenue maximization.Many components within the system are components that arealready designed and purchased from a company. This allowsfor ease of reproduction when acquiring different parts.Instead of needed to build every component in the system,many can simply be ordered and installed immediately uponarrival. Additionally, cost savings will be realized throughbulk purchasing.

C. Active PhaseThe DDS system is designed to minimize the amount of

human interaction required to keep the system operational.Training will be necessary to educate the work force on theoverall operation of the system and what support will berequired to maintain normal system operations. The onlycosts during normal operation of the DDS system, powerconsumption of the subsystems, will remain relatively low inrelation to the cost of operating the entire production facility.Viewing the DDS as an embedded system within the totalmanufacturing process reliability becomes a vitally importantconcern. A bypass mode has been designed to ensurecontinued production of the blankets following a DDS systemfailure. However, the potential loss in quality detection of theblankets during a DDS failure must be viewed as anopportunity cost that increases in proportion to the length ofdown time to allow for system repair.

D. Functionally Stable PhaseBarring systems break in time, once the system

demonstrates continued operating stability, little support willbe needed. The system was designed using a “plug and play”type interface. All of the physical components are commonand easily purchased items making for simplistic repairs. Thesystem manual will become the primary source for systemsupport and maintenance information and will include anidentified list of components as well as their specifications inthe event that an alternative product should be desired. Atesting/troubleshooting section will also be included toenhance the end user’s ability to identify problem sources in aquick and efficient manner. The utilization of a technicalsupport staff would not be employed due to the fact that itwould not be a cost effective method of dealing with thecustomers’ problems. Even if mass-produced, this productwill not see a high sales volume and is fairly inexpensive toreproduce, therefore it would be a lesser cost to the vendor toreplace a customer-defined defective system than fund asupport staff. We also expect the product’s end environment

Page 8: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

5

to be an industrial setting and expect a fair amount oftechnical support to be done by the end users themselves.

At this stage, the product can be used to create a profit ifmass-produced. It has passed the trial run of the active phaseand has proven itself worthy to send out to the customer. Theprofit will ultimately depend on the market demand andcompetition of similar products, but assuming it is industriallydesired, the profit margin could be high. To produce alternatesystems, one would only need to purchase the listedcomponents and ship them with the software and aninstruction manual. Savings could be realized if purchaseswere made in bulk, and could be run in a low overhead settingsuch as by one individual with a web-based business.

E. MaturityWhen the system reaches maturity, it may need some

modifications in software to extend its usefulness. This couldmean adding functions for new product lines, or modifying theanalysis speed of the system. For the most part, though, thesystem will still do what it was designed to do. By this stagein the product’s life, there may be competing systems that aremuch more compact and have more bells and whistles. Inorder to compete with that, costs may need to be reduced tokeep sales up. Fortunately, by this time the software shouldn’tcost anything more, and the components should cost lessthrough negotiations for bulk buying discounts. On thedownside, some of the components may no longer bemanufactured, and this may require redesign of some parts ofthe system. Stocking up on parts can alleviate some of thisbefore the parts are discontinued, but it means possibly gettingstuck with parts in inventory, and we could still run out of thecomponent. Most of the system allows for upgrading easily.The computer will allow upgrades to take place easily, andeven the operating system should allow the software tocontinue to work after new versions come out. The one catchis that the program itself may not be able to use a new capturecard without modification to the original code. All of thehardware is easily changed, but the software isn’t likely to beas easily upgraded.

Alteration costs would be reasonable for changing thehardware. If the alterations were to require altering thesoftware, the costs would be much greater. The hardware canbe easily taken out and replaced, but the software would needto be rewritten, compiled and tested. This is rarely ever asimple task.

F. RetirementOnce the DDS system reaches its retirement phase, its

removal will have a low impact on the total manufacturingsystem. The parallel nature of the DDS will allow the systemto be removed without impacting the other system functions.Many of the components in the DDS may be recycled as analternative for disposal. Total system replacement may bedesired at the time of the initial system retirement. If there is

no desire to incorporate technological advances into a newergeneration system, then the instruction and specificationmanuals may be used to recreate a duplicate system. Thesuppliers used to purchase each original system componentwill be included in the manuals to aid the in acquisition ofnew system parts.

VI. FAILURE MODES, EFFECTS AND CRITICALITY ANALYSIS

A. Failure Rate CalculationsBased on the designs of our subsystems, our failure rate

scores were assembled in several different ways including: theuse of the Prism software package, interpretation ofmanufacture’s data and specifications and analysis of thecomplexity of software algorithms.

The only data available from the lighting manufacturer [1]was the rated life span or mean time between failures. Thesevalues will be quite accurate as they are the product of themanufacturer’s own data and analysis.

Failure ratings for the camera and mounted vari-focal lenswere quoted on as an industry average from DVR SecurityCamera Systems, Inc. [2]. An average was desired due theexchangeability of components for this part of the system.The components for the amplification circuit were analyzedusing Relex Reliability Software V7.7. This product usesaverage values for the component types and calculates basedon quantity and historical failure ratings.

To determine the failure rate or MTBF of Windows XP, weneed to consider that this Operating System is one of the morestable versions of the Windows family. If the system islimited to running only one reasonably simple application,Windows XP should not be a high source of failure. We canestimate at least one year between failures for the WindowsXP Operating System.

The MIL-LITE library functions are in the 7th revision, somost of the defects in the basic functions should be correctedby now. The new revisions typically add more functions tothe libraries, so the basic functions such as frame grabbing,allocating arrays, and displaying images typically are notmodified. This means that there are not likely to be any newfaults introduced into the library functions that are going to beused in this project. We will then estimate about 5 yearsbetween failures due to the MIL-LITE library functions. Ifupdates are later installed, they could actually introduce moreproblems than they would solve.

The actual Defect Analysis Application Program that weare developing is the most likely source of software failure.Logical errors, poor scheduling of resources, and memoryleaks are the most likely problems. Of these potentialproblems, a slow memory leak could be the most severe. If asingle byte at a time is lost each second, it could take monthsor years before the system ran out of memory and began tomalfunction. A memory leak would likely be faster than onebyte per second. Most likely, memory would be lost at a ratethat could easily be seen using the GDI Object view of the

Page 9: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

6

Windows XP Task Manager. This would allow the programto be fixed before it was released for use. That would leavelogical errors as a more likely candidate for causing problemswith the program once it is put into service. These could besimple sign errors, or something as complex as allowingmultiple instances of a single thread to run concurrently.These types of problems could pop up and cause problems ifthey aren’t prevented explicitly in the design andprogramming phase. Testing may be capable of detectingsome of these problems, but the tests usually involve writingtest programs. The test programs themselves may or may notbe 100% reliable. For software, we can not just hook up adigital multimeter to the code and test it. For these reasons,we are estimating approximately an eight-month MTBF forthe application software. It will most likely be better thanthat, however it is best to do a worst case estimate. Failurerates were from the calculated MTBF data utilizing theequation:

MTBF1=λ (1)

This is a best guess provided data and thesimulation.

B. Failure Modes and1) Potential Failure M

over which informationable to produce a list ofthat are likely to failure addition to the informatsuch as environment consideration as likely the system. Presented b

Table 2: Potential Failure Modes) of likely failure modes forthe DDS system.

2) Assign a Severity rating for each effect: Based on theseries design of our system, impact to company andinteraction of system with employees, numerical values wereassigned to each failure mode. Included in the numericalrepresentation is the thought as to the ease of repair for eachfailure mode. The numbering is ordered such that failuremodes having a great impact on the manufacturing processwere given a high value and modes with a lesser impact weregiven a low score.

3) Assign an Occurrence rating for each effect: Based onthe analysis and research of system components, coupled withthe operating conditions in which the system will installed,numerical values were assigned to each failure moderepresenting the likelihood of each even occurring during thesystem’s life span. High numerical values indicate the largepotential for that specific failure mode’s chance of occurring,while small values represent modes that are unlikely to occur.

4) Assign an Detection rating for each effect: Introubleshooting a problem with any system, the ability to testand detect the failed component is vital for the continuedsuccess of the project. Based on the design of the system andthe end users, numerical values are assigned to each failuremode representing a trained employee’s ability to find thefailed component of the system. High numerical valuesrepresent ease of detection for a particular failure mechanismand low numbers represent difficulty.

5) Calculate the Risk Priority Number for Each Effect: Withall three numerical values assigned to each failure mechanism,the risk priority number (RPN) can be calculated with thefollowing equation [3]:

)(*)(*)( FDFOFSRPN = (2)Where FS = Failure Severity

FO= Failure OccurrenceFD= Failure Detection

RELIA

Part Fa

ComputerHardware 38.051Windows XP 114.15MIL-LITE 22.831Matrox Card 22.831Defect Software 173.61

OpticsCamera 50E-6Vari-focal Lens 12.5E

LightingBulbs 55.56EBallasts 2E-5

Amplifier SystemCircuit 22.221Relays 97.262

TABLE IBILITY SCORES

ilure Rate MTBF (hours)

8E-6 262805E-6 87601E-6 438001E-6 438001E-6 5760

20000-6 80000

-6 1800050000

estimate based on the manufacturer’s evaluation of software and Relex

Effect Analysisodes: In looking at the path or loop

would travel in our system, we were problem areas or parts of the systemduring the life span of the system. Inion paths of the system, other factors

and employees were taken intocandidates for catalysts for failure ofelow is the randomly ordered list (see

After we calculated the RPN’s for each failure mode, thelist was put into ascending order to show the failure modeswith the greatest risks at the top (Table 3: Failure Mode RiskPriority Numbers). Based on the order of the list, the systemwill be evaluated to assess potential solutions to reduce toRPN’s of the failure modes appearing at the top of the list.

6) Prioritize Failure modes for action: Looking at the topfour ranked failure modes, two of them (Physical Damage –Employee Based and Environmental Damage), are outside ofthe control abilities within the design process. To minimizethe damage from employees, training will be necessary toeducate the workforce on the operations of the system andhow to prevent conditions and situations that are likely todamage the system. The harsh nature of the environmentalcondition that exist within the manufacturing plant are againoutside of the control of our design team. The system hasbeen designed to cope with the environment by designing withcomponents such as sealed light fixtures and consideration of

E-9 45002200E-9 10281000

Page 10: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

7

component placement. Regardless, the environment willcontinually pose a serious threat to the operation of the DDSsystem.

The other two failure modes in the top four, are computerhardware failure and application flaw or memory leak. Thepotential for a memory leak situation will be addressed duringthe testing and debugging stage of the software’sdevelopment. Any potential problem will be addressed andcorrected to minimize this threat during normal systemoperation after the installation in the factory. Lastly, computerhardware failure will be addressed in the users manual.Information will be included that details the warranty on allpurchased components and contact information of vendors forpurchasing replacement parts.

VII. SOCIETAL CONCERNS

Disposal fees should be reasonable for this system. Sincethere is only a single computer tower and monitor, there arenot any special disposal requirements at the present time. Thelighting and cameras do not contain any parts that wouldrequire special disposal either. No hazardous components areused in the design. This means that even though there may be

waste generated in disposwaste won’t be classified acomponents may be reusedThis allows for recycling computer, monitor, and becomes completely obsolof functionality and couldapplications.

CBased on the desires a

DDS team was able to cre

the client. The system had much robustness and durabilitybuilt into it so as to assure its continued operation and thesatisfaction of the client. While there is room foradvancement and adjustment in the system, the current statusof the system is operational. SI now has the ability to use thesystem to begin a third level of quality control assurance.Future endeavors on the systems will able to use this report asa source of information from which to make improvements.

ACKNOWLEDGMENT

The DDS group would like to thank the following peoplefor their help and the knowledge they provided towards thecompletion of this project: SI Corporation, Jim Frenzel, DavidEgolf, Keith Prisbrey and Greg Klemesrud.

REFERENCES

[1] “T8/H0 Ballast: Mark III*, Standard Electronic and Centium* ElectronicApplication and Specification Guide,” [Online document], 2002, [cited2004 Oct 16], Availablehttp://www.advancetransformer.com/literature/pdf/RT-8040-R01.pdf

] “ii Digital Camera System Indicators International” [Online document],2004, [cited 2004 Oct 16], Available http://www.indicators-int.com/pdfs/camera.pdf

] D. Anderson, “Hazard Analysis in Engineering Design,” [Onlinedocument], 2001 [cited 2004 Oct 16], Available HTTP:http://www2.latech.edu/~dalea/instruction/hazards.pdf W.-K. Chen,Linear Networks and Systems (Book style). Belmont, CA: Wadsworth,1993, pp. 123–135.

FMEA Seve

Phys. DamageComp. hardware failure 1Memory LeakEnvironmental damageFixture ballast failureBulb burnoutCamera FailureVari-focal lensAmp. system failureMIL-LITE library failureRelay failureOS crash - latch heldMatrox card malfunctionComm wire disconnect"Dirty" PowerWindows XP Failure

[2

[3

TABLE 3TABLE RATINGS

rity Occurrence Detection RPN

9 8 9 6480 2 9 180

8 4 5 1607 8 2 1122 6 9 1081 10 10 1007 2 6 847 2 6 849 3 3 819 1 8 723 3 8 727 1 9 638 2 2 322 2 8 322 6 2 243 1 5 15

al of failed or replaced parts, thes hazardous material. Many of the if they are taken out of the system.and reuse of components like thecameras. Even if the computerete as a PC, it still has a great deal be used as a controller for other

ONCLUSION

nd constraints set forth by SI, theate a solution that met the needs of

Page 11: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

A-1

Appendix A: SpecificationsLighting

• 120 V• Bi-pin• Industrial sealed fixture

Filter• 35 mm• Red (RGB [256,0,0])• Screw mount

Vari-focal lens• CS lens mount• 1/3” format• 3.5-8mm zoom

Camera• 24 V• Vari-focal lens• 1/3” CCD configuration• BNC connection• EIA standard• CS lens mount

Coaxial Cable• RG6• Solid copper conductor

PC• 2.5 GHz minimum• 40 GB hard drive minimum• 256 MB RAM minimum• CD burner

PCB• Minimum trace width =0.01” (Takes into account temperature effects)• High temperature solder

Relay• 24V activated DC coil• 2 NC contacts• Timing Module

Page 12: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-1

Appendix B: Bill of Materials

Component Manufacturer/Vendor

Contact Information Part/ Reference Number Cost per unit DataSheet PageNumber

Light Housing Alloway Lighting (208) 342-5328 DM 2 40 120 GEB 101S $120.00 B-2Light Bulbs Moscow Building Supply (208) 882-4716 CD-930039 $7.00 B-4Red Filter Edmund Optics www.edmundoptics.com NT46-545 $35.00 B-5Vari-focal Lens Direct LVS www.directlvs.com TG2Z3514FCS $68.00 B-6Camera Direct LVS www.directlvs.com LTC 0335/20 $95.00 B-7Coaxial Cable MCM in ONE www.mcminone.com 9116 $110.44 B-9PC Walmart www.walmart.com SR1103WM-B $448.00 B-10Matrox Card Matrox Imaging www.matrox.com METOR2/4 $595.00 B-11MIL LITE Library Matrox Imaging www.matrox.com MIL LITE 7.5 $600.00 B-14PCB Express PCB www.expresspcb.com (Send CAD file) $98.00 B-18D1,D2 Mouser www.mouser.com 1N4148 $0.50 B-19RB Mouser www.mouser.com 30BJ250-47K $0.22 B-23RC Mouser www.mouser.com 588-TCH35P-20 $5.66 B-24NPN Mouser www.mouser.com 2N2102 $0.70 B-26DB 9 Connector Mouser www.mouser.com 152-3409 $0.95 B-29Terminal Block Mouser www.mouser.com 1725656 $1.08 B-30Relay Adykes (208) 336-3988 CS7C-22E $68.00 B-31Timing Module Adkyes (208) 336-3988 CZA7-30 $34.00 B-32

Page 13: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-2

Page 14: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-3

Page 15: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-4

Ideal for areas with minimal natural light or where color is critical. Watts: 40Diameter: 1-1/2" (T12)> Base: Medium bi-pin> Bulb Life: 9 years* (15-20,000 hours rated

life), depending on bulb type.

Page 16: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-5

Mounted Color Filters [ Specification Table | Helpful Literature | Product Matrix | Technical Images ] • Improve Contrast • Isolate Spectral Regions/Colors • Precision Colored Glass

Ideal for machine vision, color filters are best used with black and white cameras to yieldincreased contrast and resolution. Utilizing the principal "Like Colors Lighten Like Colors" willmaximize results. These colored glass filters provide wide fields of view unobtainable withangle-sensitive filters. Daylight Blue filters increase color temperature from 3200K to 5500K(daylight). Color CCD camera settings (Red/Blue gain level and AWC) yield furtheroptimization when used in conjunction with lamp selection (quartz-halogen and fluorescent). All filters can be threaded together via identical male and female threads on each mount. Colorfilters can also be used in conjunction with mounted UV filters to block unwanted ultravioletlight and protect the filters in extreme environments.

Specification Table

Red Green Blue Daylight BlueMaterial Hoya R-60 Hoya G-533 Hoya B-440 Hoya LB-120Filter Thickness 2.5mm* 2.5mm* 2.0mm** 2.5mmTotal Thickness (T) 5mm 5mm 5mm 5mmSurface Quality 80-50 80-50 80-50 80-50Refractive Index, nd 1.53 1.54 1.55 1.51

Cut-OffWavelengths 600nm 520 / 550nm 395 / 480nm 340 / 465nm

Coating Both surfaces, single layer MgF2 anti-reflection coated

Notes *M22.5, M25.5, and M30.5 filters have a filter thickness of 2.0mm.**M49, M52, M58 blue filters have a filter thickness of 2.5mm.

Page 17: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-6

Page 18: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-7

Page 19: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-8

Page 20: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-9

Page 21: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-10

Compaq Presario Desktop PC With 17" FlatScreen CRT Monitor, SR1103WM-B

Product Information

System RAM: 256 Mb Hard Drive Size: 40 GB Hard drive Monitor: 17" Flat Screen CRT Monitor Processor Type: Celeron Processor Brand: Intel Processor Speed: 2.53 GHz Operating System: Microsoft Windows XP Home Edition Multimedia Drive: CD-Writer (CDRW) 48x32x48 Frontside Bus: 533 Front Side bus Video Memory: 64 MB Graphics Type: Integrated Intel Extreme GraphicsPCI Slots: 2 USB Type: USB 2.0 USB Ports: 5 Serial Ports: 1 Parallel Port: 1 Speakers: Yes 5.25 Total Drive Bays: 1

Page 22: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-11

Page 23: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-12

Page 24: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-13

Page 25: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-14

Page 26: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-15

Page 27: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-16

Page 28: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-17

Page 29: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-18

Page 30: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-19

Page 31: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-20

Page 32: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-21

Page 33: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-22

Page 34: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-23

Page 35: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-24

Page 36: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-25

Page 37: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-26

Page 38: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-27

Page 39: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-28

Page 40: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-29

Page 41: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-30

Page 42: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-31

Page 43: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

B-32

Page 44: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

C-1

Appendix C: Test DataSection 1: Lighting and Filtration Testing

Figure C.1: Coco Analysis

Figure C.2: Straw Analysis

Page 45: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

C-2

Section 2: Software Testing and Technical Data

Each software module has been tested to verify that it works correctly. The

biggest problem with testing the software running under Windows was the difficulty in

determining the time to process an image. When the capture card grabs an image, it

sends a 640 x 480 pixel image to a data buffer using DMA. This doesn’t require much

assistance from the CPU, but when the processing begins, the amount of data processed

will vary. The CPU will only analyze the data within a Frame of Interest, so it excludes

the areas of an image that have been masked off. Typically, the image being analyzed is

around 25 – 30% of the original image size. This increases the speed of processing the

image greatly by avoiding unnecessary repetitions of calculations.

The entire image (640 x 480) can be processed in under 250ms, so the scanner is

capable of grabbing an image every quarter of a second. I couldn’t pin down an exact

time for scanning the image though. I could get approximate times by decreasing the

time between scans until the scans no longer completed, but Windows tends to ignore

duplicate Timer messages. Testing the code extensively is much tougher than developing

the code in the first place.

In an effort to conserve CPU time by minimizing cache misses, the code was

developed to make use of spatial locality. This meant that to check all the pixels in a

square box, they would need to be summed up carefully. I used summing arrays to

accomplish this. First, the data was summed up horizontally and saved in a scanning

array. The scanning array is then summed vertically, adding each element in a row to the

element in the row above it. (This is less costly in cache misses than just summing

vertically in one column, moving to the next column and summing everything vertically,

etc….) The cache misses will still occur, but they are much less frequent when

processing in this manner.

One additional way to speed up the processing would be to perform the horizontal

summing, and then save the data from the results vertically. When it came time to sum

the data again, it could be done horizontally again, minimizing cache misses even more.

I didn’t think of this until after the algorithm was completed, so it wasn’t implemented.

Page 46: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

C-3

The Matrox library functions spawn 18 threads of their own when the program is

running. This really doesn’t hurt system performance though. Many of the threads only

handle brief operations and therefore don’t require much time from the CPU.

The Scanning thread grabs an image from the capture card each time the System

Timer goes off. The main thread also grabs images from the Matrox card. To prevent

both threads from trying to grab an image at the same time, the functions in the main

thread that grab an image from the card will disable the scanning thread first. This way

there are no conflicts in which thread will get to use the capture card at any given time of

request.

The default settings for the program are as follows:

Contrast is 155

Threshold is 4600

Defect size (Win_Width) is 85

85 pixels correspond to approximately 1 foot on the belt if you consider that 480

pixels represent about 7.5 feet. This actually tends to detect defects that have dimensions

of 8 to 10 inches in diameter. It will also find larger defects, but smaller ones may not be

adequate to trigger an alarm.

A contrast value of 155 and Threshold value of 4600 is a good starting place for

tuning the system to run straw blankets. These values may be moved up or down slightly

to fine-tune the system, but they have been found to be a good starting point.

For coconut, we found that a threshold value of 1780, contrast value of 125, and

size of 85 worked for a good starting point.

These values may change depending on the size of defects you wish to detect, and

due to moisture or quality of the coconut or straw.

Page 47: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

C-4

After running the system on BT2 for several hours and monitoring the defect

reporting, we found that when the program was tuned carefully, it found 48 out of 54

defects that we would have called defects. It also saw 12 defects that were caused by

shadows or dust blowing out from under the dust collector, and 3 defects that we couldn’t

explain. The camera adjusting its sensitivity and contrast may have caused them, but we

aren’t really sure. There are enough variables to this system to make it difficult to

completely quantify all the data.

Section 3: PCB Waveforms

Figure C.3: RS232 Signal (0x00)

Page 48: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

C-5

Figure C.4: Voltage across D1

Figure C.5: Vb

Page 49: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

C-6

Figure C.6: Vc

Figure C.7: Vc magnified

Page 50: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

D-1

Appendix D: Users ManualImportant Definitions:

Threshold: The number of pixels (dots) in a square that need to be dark in order totrigger a defect report. If the number of pixels in the square is below the Threshold, nodefect is reported for that square.

Scan Size: The dimensions in pixels of a defect. The program allows dimensionsfrom 50 x 50 up to 100 x 100 pixels. Scan Size is used for changing the size of defects tobe reported. If you change the Scan Size, you will then need to adjust the Thresholdvalue.

Contrast: This is an adjustment of how light or dark pixels will appear to thescanner. The values range from 0 to 255, and 0 means that the image will appear to becompletely free of defects. A value of 255 means all pixels in the image will appear to bedefects. A value of 140 to 155 is a good place to start with the Contrast.

Search Window: This is the part of the image that you want to check for defects.The Search Window should mask out the dust collector, mesh rolls, and edges of theconveyor. When setting the Search Window, try to mask any shadows from the meshrolls and dust collectors. The shadows will always appear to be defects.

Software Instructions:

To start using the Defect Analysis system, click on the QualityChecker icon on

the computer desktop. This will start the analysis program.

When the program initially starts it uses default values for the size of the Search Window,

Contrast, Scan Size, Contrast, and Threshold.

Figure 1. Camera Calibration Menu / File Menu

Page 51: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

D-2

The first thing to do is set the Search Window. This option is found under the

Camera Calibration (Figure 1) menu selection. This will show four options. You

should select Set Search Window (Figure 2). This displays the image from the camera

on the screen with 4 red dots on it. Click on a red dot, and then click on the image to the

location you would like to set as the new border. Repeat this for all the red dots in the

image, and then click the red dot by the “done” twice. You are now done setting the

Search Window.

Figure 2. Set Search Window

Page 52: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

D-3

Next, can adjust the size of the defect the program can detect. Under the Camera

Calibration (Figure 1) menu, select Scan Size (Figure 3). This will display the image

from the camera on the screen, and some increase and decrease selections. It will also

display the current setting.

To see the size of the Scan in relation to the blanket, click on the image of the

blanket. A red square of the current Scan Size will be displayed over the blanket image.

Click the dot next to the “Done” selection to finish.

Figure 3. Set Scan Size

Page 53: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

D-4

Once Scan Size is altered, the Threshold must be set. First, select Set Threshold

(Figure 4) under the Camera Calibration (Figure 1) menu. A display will show the

current Search Window as seen by the camera. In order to make it work more

effectively, select File (Figure 1) and then Start System Scan. This starts the scanning

program, which will allow the image to refresh after each adjustment is made. If the

selections disappear, click on the screen and the selections will reappear.

When you are satisfied with the setting of the Threshold, click on the “done” dot,

and you are finished with this step. You can leave the scanner run if you like, or you may

choose to adjust the Contrast.

Figure 4. Set Threshold

Page 54: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

D-5

The Set Contrast (Figure 5) option is also under the Camera Calibration

(Figure 1) menu. This selection displays the upper left ¼ of the camera image on the

screen, followed by a modified image. The modified image is essentially a negative of

the camera image. All of the dark pixels are light and vice versa. To make the system

more sensitive to defects, increase the contrast. If it detects too many defects, lower the

sensitivity. This may require some adjustment of the Threshold to acquire proper defect

detection settings.

Figure 5. Set Contrast

Page 55: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

D-6

Troubleshooting Guide

Problem Symptoms Corrective actions

Camera image oncomputer screen isabnormal.

Image is blury ordistorted

Check light fixtures

Check camera lens for dust

Check camera alignment

Check camera focus (see Adjusting Camera Focus)

Image is blank Insure camera positioned correctly

Check camera power supply

Insure Vari-focal lens attached to camera

No image beingreceived

Swap out camera/vari-focal lens

Visually inspect communication wires.

Fix Matrox card (see Troubleshooting Matrox Card)

The frequency ofdefect detectionnon-normal andimage on computerscreen is fine.

Too many/not enough Adjust software inspection settings (see Adjusting CotrastSetting and Setting Search Window and Adjusting ScanWindow Size)

Constant Check amplifier circuit for short-circuit (seeTroubleshooting Amplifier Circuit)

Never Check communication link between computer andamplifier (See Troubleshooting Amplifier Circuit)If computer is sending correct signal (see TroubleshootingAmplifier Circuit)If computer is not sending correct signal and the programsays no defect (see Adjusting Threshold Limit)

Check wires between amplifier and relay

Check if Vc amplifier voltage is = 2.5volts, if yes, checkrelay (see Figure 1. Amplifier Schematic)If Vc never fluctuates if a defect is detected, then replaceNPN transistor (see Troubleshooting Amplifier Circuit)

Computer locks upor freezes

Defect software frozen First reboot defect program. If need be reboot computer(see Troubleshooting Software Failure)

Windows XP frozen Reboot computer (see Troubleshooting Software Failure)

Page 56: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

D-7

Troubleshooting

A) Adjusting Camera Focus

Focusing the camera can be tricky for this system, since the camera is not near thedisplay. It requires two people to communicate via radios to adjust the focus. It is alsopossible to disconnect the camera from the system and connect it into the back of thecomputer, where the lens can be focused more quickly. The camera will still need to beaimed using radio communications.

To begin focusing or aiming the camera, click on the “Camera Calibration” option in thecontrol program and then select “Focus / Aim Camera.” To see the video output fromthe camera, click on the “MIL DISPLAY #0” window icon at the bottom of the Windowsscreen. This will make this window visible. DO NOT CLOSE THIS CAMERAVIEWING WINDOW! This will prevent the video image from being viewed. To get itback, the program needs to be restarted. The program can perform all other functions,but it won’t allow you to see the image to focus the camera if this window is closed.

Begin manually adjusting the camera by twisting the two black dials on the vari-focallens. Once the picture is as clear as possible, twist the green/gray dial on the camera toachieve maximum clarity.

When the camera is aimed or focused, just click on the main control program window toput it back on top. Leaving the video run in the background does not cause anyproblems. The scanning program will work whether the video runs or not, but focusingrequires the extra video window.

B) Adjusting Threshold Limit

The Threshold value is the number of dark pixels (dots) in a square that are dark. Itworks almost like a percentage analysis. If the number of dark pixels is above theThreshold value, a defect is reported. A single defective area on a blanket may bereported as many, possibly even thousands of defects. Only one defect is required to stopthe equipment. If the Threshold value is increased too much, it can prevent the reportingof defects. If it is set too low, there will always be defects, even if none are present.

C) Adjusting Contrast Setting

This is found under the “Camera Calibration” menu selection as “Image Contrast.” Thecontrast won’t affect the ability to see an image. Contrast is used to adjust the scanningprogram’s sensitivity to the darkness of defects. If contrast is set too high, it increases thesensitivity to defects. If it is set too low, no defects will be seen, defeating the purpose ofthis system. The default level for contrast is 40, and it can be adjusted up and down fromthere. Contrast should be adjusted if the product quality isn’t matching the output of thescanning program. This can be adjusted along with the Threshold value.

Page 57: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

D-8

D) Adjusting Scan Window Size

The scan window is the square in which the pixels (dots) are checked. This option isfound under “Camera Calibration” and “Scan Size” menu options. The default value fora scan window is 85x85 pixels. This value can be adjusted up or down to allow a changein minimum or maximum size of a defect. If the scan window size is changed, a changein the Threshold value will also be necessary (see Troubleshooting threshold)

E) Setting Search Window

The search window is the portion of the image from the camera to scan for defects. Ifthere are a large number of defects showing up, it is possible that the search window mayneed to be set again. From the “Camera Calibration” menu, select “Set Search Window”and use the red buttons to select which borders to move in. Click a red button, then clickon the image at the point you want to become the new edge of the image. This allowsyou to crop the image to block out the dust collectors, mesh rolls, and edges of theconveyor. All of those may look like a defect to the system if they are not cropped.

F) Troubleshooting Software Failure

If the software from the MIL-LITE CD fails, the Matrox card won’t work properly. Thisis very unlikely, and there isn’t much you can do to detect this. A failure of the MIL-LITE library would be easier to detect by a programmer, and it would most likely havebeen detected during program development.

If the computer “locks up” it is most likely a problem with the Defect Scanner Programor Windows Operating System. First, try restarting the Defect Scanner program. If thatisn’t possible or doesn’t work, restart the computer. This will most likely fix theproblem. If it continues to lock up, the program or operating system may need to bereinstalled.

If the program seems to be working improperly it can be stopped and restarted. There isa possibility that some of the data may become corrupted during operation of theprogram. This isn’t permanent, and can be fixed by simply restarting the program.

It is possible that a “memory leak” problem may occur in the computer program where ituses up memory and doesn’t free it. This can be detected by pressing the Ctrl-Alt-Delkey combination to bring up the Task Manager window. From the Task Manager, selectthe “Processes” tab, and look in the GDI Objects column. If the “Quality Checker”process shows more than 100 GDI objects, it is likely that there is a memory leak. Thiscan only be fixed by changing the program code. This is not a highly likely problem tosee, and the code has been tested for this problem. It hasn’t appeared in the tests, and it isbeing presented as just a remote possibility if all other troubleshooting fails.

Note: Windows XP has been chosen as the Operating System because it is one of the“most stable” versions of Windows to date. This does not mean that it is exempt from

Page 58: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

D-9

problems. It just means that it “crashes” much less frequently than earlier versions,including some versions of Windows NT.

A CD with a copy of the program code and Defect Scanner program will be provided inthe event that the software needs to be reinstalled. To reinstall the Defect Scanner,simply find the program on the CD and copy it onto the hard drive of the Computer.There is no complicated installation necessary.

To reinstall the MIL-LITE software or Matrox system drivers, insert the Matrox CD andfollow the installation instructions.

It will require a programmer familiar with Windows programming to modify the programcode.

G) Troubleshooting Matrox Card

If you suspect that the Matrox image capture card is failing, there are a couple of thingsto try in order to verify that it works.

1. Make sure that the camera is connected to either the single round BNC jack on theback of the computer, or to the red #1 jack on the multiple connection cable.Close the Defect Scanner program and open the “Intellicam” program. Click onthe movie camera icon at the top of the screen. Select NTSC for this camera andclick OK. This should open a window that displays a video image. If you don’tsee this, try changing the connection to a different jack. Go from the round jackto the #1 connection on the multiple connection cable or vice versa. Then tryIntellicam again.

2. Try connecting a known “good” camera to the cable, and repeat the process.3. Make sure that the camera has power to it. It needs a 12 to 34 Volt DC power

supply connected in order to function. If you see a message that a Timeout erroroccurred, it could mean that the camera has no power, or is connected to thewrong jack.

4. If all of this fails, verify that under the “Video Signal” tab in the Intellicamprogram, Input Channel 0 is selected.

5. Replace the Matrox card – If all of the above fails, the Matrox card is likely tohave failed.

H) Troubleshooting Amplifier Circuit

If the system is constantly giving a “defect’ signal when there is no defect on the blanketthen the NPN transistor in the amplifier circuit has likely been shorted. First, check tosee if there is a voltage drop from the collector of the transistor to ground. If the voltageis zero then the transistor has been shorted internally and will need to be replaced.

If the system fails to provide the proper signal for a defect when one should be detectedthen there are two likely candidates for the problem. First, the communications link

Page 59: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

D-10

between the amplifier and the computer may have been severed. Starting with the serialport on the back of the computer check to ensure that all cables are properly attached. Atthe signal input to the amplifier circuit check to see that the positive and negative wiresare properly attached.

If the communication wires between the amplifier and the computer are properly attachedthe system continues to fail to provide a defect signal then a more in-depth diagnosticstest will need to be performed on the amplifier circuit.

1. Cover the lens to the camera to ensure that the computer will be sending a“defect” signal to the amplifier circuit.

2. Check power supply to relay3. Measure all voltages on the circuit and compare relative to the nominal values

on circuit schematic (see Figure 1. Amplifier Schematic)4. If voltage drop across resistors and/or diodes are incorrect then replace that

component and re-measure the voltages. 5. If all voltages appear to be normal and the circuit is still not working, then

connect the system using the secondary/backup circuit and retest the system.6. If the system still does not work then the problem is a software issue (see

Troubleshooting Software Failure).

Figure 1. Amplifier Schematic

Page 60: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-1

Appendix E: Software

Program Control Structure

Main Window:

WinMain( … ) - Defines the Program Window and its attributes.

WndFunc( … ) - The main Windows messaging loop

Initializes variables on startup

Handles Windows messages such as WM_TIMER, WM_CREATE,

WM_DESTROY, and WM_LBUTTONDOWN.

Handles messages from menu: IDM_CONTRAST, IDM_SETSCAN,

IDM_THRESH, IDM_CROP, IDM_EXIT, and IDM_THREAD

Scanning Thread

Thread1(…) - Performs the scanning process on the image using free CPU cycles.

- Calls support functions set2D(HDC), fillscan(HDC), and windowshop1(HDC)

- Uses Matrox functions MdigGrab(…) and MbufGet(…)

- Displays the captured image to the screen

- Only runs while the Stop flag is clear. Exits the thread if the flag is set

- Sem1 flag tells the thread that the timer went off, so it is time to perform

another scan.

Support Functions:

set2D(HDC) – Converts image data array from 1-dimensional to 2-dimensional array.

fillscan(HDC) – Sets pixels to 1 or 0 depending on the contrast level, sums them

horizontally in lines of length Win_Width, then stores them in a new array.

Page 61: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-2

windowshop1(HDC) – Vertically adds values from array of horizontal sums. It then

compares these values to the Threshold to determine whether or not a defect

exists. It also displays a contour map to be used as a tuning and debugging aid.

InitComms( ) – Initializes the Serial Port Communications

PortWrite(Byte) – Writes a single byte to the Serial Port.

DisplayContrast(HWND) – Shows screen for modifying contrast

HandleContrast(HWND,int select) – Handles changing contrast variable

SetMask(HWND,int x,int y) – Displays full captured image and red dots

HandleSetMask(HWND,int x,int y) – Sets the border parameters for the image to scan

PrintThresh(HDC) – Displays the Threshold modification screen

HandleThresh(HDC,int x,int y) – Changes the threshold variable values

PrintScan(HDC) – Displays the current defect size dimensions and change screen

HandleScan(HDC,int x,int y) – Changes the Win_Width variable to resize the defect

Note: Context switching between threads takes some time for the CPU to accomplish. I

have been asked repeatedly why I chose to do this program using multiple threads. The

answer has to do with the way the Windows Operating System works with multiple

threads. If you have a single threaded program, the Operating System will allot it a

certain amount of CPU time to execute, then force it to pause and wait for another shot at

the processor when its turn comes up again. This works for many applications, but

becomes very slow if you have many repetitive calculations to perform. Starting another

thread can help this situation because it can run in the background. A thread running in

the background essentially uses “spare” CPU cycles to run. In other words, even though

the Main messaging thread is forced to pause, the scanning thread will continue to run if

no other thread is in need of the CPU at the moment. This is very often the case, so it

becomes a great way to speed up execution. The context switch between threads is

Page 62: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-3

actually very minimal, especially when compared to the execution time of the threads.

I’m not exactly sure why a new thread can use more CPU time than the main messaging

thread, but I believe it has to do with the fact that the Operating System needs to process

any messages that the messaging thread doesn’t handle itself.

Page 63: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-4

QualityChecker.cpp

/////////////////////////////////////////////////////////////////////////////////////////// Defect Analysis Program// -----------------------// Developed for SI Corporation by Mark Hutchinson// University of Idaho Senior Design Project// Spring and Fall terms, 2004//// For MIL-LITE library instructions, see the MIL-LITE CD and read// the MilUserGuide.chm file. Everything you need to know about the Matrox// card and programming it will be found there./////////////////////////////////////////////////////////////////////////////////////////

#include <windows.h>#include <string.h>#include <stdio.h>#include "resource.h"#include "checker.h"#include <mil.h>

MIL_ID MilApp,MilSys,MilDisp,MilDigi,MilImage;

///////////////////////////////////////////////////////////////////////////////////////////// MAIN WINDOWS FUNCTION///////////////////////////////////////////////////////////////////////////////////////////

// This section sets up the main Window for the programint WINAPI WinMain(HINSTANCE hThisInst,HINSTANCE hPrevInst,LPSTR lpszArgs,int nWinMode){ HWND hwnd; MSG msg; WNDCLASSEX wcl; HANDLE hAccel; wcl.hInstance=hThisInst; wcl.lpszClassName=szWinName; wcl.lpfnWndProc=WndFunc; wcl.style=0; wcl.cbSize=sizeof(WNDCLASSEX); wcl.hIcon=LoadIcon(hThisInst,(LPCSTR) IDI_ICON1); wcl.hIconSm=LoadIcon(hThisInst,(LPCSTR) IDI_ICON1); wcl.hCursor=LoadCursor(NULL,IDC_ARROW); wcl.lpszMenuName=(LPCSTR) IDR_MENU1; wcl.cbClsExtra=0; wcl.cbWndExtra=0; wcl.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); if(!RegisterClassEx(&wcl)) return 0;

hwnd=CreateWindow(

Page 64: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-5

szWinName, "Defect Finder", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, hThisInst, NULL); hAccel = LoadAccelerators(hThisInst,(LPCSTR)IDR_ACCELERATOR1); ShowWindow(hwnd,SW_MAXIMIZE); //nWinMode); UpdateWindow(hwnd); hbit1 = LoadBitmap(hThisInst,(LPCTSTR)IDB_BITMAP2); while(GetMessage(&msg,NULL,0,0)) { if(!TranslateAccelerator(hwnd,(HACCEL)hAccel,&msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return msg.wParam;}

// The following section is the messaging loop for the Windows application// On each message, the program must perform some sort of handling routineLRESULT CALLBACK WndFunc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){ HDC hdc,memdc; // handles to Device Context and a memory DC for copying images POINT p; unsigned int R,C,pix,pix1; switch(message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDM_THREAD: if(!ThreadsOn) {

hdc=GetDC(hwnd); // Set handle to device context for current displaySelectObject(hdc,hBlackBrush);// prepare to paint black rectangleRectangle(hdc,0,0,1200,800); // clear screen

CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread1,(LPVOID)hwnd,0,&Tid1); // Starts the scanning thread ThreadsOn=1; // Set this to 1 to prevent starting another instance of the thread

Sem1=2; }

ReleaseDC(hwnd,hdc); // Must release the Device Context to prevent memory leaks break; case IDM_EXIT:

MappFreeDefault(MilApp,MilSys,MilDisp,MilDigi,MilImage); // free display buffer here if exiting through exit menu option

PostQuitMessage(0); // Tell the program to exit

Page 65: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-6

break; case IDM_SETSCAN: // This selects the routine to set Win_Width (defect size)

hdc=GetDC(hwnd); // set the Device Context Stop=1; // Set to 1 to halt scanning thread ScanSet=1; // Allow the defect size routine to work Thresh1=0; // Prevent Threshold modification if it is being done Cal=0; // Prevent Mask/Cropping if it is being done Contr=0; // Prevent Contrast adjustment if it is currently being done PrintScan(hdc); // Display the Resizing screen Stop=0; // Allow the scanning thread to run when it is restarted ThreadsOn=0; // Allow the scanning thread to be restarted ReleaseDC(hwnd,hdc); // Free the Device Context break;

case IDM_CONTRAST: Sem1=0; Stop=1; // stop scanning Thread Contr=1; // Allow Contrast modification Thresh1=0; // Prevent Threshold modification ScanSet=0; // Prevent resizing defect Cal=0; // Prevent Cropping/masking image DisplayContrast(hwnd);// Display the Contrast adjustment screen Stop=0; // Allow scanning thread to run when restarted ThreadsOn=0; // Allow scanning thread to be restarted break;

case IDM_THRESH: Sem1=0; ScanSet=0; // Prevent adjustment of defect size Contr=0; // Prevent contrast adjustment Cal=0; // Prevent Cropping/masking Thresh1=1; // Enable threshold modification hdc=GetDC(hwnd); // Set device context PrintThresh(hdc); // Display threshold modification screen ReleaseDC(hwnd,hdc); // Free up device context break;

case IDM_CROP: Sem1=0; Stop=1; // Stop scanning thread

SetBt1=1; Cal=1; // Allow Cropping/masking routine to work

Thresh1=0; // Prevent threshold modifications ScanSet=0; // Prevent defect size modifications Contr=0; // Prevent changing contrast

hdc = GetDC(hwnd); // Set Device Context SelectObject(hdc,hBlackBrush); // Prepare to blank an area Rectangle(hdc,0,0,640,800); // Clear the camera image

memdc=CreateCompatibleDC(hdc); // Set memory Device Context (for copying image)

// Capture image from camera and display it for calibrating BT

MdigGrab(MilDigi,MilImage); // Grab image into MilImage from the DigitizerMbufGet(MilImage,testarray); // Move the image data into testarray for

processingfor(R=0;R<480;R++) // Format the pixel data to display in bitmap{

for(C=0;C<COLS;C++){

Page 66: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-7

pix=testarray[R][C];pix1=(pix<<8)+pix;pix1=(pix1<<8)+pix;test2[R*COLS+C]=pix1;

}}SetBitmapBits(hbit1,PIXELS*4,test2); // Copy test2 data into pixel data of

bitmap hbit1 SelectObject(memdc,hbit1); // select bitmap into memory device context BitBlt(hdc,0,0,COLS,ROWS,memdc,0,0,SRCCOPY); // copy bitmap from memory to screen SelectObject(hdc,hWhitePen); // get a white pen SelectObject(hdc,hRedBrush); // get a red brush Ellipse(hdc,310,0,330,20); // draw red dots with white outlines Ellipse(hdc,310,460,330,480); Ellipse(hdc,0,230,20,250); Ellipse(hdc,620,230,640,250);

Ellipse(hdc,110,500,125,515); sprintf(str,"DONE"); // create a string message TextOut(hdc,60,500,str,strlen(str)); // display the text on the screen Stop=0; // allow the scanning thread to run on restart ThreadsOn=0; // allow the scanning thread to restart

ReleaseDC(hwnd,hdc); // release Device Context DeleteDC(memdc); // release memory Device Context break;

} break;

case WM_LBUTTONDOWN: // Left Mouse Button Pressed hdc = GetDC(hwnd); // set Device Context

int pass; memdc=CreateCompatibleDC(hdc); // set memory Device Context

Sem1=1; p.x=LOWORD(lParam); // get x coordinate of cursor position p.y=HIWORD(lParam); // get y coordinate

if(ScanSet) // change defect size HandleScan(hdc,p.x,p.y); // call handler routine to change defect size

if(Thresh1) // change threshold {

HandleThresh(hdc,p.x,p.y); // call handler to change threshold } if(Contr==1) // change image contrast (sensitivity) {

if(p.y<=45&&p.y>=30) // determine which choice was selected pass=1;

else if(p.y>=50&&p.y<=65) pass=2;

else if(p.y>=70&&p.y<=85) pass=3;

else if(p.y>=90&&p.y<=105) pass=4;

else if(p.y>=110&&p.y<=125) pass=5;

else pass=6;

HandleContrast(hwnd,pass); // call contrast handler routine DisplayContrast(hwnd); // display the contrast screen

}

Page 67: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-8

else if(SetBt1&&!SetMode) // Cropping/masking was selected SetMask(hwnd,p.x,p.y); // display the masking screen

else if(SetBt1&&SetMode) // now handle the masking changes {

if(!FrameExit) {

HandleSetMask(hwnd,p.x,p.y); } else {

SetBt1=0; sprintf(str,"Frame Data Saved");

} TextOut(hdc,240,300,str,strlen(str)); Foi=0; // Set frame of interest to zero SetMode=0;// mode flag is cleared

} // button was pressed, now get the position for the mask, // and store it in the frame of interest

break;

case WM_DESTROY: // closing out the window MappFreeDefault(MilApp,MilSys,MilDisp,MilDigi,MilImage); // free display resources here if not exiting through the exit menu

DeleteObject(hRedBrush); DeleteObject(hRedPen); DeleteObject(hBlackBrush); DeleteObject(hBlackPen); DeleteObject(hWhiteBrush); DeleteObject(hWhitePen); PostQuitMessage(0); break; case WM_CREATE: // things to do upon starting the Windows message loop hPort= CreateFile("COM1",GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL); // create a serial I/O stream

Border_Thresh=Thresh*9/10; // Set the Borderline threshold value to 90% of the Threshold if(hPort==INVALID_HANDLE_VALUE) { MessageBox(hwnd,TEXT("Unable to open port"),TEXT("Error"),MB_OK); return FALSE; // Failed to create the I/O stream -- won't send data to relays }

else InitComs(); if(!SetTimer(hwnd,1,500,NULL)) // Start a timer to repeatedly trigger sampling

MessageBox(hwnd,"Timer NOT initialized","Error",MB_OK); hRedPen=CreatePen(PS_SOLID,1,RGB(255,0,0)); hRedBrush=CreateSolidBrush(RGB(255,0,0)); hBlackPen=CreatePen(PS_SOLID,1,RGB(0,0,0)); hWhitePen=CreatePen(PS_SOLID,1,RGB(255,255,255)); hBlackBrush=CreateSolidBrush(RGB(0,0,0)); hWhiteBrush=CreateSolidBrush(RGB(255,255,255)); BT1.top=0; // initialize dimensions of image BT1.bottom=479; BT1.left=0; BT1.right=639;

Page 68: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-9

// Allocate Matrox variables MappAllocDefault(M_SETUP,&MilApp,&MilSys,&MilDisp,&MilDigi,&MilImage); MdigGrab(MilDigi,MilImage); // grab image MbufGet(MilImage,testarray);// put image data into testarray

break; case WM_TIMER: // timer went off

Sem1=1; // allow an analysis of an image to occur break;

default: return DefWindowProc(hwnd,message,wParam,lParam); // allow unused messages to be processed // by the system } return 0;}

// Main Analysis Thread (scanning thread)DWORD Thread1(LPVOID param){ unsigned int t,R,C,pix,pix1; HDC hdc,memdc; ThreadsOn=1;

while(!Stop) { if(Sem1) { // scan image into array1 and clear Sem1 hdc = GetDC((HWND)param); memdc=CreateCompatibleDC(hdc); Sem1 = 0;

MdigGrab(MilDigi,MilImage); // Grab image MbufGet(MilImage,temp1); // Put image data in temp1 array for(R=0;R<ROWS;R++) {

for(C=0;C<COLS;C++) // adjust image data to put into bitmap {

pix=temp1[R][C]; pix1=(pix<<8)+pix; pix1=(pix1<<8)+pix; test1[R*COLS+C]=pix1;

} }

SetBitmapBits(hbit1,(PIXELS*4),test1); // put test1 data into bitmap SelectObject(memdc,hbit1); // select hbit1 image into memory DC BitBlt(hdc,-BT1.left,-BT1.top,BT1.right,BT1.bottom,memdc,0,0,SRCCOPY);// Display image

set2D(hdc); // put image data into a 2-dimensional array fillscan(hdc); // fill up scan window array (compress horizontally) t=windowshop1(hdc); // compress data vertically, then check against threshold

Sem1=0; if(t>0) // need to trigger a system stop here (STOP ON DEFECT) {

sprintf(str1,"Some defects found: %7ld ",t); TextOut(hdc,10,650,str1,strlen(str1)); for(R=0;R<50;R++)

Page 69: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-10

PortWrite(DEFECT); // Repeatedly send defect signal toensure relay trips

} else {

sprintf(str1,"No defects found "); TextOut(hdc,10,650,str1,strlen(str1)); // Don't send anything to serial bus. Start bit in RS232 is always low, // and would probably be long enough to trip the relay just like sending a 0x00.

} ReleaseDC((HWND)param,hdc); // Free the device context

DeleteDC(memdc); // Free the memory device context Sleep(1); // give up the cpu for 1ms to allow other processes

} } ThreadsOn=0; // There is no longer a scan thread running return 0;}

//////////////////////////////////////////////////////////////////////////////////// Bitmap-array data organization and analysis routines//////////////////////////////////////////////////////////////////////////////////

void set2D(HDC hdc){ // copy 1D array into 2D array unsigned char dot; unsigned long C,R,r; C=0; r=0; { for(R=0;R<(PIXELS);R++) { if(R%640==0&& R>0) // allows 640 columns { C=0; r++; // increase row when 640 is reached } dot=test1[R+1]&0xff; // use for Set2D model temp1[C][r]=dot; // 640 X 480

// SetPixel(hdc,C,r,temp1[C][r]); // shows pixels to verify correct ops C++; }

//sprintf(str,"C %5d r %5d",C,r); // Print the Row and Column values r=479 C=640 //TextOut(hdc,500,120,str,strlen(str)); // added for testing only

}

}// Put bitmap bits into the scanning windowvoid fillscan(HDC hdc){ int R,C,Row,Col,tmp; unsigned int Win; {

Page 70: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-11

R=0; for(Row=BT1.top;Row<(BT1.bottom+1);Row++) { C=0; for(Col=BT1.left;Col<(BT1.right+1);Col++) { tmp=(temp1[Col][Row]&0x0000ff); if (tmp<Contrast) Scan1[R][C]=1; else Scan1[R][C]=0;

// SetPixel(hdc,C,R,Scan1[R][C]*255); // shows pixels to verify correct ops C++; } R++; } for(R=0;R<(BT1.bottom-BT1.top+1);R++) // Sum pixels in Scan1 array and store in box1 array { // sum to a width of Win_Width tmp=0; // i.e. if Win_Width = 85, sum 85 pixels for(Win=0;Win<Win_Width;Win++) // then store the sum in a position in box1 tmp+=Scan1[R][Win]; // next, shift 1 pixel right, & sum again for(C=Win_Width;C<=(BT1.right-BT1.left);C++) { box1[R][C-Win_Width]=tmp; // box1 is array to scan in windowscan tmp+=Scan1[R][C+1]; tmp-=Scan1[R][C-Win];

// SetPixel(hdc,C,R,box1[R][C-Win_Width]*20); // shows pixels to verifycorrect ops }

} }}int windowshop1(HDC hdc) // this is where the image is finally checked for defects{ unsigned int R,C,temp; HDC memdc; memdc=CreateCompatibleDC(hdc); int defect; defect=0; for(C=0;C<COLS;C++) // zero out the WinShop1 array WinShop1[C]=0; // before each use for(R=0;R<Win_Width;R++) { for(C=0;C<(BT1.right-BT1.left);C++) WinShop1[C]+=box1[R][C]; } for (R=Win_Width;R<(BT1.bottom-BT1.top);R++) { for(C=0;C<(BT1.right-BT1.left-Win_Width);C++) { if(WinShop1[C]>=Thresh&&WinShop1[C]<0xffff) // only erroneous sums will be > 0xffff defect++;

// shows image to verify correct ops temp=WinShop1[C]; if(temp>0xffff)

Page 71: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-12

temp=0; // correct any sums that are too large (caused by unsignedsubtractions)

if(temp<256) temp=(temp>>4); // Red and black

else if(temp>255&&temp<Border_Thresh) {

temp=0x00600000+((temp>>8)&0xff)*16; // dark blue to purple temp2[R][C]=temp;

} else if(temp>Border_Thresh&&temp<Thresh) {

temp=0x00ffff00; // light blue temp2[R][C]=temp;

} else if(temp>=Thresh) {

temp=0x0000ffff; // yellow temp2[R][C]=temp;

} SetPixel(hdc,C+20,300+R,temp); // display pixels on screen (creates contour map)

WinShop1[C]-=box1[R-Win_Width+1][C]; // subtract a summed row from the current sum WinShop1[C]+=box1[R][C]; // add a new summed row to the current sum } } DeleteDC(memdc); return defect;}

////////////////////////////////////////////////////////////////////////////////////////////// Most of the following functions aren't fully commented because they use code that// is described by comments in the above functions. Some wasn't commented because it was // just initializations./////////////////////////////////////////////////////////////////////////////////////////////

void InitComs(void) // initialize serial I/O {

DCB PortDCB;COMMTIMEOUTS CommTimeouts;PortDCB.DCBlength=sizeof(DCB);PortDCB.BaudRate=9600;PortDCB.fBinary=TRUE;PortDCB.fParity=TRUE;PortDCB.fOutxCtsFlow=FALSE;PortDCB.fOutxDsrFlow=FALSE;PortDCB.fDtrControl=DTR_CONTROL_ENABLE;PortDCB.fDsrSensitivity=FALSE;PortDCB.fTXContinueOnXoff=TRUE;PortDCB.fOutX=FALSE;PortDCB.fInX=FALSE;PortDCB.fErrorChar=FALSE;PortDCB.fNull=FALSE;

Page 72: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-13

PortDCB.fRtsControl=RTS_CONTROL_ENABLE;PortDCB.fAbortOnError=FALSE;PortDCB.ByteSize=8;PortDCB.Parity=NOPARITY;PortDCB.StopBits=ONESTOPBIT;GetCommTimeouts(hPort,&CommTimeouts);CommTimeouts.ReadIntervalTimeout=MAXDWORD;CommTimeouts.ReadTotalTimeoutConstant=0;CommTimeouts.ReadTotalTimeoutMultiplier=0;CommTimeouts.WriteTotalTimeoutMultiplier=10;CommTimeouts.WriteTotalTimeoutConstant=1000;

EscapeCommFunction(hPort,SETDTR);EscapeCommFunction(hPort,SETRTS);

}

void PortWrite(BYTE Byte) // Write a single byte to the serial port{

DWORD dwError,dwNumBytesWritten;if(!WriteFile (hPort,&Byte,1,&dwNumBytesWritten,NULL)){

dwError = GetLastError();}

}

void DisplayContrast(HWND hwnd) // Show the Contrast screen{

HDC hdc,memdc;Sem1=0;int R,C,pix1,pix;hdc=GetDC(hwnd);memdc=CreateCompatibleDC(hdc);

SelectObject(hdc,hBlackBrush);SelectObject(hdc,hBlackPen);Rectangle(hdc,0,0,1200,800); // blank the screen// Get image and then set up contrast controlsMdigGrab(MilDigi,MilImage); // Capture imageMbufGet(MilImage,testarray);for(R=0;R<ROWS;R++){

for(C=0;C<COLS;C++){

pix=testarray[R][C]; // Adjust image for Bitmappix1=(pix<<8)+pix;pix1=(pix1<<8)+pix;test2[R*COLS+C]=pix1;

}}SetBitmapBits(hbit1,(PIXELS*4),test2);SelectObject(memdc,hbit1);BitBlt(hdc,0,0,COLS/2,ROWS/2,memdc,0,0,SRCCOPY); // Display image (only top left 1/4)for(R=0;R<ROWS;R++){

for(C=0;C<COLS;C++){

Page 73: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-14

pix=testarray[R][C];if(pix<=Contrast)

test2[R*COLS+C]=0xFFFFFF; // invert pixels: Dark -> White else

test2[R*COLS+C]=0x000000; // Light -> Black}

}SetBitmapBits(hbit1,PIXELS*4,test2); // Set Bitmap with modified imageSelectObject(memdc,hbit1);BitBlt(hdc,COLS/2,0,COLS/2,ROWS/2,memdc,0,0,SRCCOPY); // Display inverted imagesprintf(str,"Current contrast (0-255) is: %3d ",Contrast);// show selectionsTextOut(hdc,640,10,str,strlen(str));sprintf(str,"Increase by 5");TextOut(hdc,640,30,str,strlen(str));sprintf(str,"Increase by 1");TextOut(hdc,640,50,str,strlen(str));sprintf(str,"Decrease by 1");TextOut(hdc,640,70,str,strlen(str));sprintf(str,"Decrease by 5");TextOut(hdc,640,90,str,strlen(str));sprintf(str,"Finished");TextOut(hdc,660,110,str,strlen(str));

SelectObject(hdc,hWhitePen); SelectObject(hdc,hRedBrush); Ellipse(hdc,740,30,755,45); Ellipse(hdc,740,50,755,65); Ellipse(hdc,740,70,755,85); Ellipse(hdc,740,90,755,105); Ellipse(hdc,740,110,755,125);

ReleaseDC(hwnd,hdc);DeleteDC(memdc);

}

void HandleContrast(HWND hwnd,int select) // actually changes the contrast variable{

HDC hdc,memdc;hdc=GetDC(hwnd);memdc=CreateCompatibleDC(hdc);

SelectObject(hdc,hBlackBrush);SelectObject(hdc,hBlackPen);Rectangle(hdc,0,0,1200,800); // Clear screenSelectObject(hdc,hWhitePen); switch(select){case 1: // Increase or Decrease Contrast according to which dot was selected

Contrast+=5;if(Contrast>=255)

Contrast=255;Ellipse(hdc,740,30,755,45);break;

case 2:Contrast++;if(Contrast>=255)

Contrast=255;Ellipse(hdc,740,50,755,65);break;

Page 74: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-15

case 3:Contrast--;if(Contrast<=0)

Contrast=0; Ellipse(hdc,740,70,755,85);

break;case 4:

Contrast-=5;if(Contrast<=0)

Contrast=0; Ellipse(hdc,740,90,755,105);

break;case 5:

Contr=0; // Ends the Contrast routineInvalidateRect(hwnd,NULL,0); // Clears screenbreak;

default:Contr=0; // Ends Contrast routineInvalidateRect(hwnd,NULL,0); // Clears screenbreak;

}DeleteDC(hdc);ReleaseDC(hwnd,memdc);

}

void SetMask(HWND hwnd,int x,int y) // Display image for masking borders{

FrameExit=0;HDC hdc,memdc;hdc=GetDC(hwnd);SelectObject(hdc,hWhitePen);SelectObject(hdc,hBlackBrush);memdc=CreateCompatibleDC(hdc);if(x>309&&x<331&&y>0&&y<21){

Ellipse(hdc,310,0,330,20);SetMode=1;Foi=1;

}else if(x>309&&x<331&&y>459&&y<481){

Ellipse(hdc,310,460,330,480);SetMode=1;Foi=2;

}else if(x>0&&x<21&&y>229&&y<251){

Ellipse(hdc,0,230,20,250);SetMode=1;Foi=3;

}else if(x>619&&x<641&&y>229&&y<251){

Ellipse(hdc,620,230,640,250);SetMode=1;Foi=4;

Page 75: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-16

}else if(x>109&&x<126&&y>499&&y<516){

Ellipse(hdc,110,500,125,515);SetMode=1;Foi=5;SelectObject(hdc,hBlackBrush);SelectObject(hdc,hBlackPen);Rectangle(hdc,0,0,1200,480); // blank screenFrameExit=1; // allow program to exit the masking routine

}ReleaseDC(hwnd,hdc);DeleteDC(memdc);

}

void HandleSetMask(HWND hwnd,int x,int y) // sets the parameters for the borders{

int tmp;POINT P1,P2;P1.x=0;P1.y=0;P2.x=640;P2.y=480;HDC hdc,memdc;hdc=GetDC(hwnd);memdc=CreateCompatibleDC(hdc);SelectObject(hdc,hWhitePen);SelectObject(hdc,hWhiteBrush);sprintf(str,"Setting Saved");switch(Foi){case 1:

BT1.top=y;if(BT1.top>BT1.bottom){

tmp=BT1.bottom;BT1.bottom=BT1.top;BT1.top=tmp;

}Rectangle(hdc,0,0,640,BT1.top);break;

case 2:BT1.bottom=y;if(BT1.top>BT1.bottom){

tmp=BT1.bottom;BT1.bottom=BT1.top;BT1.top=tmp;

}Rectangle(hdc,0,BT1.bottom,640,480);break;

case 3:BT1.left=x;if(BT1.right<BT1.left){

tmp=BT1.right;

Page 76: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-17

BT1.right=BT1.left;BT1.left=tmp;

}Rectangle(hdc,0,0,BT1.left,480);break;

case 4:BT1.right=x;if(BT1.right<BT1.left){

tmp=BT1.right;BT1.right=BT1.left;BT1.left=tmp;

}Rectangle(hdc,BT1.right,0,640,480);break;

default:// done with this -- set the exit variablesprintf(str,"Frame Saved");Stop=0;ThreadsOn=0;FrameExit=1;break;

}SelectObject(hdc,hRedBrush);

Ellipse(hdc,310,0,330,20); Ellipse(hdc,310,460,330,480); Ellipse(hdc,0,230,20,250); Ellipse(hdc,620,230,640,250);

if(FrameExit==1){

SelectObject(hdc,hBlackBrush);SelectObject(hdc,hBlackPen);Rectangle(hdc,0,0,1200,800); // clear screen

}ReleaseDC(hwnd,hdc);DeleteDC(memdc);

}

void PrintThresh(HDC hdc){

Border_Thresh=Thresh*9/10; // make sure Border_Thresh is currentHDC memdc;memdc=CreateCompatibleDC(hdc);SelectObject(hdc,hBlackBrush);Rectangle(hdc,0,0,1200,800); // clear screensprintf(str,"MUST BE DONE WHILE SCANNING FOR DEFECTS");TextOut(hdc,680,60,str,strlen(str));MaxThresh=Win_Width*Win_Width*8/10;MinThresh=Win_Width*Win_Width*2/10;sprintf(str,"Maximum Threshold value: %7d",MaxThresh);TextOut(hdc,720,100,str,strlen(str));sprintf(str,"Current Threshold is: %7d",Thresh);TextOut(hdc,720,140,str,strlen(str));sprintf(str,"Minimum Threshold value: %7d",MinThresh);TextOut(hdc,720,120,str,strlen(str));sprintf(str,"Increase by 100");

Page 77: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-18

TextOut(hdc,750,160,str,strlen(str));sprintf(str,"Increase by 10");TextOut(hdc,750,180,str,strlen(str));sprintf(str,"Decrease by 10");TextOut(hdc,750,200,str,strlen(str));sprintf(str,"Decrease by 100");TextOut(hdc,750,220,str,strlen(str));sprintf(str,"Finished");TextOut(hdc,750,240,str,strlen(str));SelectObject(hdc,hRedBrush);SelectObject(hdc,hWhitePen);Ellipse(hdc,730,160,745,175);Ellipse(hdc,730,180,745,195);Ellipse(hdc,730,200,745,215);Ellipse(hdc,730,220,745,235);Ellipse(hdc,730,240,745,255);SelectObject(memdc,hbit1);BitBlt(hdc,-BT1.left,-BT1.top,BT1.right,BT1.bottom,memdc,0,0,SRCCOPY); // display masked

imageDeleteDC(memdc);

}

void HandleThresh(HDC hdc,int x,int y) // implement changes to border dimensions{

if(y>159&&y<176){

Thresh+=100;if(Thresh>MaxThresh)

Thresh=MaxThresh;Border_Thresh=Thresh*9/10;

}if(y>179&&y<196){

Thresh+=10;if(Thresh>MaxThresh)

Thresh=MaxThresh;Border_Thresh=Thresh*9/10;

}if(y>199&&y<216){

Thresh-=10;if(Thresh<MinThresh)

Thresh=MinThresh;Border_Thresh=Thresh*9/10;

}if(y>219&&y<236){

Thresh-=100;if(Thresh<MinThresh)

Thresh=MinThresh;Border_Thresh=Thresh*9/10;

}

if(y>239&&y<256) // done with threshold adjustments{

Thresh1=0; // exit threshold

Page 78: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-19

SelectObject(hdc,hBlackBrush);Rectangle(hdc,0,0,1200,800); // clear screen

}PrintThresh(hdc); // print threshold screen againGetBitmapBits(hbit1,PIXELS,temp1);set2D(hdc);fillscan(hdc);windowshop1(hdc); // now show how the threshold change affects the contour mapif(y>239&&y<256){

Thresh1=0; // quit doing the Threshold modificationsSelectObject(hdc,hBlackBrush);Rectangle(hdc,0,0,1200,800); // clear screenSem1=1;

}}

void PrintScan(HDC hdc) // Print out the defect size selection screen{

HDC memdc;memdc=CreateCompatibleDC(hdc);SelectObject(hdc,hBlackBrush);Rectangle(hdc,0,0,1200,800);sprintf(str,"Current defect window size: %d x %d",Win_Width,Win_Width);TextOut(hdc,660,30,str,strlen(str));sprintf(str,"Defect size range: 50x50 to 100x100 pixels");TextOut(hdc,660,50,str,strlen(str));sprintf(str,"Set size then click on image to see outline");TextOut(hdc,660,70,str,strlen(str));sprintf(str,"Increase by 10");TextOut(hdc,670,90,str,strlen(str));sprintf(str,"Increase by 1");TextOut(hdc,670,110,str,strlen(str));sprintf(str,"Decrease by 1");TextOut(hdc,670,130,str,strlen(str));sprintf(str,"Decrease by 10");TextOut(hdc,670,150,str,strlen(str));sprintf(str,"Done");TextOut(hdc,670,170,str,strlen(str));SelectObject(hdc,hRedBrush);SelectObject(hdc,hWhitePen);Ellipse(hdc,650,90,665,105);Ellipse(hdc,650,110,665,125);Ellipse(hdc,650,130,665,145);Ellipse(hdc,650,150,665,165);Ellipse(hdc,650,170,665,185);SelectObject(memdc,hbit1);BitBlt(hdc,-BT1.left,-BT1.top,BT1.right,BT1.bottom,memdc,0,0,SRCCOPY);DeleteDC(memdc);

}

void HandleScan(HDC hdc,int x,int y) // Changes the Win_Width (defect size){

Stop=1;if(y>89&&y<106&&x>640){

Page 79: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-20

Win_Width+=10;if(Win_Width>100)

Win_Width=100;}if(y>109&&y<126&&x>640){

Win_Width++;if(Win_Width>100)

Win_Width=100;}if(y>129&&y<146&&x>640){

Win_Width--;if(Win_Width<50)

Win_Width=50;}if(y>149&&y<166&&x>640){

Win_Width-=10;if(Win_Width<50)

Win_Width=50;}if(y>169&&y<186&&x>640){

Stop=0; // allow scan thread to run when restartedThreadsOn=0; // allow scan thread to be restartedSelectObject(hdc,hBlackBrush);Rectangle(hdc,0,0,1200,800);ScanSet=0;

}PrintScan(hdc); // draw a red box of dimensions Win_Width x Win_Widthif(x<640){

SelectObject(hdc,hRedPen);MoveToEx(hdc,x,y,NULL);LineTo(hdc,x+Win_Width,y);LineTo(hdc,x+Win_Width,y+Win_Width);LineTo(hdc,x,y+Win_Width);LineTo(hdc,x,y);

}MaxThresh=(Win_Width*Win_Width*8/10);MinThresh=(Win_Width*Win_Width*2/10);Thresh=MaxThresh*3/4;Border_Thresh=Thresh*8/10;

}

Page 80: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-21

checker.h

#define MAXCOL1 640#define MAXCOL2 640#define MAXROW1 480#define MAXROW2 480#define MAX 5000#define PIXELS 307200#define ROWS 480#define COLS 640#define WIN_WIDTH 85#define OFFSET -84#define THRESH 4500#define BORDER_THRESH 3000#define HEADERLEN 1078#define DEFECT 0x00/////////////////////////////////////////////////////////////////// DEFINE IMPORTANT STRUCTS/////////////////////////////////////////////////////////////////

struct FrameOfInterest{ int top; int bottom; int left; int right;};struct BmpHdr{ WORD bfType; DWORD bfSize; DWORD reserved; DWORD bfOffBits; DWORD biSize; DWORD Width; DWORD Height; WORD Planes; WORD BitCount; DWORD Compression; DWORD SizeImage; DWORD XpelsMeter; DWORD YpelsMeter; DWORD ClrUsed; DWORD ClrImportant;};struct palette{ DWORD RGBx[256];};struct defect{ int R; int C; int NUM;

Page 81: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-22

};

///////////////////////////////////////////////////////////////////// FUNCTION DECLARATIONS///////////////////////////////////////////////////////////////////

LRESULT CALLBACK WndFunc(HWND,UINT,WPARAM,LPARAM); // main window DWORD Thread1(LPVOID param); // Thread for Scanning the imagevoid set2D(HDC hdc); // load bitmap into a 2D arrayvoid fillscan(HDC hdc); // fill scan array with sumsint windowshop1(HDC hdc); // check box array for defects void PortWrite(BYTE Byte); // Write byte to serial portvoid InitComs(void); // Initialize communications in serial portvoid DisplayContrast(HWND hwnd); // Display the contrast adjustment screenvoid HandleContrast(HWND hwnd,int select); // Handle contrast adjustment routinevoid SetMask(HWND hwnd,int x,int y); // Display the Masking screenvoid HandleSetMask(HWND hwnd,int x,int y); // Handle setting the borders during maskingvoid PrintThresh(HDC hdc); // Display the Threshold screenvoid HandleThresh(HDC hdc,int x,int y); // Handle the Threshold changesvoid PrintScan(HDC hdc); // Display the defect size selection screenvoid HandleScan(HDC hdc,int x,int y); // Handle the size changes to Win_Width

//////////////////////////////////////////////////////////////////////////////////////////// DECLARE GLOBALS -> This program uses numerous semaphores (flags)// It is also easier to not have to worry about passing arrays// that are accessed in numerous places.// I had to put something in here to make programming puristscringe....//////////////////////////////////////////////////////////////////////////////////////////DWORD Thresh=4600; // Set the default value for the Threshold DWORD Win_Width=85; // Make the defect size default to 85 pixels wideDWORD Border_Thresh; // The borderline threshold will be 90% of the threshold value

unsigned char temp1[ROWS][COLS]; COLORREF temp2[ROWS][COLS]; UCHAR array1[HEADERLEN];WORD bitmap1[ROWS * COLS *2];unsigned char Scan1[ROWS][COLS]; // holds inverted image infoUINT box1[ROWS][COLS-WIN_WIDTH]; // holds horizontal sumsUINT WinShop1[COLS]; // holds values in final search algorithm (vertical sums)DWORD Tid1; // Thread ID char str[255],str1[255]; // Strings for printing text to screenHBITMAP hbit1; // handle to a bitmapHPEN hRedPen,hWhitePen,hBlackPen;// handles to different colored pensHBRUSH hRedBrush,hBlackBrush,hWhiteBrush;// Brush handlesint Array1[MAXCOL1][MAXROW1]; char szWinName[]="Defect Analysis";BITMAPINFO bthdr;BOOL Sem1=0;HBITMAP *pbit; // pointer to bitmapBOOL SetBt1=0;

Page 82: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-23

BOOL SetMode=0;BOOL ThreadsOn=0; // Flag to determine whether or not to allow new thread to be startedint Cal=0; // Flag to allow masking routine to operateint Foi=0; // variable to determine which border has been selected in mask routineFrameOfInterest BT1; // Contains top, bottom, left, and right borders of image to scanHANDLE hPort; // port handle for serial communicationsunsigned char testarray[ROWS][COLS];//rows,columns coordinatesDWORD test2[ROWS*COLS];DWORD test1[ROWS*COLS];BOOL Contr = 0; // Flag for Contrast functionint Contrast = 155; // Default value for contrastBOOL FrameExit =0; // Flag for Crop functionBOOL Thresh1=0; // Flag for Threshold modificationunsigned int MaxThresh; // holds the current maximum threshold value (80% of Win_Width*Win_Width)unsigned int MinThresh; // holds current minimum threshold value (20% of Win_Width*Win_Width)BOOL Stop=0; // Flag to start and stop scanning threadBOOL ScanSet=0; // Flag for setting defect size

Page 83: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

E-24

resource.h

//{{NO_DEPENDENCIES}}// Microsoft Developer Studio generated include file.// Used by QualityChecker.rc//#define IDI_ICON1 101#define IDR_MENU1 102#define IDR_ACCELERATOR1 103#define IDB_BITMAP1 105#define IDB_BITMAP2 106#define IDB_BITMAP3 107#define IDM_THREAD 40001#define IDM_HELP 40002#define IDM_EXIT 40003#define IDM_BT1 40004#define IDM_BT2 40005#define IDM_BT1FOCUS 40006#define IDM_FOCUS 40006#define IDM_BT2FOCUS 40007#define IDM_CROP 40008#define IDM_CONTRAST 40009#define IDM_THRESH 40010#define IDM_SETSCAN 40011

// Next default values for new objects// #ifdef APSTUDIO_INVOKED#ifndef APSTUDIO_READONLY_SYMBOLS#define _APS_NEXT_RESOURCE_VALUE 108#define _APS_NEXT_COMMAND_VALUE 40012#define _APS_NEXT_CONTROL_VALUE 1000#define _APS_NEXT_SYMED_VALUE 101#endif#endif

Page 84: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

F-1

Appendix F: Installation Instructions

Section 1: Lighting

Fixtures must be mounted at an angle 65° from the horizontal. Bracketson BT2 may be used as a template.

Section 2: Camera

Supply with 24V. Connect coaxial from PC to camera. Plug the vari-focal lens into camera. Mount 8’ above feed belt.

Section 3: SoftwareIf installing the Defect Analysis software on a new system, you will first

need to install the Matrox Imaging Library from the Matrox CD. This is

required to allow the program to run properly.

The next step is to insert the Defect Analyzer CD and copy the

“QualityCheckerIII” folder onto the computer’s hard drive. Remember

the directory you copied the folder into. The executable program file is

inside the Debug folder which is contained inside the “QualityCheckerIII”

folder.

CD: Hard Drive:

QualityCheckerIII folder C:\QualityCheckerIII

Create a desktop shortcut to:

C:\QualityCheckerIII\Debug\QualityChecker.exe

Page 85: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT

F-2

Section 4: PCB

Connect 24V line from A2 of relay to terminal 1. Connect 24V GND line toterminal line 2. Attach serial cable from COMM 1 on PC to PCB.

Section 5: Relay

Connect 24V source to A1. Connect A2 to PCB. Connect machine shut offcontrols as per Figure F.1. Connect HI line of alert light to 65 and LO line to 67.

Page 86: University of Idaho ECE Senior Design Project Final ... · University of Idaho ECE Senior Design Project Final Documentation for Defect Detection System 10 December 2004 ... PROJECT