refactoring

This commit is contained in:
Nikolay Khabarov
2017-06-12 00:52:11 +03:00
parent 0fe98f4cca
commit 98bf7e914e
23 changed files with 448 additions and 405 deletions
+57 -68
View File
@@ -1,9 +1,8 @@
import unittest
import time
from cnc.coordinates import *
from cnc.pulses import *
from cnc.config import *
from cnc.coordinates import *
from cnc import hal_virtual
@@ -35,8 +34,8 @@ class TestPulses(unittest.TestCase):
0, 0, 0),
self.v)
i = 0
for dir, px, py, pz, pe in g:
if dir:
for direction, px, py, pz, pe in g:
if direction:
continue
i += 1
self.assertEqual(px, 0)
@@ -51,8 +50,8 @@ class TestPulses(unittest.TestCase):
1.0 / STEPPER_PULSES_PER_MM_E),
self.v)
i = 0
for dir, px, py, pz, pe in g:
if dir:
for direction, px, py, pz, pe in g:
if direction:
continue
i += 1
self.assertEqual(px, 0)
@@ -61,20 +60,19 @@ class TestPulses(unittest.TestCase):
self.assertEqual(pe, 0)
self.assertEqual(i, 1)
def __check_circular(self, delta, radius, plane, direction = CW):
def __check_circular(self, delta, radius, plane, direction=CW):
g = PulseGeneratorCircular(delta, radius, plane, direction, self.v)
x, y, z, e = 0, 0, 0, 0
dx, dy, dz, de = None, None, None, None
dir_changed = 0
dir_requested = False
t = -1
for dir, px, py, pz, pe in g:
if dir:
for direction_i, px, py, pz, pe in g:
if direction_i:
dx, dy, dz, de = px, py, pz, pe
dir_requested = True
continue
if dir_requested: # ignore last change
if dir_requested: # ignore last change
dir_requested = False
dir_changed += 1
if px is not None:
@@ -97,53 +95,43 @@ class TestPulses(unittest.TestCase):
def test_single_radius_circles(self):
# Check if PulseGenerator returns correctly single radius movement in
# both direction.
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(1.0 / STEPPER_PULSES_PER_MM_X, 0, 0, 0),
zero_delta = Coordinates(0, 0, 0, 0)
radius = Coordinates(1.0 / STEPPER_PULSES_PER_MM_X, 0, 0, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_XY, CW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
radius = Coordinates(-1.0 / STEPPER_PULSES_PER_MM_X, 0, 0, 0)
_, pos = self.__check_circular(zero_delta, radius,
PLANE_XY, CW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(-1.0 / STEPPER_PULSES_PER_MM_X, 0, 0, 0),
PLANE_XY, CW)
radius = Coordinates(0, 1.0 / STEPPER_PULSES_PER_MM_Y, 0, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_YZ, CW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(0, 1.0 / STEPPER_PULSES_PER_MM_Y, 0, 0),
PLANE_YZ, CW)
radius = Coordinates(0, -1.0 / STEPPER_PULSES_PER_MM_Y, 0, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_YZ, CW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(0, -1.0 / STEPPER_PULSES_PER_MM_Y, 0, 0),
PLANE_YZ, CW)
radius = Coordinates(0, 0, 1.0 / STEPPER_PULSES_PER_MM_Z, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_ZX, CW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(0, 0, 1.0 / STEPPER_PULSES_PER_MM_Z, 0),
PLANE_ZX, CW)
radius = Coordinates(0, 0, -1.0 / STEPPER_PULSES_PER_MM_Z, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_ZX, CW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(0, 0, -1.0 / STEPPER_PULSES_PER_MM_Z, 0),
PLANE_ZX, CW)
radius = Coordinates(1.0 / STEPPER_PULSES_PER_MM_X, 0, 0, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_XY, CCW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(1.0 / STEPPER_PULSES_PER_MM_X, 0, 0, 0),
PLANE_XY, CCW)
radius = Coordinates(-1.0 / STEPPER_PULSES_PER_MM_X, 0, 0, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_XY, CCW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(-1.0 / STEPPER_PULSES_PER_MM_X, 0, 0, 0),
PLANE_XY, CCW)
radius = Coordinates(0, 1.0 / STEPPER_PULSES_PER_MM_Y, 0, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_YZ, CCW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(0, 1.0 / STEPPER_PULSES_PER_MM_Y, 0, 0),
PLANE_YZ, CCW)
radius = Coordinates(0, -1.0 / STEPPER_PULSES_PER_MM_Y, 0, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_YZ, CCW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(0, -1.0 / STEPPER_PULSES_PER_MM_Y, 0, 0),
PLANE_YZ, CCW)
radius = Coordinates(0, 0, 1.0 / STEPPER_PULSES_PER_MM_Z, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_ZX, CCW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(0, 0, 1.0 / STEPPER_PULSES_PER_MM_Z, 0),
PLANE_ZX, CCW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
_, pos = self.__check_circular(Coordinates(0, 0, 0, 0),
Coordinates(0, 0, -1.0 / STEPPER_PULSES_PER_MM_Z, 0),
PLANE_ZX, CCW)
radius = Coordinates(0, 0, -1.0 / STEPPER_PULSES_PER_MM_Z, 0)
_, pos = self.__check_circular(zero_delta, radius, PLANE_ZX, CCW)
self.assertEqual(pos, Coordinates(0, 0, 0, 0))
def test_with_hal_virtual(self):
@@ -158,30 +146,29 @@ class TestPulses(unittest.TestCase):
hal_virtual.move(PulseGeneratorLinear(Coordinates(25.4, 0, 0, 0),
self.v))
hal_virtual.move(PulseGeneratorLinear(Coordinates(TABLE_SIZE_X_MM,
TABLE_SIZE_Y_MM,
TABLE_SIZE_Z_MM,
100.0), self.v))
TABLE_SIZE_Y_MM,
TABLE_SIZE_Z_MM,
100.0), self.v))
hal_virtual.move(PulseGeneratorCircular(Coordinates(0, 20, 0, 0),
Coordinates(-10, 10, 0, 0),
PLANE_XY, CW, self.v))
hal_virtual.move(PulseGeneratorCircular(Coordinates(-4, -4, 0, 0),
Coordinates(-2, -2, 0, 0),
PLANE_XY, CW, self.v))
hal_virtual.move(PulseGeneratorCircular(Coordinates(- 2.0 / STEPPER_PULSES_PER_MM_X,
- 2.0 / STEPPER_PULSES_PER_MM_Y,
0, 0),
Coordinates(- 1.0 / STEPPER_PULSES_PER_MM_X,
- 1.0 / STEPPER_PULSES_PER_MM_Y,
0, 0),
PLANE_XY, CW, self.v))
delta = Coordinates(- 2.0 / STEPPER_PULSES_PER_MM_X,
- 2.0 / STEPPER_PULSES_PER_MM_Y, 0, 0)
radius = Coordinates(- 1.0 / STEPPER_PULSES_PER_MM_X,
- 1.0 / STEPPER_PULSES_PER_MM_Y, 0, 0)
hal_virtual.move(PulseGeneratorCircular(delta, radius, PLANE_XY, CW,
self.v))
def test_twice_faster_linear(self):
# Checks if one axis moves exactly twice faster, pulses are correct.
m = Coordinates(2, 4, 0, 0)
g = PulseGeneratorLinear(m, self.v)
i = 0
for dir, px, py, pz, pe in g:
if dir:
for direction, px, py, pz, pe in g:
if direction:
continue
if i % 2 == 0:
self.assertNotEqual(px, None)
@@ -203,8 +190,8 @@ class TestPulses(unittest.TestCase):
iz = 0
ie = 0
t = -1
for dir, px, py, pz, pe in g:
if dir:
for direction, px, py, pz, pe in g:
if direction:
continue
if px is not None:
ix += 1
@@ -244,8 +231,9 @@ class TestPulses(unittest.TestCase):
g = PulseGeneratorLinear(m, self.v)
i = 0
lx = 0
for dir, px, py, pz, pe in g:
if dir:
lt, at, bt = None, None, None
for direction, px, py, pz, pe in g:
if direction:
continue
if i == 2:
at = px - lx
@@ -254,7 +242,8 @@ class TestPulses(unittest.TestCase):
bt = px - lx
lx = px
i += 1
self.assertEqual(round(60.0 / lt / STEPPER_PULSES_PER_MM_X), round(self.v))
self.assertEqual(round(60.0 / lt / STEPPER_PULSES_PER_MM_X),
round(self.v))
self.assertGreater(at, lt)
self.assertGreater(bt, lt)
@@ -263,8 +252,8 @@ class TestPulses(unittest.TestCase):
m = Coordinates(1, -2, 3, -4)
g = PulseGeneratorLinear(m, self.v)
dir_found = False
for dir, px, py, pz, pe in g:
if dir:
for direction, px, py, pz, pe in g:
if direction:
# should be once
self.assertFalse(dir_found)
dir_found = True
@@ -273,8 +262,8 @@ class TestPulses(unittest.TestCase):
m = Coordinates(-1, 2, -3, 4)
g = PulseGeneratorLinear(m, self.v)
dir_found = False
for dir, px, py, pz, pe in g:
if dir:
for direction, px, py, pz, pe in g:
if direction:
# should be once
self.assertFalse(dir_found)
dir_found = True