Robotics / CV

Laser Bug Zapper

Joel Johnston 2026-04-06 Pre-stroke design

Laser Bug Zapper

Author: Joel Johnston Date: 2026-04-06 Domain: Robotics / CV Stroke Timeline: Pre-stroke


Abstract

Autonomous targeting turret with computer vision. MG996R servos in a pan/tilt gimbal from the parametric OpenSCAD library, 1W 450nm blue laser diode with TTL (transistor-transistor logic) control, Raspberry Pi 4B running OpenCV for detection and tracking. Pipeline: camera capture → frame differencing → contour detection → centroid calculation → PID servo control → laser activation on target. Physical and software safety interlocks. $185 total build. Effective range: approximately 3 meters for flying insects.


Hardware

Gimbal and Servos

  • Servos: MG996R (2x) — 13kg/cm torque, suitable for quick directional changes
  • Configuration: Pan/tilt from parametric OpenSCAD library (same module as seeker tracker)
  • Driver: PCA9685 16-channel PWM servo driver over I2C — 12-bit resolution, hardware PWM (no CPU load for servo control)
  • Range of motion: Pan ±120 degrees, tilt +30/-60 degrees

Laser System

  • Diode: 1W 450nm (blue) laser diode module
  • Control: TTL input — logic-level on/off, no analog required
  • Driver: Dedicated laser diode driver module (constant current, protects diode from power spikes)
  • Focal length: Fixed focus at 3m (adjustable during assembly)
  • Beam characteristics: <1mm spot at 3m, Gaussian profile

Compute

  • SBC: Raspberry Pi 4B (4GB)
  • Camera: Wide-angle 160-degree USB webcam (acquisition) — wide FOV to catch targets anywhere in the patrol area
  • OS: Raspberry Pi OS Lite (headless)

CV Pipeline

Stage 1: Frame Differencing

Background subtraction using OpenCV BackgroundSubtractorMOG2. Produces a foreground mask isolating moving objects from the static background. Threshold tuned to reject small noise (dust, camera noise) while catching insect-scale motion.

Stage 2: Contour Detection

findContours on the foreground mask. Filters by:

  • Minimum area (rejects noise, requires target-scale object)
  • Maximum area (rejects large non-insect motion — hands, birds passing through frame)
  • Aspect ratio (optional — insects have roughly isometric bounding boxes in flight)

Stage 3: Centroid Calculation

Centroid of the largest qualifying contour. Converted from pixel coordinates to angular offsets from current gimbal position.

Stage 4: PID Servo Control

Two independent PID (proportional-integral-derivative) controllers — one for pan, one for tilt. Error input: pixel offset from frame center. Output: servo position delta. Gains tuned for fast acquisition without overshoot on the final approach.

Stage 5: Laser Activation

Laser fires when:

  1. Target centroid is within N pixels of frame center (on-target threshold)
  2. Software enable flag is set
  3. Physical key switch is engaged
  4. Last fire timestamp is beyond minimum refire interval (prevents sustained on-time)

On-target condition triggers a 200ms burst. Refire interval: 500ms minimum. Maximum continuous on-time enforced in firmware as a failsafe independent of software.


Safety Architecture

Layered interlocks — all must be satisfied for laser to fire:

Layer Mechanism Failure Mode
Physical Key switch (keylock, not toggle) Key removed = laser cannot fire regardless of software state
Software flag laser_enabled boolean Set to False = laser cannot fire even if on-target
On-target gate Pixel distance threshold Laser inactive while slewing to target
Maximum burst duration 200ms enforced in software Prevents sustained on-time on missed targets
Refire interval 500ms minimum between bursts Prevents rapid repeated firing
Exclusion zones Configurable polygon regions Laser will not fire if centroid is in an exclusion zone
Gimbal limit switches Mechanical stops + software limits Prevents gimbal cable wrap (mechanical) and out-of-bounds slew (software)

Exclusion zones are the most important operational safety feature. Define zones corresponding to areas where people, pets, or other sensitive targets might be — laser will not fire if the centroid is in those zones. Configurable as polygon regions in camera coordinates.


Full BOM

Component Cost
Raspberry Pi 4B (4GB) $55
MG996R servos (2x) $12
PCA9685 PWM driver $8
1W 450nm laser module + driver $25
Wide-angle USB webcam $20
3D-printed gimbal (PLA) $3
Key switch + enclosure $15
Limit switches (2x) $5
Power supply (5V 3A) $12
Misc hardware, wiring $10
Mounting hardware $20
Total ~$185

Shared Architecture with Seeker Tracker

The targeting pipeline is identical to the seeker tracker's. The only difference is the effector:

Component Bug Zapper Seeker Tracker
Gimbal MG996R (same module) MG996R (same module)
Servo driver PCA9685 (same) PCA9685 (same)
CV pipeline frame diff → contour → centroid → PID frame diff → contour → centroid → PID
Effector 1W laser (fire on target) Pi HQ camera (record on target)

The parametric OpenSCAD library and the shared CV architecture are both expressions of the primitive composition pattern — the same primitives compose into different systems.