0

我为我的作业编写了一个代码,它对我来说很好用;但是当我的朋友尝试运行它时,Spyder 给出“ZeroDivisionError:浮点除以零”。

import trimesh
import shapely
import numpy as np
import math
import pylab
import matplotlib.pyplot as plt

feed = 0.15 # mm/rev
v = 60 #m/min

mesh = trimesh.load_mesh('part_02.stl')
slicee = mesh.section(plane_origin=mesh.centroid, plane_normal=[0,0,1])

slice_2D, to_3D = slicee.to_planar()
plt.figure(1)
plt.clf()
slice_2D.show()

p = slice_2D.polygons_closed[0]
l = p.length 
all_coords = list(p.exterior.coords)

    
all_coords_x = [x[0] for x in all_coords]
all_coords_y = [x[1] for x in all_coords]
plt.figure(2)
plt.clf()
plt.axis("equal")
plt.plot(all_coords_x,all_coords_y)
pos_coords = []
for tup in all_coords:
    if tup[1] >= -0.7:
        pos_coords.append(tup)
pos_coords = pos_coords[1:]
pos_coords_x = [x[0] for x in pos_coords]
pos_coords_y = [x[1] for x in pos_coords]
plt.figure(3)
plt.clf()
plt.axis("equal")
plt.grid(True)
plt.plot(pos_coords_x,pos_coords_y,".")
k = False
total_time = 0
for i in range(len(pos_coords)):
    if i != len(pos_coords)-1:
        tup_pos = pos_coords[i]
        tup_next = pos_coords[i+1]
        if tup_pos[0] == tup_next[0]:
            #face turning
            L = abs((tup_next[1]**2) - (tup_pos[1]**2))
            T_f = (3.14*L)/(1000*v*feed)
            total_time += T_f
            k = False
        elif tup_pos[1] == tup_next[1]:
            #straight turning
            L = abs((tup_next[0]) - (tup_pos[0]))
            T_s = (3.14*tup_pos[1]*L)/(500*v*feed)
            k = False
            total_time += T_s
        else:
            tup_3rd = pos_coords[i+2]
            m1 = round( ((tup_next[1]-tup_pos[1]) / (tup_next[0]-tup_pos[0])),4 )
            m2 = round( ( (tup_3rd[1]-tup_pos[1]) / (tup_3rd[0]-tup_pos[0]) ),4 )
            if m1 == m2:
                #tapper turning
                L1 = abs((tup_next[1]**2) - (tup_pos[1]**2))
                theta = np.arctan(abs(m1))
                T_t = (3.14*L1) / (math.sin(theta)*1000*v*feed)
                k = False
                total_time += T_t
            else:
                if k == False:
                    #circular turning
                    k = True
                    for j in range(i,len(pos_coords)):
                        tup_last = pos_coords[i+2]
                        m3 = round( ((tup_last[1]-tup_pos[1]) / (tup_last[0]-tup_pos[0])),4 )
                        if abs(m3) == 1:
                            r_a = tup_last[1] - tup_pos[1]
                            if abs(m1) > abs(m3):
                                x_c = tup_pos[1]
                                T_c = (3.14*r_a*abs((x_c*3.14/2)-(r_a*(-1)))) / (500*v*feed)
                            else:
                                x_c = tup_last[1]
                                T_c = (3.14*r_a*abs((x_c*3.14/2)-(r_a*(-1)))) / (500*v*feed)
                            total_time += T_c

当我运行代码时,我得到了总时间值。但是我的朋友得到了 ZeroDivisionError。当我们检查时,我们在 all_coords 和 pos_coords 上得到不同的点。可能是什么原因?

4

0 回答 0