# $URL: svn+ssh://mrwhat@ssh.boim.com/home/mrwhat/svn/Walker/trunk/payloadBox.pl $
# $Id: payloadBox.pl 415 2014-04-19 02:38:26Z mrwhat $
#
# Code to draw new (circa 2/2014) payload box design, with latched lid.
#    -- The hinged lid with spring tabs of 1/2014 has been depricated.
#             I worry that it is too fragile.

sub printPayloadBox() {
    local ($x0,$y0,$rot) = (@_,0,0,0);

    print "<g transform=\"translate($x0,$y0) rotate($rot)\">\n";
    &printHalfPayloadBox(85.1,0,0,3);  # top/back  plates
    &printHalfPayloadBox( 0  ,0,0);  # bot/front plates
    print "</g>\n";
}

sub printHalfPayloadBox() {
    local ($x0,$y0,$rot,$flags) = (@_,0,0);
    local $leftless = $flags & 1;
    local $topHoles = $flags & 2;

    print "<desc>Payload bottom/front (or top/back) covers</desc>\n";
    #local $x=5.5;
    print "<g transform=\"translate($x0,$y0) rotate($rot)\">\n";
    #&payloadCover($x,0,0,1+2+4*$leftless);
    ##print "<g transform=\"scale(-1,1)\">\n";
    ##  &payloadCover(-180-$x,90,0,8*$leftless);

    local $insideWidth  = 75;
    local $insideHeight = 75;
    local $insideLength = 200;#2 * 8 * 11.5 + 11.5;  #9*20+10;
    #local $botThick = 3;  # thickness of bottom material
    #local $tabWidth1 = 5;  # width of tabs along front/back-bottom edges
    #local $tabWidthS = 10; # tab width along sides
    #local $sideThick = 5;  # thickness of side material
    &startPart(0,0,0);
      &drawBoxEnd($leftless+($topHoles?0:2));
      print "\nM$insideWidth,$insideHeight";
      &drawBottomPlateEdge(10,5);
      print "l0,3.2 l-5,0 l0,-3";
      &drawBoxEdge(3,-5,6);
      print "l0,3 l-5,0";
      &drawBottomPlateEdge(-10,-5,$leftless);
      print "l0,-3.3";
    print " $endPart\n";

### make all plates interchangable.  easier in the long run
#    if ($topHoles) {  # holes for payload lid catch tabs
      &startPart(0,$insideHeight+3.3);
	local ($hr,$nf) = ($rad4-0.2,27);
	local ($xa,$xb) = (10,$insideWidth-10);
	local ($ya,$yb,$yc) = (30,$insideLength/2,$insideLength-30);
	&drawCircle($xa,$ya,$hr,$nf);
	&drawCircle($xb,$ya,$hr,$nf);
	&drawCircle($xa,$yb,$hr,$nf);
	&drawCircle($xb,$yb,$hr,$nf);
	&drawCircle($xa,$yc,$hr,$nf);
	&drawCircle($xb,$yc,$hr,$nf);
      print $endPart;
#    } else {
	&ArduinoUnoMountHoles(60,$insideHeight+3.3+3,90); # good space from back of bottom plate
#    }
    # put these on both plates, Could put electronics on lid
    &motorDriverModuleMountHoles($insideWidth/2 - 43/2+3,
				 $insideHeight+3.3+$insideLength-45);

    # 7x9cm protoboard has holes at 6.2x8.2cm, but much slop
    &startPart(6.5,$insideHeight+3.3+55);
    &drawCircle( 0, 0,1.7,15);
    &drawCircle(62, 0,1.7,15);
    &drawCircle(62,82,1.7,15);
    &drawCircle( 0,82,1.7,15);
    print "$endPart;";

    # 5x7 protoboard has small holes (M2? #2) at 4.5x6.5cm
    &startPart(16.5,$insideHeight+3.3+45);
    &drawCircle( 0, 0,1.4,15);
    &drawCircle(45, 0,1.4,15);
    &drawCircle(45,65,1.4,15);
    &drawCircle( 0,65,1.4,15);
    print $endPart;

    print " </g>\n";
}

sub printCatchBar() {  # origin, center of bar
    local ($x0,$y0,$rot) = (@_,0,0,0);

    local ($w,$h,$r,$xa) = (75/2,8,3,10);
    local $ra = 20;
    local $rh = $rad4 + 0.3;
    &startPart($x0,$y0,$rot);
    &drawCircle( $w-$xa,0,$rh,19);
    &drawCircle(-$w+$xa,0,$rh,19);
    print "M-$w,-$h l0,-$r";
    &drawArc('L',-$w+$r,-$h-$r+$ra,$ra, -90,-50,6);
    &drawArc('L',-15,-3*$r,2*$r,120,90,-6);
    &drawArc('L', 15,-3*$r,2*$r, 90,60,-6);
    &drawArc('L', $w-$r,-$h-$r+$ra,$ra,-130,-90,6);
    print "l$r,0 l0,$r l5,0 l0,16 l-5,0 l0,$r";
    &drawArc('L', $w-$r, $h+$r-$ra,$ra,  90,130,6);
    &drawArc('L', 15, 3*$r,2*$r,-60,-90,-6);
    &drawArc('L',-15, 3*$r,2*$r,-90,-120,-6);
    &drawArc('L',-$w+$r, $h+$r-$ra,$ra, 50,90,6);
    print "l-$r,0 l0,-$r l-5,0 l0,-16 l5,0";
    print $endPart;
}

