From a5ea538a9dc4de0e90a896a8bb8251b20e3167e8 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 27 Jun 2025 12:36:28 +0200 Subject: [PATCH 1/5] feat: abstracted previously hardcoded color sensor parameters --- arduino_alvik/arduino_alvik.py | 26 +++++++++++------------ arduino_alvik/constants.py | 39 +++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index d1a5629..5c4558d 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -1120,34 +1120,34 @@ def hsv2label(h, s, v) -> str: if None in [h, s, v]: return 'UNDEFINED' - if s < 0.1: - if v < 0.05: + if s < min_saturation: + if v < balck_value: label = 'BLACK' - elif v < 0.15: + elif v < grey_value: label = 'GREY' - elif v < 0.8: + elif v < light_grey_value: label = 'LIGHT GREY' else: label = 'WHITE' else: - if v > 0.1: - if 20 <= h < 90: + if v > min_color_value: + if hsv_limits["thresholds"][0] <= h < hsv_limits["thresholds"][1]: label = 'YELLOW' - elif 90 <= h < 140: + elif hsv_limits["thresholds"][1] <= h < hsv_limits["thresholds"][2]: label = 'LIGHT GREEN' - elif 140 <= h < 170: + elif hsv_limits["thresholds"][2] <= h < hsv_limits["thresholds"][3]: label = 'GREEN' - elif 170 <= h < 210: + elif hsv_limits["thresholds"][3] <= h < hsv_limits["thresholds"][4]: label = 'LIGHT BLUE' - elif 210 <= h < 250: + elif hsv_limits["thresholds"][4] <= h < hsv_limits["thresholds"][5]: label = 'BLUE' - elif 250 <= h < 280: + elif hsv_limits["thresholds"][5] <= h < hsv_limits["thresholds"][6]: label = 'VIOLET' else: # h<20 or h>=280 is more problematic - if v < 0.5 and s < 0.45: + if v < hsv_limits["high_h_v_thre"][0] and s < hsv_limits["high_h_s_thre"][0]: label = 'BROWN' else: - if v > 0.77: + if v > hsv_limits["high_h_v_thre"][1]: label = 'ORANGE' else: label = 'RED' diff --git a/arduino_alvik/constants.py b/arduino_alvik/constants.py index 47a7c06..9456365 100644 --- a/arduino_alvik/constants.py +++ b/arduino_alvik/constants.py @@ -1,5 +1,38 @@ +""" +HW0: Alvik carrier board with APDS9960 +HW1: Alvik carrier board with APDS9999 +""" +USES_HARDWARE = "HW0" # Change to "HW1" for hardware version 1 + # COLOR SENSOR -COLOR_FULL_SCALE = 4097 -WHITE_CAL = [450, 500, 510] -BLACK_CAL = [160, 200, 190] \ No newline at end of file + +if USES_HARDWARE == "HW0": + COLOR_FULL_SCALE = 4097 + WHITE_CAL = [450, 500, 510] + BLACK_CAL = [160, 200, 190] + + min_saturation = 0.1 + balck_value = 0.05 + grey_value = 0.15 + light_grey_value = 0.8 + min_color_value = 0.1 + + hsv_limits = {"thresholds": [20, 90, 140, 170, 210, 250, 280], + "high_h_v_thre": [0.5, 0.77], + "high_h_s_thre": [0.45]} + +elif USES_HARDWARE == "HW1": + COLOR_FULL_SCALE = 4097 + WHITE_CAL = [726, 1478, 775] + BLACK_CAL = [385, 809, 411] + + min_saturation = 0.1 + balck_value = 0.05 + grey_value = 0.15 + light_grey_value = 0.17 + min_color_value = 0.1 + + hsv_limits = {"thresholds": [46, 46, 129, 150, 220, 235, 257], + "high_h_v_thre": [0.27, 0.83], + "high_h_s_thre": [0.64]} From 4848e1ea6309cd778843916217e5afe704e3f5f6 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 27 Jun 2025 13:13:46 +0200 Subject: [PATCH 2/5] fix: typo --- arduino_alvik/arduino_alvik.py | 2 +- arduino_alvik/constants.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 5c4558d..5c4bde8 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -1121,7 +1121,7 @@ def hsv2label(h, s, v) -> str: return 'UNDEFINED' if s < min_saturation: - if v < balck_value: + if v < black_value: label = 'BLACK' elif v < grey_value: label = 'GREY' diff --git a/arduino_alvik/constants.py b/arduino_alvik/constants.py index 9456365..3f89ca2 100644 --- a/arduino_alvik/constants.py +++ b/arduino_alvik/constants.py @@ -13,7 +13,7 @@ BLACK_CAL = [160, 200, 190] min_saturation = 0.1 - balck_value = 0.05 + black_value = 0.05 grey_value = 0.15 light_grey_value = 0.8 min_color_value = 0.1 @@ -28,7 +28,7 @@ BLACK_CAL = [385, 809, 411] min_saturation = 0.1 - balck_value = 0.05 + black_value = 0.05 grey_value = 0.15 light_grey_value = 0.17 min_color_value = 0.1 From 04ff81753bdca6ce057e4591f70d14681676e85c Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 27 Jun 2025 17:36:07 +0200 Subject: [PATCH 3/5] mod: color sensor params definition --- arduino_alvik/arduino_alvik.py | 38 ++++++++++++------ arduino_alvik/constants.py | 71 +++++++++++++++++++--------------- 2 files changed, 66 insertions(+), 43 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 5c4bde8..12e1690 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -110,6 +110,7 @@ def __init__(self, stack_size = THREAD_STACK_SIZE): self._blue = None self._white_cal = None self._black_cal = None + self._color_thresholds = hw0_color_thresholds self._left_line = None self._center_line = None self._right_line = None @@ -138,6 +139,7 @@ def __init__(self, stack_size = THREAD_STACK_SIZE): self._waiting_ack = None self._version = list(map(int, __version__.split('.'))) self._fw_version = [None, None, None] + self._hw_version = None self._required_fw_version = list(map(int, __required_firmware_version__.split('.'))) self._touch_events = _ArduinoAlvikTouchEvents() self._move_events = _ArduinoAlvikMoveEvents() @@ -807,6 +809,8 @@ def _parse_message(self) -> int: elif code == 0x7E: # firmware version _, *self._fw_version = self._packeter.unpacketC3B() + self._hw_version = self._fw_version[0] >> 4 + self._fw_version[0] &= 0x0F else: return -1 @@ -954,14 +958,21 @@ def _limit(value: float, lower: float, upper: float) -> float: return value def _set_color_reference(self): + + if self._hw_version == HW_REVISION_1_6: + self._color_thresholds = hw1_color_thresholds + # Insert other hardware revisions here + else: # fallback to HW_REVISION_1_3 + self._color_thresholds = hw0_color_thresholds + try: from color_calibration import BLACK_CAL as _B except ImportError: - _B = BLACK_CAL + _B = self._color_thresholds["BLACK_CAL"] try: from color_calibration import WHITE_CAL as _W except ImportError: - _W = WHITE_CAL + _W = self._color_thresholds["WHITE_CAL"] self._black_cal = _B self._white_cal = _W @@ -1105,32 +1116,35 @@ def get_color_label(self) -> str: Returns the label of the color as recognized by the sensor :return: """ - return self.hsv2label(*self.get_color(color_format='hsv')) + return self.hsv2label(*self.get_color(color_format='hsv'), self._color_thresholds) @staticmethod - def hsv2label(h, s, v) -> str: + def hsv2label(h, s, v, thresholds) -> str: """ Returns the color label corresponding to the given normalized HSV color input - :param h: - :param s: - :param v: + :param h: hue + :param s: saturation + :param v: value + :param thresholds: a dictionary with the color thresholds :return: """ if None in [h, s, v]: return 'UNDEFINED' - if s < min_saturation: - if v < black_value: + hsv_limits = thresholds["HSV_LIMITS"] + + if s < thresholds["MIN_SATURATION"]: + if v < thresholds["BLACK_VALUE"]: label = 'BLACK' - elif v < grey_value: + elif v < thresholds["GREY_VALUE"]: label = 'GREY' - elif v < light_grey_value: + elif v < thresholds["LIGHT_GREY_VALUE"]: label = 'LIGHT GREY' else: label = 'WHITE' else: - if v > min_color_value: + if v > thresholds["MIN_COLOR_VALUE"]: if hsv_limits["thresholds"][0] <= h < hsv_limits["thresholds"][1]: label = 'YELLOW' elif hsv_limits["thresholds"][1] <= h < hsv_limits["thresholds"][2]: diff --git a/arduino_alvik/constants.py b/arduino_alvik/constants.py index 3f89ca2..e98afd4 100644 --- a/arduino_alvik/constants.py +++ b/arduino_alvik/constants.py @@ -1,38 +1,47 @@ """ HW0: Alvik carrier board with APDS9960 HW1: Alvik carrier board with APDS9999 + +HW_REVISION_1_3 0 // apds9960 1.3 - 1.5 +HW_REVISION_1_6 1 // apds9999 1.6 + """ -USES_HARDWARE = "HW0" # Change to "HW1" for hardware version 1 +HW_REVISION_1_3 = HW0 = 0 +HW_REVISION_1_6 = HW1 = 1 +# *** HW_REVISION_1_3 DEFINITIONS # COLOR SENSOR - -if USES_HARDWARE == "HW0": - COLOR_FULL_SCALE = 4097 - WHITE_CAL = [450, 500, 510] - BLACK_CAL = [160, 200, 190] - - min_saturation = 0.1 - black_value = 0.05 - grey_value = 0.15 - light_grey_value = 0.8 - min_color_value = 0.1 - - hsv_limits = {"thresholds": [20, 90, 140, 170, 210, 250, 280], - "high_h_v_thre": [0.5, 0.77], - "high_h_s_thre": [0.45]} - -elif USES_HARDWARE == "HW1": - COLOR_FULL_SCALE = 4097 - WHITE_CAL = [726, 1478, 775] - BLACK_CAL = [385, 809, 411] - - min_saturation = 0.1 - black_value = 0.05 - grey_value = 0.15 - light_grey_value = 0.17 - min_color_value = 0.1 - - hsv_limits = {"thresholds": [46, 46, 129, 150, 220, 235, 257], - "high_h_v_thre": [0.27, 0.83], - "high_h_s_thre": [0.64]} +hw0_color_thresholds = { + "COLOR_FULL_SCALE": 4097, + "WHITE_CAL": [450, 500, 510], + "BLACK_CAL": [160, 200, 190], + + "MIN_SATURATION": 0.1, + "BLACK_VALUE": 0.05, + "GREY_VALUE": 0.15, + "LIGHT_GREY_VALUE": 0.8, + "MIN_COLOR_VALUE": 0.1, + + "HSV_LIMITS": {"thresholds": [20, 90, 140, 170, 210, 250, 280], + "high_h_v_thre": [0.5, 0.77], + "high_h_s_thre": [0.45]} +} + +# *** HW_REVISION_1_6 DEFINITIONS +# COLOR SENSOR +hw1_color_thresholds = { + "COLOR_FULL_SCALE": 4097, + "WHITE_CAL": [726, 1478, 775], + "BLACK_CAL": [385, 809, 411], + + "MIN_SATURATION": 0.1, + "BLACK_VALUE": 0.05, + "GREY_VALUE": 0.15, + "LIGHT_GREY_VALUE": 0.17, + "MIN_COLOR_VALUE": 0.1, + + "HSV_LIMITS": {"thresholds": [46, 46, 129, 150, 220, 235, 257], + "high_h_v_thre": [0.27, 0.83], + "high_h_s_thre": [0.64]} +} From 96bb7204e452caab5538c91824054c6b59d864b9 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Mon, 30 Jun 2025 10:15:56 +0200 Subject: [PATCH 4/5] feat: get hw version methods --- arduino_alvik/arduino_alvik.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 12e1690..ecc75a9 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -1208,6 +1208,8 @@ def get_version(self, version: str = 'fw') -> str: return self.get_fw_version() elif version == 'lib' or version == 'LIB': return self.get_lib_version() + elif version == 'hw' or version == 'HW' or version == 'hardware': + return self.get_hw_version() else: return f'{None, None, None}' @@ -1224,7 +1226,14 @@ def get_fw_version(self) -> str: :return: """ return f'{self._fw_version[0]}.{self._fw_version[1]}.{self._fw_version[2]}' - + + def get_hw_version(self) -> str: + """ + Returns the hardware version of the Alvik Carrier + :return: + """ + return f'{self._hw_version}' + def get_required_fw_version(self) -> str: """ Returns the required firmware version of the Alvik Carrier for this micropython library From c2126bf9597864372438b6498fa87810053e205f Mon Sep 17 00:00:00 2001 From: Giovanni Bruno Date: Mon, 30 Jun 2025 12:25:45 +0200 Subject: [PATCH 5/5] mod: first color calibration --- arduino_alvik/constants.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arduino_alvik/constants.py b/arduino_alvik/constants.py index e98afd4..fdc015b 100644 --- a/arduino_alvik/constants.py +++ b/arduino_alvik/constants.py @@ -41,7 +41,7 @@ "LIGHT_GREY_VALUE": 0.17, "MIN_COLOR_VALUE": 0.1, - "HSV_LIMITS": {"thresholds": [46, 46, 129, 150, 220, 235, 257], - "high_h_v_thre": [0.27, 0.83], + "HSV_LIMITS": {"thresholds": [46, 60, 135, 165, 220, 245, 257], + "high_h_v_thre": [0.5, 0.60], "high_h_s_thre": [0.64]} }