//gear sizes calculator

sn=36; //spinning gear teeth
fn=64; //fixed gear teeth
gg=0.5; //[mm] gap between fixed and spinning gear enclosing circle
mss=1/16*25.4; //[mm] magnet side length

//at the inner surface of spinning gear, have magnets adjacent for smallest size
ro=(mss/2)/tan(%pi/sn);//inscribed radius of polygon
rh=(mss/2)/sin(%pi/sn);//circumscribed radius of polygon
rs=rh*(ro+mss)/ro; //outer radius enclosing magnets
mprintf("Spinning gear %f teeth length %f\n",sn,mss);
mprintf("\tInscribed radius %f\n\tCircumscribed radius %f\n\tEnclosing radius %f\n",ro,rh,rs);

if(%f)
xrpoly([0,0],sn,rh,%pi/sn);//inner wall of magnets
xrect(ro,mss/2,mss,mss);//example magnet
xarc(-rs,rs,2*rs,2*rs,0,360*64);//outer radius of magnets
isoview on;
end

//at the fixed gear centerline, magnet pitch should match that of spinning gear
ds=(2*%pi*(rs+gg))/sn;//spacing of ds teeth away from gear
fc=ds*fn;//expected central circumference of fixed gear
fd=fc/%pi;//central diameter of fixed gear
fi=(fd-mss)/2;//inscribed radius of polygon
fs=fi*tan(%pi/fn)*2;//actual side length of fixed gear
fq=sqrt((fs/2)^2+fi^2);//circumscribed radius of polygon
fod=fq*(fi+mss)/fi; //radius that will enclose fixed gear magnets
mprintf("Fixed gear %f teeth at gap %f\n",fn,gg);
mprintf("\tInscribed radius %f\n\tCircumscribed radius %f\n\tEnclosing radius %f\n\tInner side length %f\n",fi,fq,fod,fs);

if(%f)
xrpoly([0,0],fn,fq,%pi/fn);//inner wall of magnets
xrect(fi,mss/2,mss,mss);//example magnet
xarc(-fod,fod,fod*2,fod*2,0,360*64);//outer radius of magnets
isoview on;
end

//laser mirror sizes
msl=15.2; //[mm] mirror side length
mth=1; //[mm] mirror thickness
nm=3; //mirrors
ris=(msl/2)/tan(%pi/nm);//inscribed radius of polygon
rcs=(msl/2)/sin(%pi/nm);//circumscribed radius of polygon
ros=rcs*(ris+mth)/ris; //outer radius enclosing mirrors

if(%t)
xrpoly([0,0],nm,rcs,%pi/nm);//inner wall of mirrors
xrect(ris,msl/2,mth,msl);//example mirror
xarc(-ros,ros,2*ros,2*ros,0,360*64);//outer radius of mirrors
isoview on;
end

mprintf("Mirror block %f mirrors length %f thickness %f\n",nm, msl, mth);
mprintf("\tInscribed radius %f\n\tCircumscribed radius %f\n\tEnclosing radius %f\n",ris,rcs,ros);