sub printCatchTab() {  # origin, center edge of tab
    local ($x0,$y0,$rot,$flags) = (@_,0,0,0);
    local $topless = $flags & 1;

    local $rh = $rad4 + 0.3;
    &startPart($x0,$y0,$rot);
    &drawCircle(10,0,$rh,19);
    print "M10,-8 l-15,0 l0,-3";
    &drawArc(             'L',-6,-8,3, -90,-180,-8);
    &drawArc($topless?'M':'L',-6, 8,3,-180,-270,-8);
    print "l1,0 l0,-3\n";
    &drawArc('L',10,0,8,90,-90,-9);
    print $endPart;
}

# assumes edge goes in +y (down) direction starting at 0,0
sub drawBottomPlateEdge() {
    local ($tw,$th,$flags) = (@_,0);
    local $leftless = $flags & 1;

    #local $tr = 3;
    local $sign = $tw < 0 ? -1 : 1;
    #printf("M0,0 l0,%.1f ",$tw);
    #&drawArc('L',$tr*$sign,    $tw+3*$sign,3,-180,90,12);
    #&drawArc('L',$tr*$sign,2.5*$tw-3*$sign,3,  90, 0,12);
    #printf("l0,%.1f l%.1f,0 l0,%.1f l%.1f,0 l0,%.1f l%.1f,0 l0,%.1f l%.1f,0 ",
	#   -$sign*$tr, -$sign*$th*1.4,
	#    $sign*$tr, .6*$th,
	#    $tw.6*$th,$sign*$tr,
	#   1.4*$sign*$th,0);
    $th1 = -$th;
    $tw1 = 1.5*$tw;
    local $ta = 3.2*$sign;
    local $le = $leftless ? 'm' : 'l';  # left edge symbol
    $le .= "0,$tw";
    print " l0,$ta l$th,0 $le l$th1,0 l0,$tw1 l$th,0 $le l$th1,0 l0,$tw1 l$th,0 $le l$th1,0 l0,$tw l$th,0 $le l$th1,0 l0,$tw1 l$th,0\n";
    print "$le l$th1,0 l0,$tw1 l$th,0 $le l$th1,0 l0,$tw l$th,0 $le l$th1,0 l0,$tw1 l$th,0 $le l$th1,0 l0,$tw1 l$th,0 $le l$th1,0 ";
}

sub drawBottomPlateEdges() {
    local ($iw,$ih,$flags) = (@_,0);
    local $drawLeft = $flags & 1;
    local $skipLastTab = $flags & 2;

    local $te = 3.3;  # tab edge width with a little extra slop
    local $tw = 5;
    local $tl = 11.5;
    local $i;
    print "M$iw,$ih l0,$te";
    for ($i=0; $i<8; $i++) { print " l$tw,0 l0,$tl l-$tw,0 l0,$tl"; }
    print " l$tw,0 l0,$tl l-$tw,0";
    print " l0,$te\n" if (!$skipLastTab);
    if ($drawLeft) {
	print "M0,$ih l0,$te";
	for ($i=0; $i<8; $i++) { print " l-$tw,0 l0,$tl l$tw,0 l0,$tl"; }
	print " l-$tw,0 l0,$tl l$tw,0 l0,$te\n";
    }
}

sub drawBoxEnd() {
    local ($flags) = (@_,0,0);
    local $leftless = $flags & 1;
    local $unoHole  = $flags & 2;

    local $insideWidth  = 75;
    local $insideHeight = 75;

    &drawSlot($insideWidth/2-3,              8,6,2);
    &drawSlot($insideWidth-10,$insideHeight/2-3,2,6);
    &drawSlot(             10,$insideHeight/2-3,2,6);

### just make all plates interchangable.  Easier to maintain in the long run.
### Extra hole won't hurt (much)
#    if ($unoHole) {
    if (1) {
	# bottom of hole should be 6mm from bottom of box
	# edge 7mm in from center of neareast bolt hole
	# 75-6=69, bottom     
	print "M37.5,69 l14,0 l0,-12 l-14,0 l0,12";
    } else {
	&drawSlot($insideWidth/2-3,$insideHeight-8,6,2);
    }

    local $tw = 5;
    local $th = 3;
    print "M0,0";
    &drawBoxEdge($th,$tw,7);
    $th = 10;
    $thMid = 15;
    print " l0,$th l$tw,0 l0,$th l-$tw,0";
    print " l0,$th l$tw,0 l0,$thMid l-$tw,0";
    print " l0,$th l$tw,0 l0,$th l-$tw,0 l0,$th";
    $th = 3;
    &drawBoxEdge($th,-$tw,7);
    $th = 10;
    local $el = $leftless ? 'm' : 'l';  # left edge symbol
    $el .= "0,-";
    print " l0,-$th l-$tw,0 $el$th l$tw,0";
    print " l0,-$th l-$tw,0 $el$thMid l$tw,0";
    print " l0,-$th l-$tw,0 $el$th l$tw,0 l0,-$th\n";
}

