#!/usr/bin/env python3
from numpy import around
from decimal import *
"""
Equations from https://github.com/prototypicall/Didge/blob/master/doc/How.md
"""
if __name__ == '__main__':
leadscrewTPI = 12 # my lathe TPI
encoder_res = 4 * 1024 # edges per revolution
microstep = 8 # setting on controller
basestep = 200 # number of normal steps/rev for stepper
S = basestep * microstep
n = 4 # how many decimal places
print("leadscrew TPI = ", leadscrewTPI)
imperial = False
if imperial:
myTPI = 40
latheTPI = leadscrewTPI
N = leadscrewTPI * S
D = myTPI * encoder_res
print("desired thread TPI = ", myTPI)
print("N = ", N)
print("D = ", D)
else:
mypitch = 0.5 # in mm
leadscrewpitch = 25.4/leadscrewTPI # in mm
N = mypitch * S
D = leadscrewpitch * encoder_res
print("desired thread pitch = ", mypitch, " mm")
print("N = ", N)
print("D = ", D)
ratio = Decimal(format(N/D,'f'))
ration= Decimal(format(around(N/D,n),'f'))
print("true ratio = ", N/D)
print("approx ratio = ", around(N/D,n))
print("ratio as integer ratio = ", ratio.as_integer_ratio())
b = Decimal(format(ration,'f'))
print("approx ratio as integer ratio = ", b.as_integer_ratio())
if (ratio > ration):
error = (1 - around(N/D,n)/(N/D))*100
else:
error = (1 - (N/D)/around(N/D,n))*100
print("Error in percent due to approximation = ", around(error, 3))
print("Encoder ppr = ", encoder_res)
print("Steps per rev = ", S)