mirror of
https://github.com/sinseman44/PyCNC.git
synced 2026-07-16 08:37:09 +00:00
heaters implementation: extruder and bed, adding answers to gcode commands
This commit is contained in:
@@ -20,6 +20,15 @@ class TestGCode(unittest.TestCase):
|
||||
self.assertEqual(gc.coordinates(self.default, 1).z, 0.0)
|
||||
self.assertEqual(gc.coordinates(self.default, 1).e, 99.0)
|
||||
|
||||
def test_has(self):
|
||||
gc = GCode.parse_line("g1X2Y3z4E5F50")
|
||||
self.assertTrue(gc.has("G"))
|
||||
self.assertTrue(gc.has("X"))
|
||||
self.assertTrue(gc.has("Y"))
|
||||
self.assertTrue(gc.has("Z"))
|
||||
self.assertTrue(gc.has("E"))
|
||||
self.assertTrue(gc.has("F"))
|
||||
|
||||
def test_parser(self):
|
||||
gc = GCode.parse_line("G1X2Y-3Z4E1.5")
|
||||
self.assertEqual(gc.command(), "G1")
|
||||
|
||||
+56
-1
@@ -3,11 +3,14 @@ import unittest
|
||||
from cnc.gcode import *
|
||||
from cnc.gmachine import *
|
||||
from cnc.coordinates import *
|
||||
from cnc.heater import *
|
||||
from cnc.pid import *
|
||||
|
||||
|
||||
class TestGMachine(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
Pid.FIX_TIME_S = 0.01
|
||||
Heater.LOOP_INTERVAL_S = 0.001
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
@@ -192,6 +195,58 @@ class TestGMachine(unittest.TestCase):
|
||||
m.do_command, GCode.parse_line("M3S999999999"))
|
||||
m.do_command(GCode.parse_line("M5"))
|
||||
|
||||
def test_m104_m109(self):
|
||||
m = GMachine()
|
||||
m.do_command(GCode.parse_line("M104S"+str(MIN_TEMPERATURE)))
|
||||
self.assertEqual(m.extruder_target_temperature(), MIN_TEMPERATURE)
|
||||
m.do_command(GCode.parse_line("M104S0"))
|
||||
self.assertEqual(m.extruder_target_temperature(), 0)
|
||||
# blocking heating should be called with max temperature since virtual
|
||||
# hal always return this temperature.
|
||||
m.do_command(GCode.parse_line("M109S" + str(EXTRUDER_MAX_TEMPERATURE)))
|
||||
self.assertEqual(m.extruder_target_temperature(),
|
||||
EXTRUDER_MAX_TEMPERATURE)
|
||||
m.do_command(GCode.parse_line("M104S0"))
|
||||
self.assertEqual(m.extruder_target_temperature(), 0)
|
||||
self.assertRaises(GMachineException, m.do_command,
|
||||
GCode.parse_line("M104S"+str(MIN_TEMPERATURE - 1)))
|
||||
self.assertRaises(GMachineException, m.do_command,
|
||||
GCode.parse_line("M109S"
|
||||
+ str(EXTRUDER_MAX_TEMPERATURE + 1)))
|
||||
self.assertRaises(GMachineException, m.do_command,
|
||||
GCode.parse_line("M109"))
|
||||
|
||||
def test_m106_m107(self):
|
||||
m = GMachine()
|
||||
m.do_command(GCode.parse_line("M106"))
|
||||
self.assertTrue(m.fan_state())
|
||||
m.do_command(GCode.parse_line("M106S0"))
|
||||
self.assertFalse(m.fan_state())
|
||||
m.do_command(GCode.parse_line("M106S123"))
|
||||
self.assertTrue(m.fan_state())
|
||||
m.do_command(GCode.parse_line("M107"))
|
||||
self.assertFalse(m.fan_state())
|
||||
|
||||
def test_m140_m190(self):
|
||||
m = GMachine()
|
||||
m.do_command(GCode.parse_line("M140S"+str(MIN_TEMPERATURE)))
|
||||
self.assertEqual(m.bed_target_temperature(), MIN_TEMPERATURE)
|
||||
m.do_command(GCode.parse_line("M140S0"))
|
||||
self.assertEqual(m.bed_target_temperature(), 0)
|
||||
# blocking heating should be called with max temperature since virtual
|
||||
# hal always return this temperature.
|
||||
m.do_command(GCode.parse_line("M190S" + str(BED_MAX_TEMPERATURE)))
|
||||
self.assertEqual(m.bed_target_temperature(), BED_MAX_TEMPERATURE)
|
||||
m.do_command(GCode.parse_line("M190S0"))
|
||||
self.assertEqual(m.bed_target_temperature(), 0)
|
||||
self.assertRaises(GMachineException, m.do_command,
|
||||
GCode.parse_line("M140S"+str(MIN_TEMPERATURE - 1)))
|
||||
self.assertRaises(GMachineException, m.do_command,
|
||||
GCode.parse_line("M190S"
|
||||
+ str(BED_MAX_TEMPERATURE + 1)))
|
||||
self.assertRaises(GMachineException, m.do_command,
|
||||
GCode.parse_line("M190"))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
import unittest
|
||||
|
||||
from cnc.heater import *
|
||||
from cnc.pid import *
|
||||
from cnc.config import *
|
||||
|
||||
|
||||
class TestHeater(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._target_temp = 100
|
||||
Pid.FIX_TIME_S = 0
|
||||
Heater.LOOP_INTERVAL_S = 0.001
|
||||
self._control_counter = 0
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def __get_temperature(self):
|
||||
return self._target_temp
|
||||
|
||||
def __get_bad_temperature(self):
|
||||
return self._target_temp / 2
|
||||
|
||||
# noinspection PyUnusedLocal
|
||||
def __control(self, percent):
|
||||
self._control_counter += 1
|
||||
|
||||
def test_start_stop(self):
|
||||
# check if thread stops correctly
|
||||
he = Heater(self._target_temp, EXTRUDER_PID, self.__get_temperature,
|
||||
self.__control)
|
||||
self.assertEqual(self._target_temp, he.target_temperature())
|
||||
he.stop()
|
||||
self._control_counter = 0
|
||||
he.join(5)
|
||||
self.assertEqual(self._control_counter, 0)
|
||||
self.assertFalse(he.is_alive())
|
||||
|
||||
def test_async(self):
|
||||
# check asynchronous heating
|
||||
self._control_counter = 0
|
||||
he = Heater(self._target_temp, EXTRUDER_PID, self.__get_temperature,
|
||||
self.__control)
|
||||
j = 0
|
||||
while self._control_counter < 3:
|
||||
time.sleep(0.01)
|
||||
j += 1
|
||||
if j > 500:
|
||||
he.stop()
|
||||
raise Exception("Heater timeout")
|
||||
he.stop()
|
||||
self.assertTrue(he.is_fixed())
|
||||
|
||||
def test_sync(self):
|
||||
# test wait() method
|
||||
self._control_counter = 0
|
||||
he = Heater(self._target_temp, EXTRUDER_PID, self.__get_temperature,
|
||||
self.__control)
|
||||
he.wait()
|
||||
he.stop()
|
||||
self.assertGreater(self._control_counter, 1) # one call for stop()
|
||||
self.assertTrue(he.is_fixed())
|
||||
|
||||
def test_fail(self):
|
||||
# check if heater will not fix with incorrect temperature
|
||||
self._control_counter = 0
|
||||
he = Heater(self._target_temp, EXTRUDER_PID, self.__get_bad_temperature,
|
||||
self.__control)
|
||||
j = 0
|
||||
while self._control_counter < 10:
|
||||
time.sleep(0.01)
|
||||
j += 1
|
||||
if j > 500:
|
||||
he.stop()
|
||||
raise Exception("Heater timeout")
|
||||
he.stop()
|
||||
self.assertGreater(self._control_counter, 10) # one call for stop()
|
||||
self.assertFalse(he.is_fixed())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
+37
-22
@@ -1,6 +1,7 @@
|
||||
import unittest
|
||||
|
||||
from cnc.pid import *
|
||||
from cnc.config import *
|
||||
|
||||
|
||||
class TestPid(unittest.TestCase):
|
||||
@@ -8,64 +9,78 @@ class TestPid(unittest.TestCase):
|
||||
self._environment_temp = 25
|
||||
# Coefficients below were chosen by an experimental way with a real
|
||||
# hardware: reprap heating bed and extruder.
|
||||
self._bed_c = 0.00231 # bed cooling coefficient
|
||||
self._bed_h = 0.25 # bed heating coefficient
|
||||
self._extruder_c = 0.0039 # extruder cooling coefficient
|
||||
self._extruder_h = 3.09 # extruder heating coefficient
|
||||
# See ../utils/heater_model_finder.py to find out this coefficients.
|
||||
self._bed_c = 0.0027 # bed cooling coefficient
|
||||
self._bed_h = 0.2522 # bed heating coefficient
|
||||
self._extruder_c = 0.0108 # extruder cooling coefficient
|
||||
self._extruder_h = 3.4070 # extruder heating coefficient
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def __simulate(self, target_temp, environment_temp, cool, heat):
|
||||
def __simulate(self, target_temp, pid_c, environment_temp, cool, heat):
|
||||
# Simulate heating some hypothetical thing with heater(h is a heat
|
||||
# transfer coefficient, which becomes just a delta temperature each
|
||||
# second) from environment temperature to target_temp. Consider that
|
||||
# there is natural air cooling process with some heat transfer
|
||||
# coefficient c. Heating power is controlled by PID.
|
||||
pid = Pid(target_temp, 0)
|
||||
pid = Pid(target_temp, pid_c, 0)
|
||||
temperature = environment_temp
|
||||
heater_power = 0
|
||||
fixed_at = None
|
||||
zeros_counter = 0
|
||||
for j in range(1, 15 * 60 + 1): # simulate for 15 minutes
|
||||
total_counter = 0
|
||||
iter_pes_s = 2 # step is 0.5s
|
||||
j = 1
|
||||
for k in range(1, 20 * 60 * iter_pes_s + 1): # simulate for 20 minutes
|
||||
j = k / float(iter_pes_s)
|
||||
# natural cooling
|
||||
temperature -= (temperature - environment_temp) * cool
|
||||
temperature -= ((temperature - environment_temp) * cool
|
||||
/ float(iter_pes_s))
|
||||
# heating
|
||||
temperature += heat * heater_power
|
||||
temperature += heat * heater_power / float(iter_pes_s)
|
||||
heater_power = pid.update(temperature, j)
|
||||
if fixed_at is None:
|
||||
if pid.is_fixed():
|
||||
fixed_at = j
|
||||
else:
|
||||
self.assertLess(abs(temperature - target_temp),
|
||||
pid.FIX_ACCURACY * target_temp,
|
||||
pid.FIX_ACCURACY * target_temp * 5.0,
|
||||
msg="PID failed to control temperature "
|
||||
"{}/{} {}".format(temperature, target_temp, j))
|
||||
if heater_power == 0.0:
|
||||
zeros_counter += 1
|
||||
self.assertLess(zeros_counter, 20, msg="PID turns on/off, instead of "
|
||||
"fine control")
|
||||
self.assertLess(fixed_at, 600,
|
||||
msg="failed to heat in 10 minutes, final temperature "
|
||||
total_counter += 1
|
||||
self.assertLess(abs(temperature - target_temp),
|
||||
pid.FIX_ACCURACY * target_temp,
|
||||
msg="PID failed to control temperature "
|
||||
"{}/{} {}".format(temperature, target_temp, j))
|
||||
self.assertLess(zeros_counter, total_counter * 0.05,
|
||||
msg="PID turns on/off, instead of fine control")
|
||||
self.assertLess(fixed_at, 900,
|
||||
msg="failed to heat in 15 minutes, final temperature "
|
||||
"{}/{}".format(temperature, target_temp))
|
||||
|
||||
def test_simple(self):
|
||||
pid = Pid(50, 0)
|
||||
pid = Pid(50, EXTRUDER_PID, 0)
|
||||
self.assertEqual(0, pid.update(100, 1))
|
||||
self.assertEqual(1, pid.update(0, 2))
|
||||
pid = Pid(50, BED_PID, 0)
|
||||
self.assertEqual(0, pid.update(100, 1))
|
||||
self.assertEqual(1, pid.update(0, 2))
|
||||
|
||||
def test_bed(self):
|
||||
# check if bed typical temperatures can be reached in simulation
|
||||
for target in range(50, 101, 10):
|
||||
self.__simulate(target, self._environment_temp,
|
||||
self._bed_c, self._bed_h)
|
||||
|
||||
def test_extruder(self):
|
||||
# check if extruder typical temperatures can be reached in simulation
|
||||
for target in range(150, 251, 10):
|
||||
self.__simulate(target, self._environment_temp,
|
||||
self.__simulate(target, EXTRUDER_PID, self._environment_temp,
|
||||
self._extruder_c, self._extruder_h)
|
||||
|
||||
def test_bed(self):
|
||||
# check if bed typical temperatures can be reached in simulation
|
||||
for target in range(50, 101, 10):
|
||||
self.__simulate(target, BED_PID, self._environment_temp,
|
||||
self._bed_c, self._bed_h)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user