sub drawBoxEdge() {
    local ($th,$tw,$nt,$flags) = (@_,0);

    local $flip = 0;
    local $tab = " l$tw,0 l0,-$th l$tw,0 l0,$th";
    if ($tw<0) { 
	$flip=1;
	$tw=-$tw;
	$tab = " l-$tw,0 l0,$th l-$tw,0 l0,-$th";
    }
    local $i;
    for ($i=0; $i<$nt; $i++) { print $tab; }
    $tw = -$tw if ($flip);
    print " l$tw,0\n";
}

# version of payload with trap-door top and clips, 200mm long
sub printPayloadSide() {
    local ($x0,$y0,$rot,$flags) = (@_,0,0);
    local $edgeless = $flags&1;

    printf("<desc>Payload side plate</desc>
<g transform=\"translate($x0,$y0) rotate($rot)\%s\">\n",
	   $edgeless?" scale(1,-1)":"");

    local $bHole = $irB - 0.5*$fuzz; # a little tight, non-spinning
    local $eRad = 6;  # radius of edge corners

    &startPart(0,0);
      &drawCircle(40,100-$Bx,$bHole,24);
      &drawCircle(40,100+$Bx,$bHole,24);

      print "M-0.1,0";   &drawBottomTabHoles(-3.2,10);
      print "M9.8,0";    &drawSideTabHoles(  -3.2,10);
      print "M9.8,200.5";&drawSideTabHoles(   3.2,10);

      # print side and bottom perimiter.  Top is trickier. There is a seperate function for that
      print "M69,209";
      &drawArc(              'L',-2,203,6, 90,180,12);
      &drawArc($edgeless?'M':'L',-2, -3,6,180,270,12);

      print "L69,-9";
    print $endPart;

    print "<desc>Top Tabs</desc>\n<g transform=\"translate(75,0)\">\n";
      &printTopTabs();
    print "</g>\n";

    print "\n<desc>Holes and slots for wire pass-throughs, tie-downs and ventillation</desc>\n";
    &startPart(0,0);
    #&drawSlot(     8,100-3,2,6); # remove this slot if using motor hole
    &drawSlot(  75-8,100-3,2,6);
    &drawSlot(37.5-3,    8,6,2);
    &drawSlot(37.5-3,200-8,6,2);
    &drawCircle(   12,    12,2,17);
    &drawCircle(75-12,    12,2,17);
    &drawCircle(   12,200-12,2,17);
    &drawCircle(75-12,200-12,2,17);
    &drawCircle(20,100-40,2,17);
    &drawCircle(20,100+40,2,17);
    &drawCircle(37.5,100,2,17);
    print $endPart;
    &drawMotorHole(40+$Ay-$dA_Motor,100);

    print "</g>\n";
}

# flattened hole for motor back, in case it is long isreali motor that might
# extend into payload bay
sub drawMotorHole() {
    local ($x0,$y0) = @_;
    local $arcLim=53;
    local $mr = $motorRad + 0.4;  # let this be a little loose
    &startPart($x0,$y0,-90);
    &drawArc('M',0,0,$mr,   -$arcLim,    $arcLim,12);
    &drawArc('L',0,0,$mr,180-$arcLim,180+$arcLim,12);
    #print " Z $endPart";
    print $endPart; # don't need to close loop if motor falls on tab
}

sub printPayloadSides() {
    local ($x0,$y0,$rot) = (@_,0);

    print "<g transform=\"translate($x0,$y0) rotate($rot)\">\n";
    &printPayloadSide(0,0);
    &printPayloadSide(-16,0,180,1);
    print "</g>\n"; # end of group translation
}

sub drawBottomTabHoles() {
    local ($tw,$th) = @_; #-3.2,10

    local $th15 = $th * 1.5;
    local $th25 = $th * 2.5;
    local $th20 = $th * 2;
    $th0 = $th + 0.4;  # add some fuzz
    &drawTabHole($tw,$th0);
    print "m0,$th25 ";  &drawTabHole($tw,$th0);
    print "m0,$th25 ";  &drawTabHole($tw,$th0);
    print "m0,$th20 ";  &drawTabHole($tw,$th0);
    print "m0,$th25 ";  &drawTabHole($tw,$th0);
    print "m0,$th25 ";  &drawTabHole($tw,$th0);
    print "m0,$th20 ";  &drawTabHole($tw,$th0);
    print "m0,$th25 ";  &drawTabHole($tw,$th0);
    print "m0,$th25 ";  &drawTabHole($tw,$th0);
}

