# Automatic Thread Defect Examination System
## 2 Automatic TH Examination
### Inspection Algorithm  
The algorithm for controlling the servo motor inside the ID component is presented as Algorithm 1. The servo motor can measure the torque in real time. A programmable logic controller (PLC) is utilized for programming Algorithm 1. A PLC is an industrial computer that has been adapted for the control of manufacturing processes involving machines, robotics, or any similar activity that requires high reliability [17].  
When the ID is initiated, the operator designates the revolutions and \(\delta\) of the servo motor inside the ID component. The ID examines the TH while rotating by the number of revolutions designated by the operator. If the torque during the examination is larger than \(\delta\), this indicates faults in the examined TH. When a faulty TH is detected, the ID reverses its revolving direction and returns to its initial position.  
Algorithm 1 is utilized to examine a TH. In this algorithm, \(Fault\) implies that the examined TH has faults, and \(noFault\) implies that the examined TH has no faults.  
When an examination is completed, the ID shaft must return to its initial position. Thus, we installed two protective barriers, one on the cover frame and one on the ID shaft. Figure 9 shows the model of the two protective barriers (the parts inside the red circle). These protective barriers are utilized to allow the ID shaft to return to its initial position whenever an examination is completed. In other words, these protective barriers remove any errors in the ID shaft's initial position.  
Figure 8: The model of the drill chuck support. The drill chuck support exists at the lower part of the ID component in Figure 3. The drill chuck support is utilized to fix a commercial drill chuck tightly.  
As the ID shaft returns to the initial position, the shaft rotates half a revolution less than the designated return rotation value (\(N\) or \(n\) in Algorithm 1). Thereafter, the motor rotates at low speed and stops at the moment when sufficient torque is applied due to the barriers on the cover frame and the ID shaft. The slowness of this motion reduces the strain on the protective barriers.  
Figure 10 shows the model of the camera used in our examination system. The camera is connected to the cover frame shown in (b) of Figure 5. The camera measures the TH in real time and estimates the TH size.  
The camera utilizes a Raspberry Pi [18] for estimating the size of the examined TH. In order to estimate the TH size from the camera image, template matching is applied using OpenCV [19]. templates for every TH size are stored and matched with the TH from the camera's measurements. By observing the similarity between a template and the TH from the camera measurements, the ID is able to estimate the size of the examined TH.  
Figure 11 shows an example of a photo measured by the camera. The Region Of Interest (ROI) is depicted as a green circle. Figure 11 shows that the TH image is inside the ROI. Template matching using OpenCV [19] is applied to estimate the TH size in the ROI. Figure 11 shows that the estimated size is M3, which is correct.  
Figure 9: We installed two protective barriers on the cover frame and the ID shaft. This figure shows the model of the two protective barriers (the parts inside the red circle).