Skip to content

Commit aaf6f62

Browse files
authored
SW-339 added max dust factor to the laser head profiles (mrbeam#1505)
1 parent 36dfa3a commit aaf6f62

File tree

4 files changed

+94
-10
lines changed

4 files changed

+94
-10
lines changed

octoprint_mrbeam/analytics/usage_handler.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import yaml
66

7+
from octoprint_mrbeam.iobeam.iobeam_handler import IoBeamValueEvents
78
from octoprint_mrbeam.mrb_logger import mrb_logger
89
from octoprint.events import Events as OctoPrintEvents
910
from octoprint_mrbeam.mrbeam_events import MrBeamEvents
@@ -20,7 +21,6 @@ def usageHandler(plugin):
2021

2122

2223
class UsageHandler(object):
23-
MAX_DUST_FACTOR = 2.0
2424
MIN_DUST_FACTOR = 0.5
2525
MAX_DUST_VALUE = 0.5
2626
MIN_DUST_VALUE = 0.2
@@ -42,12 +42,6 @@ def __init__(self, plugin):
4242
self.start_ntp_synced = None
4343

4444
self._last_dust_value = None
45-
self._dust_mapping_m = (self.MAX_DUST_FACTOR - self.MIN_DUST_FACTOR) / (
46-
self.MAX_DUST_VALUE - self.MIN_DUST_VALUE
47-
)
48-
self._dust_mapping_b = (
49-
self.MIN_DUST_FACTOR - self._dust_mapping_m * self.MIN_DUST_VALUE
50-
)
5145

5246
analyticsfolder = os.path.join(
5347
self._settings.getBaseFolder("base"),
@@ -80,12 +74,26 @@ def _on_mrbeam_plugin_initialized(self, event, payload):
8074
self._laser_head_serial = self._lh["serial"]
8175
else:
8276
self._laser_head_serial = "no_serial"
83-
77+
self._calculate_dust_mapping()
8478
self._init_missing_usage_data()
8579
self.log_usage()
8680

8781
self._subscribe()
8882

83+
def _calculate_dust_mapping(self):
84+
max_dust_factor = self._laserhead_handler.current_laserhead_max_dust_factor
85+
self._dust_mapping_m = (max_dust_factor - self.MIN_DUST_FACTOR) / (
86+
self.MAX_DUST_VALUE - self.MIN_DUST_VALUE
87+
)
88+
self._dust_mapping_b = (
89+
self.MIN_DUST_FACTOR - self._dust_mapping_m * self.MIN_DUST_VALUE
90+
)
91+
self._logger.debug(
92+
"new dust mapping -> {} - {} - {}".format(
93+
max_dust_factor, self._dust_mapping_m, self._dust_mapping_b
94+
)
95+
)
96+
8997
def log_usage(self):
9098
self._logger.info(
9199
"Usage: total_usage: {}, pre-filter: {}, main filter: {}, current laser head: {}, mechanics: {}, compressor: {} - {}".format(
@@ -119,6 +127,9 @@ def _subscribe(self):
119127
self._event_bus.subscribe(
120128
MrBeamEvents.LASER_HEAD_READ, self.event_laser_head_read
121129
)
130+
self._plugin.iobeam.subscribe(
131+
IoBeamValueEvents.LASERHEAD_CHANGED, self._event_laserhead_changed
132+
)
122133

123134
def event_laser_head_read(self, event, payload):
124135
# Update laser head info if necessary --> Only update if there is a serial number different than the previous
@@ -166,6 +177,17 @@ def event_stop(self, event, payload):
166177

167178
self.write_usage_analytics(action="job_finished")
168179

180+
def _event_laserhead_changed(self, event):
181+
"""
182+
will be triggered if the laser head changed,
183+
refreshes the laserhead max dust factor that will be used for the new laser head
184+
185+
Returns:
186+
187+
"""
188+
self._logger.debug("Laserhead changed recalculate dust mapping")
189+
self._calculate_dust_mapping()
190+
169191
def _set_time(self, job_duration):
170192
if job_duration is not None and job_duration > 0.0:
171193

octoprint_mrbeam/iobeam/laserhead_handler.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from octoprint_mrbeam.iobeam.iobeam_handler import IoBeamValueEvents
77

88
LASERHEAD_MAX_TEMP_FALLBACK = 55.0
9+
LASERHEAD_MAX_DUST_FACTOR_FALLBACK = 3.0 # selected the highest factor
910

1011
# singleton
1112
_instance = None
@@ -34,6 +35,7 @@ def __init__(self, plugin):
3435
self._event_bus = plugin._event_bus
3536
self._plugin_version = plugin.get_plugin_version()
3637
self._iobeam = None
38+
self._laserhead_properties = {}
3739

3840
self._lh_cache = {}
3941
self._last_used_lh_serial = None
@@ -443,7 +445,7 @@ def current_laserhead_max_temperature(self):
443445
float: Laser head max temp
444446
445447
"""
446-
current_laserhead_properties = self._load_current_laserhead_properties()
448+
current_laserhead_properties = self._get_laserhead_properties()
447449

448450
# Handle the exceptions
449451
if((isinstance(current_laserhead_properties, dict) is False) or
@@ -472,7 +474,7 @@ def default_laserhead_max_temperature(self):
472474

473475
def _load_current_laserhead_properties(self):
474476
"""
475-
Loads the current detected laser head related properties and return them
477+
Loads the current detected laser head related properties from the laser head profile files and return them
476478
477479
Returns:
478480
dict: current laser head properties, None: otherwise
@@ -505,7 +507,65 @@ def _load_current_laserhead_properties(self):
505507
e, lh_properties_file_path))
506508
return None
507509

510+
def _get_laserhead_properties(self):
511+
"""
512+
returns the current saved laser head properties or load new if the laser head id changed
513+
514+
Returns:
515+
dict: current laser head properties, None: otherwise
516+
517+
"""
518+
# 1. get the ID of the current laser head
519+
laserhead_id = self.get_current_used_lh_model_id()
520+
self._logger.debug("laserhead id compare {} - {}".format(laserhead_id, self._laserhead_properties.get("laserhead_id", None)))
521+
if laserhead_id != self._laserhead_properties.get("laserhead_id", None):
522+
self._logger.debug("new laserhead_id -> load current laserhead porperties")
523+
# 2. Load the corresponding yaml file and return it's content
524+
self._laserhead_properties = self._load_current_laserhead_properties()
525+
if self._laserhead_properties is not None:
526+
self._laserhead_properties.update({'laserhead_id': laserhead_id})
527+
else:
528+
self._logger.debug("no new laserhead_id -> return current laserhead_properties")
529+
530+
self._logger.debug(
531+
"_laserhead_properties - {}".format(self._laserhead_properties))
532+
return self._laserhead_properties
533+
534+
@property
535+
def current_laserhead_max_dust_factor(self):
536+
"""
537+
Return the current laser head max dust factor
538+
539+
Returns:
540+
float: Laser head max dust factor
541+
542+
"""
543+
current_laserhead_properties = self._get_laserhead_properties()
544+
545+
# Handle the exceptions
546+
if ((isinstance(current_laserhead_properties, dict) is False) or
547+
("max_dust_factor" not in current_laserhead_properties) or
548+
(isinstance(current_laserhead_properties["max_dust_factor"], float) is False)):
549+
# Apply fallback
550+
self._logger.debug("Current laserhead properties: {}".format(current_laserhead_properties))
551+
self._logger.exception(
552+
"Current Laserhead max dust factor couldn't be retrieved, fallback to the factor value of: {}".format(
553+
self.default_laserhead_max_dust_factor))
554+
return self.default_laserhead_max_dust_factor
555+
# Reaching here means, everything looks good
556+
self._logger.debug("Current Laserhead max dust factor:{}".format(current_laserhead_properties["max_dust_factor"]))
557+
return current_laserhead_properties["max_dust_factor"]
558+
559+
@property
560+
def default_laserhead_max_dust_factor(self):
561+
"""
562+
Default max dust factor for laser head. to be used by other modules at init time
563+
564+
Returns:
565+
float: Laser head default max dust factor
566+
"""
508567

568+
return LASERHEAD_MAX_DUST_FACTOR_FALLBACK
509569

510570

511571

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
max_temperature: 55.0
2+
max_dust_factor: 2.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
max_temperature: 59.0
2+
max_dust_factor: 3.0

0 commit comments

Comments
 (0)