sub printTopTabs() {
    local $tl = 10;
    local $th0 = 3.2;

    &startPart(0,0);
    print "M-6,-9 ";
    &drawArc('L',-3,-3,6,-90,0,12);
    print "l0,3 l-3,0.2 l0,10.4";
    print $endPart;

    &printTabCatch(10.5);

    local $th1 = -$th0;
    &startPart(0,50);
    print "M0,-.1 l0,10.4 l$th0,0.3 l0,9.2 l$th1,0.4 l0,10.2";
    print $endPart;

    &printTabCatch(80.5);

    &startPart(0,119.9);
    print "M0,0 l0,10.4 l$th0,0.3 l0,9.2 l$th1,0.3 l0,10.4";
    print $endPart;

    &printTabCatch(150.5);

    &startPart(0,189.9);
    print "M0,0 l0,10.2 l3,0.2 ";
    &drawArc('L',-3,13.1,6,0,90,12);
    print " l-3,0 $endPart";
}

sub printTabCatch() {
    local ($y0) = @_;

    print "<desc>Trap-door tab catch</desc>";
    &startPart(0,$y0);
    print "M0,-0.1";
    &drawArc('L',3, 6.2,6,-90, 0,12);
    &drawArc('L',6,11.5,3,  0,90,12);
    print "l-1,0 l0,-3 l-3,0 l0,3 l-2,0 l0,10.4 l2,0 l0,3 l3,0 l0,-3";
    &drawArc('L',6,28.0,3,-90, 0,12);
    &drawArc('L',3,33.3,6,  0,90,12);
    print "l-3,0.2 $endPart";
}

sub drawSideTabHoles() {
    local ($tw,$th) = @_; #-3.2,10

    local $th15 = $th * 1.5;
    local $th25 = $th * 2.5;
    local $th20 = $th * 2;
    $th0 = $th + 0.4;  # add some fuzz
    &drawTabHole($th0,$tw);
    print "m$th20,0 ";  &drawTabHole($th15+.4,$tw);
    print "m$th25,0 ";  &drawTabHole($th0,$tw);
}

sub drawTabHole() {
    local ($tw,$th) = @_; #-3.2,10

    local $tw1 = -$tw;
    local $th1 = -$th;
    print " l$tw,0 l0,$th l$tw1,0 l0,$th1\n";
}


#========================== Depricated hinged style
#  Can't bring myself to delete the code since it worked quite well.
#  I want to keep an example of how to do those spring-catch tabs around.
sub printPayloadSideSet() {
    local ($x0,$y0,$rot) = (@_,0);

    print "<g transform=\"translate($x0,$y0) rotate($rot)\">\n";
    &printHingePayloadSide(0,  0,0  );
    &printHingePayloadSide(0,-96,0,1);
    print "</g>\n"; # end of group translation
}

sub drawTab5() {
    local ($x,$y,$dx,$dy) = @_;

    $y-=$dy;print " L$x,$y";
    $x+=$dx;print " L$x,$y";
    $y+=$dy;print " L$x,$y";
    $x+=$dx;print " L$x,$y";
    return($x,$y);
}

sub drawTab1() {
    local ($x,$y,$dx,$dy) = @_;

    $y+=$dy;print " L$x,$y";
    $x-=$dx;print " L$x,$y";
    $y+=$dy;print " L$x,$y";
    $x+=$dx;print " L$x,$y";
    return($x,$y);
}
sub drawTab2() {
    local ($x,$y,$dx,$dy) = @_;

    $x+=$dx;print " L$x,$y";
    $y+=$dy;print " L$x,$y";
    $x+=$dx;print " L$x,$y";
    $y-=$dy;print " L$x,$y";
    return($x,$y);
}
sub drawTab3() {
    local ($x,$y,$dx,$dy) = @_;

    $x+=$dx;print " L$x,$y";
    $y+=$dy;print " L$x,$y";
    $x-=$dx;print " L$x,$y";
    $y+=$dy;print " L$x,$y";
    return($x,$y);
}
sub drawTab4() {
    local ($x,$y,$dx,$dy) = @_;

    $y+=$dy;print " L$x,$y";
    $x+=$dx;print " L$x,$y";
    $y-=$dy;print " L$x,$y";
    $x+=$dx;print " L$x,$y";
    return($x,$y);
}

