PYTHON   55

# torture

Guest on 11th July 2022 07:41:58 PM

1. """
2. This program creates "torture test" ngc files.  These include arcs (helices)
3. in any plane, straight feeds, and traverses all in a random order with
4. differing feeds.
5.
6. Run the resulting file in a stepper inifile with no headroom, and kick
7. things up a notch by using different max velocities and accelerations.
8. Turning feed override past 100% has also helped turn up bugs.
9. """
10.
11. from random import *
12. from math import sin, cos, pi
13.
14. distances = [-10,1,-.1,0,0,.1,1,10]
15. radii = [.1, 1, 10]
16. arcangles = range(0, 360, 45)
17. aangles = [-361,-89,-1,1,91,359]
18. chance_angular = .5
19. chance_angular_helix = .5
20.
21. def feed():
22.     "Half the time, change the feed rate"
23.     if random() < .5: return
24.     print "F%d" % randrange(100, 1000, 10),
25.
26. def torture_arc(x, y, z, a):
27.     "Generate a random arc (helix)"
28.     plane = randrange(3)
29.     print "G%d" % (plane+17),
30.
31.     direction = randrange(2)
32.     print "G%d" % (direction+2),
33.
34.     feed()
35.
36.     theta = choice(arcangles)
37.     theta1 = choice(arcangles)
39.     q = choice(distances)
40.
41.     print "(%d %d)" % (theta*180/pi, theta1*180/pi),
42.     if plane == 0: # XY plane
43.         ox = -cos(theta)*r
44.         oy = -sin(theta)*r
45.         print "I%f J%f" % (ox, oy),
46.
47.         z = z + q
48.         x = x + ox + cos(theta1)*r
49.         y = y + oy + sin(theta1)*r
50.
51.     elif plane == 1: # XZ plane
52.         ox = -cos(theta)*r
53.         oz = -sin(theta)*r
54.         print "I%f K%f" % (ox, oz),
55.
56.         x = x + ox + sin(theta1)*r
57.         z = z + oz + cos(theta1)*r
58.         y = y + q
59.
60.     else: # YZ plane
61.         oy = -cos(theta)*r
62.         oz = -sin(theta)*r
63.         print "J%f K%f" % (oy, oz),
64.
65.         x = x + q
66.         y = y + oy + cos(theta1)*r
67.         z = z + oz + sin(theta1)*r
68.
69.     if random() < chance_angular_helix:
70.         a = a + choice(aangles)
71.         print "A%f" % a,
72.
73.     print "X%f Y%f Z%f" % (x, y, z)
74.
75.     return x, y, z, a
76.
77. def torture_line(x, y, z, a):
78.     "Generate a random traverse or straight feed"
79.     kind = randrange(2)
80.     print "G%d" % kind,
81.
82.     if kind == 1: feed()
83.
84.     x = x + randrange(-10, 11)/2.
85.     y = y + randrange(-10, 11)/2.
86.     z = z + randrange(-10, 11)/2.
87.
88.     if random() < chance_angular:
89.         a = a + randrange(-180, 80)/2.
90.         print "A%f" % a,
91.     print "X%f Y%f Z%f" % (x, y, z)
92.
93.     return x, y, z, a
94.
95. def torture_main(runs):
96.     """Generate random chains of movement several times, restarting near the
97.     center each time motion gets too far away."""
98.     funcs = [torture_line, torture_arc]
99.     #funcs = [torture_arc]
100.     def R(x,l=-50, k=50): return x < l or x > k
101.
102.     print "g21"
103.     for i in range(runs):
104.         a = x = y = 0
105.         z = 20
106.         if chance_angular or chance_angular_helix:
107.             print "G0 X0 Y0 A0 Z20"
108.         else:
109.             print "G0 X0 Y0 Z20"
110.         print "F100"
111.
112.         while 1:
113.             x, y, z, a = choice(funcs)(x, y, z, a)
114.             if R(x) or R(y) or R(z,-10,30) or R(a,-30000,30000): break
115.
116. # Do ten runs then print m2
117. torture_main(20)
118. print "m2"