The Smell of Molten Projects in the Morning

Ed Nisley's Blog: Shop notes, electronics, firmware, machinery, 3D printing, laser cuttery, and curiosities. Contents: 100% human thinking, 0% AI slop.

Category: Software

General-purpose computers doing something specific

  • Monthly Science: Minimum Groundwater Temperatures, 2006-2014

    The picture says it all:

    Basement Air Groundwater Minimum Temperatures - 2006-2014
    Basement Air Groundwater Minimum Temperatures – 2006-2014

    Much as we thought, this past winter was really cold.

    The data consists of all 3/4 million data logger records concatenated into one huge CSV file, fed through a Sed pipe to normalize all the dates & suchlike, then passed into a Python script that produces one record for each day (all 2561 of ’em) containing the date, minimum air & water temperatures, and the minimum relative humidity.

    This needs (a lot) more work to be pretty, but at least the pieces hang together.

    The Python source code:

    #!/usr/bin/python3
    ''' Extract minimum groundwater / air temperatures & humidity from CSV files
    '''
    
    import sys
    import csv
    import datetime
    import string
    
    # Columns in Hobo datalogger CSV file
    
    SEQNUM = 0
    DATETIME = 1
    AIRTEMP = 2
    RELHUM = 3
    WATERTEMP = 4
    
    datapoints = {}
    
    with open('AllClean.csv',encoding='iso-8859-15') as dbi:
        for row in csv.reader(dbi):
            if (not row[SEQNUM].startswith("#")):            # discard comments
                logdt = datetime.datetime.strptime(row[DATETIME],'%m/%d/%Y %H:%M:%S')
                logdate = datetime.datetime.date(logdt)
                if (logdate in datapoints):             # accumulate minimum temps & RH
                    datapoints[logdate][0] = min(datapoints[logdate][0],row[AIRTEMP]) 
                    datapoints[logdate][1] = min(datapoints[logdate][1],row[RELHUM]) 
                    datapoints[logdate][2] = min(datapoints[logdate][2],row[WATERTEMP]) 
                else:
                    datapoints[logdate] = [row[AIRTEMP], row[RELHUM], row[WATERTEMP]]
    
    with open('AllMinData.csv','w',newline='') as csvf:
        dbo = csv.writer(csvf)
        dbo.writerow(('#Date','Min Air T','Min RH','Min Water T'))
        for key,value in sorted(datapoints.items()):
            dbo.writerow([key,value[0],value[1],value[2]])
    

    The encoding='iso-8859-15' for the input file turns out to be absolutely essential, as the Hoboware program generating the CSV files uses a 0xb0 character for the usual degree symbol. Alas, that chokes the default utf-8, ascii, and even cp437 codecs. Took a while to figure that out, it did, indeed.

    There remain random anomalies in the data, in addition to the glitches produced by unplugging the remote temperature sensor cable. I may simply discard the last few records of each CSV file; right now, the Gnuplot code simply ignores temperatures under 30 °F and over 80 °F.

    The Gnuplot script that produced the graph consisted of some hand-fed tweakery based on the guts of the routine that plotted the original records, with the output image bank-shotting off the clipboard into GIMP on its way to becoming a PNG file. Phew!

  • Slicing Anomaly: Thin Fill

    Having discovering that the chocolate mold positives suffered from sparse top infill, to the extent that silicone rubber would flow right though the surface…

    Tux Gradient - PLA positive detail
    Tux Gradient – PLA positive detail

    … I ran off a few variations of the classic 20 mm calibration “cube” (which is 10 mm tall):

    Solid cube - thin top infill - on platform
    Solid cube – thin top infill – on platform

    Not only were the infilled surfaces porous, I could see right through the block! That’s impossible to photograph, but here’s a laser beam shining through the entire 10 mm stack, showing how precisely the M2 aligns 50 under-filled thread layers:

    Solid cube - laser transmission
    Solid cube – laser transmission

    The yellow spot in the middle marks the overexposed laser beam. There’s a distinct beam passing through the block that, with the proper orientation, can create a spot on the cutting mat atop my desk.

    In fact, I can blow air through the blocks; one could use them as (rather coarse) air filters.

    Normally, underfill happens when a mechanical problem prevents the printer from feeding enough filament to keep up with demand, but that’s not the case here: the perimeter threads came out exactly 0.4 mm wide for the entire height of the cube, as you can see if you click the picture for more dots. The top and bottom infill, plus all the interior threads, seem to be about half the nominal width and don’t touch their neighbors on the same XY plane at all.

    Alex Ustyantsev’s incomparable G-Code Analyzer shows that Slic3r baked the problem right into the G-Code, so the M2 is cranking out exactly the right amount of filament:

    Solid cube - Slic3r thin infill
    Solid cube – Slic3r thin infill

    The colors show the length of extruder filament per millimeter of XY motion, not the usual XY speed, with the two perimeter threads at 0.033 mm/mm and the interior at 0.18 mm/mm. In round numbers, the G-Code starves the infill by a factor of 1.8, which is close enough to the factor of two I’d guessed going into this mess.

    Being that type of guy, I set the exact extrusion thickness and width (0.20 x 0.40 mm), rather than let Slic3r pick them. The extruded thread has a fixed cross-section of (roughly) 0.080 mm2 and a millimeter of XY motion thus requires 0.080 mm3 of filament.

    The PLA filament measures 1.79 mm diameter, for a cross-section of 2.5 mm2. Getting 0.080 mm3 from the incoming filament requires feeding 0.032 mm into the extruder, which is almost exactly what you see for the perimeter threads.

    After restoring Slic3r’s default configuration, the problem Went Away, which suggests that I backed the algorithms into a corner with some perverse combination of settings. Rebuilding my usual configuration from the defaults also worked fine, so it’s obviously not Slic3r’s problem.

    Which one is not like the other ones?

    Solid cube tests
    Solid cube tests

    You can see the thin infill on three of those cubes, with the solid one in the lower right showing how it should look.

    The solid cube weighs 4.4 g and the thin-fill variations weigh 2.7 to 2.9 g. Assuming PLA density = 1.25 g/cm3 and “cube” volume = 4 cm3, a completely solid cube should weigh 5.0 g. I think 4.4 g is close enough; the top surface came out flat with nice adjacent-thread fusion. Working backwards, the average fill = 88%; the perimeter is fused-glass solid, so the actual infill will be a bit under that.

    I generally run Slic3r from my desktop box, with ~/.Slic3r symlinked to the actual config directory and its files on the NFS server downstairs. Perhaps running different versions of Slic3r on two or three different boxes, all using the same config files, wrecked something that didn’t show up in the UI and produced bad slices. I probably ran two different versions of Slic3r at the same time against the same files, although I wasn’t simultaneously typing at both keyboards.

    Moral of the story: check the G-Code before assuming a hardware failure!

  • Revised Thinwall Open Box Calibration Object

    Which thinwall open box is better?

    Object A:

    Thinwall Open Box - Minkowski - solid model
    Thinwall Open Box – Minkowski – solid model

    or Object B:

    Thinwall Open Box - hull - solid model
    Thinwall Open Box – hull – solid model

    The latter, of course: I blundered the inner corner radius, which occasionally produced little tiny dots of infill that shouldn’t be there. Just one of those errors that hides in plain sight until something else goes wrong, then it’s obvious.

    Rather than fix the Minkowski version, I rebuilt it using the hull() operator to shrinkwrap four cylinders for each solid, then remove the smaller block from the larger. Commenting out the hull() operators  shows that the cylinders now line up properly:

    Thinwall Open Box - un-hulled - solid model
    Thinwall Open Box – un-hulled – solid model

    The OpenSCAD source code:

    // Thin wall open box calibration piece
    // Adapted from Coasterman's Calibration set
    // Ed Nisley - KE4ZNU - Dec 2011
    // Adjust for Slic3r/M2 - March 2013
    // Reworked for hull() with correct corner radii - April 2014
    
    //-------
    //- Extrusion parameters must match reality!
    //  None of the fill parameters matter
    
    ThreadThick = 0.20;
    ThreadWidth = 0.40;
    
    Protrusion = 0.1;           // make holes end cleanly
    
    function IntegerMultiple(Size,Unit) = Unit * ceil(Size / Unit);
    
    //-------
    // Dimensions
    
    Height = IntegerMultiple(5.0,ThreadThick);
    
    WallThick = ThreadWidth;
    
    CornerRadius = 2.0;
    CornerSides = 4*8;
    
    SideLen = 20.0 - 2*CornerRadius;
    
    Rotation = 45;
    
    //-------
    
    module ShowPegGrid(Space = 10.0,Size = 1.0) {
    
      Range = floor(50 / Space);
    
        for (x=[-Range:Range])
          for (y=[-Range:Range])
            translate([x*Space,y*Space,Size/2])
              %cube(Size,center=true);
    }
    
    //-------
    
    ShowPegGrid();
    
    rotate(Rotation)
    	difference() {
    		hull() {
    			for (i=[-1,1], j=[-1,1])
    				translate([i*SideLen/2,j*SideLen/2,0])
    					cylinder(r=CornerRadius,h=Height,$fn=CornerSides);
    		}
    		hull() {
    			for (i=[-1,1], j=[-1,1])
    				translate([i*SideLen/2,j*SideLen/2,-Protrusion])
    					cylinder(r=(CornerRadius - WallThick),h=(Height + 2*Protrusion),$fn=CornerSides);
    		}
    	}
    
  • Wide-Angle Lens Distortion Correction

    The Sony HDR-AS30V camera lens has a view angle of 120° or 170°, achieved by internal image processing rather than mechanical lens adjustments. For most action-camera purposes you don’t care about fisheye distortion, but sometimes a more rectilinear picture will look better, in which case the GIMP’s Lens Distortion filter comes in handy.

    A still image at 120°, which doesn’t look all that bad, really:

    Sony HDR-AS30V 120 angle - as captured
    Sony HDR-AS30V 120 angle – as captured

    Applying Main=-25 gives this:

    Sony HDR-AS30V 120 angle - corrected
    Sony HDR-AS30V 120 angle – corrected

    A frame captured from video at 170°, with the overhead wires hanging upward:

    Sony HDR-AS30V 170 angle - as captured
    Sony HDR-AS30V 170 angle – as captured

    Applying Main=-25, Edge=-12.5, Zoom=+8 flattens them enough to be acceptable:

    Sony HDR-AS30V 170 angle - corrected
    Sony HDR-AS30V 170 angle – corrected

    The main effect of the Zoom parameter seems to be discarding the severely distorted remnants around the edges of the corrected 170° view. Sometimes, those pixels around the edges can be very, very important, so I’d rather make that decision after the fact.

    If you must fix many images at once, Fred’s defisheye ImageMagick Script would certainly be useful. There’s also a bare-knuckles ImageMagick version, including how to measure lens parameters.

  • Strobe Photography: Control Program

    A solderless breadboard sufficed for the simple circuitry behind the strobe controller:

    Strobe Photography - control breadboard
    Strobe Photography – control breadboard

    I used a separate 7.5 V supply for the Arduino Pro Mini to keep the relay noise out of the VCC circuit, but that’s probably not really necessary; you could back-drive the Pro Mini’s regulator with +5 V and it’d be perfectly happy. There’s a +5 V wall wart for the relay, LEDs, and so forth.

    Protip: you do not want to drive all the other circuitry through the Pro Mini’s tiny little regulator. Work out the power dissipation in the regulator caused by a 130 Ω relay, about 10 mA for the laser, 100 mA for the white LED, and whatever the Pro Mini draws. Yeah, some of those are intermittent loads, but work it out anyway.

    A 1.5 V bench supply powers the Xenon strobe in place of the AA alkaline cell I used at first. The boost circuit pins the supply at 3 A for a few seconds, then settles at about 350 mA (!) while idling; no wonder the poor little AA cells don’t last very long!

    The control program is also dead simple; it’s mostly a state machine that notices when the photocurrent drops to zero, then steps through a series of fixed delays while turning the laser, LED, and strobe outputs on and off.

    The default values highlight a falling object about 200 mm below the laser beam-break sensor, assuming you release the object just above the beam:

    Ball at 200 mm - detail
    Ball at 200 mm – detail

    The laser beam is at the 200 mm mark, so that ball passing 400 mm has dropped 200 mm.

    The quadrature encoder knob recycles the same interrupt handler I used earlier, with the shaft button selecting either the LED delay (pushed) or the Xenon strobe delay (released). There’s precious little error checking, as befits a quick hack job, so use at your own risk…

    The Arduino source code:

    // Optical flash triggering
    // Ed Nisley - KE4ANU - March 2014
    
    //----------
    // Pin assignments
    
    const byte PIN_KNOB_A = 2;			// knob A switch - must be on ext interrupt 2
    const byte PIN_KNOB_B = 4;			//  .. B switch
    const byte PIN_KNOB_SWITCH = A3;	//  .. shaft push switch
    
    const byte PIN_PHOTOCURRENT = A0;	// photodiode current input
    
    const byte PIN_LASER = 8;   		// laser drive -active
    const byte PIN_LED = 7;		   		// LED drive -active
    const byte PIN_FLASH = 12;			// Xenon flash relay -active
    
    const byte PIN_SYNC = 13;			// scope sync - and Arduino LED
    
    //----------
    // Constants
    
    enum FALLING_STATES {F_IDLE,F_WAIT,F_DETECT,F_PREFALL,F_LED,F_MD,F_FLASH,F_CLEAR};
    
    enum KNOB_STATES {KNOB_CLICK_0,KNOB_CLICK_1};
    
    //----------
    // Globals
    
    const unsigned long UPDATEMS = 250;	// update displays only this many ms apart
    
    volatile char KnobCounter = 0;
    volatile byte KnobState;
    
    byte Button, PrevButton;
    
    byte Falling = F_IDLE;				// cold start the detection state machine
    
    unsigned long FallStart;			// when we we detected the falling object
    
    unsigned int DetectLevel = 200;		// ADC reading for object detection
    
    unsigned int DelayLED = 1;			// ms from trigger detect to LED preflash
    
    unsigned int DelayFlash = 180;		//  ... to Xenon flash
    
    unsigned int DelayClear = 6000;		//  ... after impact to allow camera restart
    
    const byte PulseLED = 50;			// ms LED on to pass motion detection threshold
    const byte PulseFlash = 20;			// ms Xenon flash relay on
    
    const unsigned int RelayAdvance = 3;	// ms relay activation to Xenon flash
    
    unsigned long MillisNow;
    unsigned long MillisThen;
    
    //-- Helper routine for printf()
    
    int s_putc(char c, FILE *t) {
      Serial.write(c);
    }
    
    //-- Knob interrupt handler
    
    void KnobHandler(void)
    {
    	byte Inputs;
    	Inputs = digitalRead(PIN_KNOB_B) << 1 | digitalRead(PIN_KNOB_A);  // align raw inputs
    //	Inputs ^= 0x02;                             // fix direction
    
    	switch (KnobState << 2 | Inputs) {
    	case 0x00 : 				// 0 00 - glitch
            break;
    	case 0x01 : 				 // 0 01 - UP to 1
            KnobCounter++;
    		KnobState = KNOB_CLICK_1;
    		break;
    	case 0x03 : 				 // 0 11 - DOWN to 1
            KnobCounter--;
    		KnobState = KNOB_CLICK_1;
    		break;
    	case 0x02 : 				 // 0 10 - glitch
            break;
    	case 0x04 : 				 // 1 00 - DOWN to 0
            KnobCounter--;
    		KnobState = KNOB_CLICK_0;
    		break;
    	case 0x05 : 				 // 1 01 - glitch
            break;
    	case 0x07 : 				 // 1 11 - glitch
            break;
    	case 0x06 : 				 // 1 10 - UP to 0
            KnobCounter++;
    		KnobState = KNOB_CLICK_0;
    		break;
    	default :  					// something is broken!
            KnobCounter = 0;
    		KnobState = KNOB_CLICK_0;
    	}
    }
    
    //------------------
    // Set things up
    
    void setup() {
    
    	pinMode(PIN_SYNC,OUTPUT);
    	digitalWrite(PIN_SYNC,LOW);			// show we arrived
    
    	pinMode(PIN_KNOB_B,INPUT_PULLUP);
    	pinMode(PIN_KNOB_A,INPUT_PULLUP);
    	pinMode(PIN_KNOB_SWITCH,INPUT_PULLUP);
    
        pinMode(PIN_LASER,OUTPUT);
        digitalWrite(PIN_LASER,HIGH);
    
        pinMode(PIN_LED,OUTPUT);
        digitalWrite(PIN_LED,HIGH);
    
        pinMode(PIN_FLASH,OUTPUT);
        digitalWrite(PIN_FLASH,HIGH);
    
    	KnobState = digitalRead(PIN_KNOB_A);
    	Button = PrevButton = !digitalRead(PIN_KNOB_SWITCH);
    
    	attachInterrupt((PIN_KNOB_A - 2),KnobHandler,CHANGE);
    
    	Falling = F_IDLE;
    
    	Serial.begin(9600);
    	fdevopen(&s_putc,0);				// set up serial output for printf()
    
    	printf("Xenon Flash Trigger\r\nEd Nisley - KE4ZNU - March 2014\r\n");
    
    	MillisThen = millis();
    
    }
    
    //------------------
    // Go flash!
    
    void loop() {
    
    	MillisNow = millis();
    
    	if (KnobCounter) {
    		Button = !digitalRead(PIN_KNOB_SWITCH);
    		if (Button)
    			DelayLED += KnobCounter;
    		else
    			DelayFlash += KnobCounter;
    
    		DelayLED = min(DelayLED,DelayFlash - PulseLED);
    		printf("Knob: %d, LED: %d, Flash: %d\n",KnobCounter,DelayLED,DelayFlash);
    		KnobCounter = 0;
    	}
    
    	digitalWrite(PIN_SYNC,HIGH);
    
    	switch (Falling) {
    	case F_IDLE :								// turn on laser for object detection
    		digitalWrite(PIN_LASER,LOW);
    		printf("Laser on, stabilizing... ");
    		while (analogRead(PIN_PHOTOCURRENT) <= DetectLevel) {
    			printf("*");
    		}
    		printf("\nReady!\n");
    		Falling = F_WAIT;
    		break;
    	case F_WAIT :								// record starting time of beam break
    		if (analogRead(PIN_PHOTOCURRENT) < DetectLevel) {
    			FallStart = millis();
    			Falling = F_DETECT;
    		}
    		break;
    	case F_DETECT :								// turn off laser to signal detection
    		digitalWrite(PIN_LASER,HIGH);
    		Falling = F_PREFALL;
    		break;
    	case F_PREFALL :							// turn on LED to trigger camera motion detection
    		if ((millis() - FallStart) >= DelayLED) {
    			digitalWrite(PIN_LED,LOW);
    			Falling = F_LED;
    		}
    		break;
    	case F_LED : 								// turn off LED
    		if ((millis() - FallStart) >= (DelayLED + PulseLED)) {
    			digitalWrite(PIN_LED,HIGH);
    			Falling = F_MD;
    		}
    		break;
    	case F_MD :									// fire the strobe to take picture
    		if ((millis() - FallStart) >= (DelayFlash - RelayAdvance)) {
    			digitalWrite(PIN_FLASH,LOW);
    			Falling = F_FLASH;
    		}
    		break;
    	case F_FLASH :								// turn off strobe relay
    		if ((millis() - FallStart) >= (DelayFlash - RelayAdvance + PulseFlash)) {
    			digitalWrite(PIN_FLASH,HIGH);
        		printf("Flash with LED delay: %d, Xenon delay: %d ...",DelayLED,DelayFlash);
    			Falling = F_CLEAR;
    		}
    		break;
    	case F_CLEAR :								// wait for camera to recycle
    		if ((millis() - FallStart) >= DelayClear) {
    			printf("done\n");
    			Falling = F_IDLE;
    		}
    		break;
    	default :
    		printf("** Bad Falling state: %02X",Falling);
    		Falling = F_IDLE;
    	}
    
    	digitalWrite(PIN_SYNC,LOW);
    
    	if ((MillisNow - MillisThen) > UPDATEMS) {
    //		printf("State: %02X\n",Falling);
    		MillisThen = MillisNow;
    	}
    
    }
    
    
  • Laser-photodiode Beam-Break Sensor Fixture

    The game plan: drop a small object through a laser beam that shines on a photodiode, thus causing an electrical signal that triggers various flashes and cameras and so forth and so on. This fixture holds the laser and photodiode in the proper orientation, with enough stability that you (well, I) can worry about other things:

    Laser-photodiode fixture - on blade
    Laser-photodiode fixture – on blade

    It’s mounted on the blade of a dirt-cheap 2 foot machinist’s square clamped to the bench which will probably get a few holes drilled in its baseplate for more permanent mounting.

    The solid model looks about like you’d expect:

    Laser-photodiode fixture - solid model
    Laser-photodiode fixture – solid model

    There’s a small hole in the back for an 8-32 setscrew that locks it to the blade; the fit turned out snug enough to render the screw superfluous. I added those two square blocks with the holes after I taped the wires to the one in the picture.

    The two semicircular (well, half-octagonal) trenches have slightly different diameters to suit the heatshrink tubing around the photodiode (a.k.a., IR LED) and brass laser housing. A dab of fabric adhesive holds the tubes in place, in addition to the Gorilla Tape on the ends.

    The laser came focused at infinity, of course. Unscrewing the lens almost all the way put the focus about 3/4 of the way across the ring; call it 40 mm. The beam is rectangular, about 1×2 mm, at the center of the ring, and I rotated the body to make the short axis vertical; that’s good enough for my purposes.

    The cable came from a pair of cheap earbuds with separate Left/Right pairs all the way from the plug.

    The model builds in one piece, of course, and pops off the platform ready to use:

    Laser-photodiode fixture - on platform
    Laser-photodiode fixture – on platform

    If you were doing this for an analytic project, you’d want a marker for the beam centerline on the vertical scale, but that’s in the nature of fine tuning. As it stands, the beam sits 8 mm above the base and flush with the top surface of the ring; if that were 10 mm, it’d be easier to remember.

    The OpenSCAD source code has a few tweaks and improvements:

    // Laser and LED-photodiode break-beam sensor
    // Ed Nisley - KE4ZNU - March 2014
    
    Layout = "Show";			// Build Show Ring Mount Guide
    
    //- Extrusion parameters must match reality!
    //  Print with 2 shells and 3 solid layers
    
    ThreadThick = 0.20;
    ThreadWidth = 0.40;
    
    HoleWindage = 0.2;			// extra clearance
    
    Protrusion = 0.1;			// make holes end cleanly
    
    AlignPinOD = 1.70;			// assembly alignment pins: filament dia
    
    inch = 25.4;
    
    function IntegerMultiple(Size,Unit) = Unit * ceil(Size / Unit);
    
    //----------------------
    // Dimensions
    
    LaserOD = 6.0;				// brass focus tube
    LaserLength = 20.0;			//  ... wire clearance
    
    SensorOD = 6.5;				// including light shield
    SensorLength = 20.0;		//  ... wire clearance
    
    RingSize = [50.0,70.0,8.0,8*4];	// support ring dimensions
    RING_ID = 0;
    RING_OD = 1;
    RING_THICK = 2;
    RING_SIDES = 3;
    
    StrutWidth = 2.5;					// strut supporting this thing
    StrutLength = 26.5;
    
    StrutBlock = [10.0,35.0,20.0];		// block around the clearance slot
    BLOCK_WIDTH = 0;
    BLOCK_LENGTH = 1;
    BLOCK_HEIGHT = 2;
    
    StrutScrewTap = 2.7;				// 6-32 SHCS
    
    GuideID = 4.0;						// guide for cables
    GuideOD = 3*GuideID;
    
    BuildSpace = 3.0;					// spacing between objects on platform
    
    //----------------------
    // Useful routines
    
    module PolyCyl(Dia,Height,ForceSides=0) {			// based on nophead's polyholes
    
      Sides = (ForceSides != 0) ? ForceSides : (ceil(Dia) + 2);
    
      FixDia = Dia / cos(180/Sides);
    
      cylinder(r=(FixDia + HoleWindage)/2,
               h=Height,
               $fn=Sides);
    }
    
    module ShowPegGrid(Space = 10.0,Size = 1.0) {
    
      RangeX = floor(100 / Space);
      RangeY = floor(125 / Space);
    
    	for (x=[-RangeX:RangeX])
    	  for (y=[-RangeY:RangeY])
    		translate([x*Space,y*Space,Size/2])
    		  %cube(Size,center=true);
    
    }
    
    module Ring() {
    
    	difference() {
    		union() {
    			rotate(180/RingSize[RING_SIDES])
    				cylinder(d=RingSize[RING_OD],h=RingSize[RING_THICK],
    						$fn=RingSize[RING_SIDES]);
    			translate([-LaserOD,(-LaserLength - RingSize[RING_ID]/2),0])
    				cube([2*LaserOD,LaserLength,RingSize[RING_THICK]],center=false);
    			translate([-SensorOD,(-0*SensorLength + RingSize[RING_ID]/2),0])
    				cube([2*SensorOD,SensorLength,RingSize[RING_THICK]],center=false);
    		}
    		rotate(180/RingSize[RING_SIDES])
    			translate([0,0,-Protrusion])
    				cylinder(d=RingSize[RING_ID],h=(RingSize[RING_THICK] + 2*Protrusion),
    						$fn=RingSize[RING_SIDES]);
    		translate([0,0,RingSize[RING_THICK]])
    			rotate([90,0,0]) rotate(180/8)
    				PolyCyl(LaserOD,3*LaserLength,8);
    		translate([0,0,RingSize[RING_THICK]])
    			rotate([-90,0,0]) rotate(180/8)
    				PolyCyl(SensorOD,3*SensorLength,8);
    	}
    }
    
    module Mount() {
    	translate([0,0,StrutBlock[2]/2])
    		difference() {
    			cube(StrutBlock,center=true);
    			cube([StrutWidth,StrutLength,2*StrutBlock[2]],center=true);
    			translate([0,-StrutLength/3,0])
    				rotate([90,0,0])
    					PolyCyl(StrutScrewTap,StrutLength/2,6);
    		}
    }
    
    module Guide() {
    
    	difference() {
    		translate([0,0,RingSize[RING_THICK]/2])
    			cube([GuideOD,GuideOD,RingSize[RING_THICK]],center=true);
    		translate([0,0,-Protrusion]) rotate(180/8)
    			PolyCyl(GuideID,(RingSize[RING_THICK] + 2*Protrusion),8);
    	}
    }
    
    module Assembly() {
    	Ring();
    	translate([(RingSize[RING_OD]/2 + StrutBlock[BLOCK_LENGTH]/2
    				- (StrutBlock[BLOCK_LENGTH] - StrutLength)/2) + Protrusion,0,0])
    		rotate(90)
    			Mount();
    	for (i=[-1,1])
    		translate([(RingSize[RING_OD]/2 + GuideID/2),
    				  i*(StrutBlock[BLOCK_WIDTH]/2 + GuideID),
    				  0])
    			Guide();
    }
    
    //- Build it
    
    ShowPegGrid();
    
    if (Layout == "Ring") {
    	Ring();
    }
    
    if (Layout == "Mount") {
    	Mount();
    }
    
    if (Layout == "Guide") {
    	Guide();
    }
    
    if (Layout == "Show") {
    	Assembly();
    }
    
    if (Layout == "Build") {
    
    	translate([-5/2,-5/2,0])
    		cube(5);
    }
    
  • Browning Hi-Power Magazine Capacity Reduction: Blocks

    After a bit of trial fitting and tweaking, just three parameters cover the variations for the magazines in hand:

    • Offset of screw from front-to-back center
    • Height of spring retaining crimp
    • Distance between screw and crimp

    Collecting those numbers in a single array, with constants to select the entries, makes some sense:

    //BlockData =  [-0.5, 1.5, 11.5];	// Browning OEM
    BlockData = [-1.5, 2.0, 9.0];		// Generic 1
    
    SCREWOFFSET = 0;
    CRIMPHEIGHT = 1;
    CRIMPDISTANCE = 2;
    

    Although commenting out an undesired variable isn’t fashionable, OpenSCAD doesn’t have a practical mechanism to set specific values based on a control variable:

    • if-then-else deals with geometric objects
    • (boolean)?when_true:when_false (the ternary operator) doesn’t scale well

    You could, of course, depend on OpenSCAD’s behavior of using the last (in syntactic order) instance of a “variable”, but IMHO that’s like depending on semantic whitespace.

    In any event, the rest of the block builds itself around those three values by recomputing all of its dimensions.

    The Browning OEM block looks like this:

    Browning Hi-Power Magazine Block - solid model - BHP OEM
    Browning Hi-Power Magazine Block – solid model – BHP OEM

    The Generic floorplate has a much larger spring retaining crimp, so the block has far more overhang:

    Browning Hi-Power Magazine Block - solid model - Generic 1
    Browning Hi-Power Magazine Block – solid model – Generic 1

    As before, the yellow widgets are built-in support structures separated from the main object by one thread thickness and width. That seems to maintain good vertical tolerance and allow easy removal; the structures snap free with minimal force. A closeup look shows the gaps:

    Browning Hi-Power Magazine Block - solid model - Generic 1 - support detail
    Browning Hi-Power Magazine Block – solid model – Generic 1 – support detail

    The main shape now has a 2 mm taper to ease the magazine spring past the upper edge of the block. The horn remains slightly inset from the side walls to ensure that the whole thing remains manifold:

    Browning Hi-Power Magazine Block - solid model - Generic 1 - whole end
    Browning Hi-Power Magazine Block – solid model – Generic 1 – whole end

    The whole object looks about the same, though:

    Browning Hi-Power Magazine Block - solid model - Generic 1 - whole side
    Browning Hi-Power Magazine Block – solid model – Generic 1 – whole side

    The shape descends from the geometry I used for the stainless steel block, with the additional internal channel (on the right in the models) to be filled with steel-loaded epoxy during assembly. That should make the whole block sufficiently robust that you must destroy the floorplate and distort the spring to get it out; wrecking the magazine’s innards should count as not “readily” modifiable.

    Some destructive testing seems to be in order…

    The OpenSCAD source code:

    // Browning Hi-Power Magazine Plug
    // Ed Nisley KE4ZNU December 2013
    //	February 2014 - easier customization for different magazine measurements
    
    Layout = "Whole";			// Whole Show Split
    							//  Whole = upright for steel or plastic
    							//  Show = section view for demo, not for building
    							//  Split = laid flat for plastic show-n-tell assembly
    
    AlignPins = true && (Layout == "Split");	// pins only for split show-n-tell
    
    Support = true && (Layout != "Split");		// no support for split, optional otherwise
    
    // Define magazine measurements
    
    //BlockData =  [-0.5, 1.5, 11.5];		// Browning OEM
    BlockData = [-1.5, 2.0, 9.0];		// Generic 1
    
    SCREWOFFSET = 0;
    CRIMPHEIGHT = 1;
    CRIMPDISTANCE = 2;
    
    //- Extrusion parameters must match reality!
    //  Print with 2 shells and 3 solid layers
    
    ThreadThick = 0.20;
    ThreadWidth = 0.40;
    
    HoleWindage = 0.2;
    
    Protrusion = 0.1;			// make holes end cleanly
    
    function IntegerMultiple(Size,Unit) = Unit * ceil(Size / Unit);
    
    //----------------------
    // Dimensions
    
    Angle = 12.5;				// from vertical
    
    SpringID = 10.3;			// magazine spring curvature (measure with drill shank)
    SpringRadius = SpringID / 2;
    Taper = 2.0;				// total taper toward top
    
    Length = 24.5;				// front-to-back perpendicular to magazine shaft
    Height = 17.0;				// bottom-to-top, parallel to magazine shaft
    
    RectLength = Length - SpringID;	// block length between end radii
    
    HornBaseOD = 8.0;			// fits between follower pegs to prevent shortening
    HornTipOD = 5.0;
    HornAddTip = (HornTipOD/2)*tan(Angle);
    HornAddBase = (HornBaseOD/2)*tan(Angle);
    HornAddLength = HornAddTip + HornAddBase + 2*Protrusion;
    HornLength = 12.0;			// should recompute ODs, but *eh*
    
    ScrewOD = 3.0 - 0.25;		// screw hole dia - minimal thread engagement
    ScrewLength = Height - 5.0;
    ScrewOffset = BlockData[SCREWOFFSET];	//   ... from centerline on XY plane
    
    NutOD = 5.8;						// hex nut dia across flats
    NutThick = 2.4;						//  ... generous allowance for nut
    NutTrapLength = 1.5*NutThick;		// allow for epoxy buildup
    NutTrapBaseHeight = 5.0;			//  ... base height from floor plate
    
    CrimpHeight = IntegerMultiple(BlockData[CRIMPHEIGHT],ThreadThick);		// vertical clearance for spring crimp tab on base plate
    
    CrimpDistance = BlockData[CRIMPDISTANCE];		//  ... clip to screw hole center
    CrimpOffset = -(CrimpDistance - ScrewOffset);	// ... horizontal from centerline
    
    SupportLength = 4.0;		// length of support struts under Trim
    SupportWidth = IntegerMultiple(0.9*SpringID,4*ThreadWidth);	// ... size needed for platform adhesion
    SupportThick = CrimpHeight - ThreadThick;	// ... clearance for EZ removal
    
    VentDia = 2.5;				// air vent from back of screw recess
    //VentOffset = CrimpOffset + VentDia/2 + 5*ThreadWidth;
    VentOffset = -(NutOD + 4*ThreadWidth);
    VentLength = ScrewLength + VentDia;
    
    RecessDia = 3.5;			// additional air vent + weight reduction
    RecessLength = ScrewLength + RecessDia/2;		//  ... internal length
    RecessOffset = Length/2 - RecessDia/2 - 5*ThreadWidth;	//  ... offset from centerline
    
    PinOD = 1.72;				// alignment pins
    PinLength = 4.0;
    PinInset = 0.6*SpringRadius;	// from outside edges
    echo(str("Alignment pin length: ",PinLength));
    
    NumSides = 8*4;				// default cylinder sides
    
    Offset = 5.0/2;				// from centerline for build layout
    
    //----------------------
    // Useful routines
    
    function Delta(a,l) = l*tan(a);				// incremental length due to angle
    
    // Locating pin hole with glue recess
    //  Default length is two pin diameters on each side of the split
    
    module LocatingPin(Dia=PinOD,Len=0.0) {
    
    	PinLen = (Len != 0.0) ? Len : (4*Dia);
    
    	translate([0,0,-ThreadThick])
    		PolyCyl((Dia + 2*ThreadWidth),2*ThreadThick,4);
    
    	translate([0,0,-2*ThreadThick])
    		PolyCyl((Dia + 1*ThreadWidth),4*ThreadThick,4);
    
    	translate([0,0,-(Len/2 + ThreadThick)])
    		PolyCyl(Dia,(Len + 2*ThreadThick),4);
    
    }
    
    module PolyCyl(Dia,Height,ForceSides=0) {			// based on nophead's polyholes
    
      Sides = (ForceSides != 0) ? ForceSides : (ceil(Dia) + 2);
    
      FixDia = Dia / cos(180/Sides);
    
      cylinder(r=(FixDia + HoleWindage)/2,
               h=Height,
    	   $fn=Sides);
    }
    
    module ShowPegGrid(Space = 10.0,Size = 1.0) {
    
      Range = floor(50 / Space);
    
    	for (x=[-Range:Range])
    	  for (y=[-Range:Range])
    		translate([x*Space,y*Space,Size/2])
    		  %cube(Size,center=true);
    
    }
    
    //----------------------
    // The magazine block
    
    module Block(SectionSelect = 0) {
    
    CropHeight = Height*cos(Angle);				// block height perpendicular to base
    echo(str("Perpendicular height: ",CropHeight));
    
    	difference() {
    		union() {
    			intersection() {
    				rotate([Angle,0,0])
    					hull() {
    						for (i=[-1,1])
    							translate([0,i*RectLength/2,-((Length/2)*sin(Angle) + Protrusion)])
    								cylinder(r1=SpringRadius,r2=(SpringRadius - Taper/2),
    										 h=(Height + 2*(Length/2)*sin(Angle) + 2*Protrusion),
    										 $fn=NumSides);
    					}
    				translate([0,0,CropHeight/2])
    					cube([2*SpringID,3*Length,CropHeight],center=true);
    			}
    			translate([0,-Height*sin(Angle),Height*cos(Angle)])
    				resize([(SpringID - Taper),0,0])
    					intersection() {
    						rotate([Angle,0,0])
    							translate([0,0,-(HornAddBase + Protrusion)])
    								cylinder(r1=HornBaseOD/2,
    										r2=HornTipOD/2,
    										h=(HornLength + HornAddLength + Protrusion),
    										$fn=NumSides);
    					cube([2*SpringID,Length,2*(HornLength*cos(Angle) + Protrusion)],center=true);
    				}
    		}
    
    		translate([0,ScrewOffset,-Protrusion])		// screw
    			rotate(180/6)
    				PolyCyl(ScrewOD,(ScrewLength + Protrusion),6);
    
    		translate([0,ScrewOffset,NutTrapBaseHeight])		// nut trap in center
    			rotate(180/6)
    				PolyCyl(NutOD,NutTrapLength,6);
    
    		translate([0,ScrewOffset,-Protrusion])		// nut clearance at base
    			rotate(180/6)
    				PolyCyl(NutOD,(1.1*NutThick + Protrusion),6);
    
    		translate([SpringID/2,CrimpOffset,-Protrusion])
    			rotate(180)
    				cube([SpringID,Length,(CrimpHeight + Protrusion)],center=false);
    
    		if (AlignPins)								// alignment pins
    			if (true)
    				translate([0,-CropHeight*tan(Angle),CropHeight])
    					rotate([0,90,0]) rotate(45 + Angle)
    						LocatingPin(PinOD,PinLength);
    			else
    			for (i=[-1,1])			// cannot use these with additional vents * channels
    				rotate([Angle,0,0])
    				translate([0,
    							(i*((Length/2)*cos(Angle) - PinInset)),
    							(CropHeight/2 - i*2*PinInset)])
    					rotate([0,90,0]) rotate(45 - Angle)
    						LocatingPin(PinOD,PinLength);
    
    		translate([0,(ScrewOffset + 1.25*NutOD),ScrewLength])	// air vent
    			rotate([90,0,0]) rotate(180/8)
    				PolyCyl(VentDia,3*NutOD,8);
    		translate([0,VentOffset,-(VentDia/2)*tan(Angle)])
    			rotate([Angle,0,0]) rotate(180/8)
    				PolyCyl(VentDia,VentLength,8);
    
    		translate([0,RecessOffset,0])			// weight reduction recess
    			rotate([Angle,0,0]) rotate(180/8)
    				translate([0,0,-((RecessDia/2)*tan(Angle))])
    				PolyCyl(RecessDia,(RecessLength + (RecessDia/2)*tan(Angle)),8);
    
    		if (SectionSelect == 1)
    			translate([0*SpringID,-2*Length,-Protrusion])
    				cube([2*SpringID,4*Length,(Height + HornLength + 2*Protrusion)],center=false);
    		else if (SectionSelect == -1)
    			translate([-2*SpringID,-2*Length,-Protrusion])
    				cube([2*SpringID,4*Length,(Height + HornLength + 2*Protrusion)],center=false);
    	}
    
    SupportSlots = (SupportWidth / (4*ThreadWidth)) / 2;		// SupportWidth is multiple of 4*ThreadWidth
    
    	if (Support)
    	color("Yellow") {
    		translate([0,(CrimpOffset - SupportLength/2),SupportThick/2])
    			difference() {
    				translate([0,-ThreadWidth,0])
    					cube([(SupportWidth - Protrusion),SupportLength,SupportThick],center=true);
    				for (i=[-SupportSlots:SupportSlots])
    					translate([i*4*ThreadWidth + 0*ThreadWidth,ThreadWidth,0])
    						cube([(2*ThreadWidth),SupportLength,(SupportThick + 2*Protrusion)],center=true);
    			}
    
    		translate([0,ScrewOffset,0])
    			for (j=[0:5]) {
    				rotate(30 + 360*j/6)
    					translate([(NutOD/2 - ThreadWidth)/2,0,(1.1*NutThick - ThreadThick)/2])
    						color("Yellow")
    						cube([(NutOD/2 - ThreadWidth),
    							(2*ThreadWidth),
    							(1.1*NutThick - ThreadThick)],
    							center=true);
    			}
    
    	}
    
    }
    
    //-------------------
    // Build it...
    
    ShowPegGrid();
    
    if (Layout == "Show")
    	Block(1);
    
    if (Layout == "Whole")
    	Block(0);
    
    if (Layout ==  "Split") {
    	translate([(Offset + Length/2),Height/2,0])
    		rotate(90) rotate([0,-90,-Angle])
    			Block(-1);
    	translate([-(Offset + Length/2),Height/2,0])
    		rotate(-90) rotate([0,90,Angle])
    			Block(1);
    }