sub printOldPayloadBotEnds() {
    local ($x0,$y0,$rot,$flags) = (@_,0,0);
    local $leftless = $flags & 1;

    print "<desc>old Payload bottom, and front</desc>\n";
    print "<g transform=\"translate($x0,$y0) rotate($rot)\">\n";

print "<g transform=\"translate(72,0)\">\n";  # offset dead code reference drawiung


    #local ($x0,$y0,$rot,$flags) = @_;
    local ($x0,$y0,$rot,$flags) = (5.5,0,0,1+2+4*$leftless);
    local $PCBmounts = $flags&1; # usually on bottom only
    local $edgeless  = $flags&2;
    local $leftless  = $flags&4;
    local $rightless = $flags&8;

    local $mTabLen = 10;  # length of tabs into payload mount sides
    local $tabLen = 5; # length of tabs to other thin pannels.
    local $wTabs = 9;
    local $hTabs = 3;
    local $iw = 2*$mTabLen*$wTabs - $mTabLen;
    local $ih = 2*$mTabLen*$hTabs + $mTabLen/2;


    local $sw = 3; # nominal slot width
    local $td = 5; # depth of tabs into side plates
    if ($PCBmounts) {
        &protoboardHoles(20,40,-90);
        &motorDriverModuleMountHoles(114,$ih-$tabLen+.75,-90);
        &raspPiMountHoles(15,15,0);
    }

&startPart(30,20);&drawSpringTabCatch(3,7);print "$endPart\n";
#&startPart(50,30);&drawSpringTab(3,7);print "$endPart\n";

    &startPart(0,0);

    # mid-side holes, motor wires can exit, AND can be for optional cable ties
    local $y1=85; # width of payload bay
    local ($s1,$s2) = (8,2); # slot dimensions
    local $inset = 8;  # how far in from edge are slots
    &drawSlot($iw/2-$s1/2,    $inset,$s1,$s2);
    &drawSlot($iw/2-$s1/2,$y1-$inset,$s1,$s2);

    # ventillation and tie-downs
    &drawSlot($iw*.2-$s1/2,    $inset,$s1,$s2);
    &drawSlot($iw*.2-$s1/2,$y1-$inset,$s1,$s2);
    &drawSlot($iw*.8-$s1/2,    $inset,$s1,$s2);
    &drawSlot($iw*.8-$s1/2,$y1-$inset,$s1,$s2);

    &drawSlot(    $inset,         10,$s2,$s1);
    &drawSlot(    $inset,($y1-$s1)/2,$s2,$s1);
    &drawSlot(    $inset, $y1-$s1-10,$s2,$s1);
    &drawSlot($iw-$inset,         10,$s2,$s1);
    &drawSlot($iw-$inset,($y1-$s1)/2,$s2,$s1);
    &drawSlot($iw-$inset, $y1-$s1-10,$s2,$s1);

    local $bTabs = 8;
    printf("M%.2f,%.2f ",-$sw-.5,0);
    &tabEdge(0,0,$mTabLen,-$td,$wTabs);
    local $x2 = $wTabs*$mTabLen*2-$mTabLen;
    local $x1 = $x2+$sw;

    if ($rightless) { printf(" L%.2f,0",$x1+1);
    } else {                  &tabEdgeV($x1+1,0,-$sw-1,$tabLen,$bTabs); }
    printf(" %s%.2f,%.2f L%.2f,$y1 ",$rightless?'M':'L',
           $x1+1.5,$y1-5,$x1+1.5); # extra half-tab pair

    if ($edgeless) {
        printf(" M%.2f,%.2f ",$mTabLen-$sw-1,$y1+$td);
    } else {
        &tabEdge($x2,$y1,-$mTabLen,$td,$wTabs);
    }
    &tabEdgeV(0,$y1+$td,-$sw-1,-$td,$bTabs+1);

    &drawSlot(-$ih/2-5-$sw ,      5 + $s2,$s1,$s2);
    &drawSlot(-$ih/2-5-$sw ,  $y1-5 - $s2,$s1,$s2);
    &drawSlot(          -12,      5 + $s2,$s2,$s1);
    &drawSlot(          -12,$y1/2 - $s1/2,$s2,$s1);
    &drawSlot(          -12,$y1-$s1-5-$s2,$s2,$s1);
    &drawSlot(-$ih-2*$sw+10,$y1-$s1-5-$s2,$s2,$s1);
    &drawSlot(-$ih-2*$sw+10,$y1/2 - $s1/2,$s2,$s1);
    &drawSlot(-$ih-2*$sw+10,      5 + $s2,$s2,$s1);
    print " M-$sw,0 ";
    &tabEdge(2.5-$sw-1-$mTabLen,0,-$mTabLen,-$td,$hTabs);
    if ($leftless) { printf(" L%.2f,0",-$ih-$sw-1);
    } else {        &tabEdgeV(-$ih-$sw-1,0,-$sw-1,$td,$bTabs); }
    printf(" %s%.2f,%.2f L%.2f,$y1 ",$leftless?'M':'L',-$ih-$sw-1,$y1-$tabLen,-$ih-$sw-1);

    if (!$leftless) {
        &tabEdge(-$ih-$sw-1+7.5,$y1,$mTabLen,$td,$hTabs);
        print " L-$sw,$y1 ";
    }

    print "$endPart</g>\n";
    print "</g>\n";
}

