Field fix / guideDesign DocumentVendor-agnostic (IEC 61131-3)Manufacturingindustrial-automation

Vision-Guided Inspection & QA — Control Design

Shared by TSIMEREZIVO Team · 4 Oct 2026 · 41 views

# Manufacturing Template: Vision-Guided Inspection & QA ## 1. Overview **Purpose:** Automated optical inspection of manufactured parts using machine vision (camera + image processing) to detect defects, classify products, and trigger downstream quality actions (accept/reject/rework). Applications: electronics assembly, printed circuit board inspection, pharmaceutical tablet/capsule verification, food product shape/color verification. **Industry Standards:** - ISO 13849-1 (Safety of machinery control) - AIA GS1 (Vision systems integration standards) - ANSI/ISPE Guide for Vision System Validation - ISO 9000 series (Quality management) **Critical Constraints:** - **Lighting reproducibility:** ±10% illumination variation causes 5–10% class error (stable lighting mandatory) - **Camera frame rate:** Must exceed line speed; typical 30–100 fps for 1–2 m/s conveyor - **Processing latency:** <50 ms per image (decision must be made before part leaves view) - **Defect resolution:** Detect flaws >0.5 mm (pixel size typically 0.1–0.2 mm at ~1m distance) - **False positive rate:** <1% (quality operator trust; <1% good parts incorrectly rejected) - **Classification model accuracy:** >99% on training dataset; revalidate after equipment changes **Relevant Physics:** - Camera exposure: **I = L × (f/#)² × Δt** (illuminay intensity × aperture² × shutter time) - Defect contrast: **C = (I_good − I_defect) / I_good** (need >15% contrast for detection) - Depth of field: **DOF = 2 × (f/#) × c × (1 + m) / (m² × N)** (focus tolerance vs. working distance) - Pixel resolution: **pxl_size_mm = sensor_width_mm / pixels** (smaller = more detail; slower processing) --- ## 2. Control Philosophy **Challenge:** Camera image must be acquired, processed (edge detection, defect classification), and decision made—all in <50 ms before part moves out of view. Training machine-learning model requires hundreds of images per defect type. Production system must adapt to lighting changes without retraining. **Solution: Hybrid Rule-Based + ML Classification with Lighting Compensation** ``` Pipeline Architecture: Stage 1: Image Acquisition & Preprocessing - Trigger camera synchronized to part arrival (proximity or encoder marker) - Acquire image at fixed exposure & focus - Extract ROI (region of interest) around expected part location - Normalize brightness: divide by running average illumination (compensation) Stage 2: Feature Extraction (Rule-Based) - Edge detection: Sobel or Canny edge filter - Contour matching: compare part outline to golden template - Measure: area, perimeter, aspect ratio, symmetry - Detect obvious defects: holes, cracks, missing edges Stage 3: Machine Learning Classifier - Pre-trained CNN (convolutional neural network) on 1000+ images - 5 output classes: PERFECT | MINOR_FLAW | MAJOR_FLAW | SCRAP | NO_PART - Confidence score (0–1); only accept decisions >95% confidence Stage 4: Lighting Compensation - Monitor illuminometer (lux sensor) continuously - If light drifts >10%: auto-adjust camera exposure & LED intensity - Recalibrate PLC feature extraction thresholds dynamically Stage 5: Decision & Action - PERFECT → Continue conveyor (accept) - MINOR_FLAW → Slow conveyor; log to quality database for review - MAJOR_FLAW → Divert to rework bin (solenoid valve activates pneumatic arm) - SCRAP → Eject to scrap bin - NO_PART → Log gap; continue Quality Tracking: - Every decision logged: timestamp, image, defect class, confidence - Monthly trend analysis: defect rate, equipment-induced issues ``` **Why this works:** - Hybrid approach: rules catch systematic defects; ML handles variability - Lighting compensation ensures reliable performance across shifts/seasons - High-speed decision (USB 3.0 vision module = <30 ms processing typical) - Traceability: 100% of parts inspected and logged for recall/warranty analysis --- ## 3. Control Logic **Pseudocode: Vision Inspection System** ```pascal { === VISION-GUIDED INSPECTION === } PROCEDURE Vision_Inspection_Master CONSTANTS CONFIDENCE_THRESHOLD_PERCENT = 95 DEFECT_RESOLUTION_MM = 0.5 { detect flaws >0.5 mm } ILLUMINATION_STABILITY_PERCENT = 10 { alert if lux drifts >±10% } PROCESSING_TIMEOUT_MS = 40 { stop processing if >40 ms } VARIABLES state : ENUMERATION { IDLE, WAITING_PART, INSPECTING, DECISION_MADE } camera_image : ARRAY OF BYTE { raw RGB image } image_timestamp : DATETIME part_detected : BOOLEAN roi_extracted : BOOLEAN { Feature extraction results } part_outline_contour : ARRAY OF POINT part_area_mm2 : REAL part_perimeter_mm : REAL part_aspect_ratio : REAL symmetry_score : REAL { 0–1, perfect circle = 1.0 } edge_defect_count : INTEGER crack_detected : BOOLEAN { ML classifier } cnn_input_tensor : ARRAY[224,224,3] OF BYTE { RGB normalized image } cnn_softmax_output : ARRAY[5] OF REAL { 5 class probabilities } predicted_class : text { PERFECT / MINOR / MAJOR / SCRAP / NOPART } predicted_confidence : REAL { 0–1 } { Lighting monitoring } illumination_lux : REAL illumination_baseline_lux : REAL illumination_drift_percent : REAL { Downstream actions } accept_signal : BOOLEAN reject_signal : BOOLEAN rework_signal : BOOLEAN BEGIN { === CONTINUOUS MONITORING === } { Read ambient and ring-light illuminance } illumination_lux = Read_Illuminometer_Sensor() IF MOD(scan_count, 100) = 0 THEN { baseline update, every 10 sec } illumination_baseline_lux = illumination_lux { rolling average } END IF illumination_drift_percent = ABS(illumination_lux - illumination_baseline_lux) / illumination_baseline_lux × 100.0 IF illumination_drift_percent > ILLUMINATION_STABILITY_PERCENT THEN { Auto-adjust camera exposure & LED intensity } exposure_ms := exposure_ms × (illumination_baseline_lux / illumination_lux) led_intensity_cmd := led_intensity_cmd × (illumination_baseline_lux / illumination_lux) Log_Warning("ILLUMINATION_DRIFT_AUTO_CORRECTED") END IF { === STATE MACHINE === } SELECT state CASE IDLE: accept_signal := FALSE reject_signal := FALSE rework_signal := FALSE { Wait for part arrival signal (conveyor encoder or proximity sensor) } IF Part_Arrived_Signal THEN state := WAITING_PART END IF CASE WAITING_PART: { Wait for conveyor to position part in camera view } { Typically 100–200 ms to allow settling } IF Part_In_View_Dwell_Complete THEN { Trigger camera acquisition } Camera_Trigger() state := INSPECTING END IF CASE INSPECTING: { === IMAGE PROCESSING & ANALYSIS === } processing_start_ms := Get_Timestamp_MS() { Acquire and receive image from camera } camera_image = Receive_Image_From_Camera() { USB 3.0, typically <10 ms } image_timestamp = Get_Timestamp() { === PREPROCESSING: Lighting Normalization === } normalised_image = Normalise_Image_Brightness(camera_image, illumination_lux) { === FEATURE EXTRACTION: Rule-Based Analysis === } part_outline_contour = Find_Part_Contour(normalised_image) part_detected = (LENGTH(part_outline_contour) > 50) { minimum contour length } IF NOT part_detected THEN predicted_class := "NOPART" predicted_confidence := 0.99 ELSE { Calculate geometric features } part_area_mm2 = Calculate_Area(part_outline_contour) × PIXEL_SIZE_MM² part_perimeter_mm = Calculate_Perimeter(part_outline_contour) × PIXEL_SIZE_MM part_aspect_ratio = Max_Dimension / Min_Dimension symmetry_score = Measure_Contour_Symmetry(part_outline_contour) { 0–1 } { Detect cracks and edge defects } canny_edges = Apply_Canny_Edge_Filter(normalised_image, threshold=30) edge_histogram = Analyze_Edge_Gradient_Histogram(canny_edges) crack_detected = (edge_histogram_peak > 0.6) edge_defect_count = Count_Discontinuities_In_Contour(part_outline_contour) { === MACHINE LEARNING CLASSIFIER === } { Resize image to CNN input size } cnn_input_tensor = Resize_Image(normalised_image, [224, 224]) cnn_input_tensor = Normalize_Pixel_Values(cnn_input_tensor) { 0–1 range } { Run CNN inference } cnn_softmax_output = Feed_Forward_CNN(cnn_input_tensor) { Classes: [PERFECT=0, MINOR=1, MAJOR=2, SCRAP=3, NOPART=4] } { Get prediction with highest confidence } max_confidence_idx, predicted_confidence = Argmax(cnn_softmax_output) class_names := ["PERFECT", "MINOR_FLAW", "MAJOR_FLAW", "SCRAP", "NOPART"] predicted_class := class_names[max_confidence_idx] END IF { === DECISION LOGIC === } processing_time_ms = Get_Timestamp_MS() - processing_start_ms IF processing_time_ms > PROCESSING_TIMEOUT_MS THEN Log_Warning("VISION_PROCESSING_TIMEOUT") predicted_confidence := 0.0 { no decision if timed out } END IF { === POST-PROCESSING OVERRIDE === } { Rule-based overrides if ML confidence low or conflicting } IF predicted_confidence < (CONFIDENCE_THRESHOLD_PERCENT / 100.0) THEN { Low confidence; use rules to make decision } IF crack_detected OR edge_defect_count > 3 THEN predicted_class := "MAJOR_FLAW" predicted_confidence := 0.85 ELSIF part_area_mm2 < (NOMINAL_AREA_MM2 × 0.95) THEN predicted_class := "MINOR_FLAW" predicted_confidence := 0.90 ELSE predicted_class := "PERFECT" predicted_confidence := 0.98 END IF END IF state := DECISION_MADE CASE DECISION_MADE: { === EXECUTE QUALITY DECISION === } CASE predicted_class WHEN "PERFECT": accept_signal := TRUE Log_Event("QUALITY_PASS", confidence_percent=ROUND(predicted_confidence×100)) WHEN "MINOR_FLAW": rework_signal := TRUE Log_Event("QUALITY_MINOR_REWORK", defect=predicted_class) WHEN "MAJOR_FLAW", "SCRAP": reject_signal := TRUE Log_Event("QUALITY_REJECT", defect=predicted_class) WHEN "NOPART": accept_signal := TRUE { empty position, continue line } Log_Event("NO_PART_DETECTED") END CASE { === DOWNSTREAM ACTUATION === } IF accept_signal THEN Solenoid_Diverter_Valve_ACCEPT() { direct to next station } ELSIF reject_signal THEN Solenoid_Diverter_Valve_SCRAP() { route to scrap chute } ELSIF rework_signal THEN Slow_Conveyor_Speed(0.3) { slow to 30% speed for manual pickup } END IF { === IMAGE ARCHIVAL & LOGGING === } Log_Quality_Record( timestamp: image_timestamp, image_buffer: camera_image, part_area: part_area_mm2, predicted_class: predicted_class, confidence: predicted_confidence, features: {area, perimeter, aspect_ratio, symmetry} ) { Reset and return to idle } state := IDLE END SELECT END PROCEDURE { === ML MODEL TRAINING (Offline, Not In Production Loop) === } PROCEDURE Train_CNN_Classifier_OFFLINE { Run monthly with 500+ new images from production } VARIABLES training_dataset : ARRAY OF (image, label) PAIRS validation_dataset : ARRAY OF (image, label) PAIRS cnn_model : CNNWeights loss_history : ARRAY OF REAL BEGIN { Load labeled images from database } training_dataset = Query_Quality_Database("WHERE operator_validated = TRUE") { 80% training, 20% validation split } { Data augmentation: rotate, flip, brightness adjust } augmented_training_set = Augment_Images(training_dataset, rotations=[−15, 0, 15]) { Train CNN (PyTorch / TensorFlow framework) } cnn_model = Train_Model( architecture: "ResNet50", optimizer: "Adam", learning_rate: 0.001, epochs: 50, batch_size: 32, augmentation: TRUE ) { Validate on held-out set } validation_accuracy = Compute_Accuracy(cnn_model, validation_dataset) IF validation_accuracy > 0.99 THEN { Deploy new model } Export_Model_To_PLC(cnn_model) Log_Event("MODEL_TRAINED_ACCURACY=" + STR(validation_accuracy)) ELSE Log_Warning("MODEL_ACCURACY_LOW_TRAINING_REQUIRED") END IF END PROCEDURE ``` --- ## 4. Tag List / Diagnostics ``` CAMERA_TRIGGER_SIGNAL { 0/1, initiate image grab } CAMERA_IMAGE_TIMESTAMP { ISO datetime string } PART_DETECTED_BOOLEAN { 0/1 } PART_AREA_MM2 { calculated } PREDICTED_CLASS { text: PERFECT / MINOR / MAJOR / SCRAP / NOPART } PREDICTED_CONFIDENCE_PERCENT { 0–100% } ILLUMINATION_LUX { ambient lighting level } ILLUMINATION_DRIFT_PERCENT { deviation from baseline } PROCESSING_TIME_MS { image processing duration } ACCEPT_PART_SIGNAL { 0/1, route to accept } REJECT_PART_SIGNAL { 0/1, route to scrap } REWORK_SIGNAL { 0/1, trigger manual intervention } QUALITY_PASS_COUNT { cumulative good parts } QUALITY_REJECT_COUNT { cumulative rejects } QUALITY_DEFECT_RATE_PERCENT { reject_count / total_inspected } ``` --- ## 5. Alarm & Trip Matrix (Condensed) | **Condition** | **Severity** | **Action** | |---|---|---| | **Processing Timeout** | ⚠ WARN | Default to REJECT; reprocess | | **Camera Disconnected** | 🔴 CRIT | Stop line; manual inspection | | **Illumination Drift >10%** | ⚠ WARN | Auto-adjust camera; re-baseline | | **Model Accuracy <99%** | 🔴 CRIT | Retrain model offline; use rules | | **False Positive Rate >2%** | ⚠ WARN | Increase confidence threshold | --- ## 6. HMI Layout (Condensed) ``` ╔════════════════════════════════════════════════════════════════╗ ║ VISION INSPECTION SYSTEM — ELECTRONICS ASSEMBLY LINE ║ ╚════════════════════════════════════════════════════════════════╝ INSPECTION STATUS: [ RUNNING ] FPS: 28/30 Processing: 18 ms PART CLASSIFICATION (Last 50 Parts) ✓ PERFECT: 45 parts (90%) [██████████████████░░] ~ MINOR: 3 parts (6%) [██░░░░░░░░░░░░░░░░░░] ✗ MAJOR: 2 parts (4%) [██░░░░░░░░░░░░░░░░░░] MODEL CONFIDENCE: 96.8% average (threshold 95%) Defect Rate: 4% (2 rejects per 50 parts) LATEST IMAGE: [Live camera feed showing part outline + detected edges] Predicted: MAJOR_FLAW (95% confidence) Area: 450 mm² (nominal 500 mm²) Symmetry: 0.88 (good) Cracks detected: 2 Action: REJECT → Scrap Chute ``` --- ## 7. Configuration **JSON (vision_inspection_config.json)** ```json { "camera": { "model": "Basler_dart_BCON", "resolution_px": [2048, 2448], "sensor_size_mm": [5.3, 6.3], "pixel_size_um": 2.4, "working_distance_mm": 300, "field_of_view_mm": [190, 228], "frame_rate_fps": 30, "exposure_ms": 10, "trigger_mode": "external_rising_edge" }, "optics": { "lens_focal_length_mm": 12, "f_number": 2.8, "depth_of_field_mm": 15, "working_distance_tolerance_mm": 5 }, "lighting": { "type": "LED_ring_light", "color_temperature_k": 5000, "intensity_adjustable": true, "illumination_target_lux": 10000, "stability_monitor": "illuminometer_sensor" }, "processing": { "roi_size_px": [224, 224], "edge_detection_algorithm": "Canny", "canny_threshold_low": 50, "canny_threshold_high": 150, "feature_extraction_timeout_ms": 40, "ml_model_framework": "TensorFlow", "cnn_architecture": "MobileNetV3", "input_tensor_size": [224, 224, 3], "output_classes": ["PERFECT", "MINOR_FLAW", "MAJOR_FLAW", "SCRAP", "NO_PART"], "confidence_threshold_percent": 95 }, "quality_thresholds": { "part_area_nominal_mm2": 500, "area_tolerance_percent": 5, "symmetry_min_score": 0.85, "max_edge_defects": 2, "crack_confidence_threshold": 0.6 }, "database": { "store_all_images": true, "image_retention_days": 90, "archive_rejected_images": true } } ``` --- ## 8. Implementation Checklist (Condensed) ### Setup Phase - [ ] Camera mounted, lens focused at working distance - [ ] LED ring light calibrated to 10,000 lux ±5% - [ ] Illuminometer sensor placed near ROI - [ ] Test images of perfect and known defect parts acquired ### Model Training - [ ] Collect 500+ labeled images (200 PERFECT, 150 MINOR, 100 MAJOR, 50 SCRAP) - [ ] Train CNN for 50 epochs; validate accuracy >99% - [ ] Deploy model to PLC vision module ### Commissioning - [ ] 100-part test run; validate accuracy on production parts - [ ] Measure false positive rate; should be <1% - [ ] Verify processing time <40 ms per image --- **Document Version:** 1.0 **Last Updated:** 2026-04-13 **Status:** Ready for Production

More like this

Rate, comment, and get full access

Members can download files, rate contributions, and discuss fixes with the engineers who posted them — free.

Join the library