Comparatron - an Affordable Digital Optical Comparator for Reverse-engineering

by CameronCoward in Workshop > Tools

12967 Views, 158 Favorites, 0 Comments

Comparatron - an Affordable Digital Optical Comparator for Reverse-engineering

comparatron_software1.png
comparatron1.jpg
Comparatron Demonstration - DIY digital optical comparator

Optical comparators are instruments for taking very precise measurements of physical objects through visual means. They're very useful for reverse-engineering and for CAD/PCB designs that need to interface with existing objects (for example: designing an enclosure that is an exact fit for an existing PCB).

This project combines CNC, 3D printing, CAD, and Python programming. Comparatron is perfect for generating DXF files that you can then use in Autodesk Fusion 360, Eagle, and many other design programs. I built this because I needed a way to get precise dimensions of objects with complex shapes, for use in Fusion 360 and PCB design.

GitHub Repository (standalone executable is here)

https://imgur.com/a/yOdN4yp (this video demonstrates the accuracy)

Background

Don't worry, you can skip this part if you don't want to read rambling about my life story...

Before I transitioned to writing full-time, I was a mechanical designer/drafter. For a short time, I worked for a medical company where my entire job was reverse-engineering medical devices. I had to create perfectly accurate CAD models of existing medical devices manufactured by other companies and some of those (like screws for finger bones and toes) were incredibly small—too small to see the details with naked eye or to measure with calipers/micrometers.

In order to find the dimensions of those objects, I used an instrument called an optical comparator. It let me zoom in on tiny parts and measure distances between points, like the pitch of itty-bitty screw threads. I could then use those measurements to recreate the parts in CAD.

Fast forward to the present. I was working on a project in which I needed to reproduce one half of a key fob enclosure in CAD. That key fob has a curved shape and lots of small details. I spent many hours attempting to create an accurate 3D model using calipers and photos, but wasn't able to get it right.

I found myself thinking "man, I sure wish I had that optical comparator..." I even searched eBay for old, used optical comparators. But they're big, heavy, and expensive. Even if I could afford one, I wouldn't have room for one.

Then I started thinking about what an optical comparator actually needs to accomplish. All it really needs to do is give you a clear, close-up view of an object and move with precision.

I realized I already had what I needed to build my own digital optical comparator: a CNC pen plotter. A while back, I reviewed an iDraw Pen Plotter 2.0. After I finished my review, I put it on a shelf because I had no real use for it. But that machine has full GRBL CNC control and I figured it would be the perfect base for an optical comparator

Supplies

Good news! The folks at iDraw liked this project so much that they're giving everyone 15% off their pen plotters. Just go to their store (https://idrawpenplotter.com/shop) and use the coupon code "CAMERON" to get the discount.


  1. iDraw Pen Plotter 2.0 - A4 Size
  2. Jiusion 40 to 1000x Magnification Endoscope
  3. LitEnergy Portable A4 Tracing LED Copy Board Light Box
  4. 3D-printed spur gear
  5. 3D-printed microscope mount

Optional parts:

  1. USB Hub
  2. USB extension cable
  3. 3M Commands strips (to attach light board and USB hub)
  4. Zip ties (to tidy wiring)

Tools:

  1. 3D printer (resin or filament is fine)
  2. Windows PC

Software:

  1. Comparatron software (executable or Python script)
  2. Autodesk Fusion 360, Eagle, AutoCAD, or any other software that can accept a DXF file

Machine Overview

comparatron_microscope_mount.jpg

Starting with an assembled iDraw Pen Plotter 2.0, only a few modifications are required to build Comparatron.

First, remove the pen lift mechanism so that only the stepper motor and linear rail remain.

Then use four M3x10 button head machine screws to attach to the 3D-printed microscope mount to the linear rail bearing block.

Press-fit the 3D-printed spur gear (the pinion) onto the stepper motor shaft, meshing the gear teeth with the rack.