sub printHingedPayloadLid() {
    local ($x0,$y0,$rot) = (@_,0);

    print "<desc>Payload lid</desc>\n";
    &startPart($x0,$y0,$rot);

    local $insideWidth  = 95;
    &drawSlot( 12,              20,2,6);
    &drawSlot( 60,              12,6,2);
    &drawSlot(140,              12,6,2);
    &drawSlot(180,$insideWidth/2-3,2,6);
    &drawSlot(140,$insideWidth -12,6,2);
    &drawSlot( 60,$insideWidth -12,6,2);
    &drawSlot( 12,$insideWidth -26,2,6);

    local ($x,$y) = (5,0);
    print " M0,0 L$x,$y";
    $y -=  5;  print " L$x,$y";
    $x +=  5;  print " L$x,$y";
    $y +=  5;  print " L$x,$y";
    local $i;
    $x += 5;
    for ($i=0; $i < 4; $i++) { ($x,$y) = &drawTab2( $x,$y,15,-5); }
    ($x,$y) = &drawTab2($x,$y,15,-5);
    $x = 193.5;
    local $dx = 4;
    $y = -5;
    print "\nL175,0 L175,-5 L$x,$y";
    $y += 5+7.5-.25;  print " L$x,$y";
    $x -= $dx;  print " L$x,$y";
    $y += 5+.5; print " L$x,$y";
    local $i;
    for ($i=0; $i < 3; $i++) {
        $x += $dx;  print " L$x,$y";
        $y += 9.5;  print " L$x,$y";
        $x -= $dx;  print " L$x,$y";
        $y += 10.5; print " L$x,$y";
    }
    $x += $dx;  print "\nL$x,$y";
    $y += 9.5;  print " L$x,$y";
    $x -= $dx;  print " L$x,$y";
    $y += 5.5;  print " L$x,$y";
    $x += $dx;  print " L$x,$y";
    $y += 5+7.5+.25;  print " L$x,$y\n";

    #$x -= 20;  print " L$x,$y";
    $x -= 18.5;  print " L$x,$y";
    $y -= 5;   print " L$x,$y";
    $x = 180; # 140;
    for ($i=0; $i < 5; $i++) { ($x,$y) = &drawTab2( $x,$y,-15,5); }
    $x -= 20;  print "\nL$x,$y";
    $y +=  5;  print " L$x,$y";
    $x -=  5;  print " L$x,$y";
    $y -=  5;  print " L$x,$y";
    $x -=  5;  print " L$x,$y L0,0\n";

    print "$endPart\n";
}

sub printPayloadBotEnds() {
    local ($x0,$y0,$rot,$flags) = (@_,0,0);
    local $leftless = $flags & 1;

    print "<desc>Payload bottom, front, and back</desc>\n";
    #local $x=5.5;
    print "<g transform=\"translate($x0,$y0) rotate($rot)\">\n";
    #&payloadCover($x,0,0,1+2+4*$leftless);
    ##print "<g transform=\"scale(-1,1)\">\n";
    ##  &payloadCover(-180-$x,90,0,8*$leftless);

    local $insideWidth  = 95;
    local $insideHeight = 65;
    local $insideLength = 9*20+10;
    &printCatchEnd($insideWidth,3,5,10,7.5);
    &printBottomPlate($insideHeight,$insideWidth,$bottomLength,5,10);
    &printFlatEnd($insideWidth,3,5,10,7.5);
    print "</g>\n";
}

sub printCatchEnd() {
    local ($insideWidth,$tw,$tww,$clipTabWidth,$w0) = @_;
    # tw   -- tab width, material width
    # tww  -- tab width, wide -- dove-tail tab width, wider
    # clipTabWidth -- width of spring-clip tabs, also width
    #                 of tabs into side plates
    $tws = $clipTabWidth;  # width of tabs into side plates
    # w0   -- extra width at corners of end plate

    #local $insideWidth = 95;
    #local $tw = 3;  # small tab width
    #local $tww = 5;  # tab width, wide
    #local $clipTabWidth = 10;
#&startPart(20,200,0);&drawSpringTab(3,10);print "$endPart\n";
    &printSpringTab($tw+.3,$clipTabWidth,0,32.5,-90);
    &printSpringTab($tw+.3,$clipTabWidth,0,72.5,-90);
    &startPart($tw+.2,$insideWidth,-90);
    &drawSlot($insideWidth/2-3, 8,6,2);
    &drawSlot($insideWidth/2-3,57,6,2);
    &drawSlot(85,29,2,6);
    &drawSlot(10,29,2,6);
    #local $w0 = 7.5;
    local $x = $w0;
    local $y = 0;
    local $dy = $tw + .4;  # a little extra length on the tabs
    print "M0,0 L$x,0";
    $y -= $dy;             print " L$x,$y";
    $x += $clipTabWidth/2; print " L$x,$y";
    $y += $dy;             print " L$x,$y";
    $x += $clipTabWidth;   print " L$x,$y";
    $x += $clipTabWidth;   print " M$x,$y";
    $x += $clipTabWidth;   print " L$x,$y";
    $y -= $dy;             print " L$x,$y";
    $x += $clipTabWidth;   print " L$x,$y";
    $y += $dy;             print " L$x,$y";
    $x += $clipTabWidth;   print " L$x,$y";
    $x += $clipTabWidth;   print " M$x,$y";
    $x += $clipTabWidth;   print " L$x,$y";
    $y -= $dy;             print " L$x,$y";
    $x += $clipTabWidth/2; print " L$x,$y";
    $y += $dy;             print " L$x,$y";
    $x += $w0;             print " L$x,$y";

    $y += $w0;             print " L$x,$y";
    $x += $tww;            print " L$x,$y";
    ($x,$y) = &drawTab1($x,$y,$tww,$clipTabWidth);
    ($x,$y) = &drawTab1($x,$y,$tww,$clipTabWidth);
    $y += $clipTabWidth; print " L$x,$y";
    $x -= $tww;          print " L$x,$y";
    $y += $w0;           print " L$x,$y";

    for ($i=0; $i < 9; $i++) { ($x,$y) = &drawTab2($x,$y,-$tww,$tw+.4); }
    $x -= $tww; print " L$x,$y";
    $y -= $w0;  print " L$x,$y";
    ($x,$y) = &drawTab3($x,$y,-$tww,-$clipTabWidth);
    ($x,$y) = &drawTab3($x,$y,-$tww,-$clipTabWidth);
    $x -= $tww;           print " L$x,$y";
    $y -= $clipTabWidth;  print " L$x,$y";
    $x += $tww;           print " L$x,$y";
    $y -= $w0;            print " L$x,$y";
    print " $endPart\n";
}

#    &printFlatEnd($insideWidth,3,5,10,7.5);
sub printFlatEnd() {
    local ($insideWidth,$tw,$tww,$clipTabWidth,$w0) = @_;
    # tw   -- tab width, material width
    # tww  -- tab width, wide -- dove-tail tab width, wider
    # clipTabWidth -- width of spring-clip tabs, also width
    #                 of tabs into side plates
    $tws = $clipTabWidth;  # width of tabs into side plates
    # w0   -- extra width at corners of end plate

    &startPart(3*($tw+.2)+190+2*20+10+15,0);
    #&drawSlot( 8,$insideWidth/2-3,2,6);
    &drawSlot( 8,30,2,6);
    &drawSlot(57,$insideWidth/2-3,2,6);
    &drawSlot(29,85,6,2);
    &drawSlot(29,10,6,2);

    local $y = 0;
    local $dy = $tw + .4;  # a little extra length on the tabs
    local $x = $w0;
    print "M0,0 L$x,0";
    ($x,$y) = &drawTab5($x,$y,$clipTabWidth,$tww);
    ($x,$y) = &drawTab5($x,$y,$clipTabWidth,$tww);
    $y -= $tww;          print " L$x,$y";
    $x += $clipTabWidth; print " L$x,$y";
    $y += $tww;          print " L$x,$y";
    $x += $w0+$dy;       print " L$x,$y";

    $y += $insideWidth;  print " L$x,$y";
    $x -= $w0+$dy;       print " L$x,$y";
    ($x,$y) = &drawTab5($x,$y,-$clipTabWidth,-$tww);
    ($x,$y) = &drawTab5($x,$y,-$clipTabWidth,-$tww);
    $y += $tww;          print " L$x,$y";
    $x -= $clipTabWidth; print " L$x,$y";
    $y -= $tww;          print " L$x,$y";
    $x -= $w0;           print " L$x,$y";

    # Cut hole for USB plug on UNO.
    # (Try to mount nano to try and re-use this hole?)
    $x = 6;  # if UNO is mounted on a 3/16" standoff/spacer
    $y = 67; # this is a rough guess.  need to refine  about 5mm in from center of nearest mounting screw
    print "\nM$x,$y";
    $x += 12; print " L$x,$y";
    $y -= 13; print " L$x,$y";
    $x -= 12; print " L$x,$y";
    $y += 13; print " L$x,$y";

    print " $endPart\n";
}