Clip the microscope onto the mount. Use some tape if necessary to make sure it doesn't wiggle around.

Remove the spring clip on the pen plotter's drawing board, then position the LED-backlit tracing board on top.

If you want, use some 3M command strips to secure the tracing board.

Route the pen plotter, tracing board, and microscope USB cables underneath the machine and secure them with zip ties. If you want tidier wiring, plug the USB cables into the USB hub and secure the USB hub underneath the machine with 3M command strips.

I recommend keeping the microscope at the lowest magnification (40x), as it really isn't useful to zoom in any further. 40x magnification is plenty to get a very clear view of part edges and is already at the limits of the machine's positioning accuracy.

3D Modeling

comparatron_3D_parts.png

This project required two 3D-printed parts: the spur gear for the rack-and-pinion Z-lift mechanism and the microscope mount.

I designed both in Autodesk Fusion 360 CAD. If you're familiar with my work, you know that I wrote a book on how to create models in Fusion 360, so it is obviously my go-to software.

Creating the spur gear was very easy, as Fusion 360 has an add-in that will automatically create the solid for you based on the parameters you enter. In this case, I knew the general size I wanted it to be and based everything else off of that.

That spur gear is the pinion, but creating the rack is a little more complex. You could just "eyeball it" and it would probably work, but if you want to to it the right way, I highly suggest following Rob Duarte's video tutorial here.

3D Printing Parts

This is straightforward, because the parts are printable on just about any 3D printer.

I printed mine on a Phrozen Sonic Mighty 8K MSLA resin 3D printer. I used my own custom resin mix (Siraya Tech Blu + black ABS-like resin) and setup the print in Lychee slicer.

You should be able to also print both parts easily on an FDM/FFF 3D printer with light supports around the microscope mount.

The rack and pinion were designed for loose tolerances, so they shouldn't be hard to print. There is a lot of backlash in the gears, but that isn't an issue since gravity pulls the entire mount down.

Deciphering CNC Pen Plotter Commands

A huge part of the work that went into this project involved figuring out the CNC commands.

While I have a lot of experience with CNC mills, there are few details available online about the iDraw. Normally it works with an Inkscape extension, which is basically a copy of the AxiDraw Inkscape extension. But there isn't much information about what commands it accepts.

With a serial sniffer utility, I was able to figure out the details. The most important thing was the the iDraw Pen Plotter 2.0's control board runs Grbl 1.1 g-code.

Knowing that and with some experimentation, I was able to work out the commands I needed and how the machine responds to each command. That information was necessary to programmatically issue commands and receive status information.

Programming the Python Interface

comparatron_python.png

I chose to program the software in Python because it had available modules for all of the functions I required. Those modules were:

  1. DearPyGUI (for building the UI)
  2. OpenCV (for pulling the microscope video feed and adding a crosshair)
  3. NumPy (for calculations)
  4. PySerial and Serial Tools (to connect to the pen plotter and send Grbl commands)
  5. EZDXF (for creating DXF, plotting points, and exporting DXF file)

Programming is not my strong suit, so this was a learning experience. I worked on the problem in stages, figuring out each part before moving onto the next.

For example, my first step was to simply show the video feed coming from the microscope in a GUI window. Once I figured that out, I worked on adding a crosshair. Then connecting to the machine through serial and sending movement commands, and so on.

I have heavily commented my Python code so you can understand what everything does.

At this time, there are only three major shortcomings I can think of:

  1. It only works in Windows*
  2. You can only draw points (not lines or anything else)
  3. You cannot edit/delete existing points

I don't think #2 is much of a problem, since points are the most important. You can draw lines, arcs, etc. in CAD afterwards, but the points are necessary to get precise measurements.

It would be nice to be able to edit or delete drawn points, but implementing that would be complicated and I wasn't quite sure how to go about it. I also don't think it is completely necessary, as you can always edit points in CAD.

*I don't actually know for sure if it works in other operating systems or not, because I haven't tested it. Python and all of the modules are OS-agnostic, but the serial connection stuff seems to be OS-dependent,

Downloads

Software Usage

comparatron_windows.png

I hope the software is intuitive, but just in case I'll cover the basics here.

Upon launching the software, you'll get a window filled with several other windows.

The top-left window (1) is the view from the microscope. The script uses the first available camera. If this doesn't work properly, make sure no other webcams are connected to your computer. The crosshair reticle in the middle of the video feed is a reference for placing your points.

The right window (2) shows a visualization of the plotted points. It represents the ENTIRE tracing board, so if the measured object is small then it won't take up much space in that window. This is not interactive and you can't edit or move the points, it is just a visual representation of the log of drawn points.

The top-left window below the video feed (3) provides movement settings, including distance and speed. I recommend using "Slow Feed" for your actual measurements and only using "Fast Feed" for getting close to the object when you first start.

The next window to the right (4) is the jog control. This lets you move the microscope. Z adjustments should really only be used to focus the object. The machine homes to the top-right of the tracing board, so all Y-axis measurements are negative and you need to push the "Y-" button to move down onto the tracing board.

The next window to the right (5) is for drawing tools. At this time, the only tool available is "New Point." That creates a point at the global WPS coordinates, both in the DXF file and in the visual representation in the "Created Plot" window. Other tools, such as lines, may be added in the future. But points are the most important, as you will do your actual drawing in CAD using those points for reference.

The bottom-left window (6) provides everything for connecting to and setting up the pen plotter on startup. I recommend activating each command in order (top to bottom). Start by selecting the COM port of the iDraw machine from the drop-down menu. It should say something like "USB-SERIAL CH340" and the COM port number. Then click "Connect to the above COM port" to connect to the machine through serial (the Python console provides printed responses). Next, Home the machine, Unlock the motors, Set the feedrate, Set the origin point, and finally Set the machine to relative coordinates.

The last window (7) is the "DXF Output" tool. Simply specify the filename you'd like, then click "Export DXF." It will save a DXF file with your chosen name in the same folder as the Python script/executable. You can do this as many times as you like. If you use the same filename, it will overwrite the original file. Or you can use a new filename. In both cases, it will contain all of the drawn points since you first launched the software. When you're finished, I recommend pressing "Close Serial Connection" so that you can reconnect to the machine later without power cycling it.

Suggested Workflow

  1. Plug in USB cables for tracing board, microscope, and pen plotter.
  2. Plug in DC power adapter for pen plotter.
  3. Turn on the tracing board and the pen plotter.
  4. Launch Comparatron software.
  5. Connect to COM port and follow startup sequence.
  6. Move Z axis up to gain clearance.
  7. Use Fast Feed to move close to your object.
  8. Switch to Slow Feed to find an edge.
  9. Adjust Z axis until the edge is in focus.
  10. Follow the edges at Slow Feed and add points to trace your object
  11. Export DXF
  12. Click "Clean exit" button to release video feed, close serial port, and exit Comparatron program
  13. Open Autodesk Fusion 360 or other CAD software (or even PCB design software)
  14. Import the DXF (as millimeters)
  15. Use the points as needed for your design

Demonstration

Comparatron Demonstration - DIY digital optical comparator

This video shows a quick demonstration of Comparatron. In the video, I use Comparatron to measure a real object (a random key) and then use the DXF to quickly create a 3D model in Autodesk Fusion 360.

Updates:

Comparatron_3D_V1.png
IMG-2590.jpg

V1.0 10/27/22

  • Rewrote exit functions for software to cleanly close the serial connection and release the video feed.
  • Dramatically improved strength and rigidity of microscope mount, reducing flex and increasing accuracy.
  • Also changed rack and pinion parameters to keep the weight of the microscope from pulling the Z axis down when power is off.


V0.9

Original release