sub printBottomPlate() {
    local ($insideHeight,$insideWidth,$bottomLength,$tw,$tl) = @_;

    local $tw3 = 3.4;  # small tab width
    printf("<desc>Payload box bottom plate</desc>
<g transform=\"translate(%.2f,0)\">\n",
	   $insideHeight+2*$tw3-.2);
    &startPart(0,0);
    &drawSlot( 10,44,2,6);
    &drawSlot( 31,12,8,2);
    &drawSlot( 91, 8,8,2);
    &drawSlot(151,12,8,2);
    &drawSlot(151,81,8,2);
    &drawSlot( 91,85,8,2);
    &drawSlot( 31,81,8,2);
    print " M-$tw3,0 L0,0 L0,-$tw";
    local ($x,$y) = &drawTab2(0,-$tw,$tl,$tw);
    local $i;
    for ($i=1; $i < 9; $i++) {
        ($x,$y) = &drawTab2($x,$y,$tl,$tw); }
    $x += $tl; print "\nL$x,$y";
    $y += $tw; print " L$x,$y\n";

    for ($i=0; $i < 9; $i++) {
        ($x,$y) = &drawTab3($x,$y,$tw3,5); }
    $x += $tw3; print "\nL$x,$y";
    $y += 5;    print " L$x,$y";
    $x -= $tw3+.2; print " L$x,$y\n";

    for ($i=0; $i < 9; $i++) {
        ($x,$y) = &drawTab4($x,$y,-$tl,5); }
    $y += 5;   print "\nL$x,$y";
    $x -= $tl; print " L$x,$y";
    $y -= 5;   print " L$x,$y";
    $x -= $tw3;print " L$x,$y\n";

    print "$endPart\n";

    # mounting templates are self-contained parts themselves
    &motorDriverModuleMountHoles(10,$insideWidth/2-43/2+2.5);

    # These are not (yet) aligned with end plate.
    # still need to do this.
    # want to design end-plate with holes for USB cables, etc.
    # try to re-use end holes where possible
    &raspPiMountHoles(180,80,180);
    &ArduinoUnoMountHoles(189,75,180); # good space from back of bottom plate
    print "\n</g>\n";
}

# version of payload with hinge top and spring clips, 190mm long
sub printHingePayloadSide() {
    local ($x0,$y0,$rot,$flags) = (@_,0,0);
    local $edgeless = $flags&1;

    printf("<desc>Payload side plate</desc>
<g transform=\"translate($x0,$y0) rotate($rot)\%s\">\n",
	   $edgeless?" scale(1,-1)":"");
    local $sl = 10;  # slot length
    local $slv = 10; #5;  # vertical slot length
    local $xLo = $sl*int($Bx/$sl)+2.5*$sl;
    local $nx = int($xLo/$sl)+3;
    $xLo = $sl * ($nx - ((($nx+1) % 2)*0.5));
    local $top = 25; # bottom of TOP plate, from axis centerline
    local $bot = 40; # top of BOTTOM plate, from axis centerline
    local $sw = 3;  # slot width
    local $ny = int(($top+$bot+$slv)/(2*$slv));
    local $yMid = ($top-$bot/2);
    local $yLo = $yMid - ($ny+0.75)*$slv;

    &startPart(-$xLo-.5,$yLo,90); &tabHoleRow(0,0,$slv, $sw,$ny); print $endPart;
    &startPart( $xLo+.5,$yLo,90); &tabHoleRow(0,0,$slv,-$sw,$ny); print $endPart;

    &startPart(0,0);
    local $bHole = $irB - 0.5*$fuzz; # a little tight, non-spinning
    local $eRad = 6;  # radius of edge corners
    &drawCircle(-$Bx,0,$bHole,24);
    &drawCircle( $Bx,0,$bHole,24);

    #&tabHoleRow(-$xLo, $top,$sl, $sw,$nx);
    &tabHoleRow(-$xLo,-$bot-.2,$sl,-$sw,$nx);

    # add some slots for wire pass-through, tie-downs, and ventillation
    &drawSlot(      -4,-$bot+9 ,8,2);
    &drawSlot(      -4, $top-9 ,8,2);
    &drawCircle(-$xLo+20, $top-12,2,11);
    &drawCircle(-$xLo+21,-$bot+10,2,11);
    &drawCircle( $xLo-20, $top-12,2,11);
    &drawCircle( $xLo-21,-$bot+10,2,11);
    &drawSlot( $xLo- 9,  -12   ,2,8);
    &drawSlot(-$xLo+ 9,  -12   ,2,8);

    local $dx = $eRad/2 - .5;
    local $dy = $eRad-$sw+1;
    &drawCircle($xLo-7.5-.5,$top+1.5,3,23);
    &drawArc('M', $xLo+$dx,-$bot+$dy-$eRad  ,$eRad,270,360,9);

    &drawArc('L', $xLo-7,$top-3,15.5,0,130,9);
    local $tp1 = $top + $sw;
    local $x = $xLo - 29.9; print "\nL$x,$top";
    $x -= 15.9;  print " L$x,$top";

    local $i = 0;
    for ($i=0; $i < 4; $i++) {
	$x -= .3;    print " L$x,$tp1";
	$x -= 13.7;  print " L$x,$tp1";
	$x -= .3;    print " L$x,$top";
	$x -= 15.7;  print " L$x,$top";
    }
    $x -= .3;   print " L$x,$tp1";
    $x -= 8.7;  print " L$x,$tp1";
    $x -= .3;   print " L$x,$top";

    &drawArc('L',-$xLo-$dx, $top-$eRad    ,$eRad, 90,180,9);
    &drawArc('L',-$xLo-$dx,-$bot-$eRad+$dy,$eRad,180,270,9);
    printf("L%.2f,%.2lf ",$xLo+$dx,-$bot-2*$eRad+$dy) if (!$edgeless);
    print $endPart;
    print "</g>\n";
}
1;
