From b566a875985cb37936ad03a487ace26ccff799e1 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 29 Oct 2024 14:57:02 +0100 Subject: [PATCH 01/23] upd: ucPack 0.1.7 dependency --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2bc1cd9..52b08a2 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ ["arduino_alvik/stm32_flash.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/stm32_flash.py"] ], "deps": [ - ["github:arduino/ucPack-mpy", "0.1.6"], + ["github:arduino/ucPack-mpy", "0.1.7"], ["github:arduino/arduino-runtime-mpy", "0.4.0"] ], "version": "1.1.1" From 7c6d3fe8e2c4d611557b2f1c4e51c73c59e580fb Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 8 Nov 2024 11:38:01 +0100 Subject: [PATCH 02/23] feat: _wait_for_fw_check timeout on unavailable FW version --- arduino_alvik/arduino_alvik.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index d83305a..21144a4 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -254,13 +254,19 @@ def _wait_for_ack(self) -> None: sleep_ms(20) self._waiting_ack = None - def _wait_for_fw_check(self) -> bool: + def _wait_for_fw_check(self, timeout=5) -> bool: """ Waits until receives version from robot, check required version and return true if everything is ok + :param timeout: wait for fw timeout in seconds :return: """ + start = ticks_ms() while self._fw_version == [None, None, None]: sleep_ms(20) + if ticks_diff(ticks_ms(), start) < timeout * 1000: + print("Could not get FW version") + return False + if self.check_firmware_compatibility(): return True else: From e3ce2d5e3f3da0be32f05d32b28b662386c5d72d Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 8 Nov 2024 12:07:08 +0100 Subject: [PATCH 03/23] fix: AttributeError on multiple calls to ArduinoAlvik.stop --- arduino_alvik/arduino_alvik.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index d83305a..cfb4b74 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -1,5 +1,4 @@ import sys -import gc import struct from machine import I2C import _thread @@ -84,6 +83,14 @@ def __init__(self): self._move_events = _ArduinoAlvikMoveEvents() self._timer_events = _ArduinoAlvikTimerEvents(-1) + def __del__(self): + """ + This method is a stub. __del__ is not implemented in MicroPython (https://docs.micropython.org/en/latest/genrst/core_language.html#special-method-del-not-implemented-for-user-defined-classes) + :return: + """ + ... + # self.__class__._instance = None + @staticmethod def is_on() -> bool: """ @@ -377,10 +384,6 @@ def stop(self): # stop touch events thread self._stop_events_thread() - # delete _instance - del self.__class__._instance - gc.collect() - @staticmethod def _reset_hw(): """ From c019b05802b6a20e4481b52331b0c7088cc438a2 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 8 Nov 2024 16:26:26 +0100 Subject: [PATCH 04/23] fix: reimport all the modules before flashing a new FW --- examples/flash_firmware.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/flash_firmware.py b/examples/flash_firmware.py index a3327da..20bacdb 100644 --- a/examples/flash_firmware.py +++ b/examples/flash_firmware.py @@ -1,4 +1,19 @@ -# from machine import reset +import sys + + +def reload_modules(): + to_be_reloaded = [] + + for m in sys.modules: + to_be_reloaded.append(m) + del sys.modules[m] + + for m in to_be_reloaded: + print(f"Reloading {m}") + exec(f'import {m}') + + +reload_modules() from arduino_alvik import update_firmware # this is a patch to fix possible running threads on Alvik @@ -6,5 +21,4 @@ alvik = ArduinoAlvik() alvik.stop() -update_firmware('./firmware.bin') -# reset() \ No newline at end of file +update_firmware('/firmware.bin') From a74b84e8e2de3432a7d291a4ca655b22b9c1996f Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Mon, 18 Nov 2024 18:16:07 +0100 Subject: [PATCH 05/23] fix: ALVIKDEV-108 UART read extremely slow on bytewise access --- arduino_alvik/arduino_alvik.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index d83305a..4ec339e 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -272,8 +272,7 @@ def _flush_uart(): Empties the UART buffer :return: """ - while uart.any(): - uart.read(1) + uart.read(uart.any()) def _begin_update_thread(self): """ @@ -641,21 +640,22 @@ def _update(self, delay_=1): self.set_behaviour(2) if not ArduinoAlvik._update_thread_running: break - if self._read_message(): - self._parse_message() + self._read_message() sleep_ms(delay_) - def _read_message(self) -> bool: + def _read_message(self) -> None: """ Read a message from the uC :return: True if a message terminator was reached """ - while uart.any(): - b = uart.read(1)[0] - self._packeter.buffer.push(b) - if b == self._packeter.end_index and self._packeter.checkPayload(): - return True - return False + buf = bytearray(uart.any()) + uart.readinto(buf) + if len(buf): + uart.readinto(buf) + for b in buf: + self._packeter.buffer.push(b) + if b == self._packeter.end_index and self._packeter.checkPayload(): + self._parse_message() def _parse_message(self) -> int: """ From 6a69b1fdcd9f2927a61f2445e6bf95079f067596 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 19 Nov 2024 10:59:53 +0100 Subject: [PATCH 06/23] ver: 1.1.2 ready --- arduino_alvik/__init__.py | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arduino_alvik/__init__.py b/arduino_alvik/__init__.py index a5353de..dbdfc6a 100644 --- a/arduino_alvik/__init__.py +++ b/arduino_alvik/__init__.py @@ -4,7 +4,7 @@ __author__ = "Lucio Rossi , Giovanni Bruno " __license__ = "MPL 2.0" -__version__ = "1.1.1" +__version__ = "1.1.2" __maintainer__ = "Lucio Rossi , Giovanni Bruno " __required_firmware_version__ = "1.1.0" diff --git a/package.json b/package.json index 52b08a2..134ac1b 100644 --- a/package.json +++ b/package.json @@ -13,5 +13,5 @@ ["github:arduino/ucPack-mpy", "0.1.7"], ["github:arduino/arduino-runtime-mpy", "0.4.0"] ], - "version": "1.1.1" + "version": "1.1.2" } From 04d84c3394b1d9eed60f1dbdee1c38470be03738 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 19 Nov 2024 12:52:17 +0100 Subject: [PATCH 07/23] mod: remove print reimport libs --- examples/flash_firmware.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/flash_firmware.py b/examples/flash_firmware.py index 20bacdb..b98fba8 100644 --- a/examples/flash_firmware.py +++ b/examples/flash_firmware.py @@ -9,7 +9,6 @@ def reload_modules(): del sys.modules[m] for m in to_be_reloaded: - print(f"Reloading {m}") exec(f'import {m}') From 0c0bdfb099e16c73a22836ef092943109d08e092 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 19 Nov 2024 17:20:28 +0100 Subject: [PATCH 08/23] fix: _wait_for_fw_check wrong termination condition --- arduino_alvik/arduino_alvik.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 4b8f8a6..0aade44 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -270,7 +270,7 @@ def _wait_for_fw_check(self, timeout=5) -> bool: start = ticks_ms() while self._fw_version == [None, None, None]: sleep_ms(20) - if ticks_diff(ticks_ms(), start) < timeout * 1000: + if ticks_diff(ticks_ms(), start) > timeout * 1000: print("Could not get FW version") return False From 6c0f640f4e8a4b7cf3249640cd412579b723fd1a Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 20 Nov 2024 10:19:19 +0100 Subject: [PATCH 09/23] mod: rem sys module dependency on examples (straightforward) --- examples/actuators/leds_setting.py | 4 ++-- examples/actuators/move_wheels.py | 4 ++-- examples/actuators/set_servo.py | 4 ++-- examples/actuators/wheels_position.py | 4 ++-- examples/actuators/wheels_speed.py | 4 ++-- examples/communication/i2c_scan.py | 4 ++-- examples/demo/demo.py | 4 ++-- examples/demo/hand_follower.py | 14 +++++++------- examples/demo/line_follower.py | 13 ++++++------- examples/events/motion_events.py | 3 +-- examples/events/timer_one_shot_events.py | 3 +-- examples/events/timer_periodic_events.py | 3 +-- examples/events/touch_events.py | 3 +-- examples/sensors/read_color_sensor.py | 4 ++-- examples/sensors/read_imu.py | 4 ++-- examples/sensors/read_orientation.py | 4 ++-- examples/sensors/read_tof.py | 4 ++-- examples/sensors/read_touch.py | 4 ++-- examples/tests/message_reader.py | 4 ++-- examples/tests/test_idle.py | 4 ++-- examples/tests/test_version.py | 4 ++-- 21 files changed, 47 insertions(+), 52 deletions(-) diff --git a/examples/actuators/leds_setting.py b/examples/actuators/leds_setting.py index cceb6c4..3ea1020 100644 --- a/examples/actuators/leds_setting.py +++ b/examples/actuators/leds_setting.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -34,4 +34,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/actuators/move_wheels.py b/examples/actuators/move_wheels.py index d6f6e7e..94537a3 100644 --- a/examples/actuators/move_wheels.py +++ b/examples/actuators/move_wheels.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -33,4 +33,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/actuators/set_servo.py b/examples/actuators/set_servo.py index b4e9084..48720c1 100644 --- a/examples/actuators/set_servo.py +++ b/examples/actuators/set_servo.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -19,4 +19,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/actuators/wheels_position.py b/examples/actuators/wheels_position.py index 6f3a151..fc521c6 100644 --- a/examples/actuators/wheels_position.py +++ b/examples/actuators/wheels_position.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep, sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -62,4 +62,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/actuators/wheels_speed.py b/examples/actuators/wheels_speed.py index 1919f27..3a54340 100644 --- a/examples/actuators/wheels_speed.py +++ b/examples/actuators/wheels_speed.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -21,4 +21,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/communication/i2c_scan.py b/examples/communication/i2c_scan.py index a246787..6f1ed45 100644 --- a/examples/communication/i2c_scan.py +++ b/examples/communication/i2c_scan.py @@ -1,5 +1,5 @@ from time import sleep_ms -import sys + from arduino_alvik import ArduinoAlvik @@ -22,4 +22,4 @@ sleep_ms(100) except KeyboardInterrupt as e: alvik.stop() - sys.exit() \ No newline at end of file + break \ No newline at end of file diff --git a/examples/demo/demo.py b/examples/demo/demo.py index 931aa47..99039ff 100644 --- a/examples/demo/demo.py +++ b/examples/demo/demo.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -50,4 +50,4 @@ def update_led_status(val): except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/demo/hand_follower.py b/examples/demo/hand_follower.py index fcdc41e..b4289f5 100644 --- a/examples/demo/hand_follower.py +++ b/examples/demo/hand_follower.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -16,8 +16,8 @@ while not alvik.get_touch_ok(): sleep_ms(50) -try: - while True: +while True: + try: while not alvik.get_touch_cancel(): alvik.left_led.set_color(0, 0, 0) alvik.right_led.set_color(0, 0, 0) @@ -32,7 +32,7 @@ alvik.right_led.set_color(0, 1, 0) alvik.brake() sleep_ms(100) -except KeyboardInterrupt as e: - print('over') - alvik.stop() - sys.exit() + except KeyboardInterrupt as e: + print('over') + alvik.stop() + break diff --git a/examples/demo/line_follower.py b/examples/demo/line_follower.py index 041faf6..36bbb96 100644 --- a/examples/demo/line_follower.py +++ b/examples/demo/line_follower.py @@ -1,6 +1,5 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys def calculate_center(left: int, center: int, right: int): @@ -29,8 +28,8 @@ def calculate_center(left: int, center: int, right: int): while not alvik.get_touch_ok(): sleep_ms(50) -try: - while True: +while True: + try: while not alvik.get_touch_cancel(): line_sensors = alvik.get_line_sensors() @@ -58,7 +57,7 @@ def calculate_center(left: int, center: int, right: int): alvik.brake() sleep_ms(100) -except KeyboardInterrupt as e: - print('over') - alvik.stop() - sys.exit() + except KeyboardInterrupt as e: + print('over') + alvik.stop() + break diff --git a/examples/events/motion_events.py b/examples/events/motion_events.py index f7edf01..b0f8146 100644 --- a/examples/events/motion_events.py +++ b/examples/events/motion_events.py @@ -1,6 +1,5 @@ from arduino_alvik import ArduinoAlvik from time import sleep -import sys def toggle_value(): @@ -49,4 +48,4 @@ def simple_print(custom_text: str = '') -> None: except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/events/timer_one_shot_events.py b/examples/events/timer_one_shot_events.py index 8ab8a99..77856b8 100644 --- a/examples/events/timer_one_shot_events.py +++ b/examples/events/timer_one_shot_events.py @@ -1,6 +1,5 @@ from arduino_alvik import ArduinoAlvik from time import sleep -import sys def toggle_value(): @@ -68,4 +67,4 @@ def toggle_left_led(custom_text: str, val) -> None: except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/events/timer_periodic_events.py b/examples/events/timer_periodic_events.py index fd5aa28..d56e90c 100644 --- a/examples/events/timer_periodic_events.py +++ b/examples/events/timer_periodic_events.py @@ -1,6 +1,5 @@ from arduino_alvik import ArduinoAlvik from time import sleep -import sys def toggle_value(): @@ -67,4 +66,4 @@ def toggle_left_led(custom_text: str, val) -> None: except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/events/touch_events.py b/examples/events/touch_events.py index 9ef35e3..db0d6c1 100644 --- a/examples/events/touch_events.py +++ b/examples/events/touch_events.py @@ -1,6 +1,5 @@ from arduino_alvik import ArduinoAlvik from time import sleep -import sys def toggle_value(): @@ -67,4 +66,4 @@ def simple_print(custom_text: str = '') -> None: except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/sensors/read_color_sensor.py b/examples/sensors/read_color_sensor.py index 462826c..0c92871 100644 --- a/examples/sensors/read_color_sensor.py +++ b/examples/sensors/read_color_sensor.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -15,4 +15,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/sensors/read_imu.py b/examples/sensors/read_imu.py index 3c6589b..7c0d984 100644 --- a/examples/sensors/read_imu.py +++ b/examples/sensors/read_imu.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -14,4 +14,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/sensors/read_orientation.py b/examples/sensors/read_orientation.py index 23cf006..33b0113 100644 --- a/examples/sensors/read_orientation.py +++ b/examples/sensors/read_orientation.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -13,4 +13,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/sensors/read_tof.py b/examples/sensors/read_tof.py index 990bf63..20d37f0 100644 --- a/examples/sensors/read_tof.py +++ b/examples/sensors/read_tof.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -15,4 +15,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/sensors/read_touch.py b/examples/sensors/read_touch.py index 21c5ebb..6a627df 100644 --- a/examples/sensors/read_touch.py +++ b/examples/sensors/read_touch.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -27,4 +27,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/tests/message_reader.py b/examples/tests/message_reader.py index 7ae24e1..d2eb283 100644 --- a/examples/tests/message_reader.py +++ b/examples/tests/message_reader.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -25,5 +25,5 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/tests/test_idle.py b/examples/tests/test_idle.py index f65625a..1c26a97 100644 --- a/examples/tests/test_idle.py +++ b/examples/tests/test_idle.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -19,5 +19,5 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/tests/test_version.py b/examples/tests/test_version.py index 1682316..52c0660 100644 --- a/examples/tests/test_version.py +++ b/examples/tests/test_version.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() @@ -16,4 +16,4 @@ except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() \ No newline at end of file + break \ No newline at end of file From 24c7ae654fe0ab11b2bd8a5777d4aebbd6b81807 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 20 Nov 2024 10:20:04 +0100 Subject: [PATCH 10/23] mod: rem sys module dependency on examples (to be tested) --- examples/actuators/pose_example.py | 6 +++--- examples/communication/modulino.py | 4 +++- examples/demo/touch_move.py | 15 ++++++++------- examples/tests/test_meas_units.py | 6 +++--- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/examples/actuators/pose_example.py b/examples/actuators/pose_example.py index 11b675a..c0833a0 100644 --- a/examples/actuators/pose_example.py +++ b/examples/actuators/pose_example.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -74,9 +74,9 @@ sleep_ms(500) alvik.stop() - sys.exit() + break except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break diff --git a/examples/communication/modulino.py b/examples/communication/modulino.py index bf1e8e2..6398c14 100644 --- a/examples/communication/modulino.py +++ b/examples/communication/modulino.py @@ -1,6 +1,8 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms import sys + + try: from modulino import ModulinoPixels except ImportError as e: @@ -32,4 +34,4 @@ except KeyboardInterrupt as e: alvik.stop() - sys.exit() + break diff --git a/examples/demo/touch_move.py b/examples/demo/touch_move.py index b51e4ca..2f9273e 100644 --- a/examples/demo/touch_move.py +++ b/examples/demo/touch_move.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -70,6 +70,7 @@ def run_movement(movement): alvik.right_led.set_color(0, 0, 0) sleep_ms(100) + while alvik.get_touch_ok(): sleep_ms(50) @@ -77,8 +78,8 @@ def run_movement(movement): add_movement() sleep_ms(50) -try: - while True: +while True: + try: alvik.left_led.set_color(0, 0, 0) alvik.right_led.set_color(0, 0, 0) for move in movements: @@ -94,9 +95,9 @@ def run_movement(movement): alvik.brake() add_movement() sleep_ms(100) -except KeyboardInterrupt as e: - print('over') - alvik.stop() - sys.exit() + except KeyboardInterrupt as e: + print('over') + alvik.stop() + break diff --git a/examples/tests/test_meas_units.py b/examples/tests/test_meas_units.py index 468073d..67018e6 100644 --- a/examples/tests/test_meas_units.py +++ b/examples/tests/test_meas_units.py @@ -1,6 +1,6 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys + alvik = ArduinoAlvik() alvik.begin() @@ -116,9 +116,9 @@ print(f"Current speed is {alvik.get_drive_speed()} (mm/s) (rpm)") alvik.stop() - sys.exit() + break except KeyboardInterrupt as e: print('over') alvik.stop() - sys.exit() + break From a213f795ab0b38ae4cae9a8855369f6046903364 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 20 Nov 2024 11:51:35 +0100 Subject: [PATCH 11/23] mod: one-shot examples don't need while loops --- examples/actuators/pose_example.py | 124 ++++++++------- examples/tests/test_meas_units.py | 232 ++++++++++++++--------------- 2 files changed, 176 insertions(+), 180 deletions(-) diff --git a/examples/actuators/pose_example.py b/examples/actuators/pose_example.py index c0833a0..465a635 100644 --- a/examples/actuators/pose_example.py +++ b/examples/actuators/pose_example.py @@ -5,78 +5,76 @@ alvik = ArduinoAlvik() alvik.begin() -while True: - try: +try: - alvik.move(100.0, 'mm') - print("on target after move") + alvik.move(100.0, 'mm') + print("on target after move") - alvik.move(50.0, 'mm') - print("on target after move") + alvik.move(50.0, 'mm') + print("on target after move") - alvik.rotate(90.0, 'deg') - print("on target after rotation") + alvik.rotate(90.0, 'deg') + print("on target after rotation") - alvik.rotate(-45.00, 'deg') - print("on target after rotation") + alvik.rotate(-45.00, 'deg') + print("on target after rotation") - x, y, theta = alvik.get_pose() - print(f'Current pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}') + x, y, theta = alvik.get_pose() + print(f'Current pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}') - alvik.reset_pose(0, 0, 0) + alvik.reset_pose(0, 0, 0) - x, y, theta = alvik.get_pose() - print(f'Updated pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}') + x, y, theta = alvik.get_pose() + print(f'Updated pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}') + sleep_ms(500) + + print("___________NON-BLOCKING__________________") + + alvik.move(50.0, 'mm', blocking=False) + + while not alvik.is_target_reached(): + alvik.left_led.set_color(1, 0, 0) + sleep_ms(500) + alvik.left_led.set_color(0, 0, 0) + sleep_ms(500) + print("on target after move") + + alvik.rotate(45.0, 'deg', blocking=False) + while not alvik.is_target_reached(): + alvik.left_led.set_color(1, 0, 0) + sleep_ms(500) + alvik.left_led.set_color(0, 0, 0) sleep_ms(500) + print("on target after rotation") - print("___________NON-BLOCKING__________________") - - alvik.move(50.0, 'mm', blocking=False) - - while not alvik.is_target_reached(): - alvik.left_led.set_color(1, 0, 0) - sleep_ms(500) - alvik.left_led.set_color(0, 0, 0) - sleep_ms(500) - print("on target after move") - - alvik.rotate(45.0, 'deg', blocking=False) - while not alvik.is_target_reached(): - alvik.left_led.set_color(1, 0, 0) - sleep_ms(500) - alvik.left_led.set_color(0, 0, 0) - sleep_ms(500) - print("on target after rotation") - - alvik.move(100.0, 'mm', blocking=False) - while not alvik.is_target_reached(): - alvik.left_led.set_color(1, 0, 0) - sleep_ms(500) - alvik.left_led.set_color(0, 0, 0) - sleep_ms(500) - print("on target after move") - - alvik.rotate(-90.00, 'deg', blocking=False) - while not alvik.is_target_reached(): - alvik.left_led.set_color(1, 0, 0) - sleep_ms(500) - alvik.left_led.set_color(0, 0, 0) - sleep_ms(500) - print("on target after rotation") - - x, y, theta = alvik.get_pose() - print(f'Current pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}') - - alvik.reset_pose(0, 0, 0) - - x, y, theta = alvik.get_pose() - print(f'Updated pose is x={x}, y={y}, theta(deg)={theta}') + alvik.move(100.0, 'mm', blocking=False) + while not alvik.is_target_reached(): + alvik.left_led.set_color(1, 0, 0) sleep_ms(500) + alvik.left_led.set_color(0, 0, 0) + sleep_ms(500) + print("on target after move") + + alvik.rotate(-90.00, 'deg', blocking=False) + while not alvik.is_target_reached(): + alvik.left_led.set_color(1, 0, 0) + sleep_ms(500) + alvik.left_led.set_color(0, 0, 0) + sleep_ms(500) + print("on target after rotation") + + x, y, theta = alvik.get_pose() + print(f'Current pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}') + + alvik.reset_pose(0, 0, 0) + + x, y, theta = alvik.get_pose() + print(f'Updated pose is x={x}, y={y}, theta(deg)={theta}') + sleep_ms(500) - alvik.stop() - break +except KeyboardInterrupt as e: + print('Test interrupted') - except KeyboardInterrupt as e: - print('over') - alvik.stop() - break +finally: + alvik.stop() + print("END of pose example") \ No newline at end of file diff --git a/examples/tests/test_meas_units.py b/examples/tests/test_meas_units.py index 67018e6..3938a56 100644 --- a/examples/tests/test_meas_units.py +++ b/examples/tests/test_meas_units.py @@ -5,120 +5,118 @@ alvik = ArduinoAlvik() alvik.begin() -while True: - try: - - # -- LINEAR MOVEMENTS -- - - print("Move fw 0.05 m") - alvik.move(0.05, unit='m') - sleep_ms(2000) - - print("Move fw 10 cm") - alvik.move(5, unit='cm') - sleep_ms(2000) - - print("Move bw 100 mm") - alvik.move(-100, unit='mm') - sleep_ms(2000) - - print("Move fw 1 inch") - alvik.move(1, unit='in') - sleep_ms(2000) - - print(f"Current position: {alvik.get_pose()}") - alvik.reset_pose(0, 0, theta=3.1415, angle_unit='rad') - sleep_ms(2000) - - print(f"Current position: {alvik.get_pose()}") - - # -- WHEEL ROTATIONS -- - alvik.right_wheel.reset() - sleep_ms(2000) - curr_pos = alvik.right_wheel.get_position() - print(f'R wheel pos: {curr_pos}') - sleep_ms(2000) - - print("Rotate right wheel 25% fw") - alvik.right_wheel.set_position(25, unit='%') - sleep_ms(2000) - curr_pos = alvik.right_wheel.get_position() - print(f'R wheel pos: {curr_pos}') - - print("Rotate right wheel 90 deg bw") - alvik.right_wheel.set_position(-90, unit='deg') - sleep_ms(2000) - curr_pos = alvik.right_wheel.get_position() - print(f'R wheel pos: {curr_pos}') - - print("Rotate right wheel pi rad fw") - alvik.right_wheel.set_position(3.14, unit='rad') - sleep_ms(2000) - curr_pos = alvik.right_wheel.get_position() - print(f'R wheel pos: {curr_pos}') - - print("Rotate right wheel a quarter revolution bw") - alvik.right_wheel.set_position(-0.25, unit='rev') - sleep_ms(2000) - curr_pos = alvik.right_wheel.get_position() - print(f'R wheel pos: {curr_pos}') - - # -- WHEELS SPEED -- - print("Set speed 50% max_rpm (35.0 rpm)") - alvik.set_wheels_speed(50, 50, '%') - sleep_ms(1000) - print(f"Current speed is {alvik.get_wheels_speed()} rpm") - - print("Set speed 12 rpm (1 rev in 5 sec)") - alvik.set_wheels_speed(12, 12, 'rpm') - sleep_ms(1000) - print(f"Current speed is {alvik.get_wheels_speed()} rpm") - - print("Set speed -pi rad/s (1 back rev in 2 sec)") - alvik.set_wheels_speed(-3.1415, -3.1415, 'rad/s') - sleep_ms(1000) - print(f"Current speed is {alvik.get_wheels_speed()} rpm") - - print("Set speed 180 deg/s (1 back rev in 2 sec)") - alvik.set_wheels_speed(180, 180, 'deg/s') - sleep_ms(1000) - print(f"Current speed is {alvik.get_wheels_speed()} rpm") - - # -- DRIVE -- - print("Driving at 10 mm/s (expecting approx 5.6 rpm 64 deg/s)") - alvik.drive(10, 20, linear_unit='mm/s', angular_unit='%') - sleep_ms(2000) - print(f"Current speed is {alvik.get_drive_speed()} (mm/s, deg/s))") - - print("Driving at 10 mm/s (expecting approx 5.6 rpm)") - alvik.drive(10, 0, linear_unit='mm/s') - sleep_ms(2000) - print(f"Current speed is {alvik.get_wheels_speed()} rpm") - - print("Driving at 2 cm/s (expecting approx 11.2 rpm)") - alvik.drive(2, 0, linear_unit='cm/s') - sleep_ms(2000) - print(f"Current speed is {alvik.get_wheels_speed()} rpm") - - print("Driving at 1 in/s (expecting approx 14 rpm)") - alvik.drive(1, 0, linear_unit='in/s') - sleep_ms(2000) - print(f"Current speed is {alvik.get_wheels_speed()} rpm") - - print("Driving at 5 mm/s (expecting approx 5.6 rpm) pi/8 rad/s (22.5 deg/s)") - alvik.drive(5, 3.1415/8, linear_unit='mm/s', angular_unit='rad/s') - sleep_ms(2000) - print(f"Current speed is {alvik.get_drive_speed()} (mm/s) (rpm)") - - print("Driving at 5 mm/s (expecting approx 5.6 rpm) 1/8 rev/s (45 deg/s)") - alvik.drive(5, 1/8, linear_unit='mm/s', angular_unit='rev/s') - sleep_ms(2000) - print(f"Current speed is {alvik.get_drive_speed()} (mm/s) (rpm)") - - alvik.stop() - break - - except KeyboardInterrupt as e: - print('over') - alvik.stop() - break +try: + + # -- LINEAR MOVEMENTS -- + + print("Move fw 0.05 m") + alvik.move(0.05, unit='m') + sleep_ms(2000) + + print("Move fw 10 cm") + alvik.move(5, unit='cm') + sleep_ms(2000) + + print("Move bw 100 mm") + alvik.move(-100, unit='mm') + sleep_ms(2000) + + print("Move fw 1 inch") + alvik.move(1, unit='in') + sleep_ms(2000) + + print(f"Current position: {alvik.get_pose()}") + alvik.reset_pose(0, 0, theta=3.1415, angle_unit='rad') + sleep_ms(2000) + + print(f"Current position: {alvik.get_pose()}") + + # -- WHEEL ROTATIONS -- + alvik.right_wheel.reset() + sleep_ms(2000) + curr_pos = alvik.right_wheel.get_position() + print(f'R wheel pos: {curr_pos}') + sleep_ms(2000) + + print("Rotate right wheel 25% fw") + alvik.right_wheel.set_position(25, unit='%') + sleep_ms(2000) + curr_pos = alvik.right_wheel.get_position() + print(f'R wheel pos: {curr_pos}') + + print("Rotate right wheel 90 deg bw") + alvik.right_wheel.set_position(-90, unit='deg') + sleep_ms(2000) + curr_pos = alvik.right_wheel.get_position() + print(f'R wheel pos: {curr_pos}') + + print("Rotate right wheel pi rad fw") + alvik.right_wheel.set_position(3.14, unit='rad') + sleep_ms(2000) + curr_pos = alvik.right_wheel.get_position() + print(f'R wheel pos: {curr_pos}') + + print("Rotate right wheel a quarter revolution bw") + alvik.right_wheel.set_position(-0.25, unit='rev') + sleep_ms(2000) + curr_pos = alvik.right_wheel.get_position() + print(f'R wheel pos: {curr_pos}') + + # -- WHEELS SPEED -- + print("Set speed 50% max_rpm (35.0 rpm)") + alvik.set_wheels_speed(50, 50, '%') + sleep_ms(1000) + print(f"Current speed is {alvik.get_wheels_speed()} rpm") + + print("Set speed 12 rpm (1 rev in 5 sec)") + alvik.set_wheels_speed(12, 12, 'rpm') + sleep_ms(1000) + print(f"Current speed is {alvik.get_wheels_speed()} rpm") + + print("Set speed -pi rad/s (1 back rev in 2 sec)") + alvik.set_wheels_speed(-3.1415, -3.1415, 'rad/s') + sleep_ms(1000) + print(f"Current speed is {alvik.get_wheels_speed()} rpm") + + print("Set speed 180 deg/s (1 back rev in 2 sec)") + alvik.set_wheels_speed(180, 180, 'deg/s') + sleep_ms(1000) + print(f"Current speed is {alvik.get_wheels_speed()} rpm") + + # -- DRIVE -- + print("Driving at 10 mm/s (expecting approx 5.6 rpm 64 deg/s)") + alvik.drive(10, 20, linear_unit='mm/s', angular_unit='%') + sleep_ms(2000) + print(f"Current speed is {alvik.get_drive_speed()} (mm/s, deg/s))") + + print("Driving at 10 mm/s (expecting approx 5.6 rpm)") + alvik.drive(10, 0, linear_unit='mm/s') + sleep_ms(2000) + print(f"Current speed is {alvik.get_wheels_speed()} rpm") + + print("Driving at 2 cm/s (expecting approx 11.2 rpm)") + alvik.drive(2, 0, linear_unit='cm/s') + sleep_ms(2000) + print(f"Current speed is {alvik.get_wheels_speed()} rpm") + + print("Driving at 1 in/s (expecting approx 14 rpm)") + alvik.drive(1, 0, linear_unit='in/s') + sleep_ms(2000) + print(f"Current speed is {alvik.get_wheels_speed()} rpm") + + print("Driving at 5 mm/s (expecting approx 5.6 rpm) pi/8 rad/s (22.5 deg/s)") + alvik.drive(5, 3.1415/8, linear_unit='mm/s', angular_unit='rad/s') + sleep_ms(2000) + print(f"Current speed is {alvik.get_drive_speed()} (mm/s) (rpm)") + + print("Driving at 5 mm/s (expecting approx 5.6 rpm) 1/8 rev/s (45 deg/s)") + alvik.drive(5, 1/8, linear_unit='mm/s', angular_unit='rev/s') + sleep_ms(2000) + print(f"Current speed is {alvik.get_drive_speed()} (mm/s) (rpm)") + +except KeyboardInterrupt as e: + print('Test interrupted') + +finally: + alvik.stop() + print('END of measurement units test') From a508f3d63441c49f76db4a1e42b00d893ac04fac Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 20 Nov 2024 15:10:19 +0100 Subject: [PATCH 12/23] mod: timer_one_shot improvement revamp demo examples --- examples/demo/demo.py | 14 +++- examples/demo/hand_follower.py | 73 +++++++++-------- examples/demo/line_follower.py | 100 ++++++++++++----------- examples/demo/touch_move.py | 66 +++++++-------- examples/events/timer_one_shot_events.py | 20 +---- 5 files changed, 135 insertions(+), 138 deletions(-) diff --git a/examples/demo/demo.py b/examples/demo/demo.py index 99039ff..fb6baa6 100644 --- a/examples/demo/demo.py +++ b/examples/demo/demo.py @@ -1,6 +1,10 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms +from line_follower import run_line_follower +from touch_move import run_touch_move +from hand_follower import run_hand_follower + alvik = ArduinoAlvik() alvik.begin() @@ -28,11 +32,15 @@ def update_led_status(val): if alvik.get_touch_ok(): if menu_status == 0: - import line_follower + while not alvik.get_touch_cancel(): + run_line_follower(alvik) elif menu_status == 1: - import hand_follower + while not alvik.get_touch_cancel(): + run_hand_follower(alvik) elif menu_status == -1: - import touch_move + while not alvik.get_touch_cancel(): + run_touch_move(alvik) + alvik.brake() if alvik.get_touch_up() and menu_status < 1: menu_status += 1 diff --git a/examples/demo/hand_follower.py b/examples/demo/hand_follower.py index b4289f5..1b784b5 100644 --- a/examples/demo/hand_follower.py +++ b/examples/demo/hand_follower.py @@ -2,37 +2,42 @@ from time import sleep_ms -alvik = ArduinoAlvik() -alvik.begin() - -reference = 10.0 - -alvik.left_led.set_color(0, 1, 0) -alvik.right_led.set_color(0, 1, 0) - -while alvik.get_touch_ok(): - sleep_ms(50) - -while not alvik.get_touch_ok(): - sleep_ms(50) - -while True: - try: - while not alvik.get_touch_cancel(): - alvik.left_led.set_color(0, 0, 0) - alvik.right_led.set_color(0, 0, 0) - L, CL, C, CR, R = alvik.get_distance() - print(f'C: {C}') - error = C - reference - alvik.set_wheels_speed(error*10, error*10) - sleep_ms(100) - - while not alvik.get_touch_ok(): - alvik.left_led.set_color(0, 1, 0) - alvik.right_led.set_color(0, 1, 0) - alvik.brake() - sleep_ms(100) - except KeyboardInterrupt as e: - print('over') - alvik.stop() - break +def run_hand_follower(alvik): + reference = 10.0 + alvik.left_led.set_color(0, 0, 0) + alvik.right_led.set_color(0, 0, 0) + L, CL, C, CR, R = alvik.get_distance() + print(f'C: {C}') + error = C - reference + alvik.set_wheels_speed(error * 10, error * 10) + sleep_ms(100) + + +if __name__ == "__main__": + + alvik = ArduinoAlvik() + alvik.begin() + + alvik.left_led.set_color(0, 1, 0) + alvik.right_led.set_color(0, 1, 0) + + while alvik.get_touch_ok(): + sleep_ms(50) + + while not alvik.get_touch_ok(): + sleep_ms(50) + + while True: + try: + while not alvik.get_touch_cancel(): + run_hand_follower(alvik) + + while not alvik.get_touch_ok(): + alvik.left_led.set_color(0, 1, 0) + alvik.right_led.set_color(0, 1, 0) + alvik.brake() + sleep_ms(100) + except KeyboardInterrupt as e: + print('over') + alvik.stop() + break diff --git a/examples/demo/line_follower.py b/examples/demo/line_follower.py index 36bbb96..c61f3c4 100644 --- a/examples/demo/line_follower.py +++ b/examples/demo/line_follower.py @@ -12,52 +12,54 @@ def calculate_center(left: int, center: int, right: int): return centroid -alvik = ArduinoAlvik() -alvik.begin() - -error = 0 -control = 0 -kp = 50.0 - -alvik.left_led.set_color(0, 0, 1) -alvik.right_led.set_color(0, 0, 1) - -while alvik.get_touch_ok(): - sleep_ms(50) - -while not alvik.get_touch_ok(): - sleep_ms(50) - -while True: - try: - while not alvik.get_touch_cancel(): - - line_sensors = alvik.get_line_sensors() - print(f' {line_sensors}') - - error = calculate_center(*line_sensors) - control = error * kp - - if control > 0.2: - alvik.left_led.set_color(1, 0, 0) - alvik.right_led.set_color(0, 0, 0) - elif control < -0.2: - alvik.left_led.set_color(1, 0, 0) - alvik.right_led.set_color(0, 0, 0) - else: - alvik.left_led.set_color(0, 1, 0) - alvik.right_led.set_color(0, 1, 0) - - alvik.set_wheels_speed(30 - control, 30 + control) - sleep_ms(100) - - while not alvik.get_touch_ok(): - alvik.left_led.set_color(0, 0, 1) - alvik.right_led.set_color(0, 0, 1) - alvik.brake() - sleep_ms(100) - - except KeyboardInterrupt as e: - print('over') - alvik.stop() - break +def run_line_follower(alvik): + kp = 50.0 + line_sensors = alvik.get_line_sensors() + print(f' {line_sensors}') + + error = calculate_center(*line_sensors) + control = error * kp + + if control > 0.2: + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(0, 0, 0) + elif control < -0.2: + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(0, 0, 0) + else: + alvik.left_led.set_color(0, 1, 0) + alvik.right_led.set_color(0, 1, 0) + + alvik.set_wheels_speed(30 - control, 30 + control) + sleep_ms(100) + + +if __name__ == "__main__": + + alvik = ArduinoAlvik() + alvik.begin() + + alvik.left_led.set_color(0, 0, 1) + alvik.right_led.set_color(0, 0, 1) + + while alvik.get_touch_ok(): + sleep_ms(50) + + while not alvik.get_touch_ok(): + sleep_ms(50) + + while True: + try: + while not alvik.get_touch_cancel(): + run_line_follower(alvik) + + while not alvik.get_touch_ok(): + alvik.left_led.set_color(0, 0, 1) + alvik.right_led.set_color(0, 0, 1) + alvik.brake() + sleep_ms(100) + + except KeyboardInterrupt as e: + print('over') + alvik.stop() + break diff --git a/examples/demo/touch_move.py b/examples/demo/touch_move.py index 2f9273e..1ffbd1d 100644 --- a/examples/demo/touch_move.py +++ b/examples/demo/touch_move.py @@ -2,15 +2,6 @@ from time import sleep_ms -alvik = ArduinoAlvik() -alvik.begin() - -alvik.left_led.set_color(1, 0, 0) -alvik.right_led.set_color(1, 0, 0) - -movements = [] - - def blink(): alvik.left_led.set_color(1, 0, 1) alvik.right_led.set_color(1, 0, 1) @@ -19,9 +10,7 @@ def blink(): alvik.right_led.set_color(1, 0, 0) -def add_movement(): - global movements - +def add_movement(movements): if alvik.get_touch_up(): movements.append('forward') blink() @@ -71,33 +60,38 @@ def run_movement(movement): sleep_ms(100) -while alvik.get_touch_ok(): - sleep_ms(50) +def run_touch_move(alvik): + movements = [] + while not (alvik.get_touch_ok() and len(movements) != 0): + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(1, 0, 0) + alvik.brake() + add_movement(movements) + sleep_ms(100) -while not (alvik.get_touch_ok() and len(movements) != 0): - add_movement() - sleep_ms(50) + alvik.left_led.set_color(0, 0, 0) + alvik.right_led.set_color(0, 0, 0) + for move in movements: + run_movement(move) + if alvik.get_touch_cancel(): + break -while True: - try: - alvik.left_led.set_color(0, 0, 0) - alvik.right_led.set_color(0, 0, 0) - for move in movements: - run_movement(move) - if alvik.get_touch_cancel(): - break - movements = [] +if __name__ == "__main__": + alvik = ArduinoAlvik() + alvik.begin() - while not (alvik.get_touch_ok() and len(movements) != 0): - alvik.left_led.set_color(1, 0, 0) - alvik.right_led.set_color(1, 0, 0) - alvik.brake() - add_movement() - sleep_ms(100) - except KeyboardInterrupt as e: - print('over') - alvik.stop() - break + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(1, 0, 0) + + while True: + try: + + run_touch_move(alvik) + + except KeyboardInterrupt as e: + print('over') + alvik.stop() + break diff --git a/examples/events/timer_one_shot_events.py b/examples/events/timer_one_shot_events.py index 77856b8..487c4fb 100644 --- a/examples/events/timer_one_shot_events.py +++ b/examples/events/timer_one_shot_events.py @@ -2,17 +2,6 @@ from time import sleep -def toggle_value(): - """ - This function yields a generator object that toggles values between 0 and 1. - :return: - """ - value = 0 - while True: - yield value % 2 - value += 1 - - def toggle_left_led(custom_text: str, val) -> None: """ This function toggles the lef led in the red channel. It also writes some custom text. @@ -20,13 +9,12 @@ def toggle_left_led(custom_text: str, val) -> None: :param val: a toggle signal generator :return: """ - led_val = next(val) - alvik.left_led.set_color(led_val, 0, 0) - print(f"RED {'ON' if led_val else 'OFF'}! {custom_text}") + alvik.left_led.set_color(val, 0, 0) + print(f"LEFT LED -> RED {'ON' if val else 'OFF'}! {custom_text}") alvik = ArduinoAlvik() -alvik.set_timer('one_shot', 10000, toggle_left_led, ("10 seconds have passed... I won't do this again", toggle_value(), )) +alvik.set_timer('one_shot', 10000, toggle_left_led, ("10 seconds have passed... I won't do this again", 1, )) alvik.begin() @@ -67,4 +55,4 @@ def toggle_left_led(custom_text: str, val) -> None: except KeyboardInterrupt as e: print('over') alvik.stop() - break + break \ No newline at end of file From b6a7325e6e0cfba650388d5fa6bf2137dbe112f3 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 20 Nov 2024 17:31:40 +0100 Subject: [PATCH 13/23] mod: impr demo and touch_move example --- examples/demo/demo.py | 126 ++++++++++++++++++++++++------------ examples/demo/touch_move.py | 3 + 2 files changed, 86 insertions(+), 43 deletions(-) diff --git a/examples/demo/demo.py b/examples/demo/demo.py index fb6baa6..f15c93d 100644 --- a/examples/demo/demo.py +++ b/examples/demo/demo.py @@ -1,61 +1,101 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -from line_follower import run_line_follower -from touch_move import run_touch_move -from hand_follower import run_hand_follower +def blink(alvik): + alvik.left_led.set_color(1, 0, 1) + alvik.right_led.set_color(1, 0, 1) + sleep_ms(200) + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(1, 0, 0) -alvik = ArduinoAlvik() -alvik.begin() -menu_status = 0 +def add_movement(alvik, movements): + if alvik.get_touch_up(): + movements.append('forward') + blink(alvik) + while alvik.get_touch_up(): + sleep_ms(100) + if alvik.get_touch_down(): + movements.append('backward') + blink(alvik) + while alvik.get_touch_down(): + sleep_ms(100) + if alvik.get_touch_left(): + movements.append('left') + blink(alvik) + while alvik.get_touch_left(): + sleep_ms(100) + if alvik.get_touch_right(): + movements.append('right') + blink(alvik) + while alvik.get_touch_right(): + sleep_ms(100) + if alvik.get_touch_cancel(): + movements = [] + for i in range(0, 3): + val = i % 2 + alvik.left_led.set_color(val, 0, 0) + alvik.right_led.set_color(val, 0, 0) + sleep_ms(200) + while alvik.get_touch_cancel(): + sleep_ms(100) -def update_led_status(val): - if val == 0: - alvik.left_led.set_color(0, 0, 1) - alvik.right_led.set_color(0, 0, 1) - elif val == 1: - alvik.left_led.set_color(0, 1, 0) - alvik.right_led.set_color(0, 1, 0) - elif val == -1: +def run_movement(alvik, movement): + if movement == 'forward': + alvik.move(10, blocking=False) + if movement == 'backward': + alvik.move(-10, blocking=False) + if movement == 'left': + alvik.rotate(90, blocking=False) + if movement == 'right': + alvik.rotate(-90, blocking=False) + while not alvik.get_touch_cancel() and not alvik.is_target_reached(): alvik.left_led.set_color(1, 0, 0) alvik.right_led.set_color(1, 0, 0) + sleep_ms(100) + alvik.left_led.set_color(0, 0, 0) + alvik.right_led.set_color(0, 0, 0) + sleep_ms(100) -while True: +def run_touch_move(alvik) -> int: + movements = [] + while not (alvik.get_touch_ok() and len(movements) != 0): + if alvik.get_touch_cancel(): + movements.clear() + blink(alvik) + return -1 + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(1, 0, 0) + alvik.brake() + add_movement(alvik, movements) + sleep_ms(100) - update_led_status(menu_status) + alvik.left_led.set_color(0, 0, 0) + alvik.right_led.set_color(0, 0, 0) + for move in movements: + run_movement(alvik, move) + if alvik.get_touch_cancel(): + blink(alvik) + return -1 + return 1 - try: - if alvik.get_touch_ok(): - if menu_status == 0: - while not alvik.get_touch_cancel(): - run_line_follower(alvik) - elif menu_status == 1: - while not alvik.get_touch_cancel(): - run_hand_follower(alvik) - elif menu_status == -1: - while not alvik.get_touch_cancel(): - run_touch_move(alvik) - alvik.brake() +if __name__ == "__main__": + alvik = ArduinoAlvik() + alvik.begin() - if alvik.get_touch_up() and menu_status < 1: - menu_status += 1 - update_led_status(menu_status) - while alvik.get_touch_up(): - sleep_ms(100) - if alvik.get_touch_down() and menu_status > -1: - menu_status -= 1 - update_led_status(menu_status) - while alvik.get_touch_down(): - sleep_ms(100) + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(1, 0, 0) - sleep_ms(100) + while True: + try: + + run_touch_move(alvik) - except KeyboardInterrupt as e: - print('over') - alvik.stop() - break + except KeyboardInterrupt as e: + print('over') + alvik.stop() + break diff --git a/examples/demo/touch_move.py b/examples/demo/touch_move.py index 1ffbd1d..db61dec 100644 --- a/examples/demo/touch_move.py +++ b/examples/demo/touch_move.py @@ -63,6 +63,9 @@ def run_movement(movement): def run_touch_move(alvik): movements = [] while not (alvik.get_touch_ok() and len(movements) != 0): + if alvik.get_touch_cancel(): + movements.clear() + return alvik.left_led.set_color(1, 0, 0) alvik.right_led.set_color(1, 0, 0) alvik.brake() From c9376b94dd7bfe65f06cefb4f80e22b88caa9e70 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 20 Nov 2024 17:47:27 +0100 Subject: [PATCH 14/23] feat: runtime examples blink --- examples/arduino-runtime/blink.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 examples/arduino-runtime/blink.py diff --git a/examples/arduino-runtime/blink.py b/examples/arduino-runtime/blink.py new file mode 100644 index 0000000..7626cdb --- /dev/null +++ b/examples/arduino-runtime/blink.py @@ -0,0 +1,24 @@ +from time import sleep_ms + +from arduino import start +from arduino_alvik import ArduinoAlvik + + +alvik = ArduinoAlvik() + + +def setup(): + alvik.begin() + + +def loop(): + print('loopy loop') + alvik.left_led.set_color(0, 0, 1) + alvik.right_led.set_color(0, 0, 1) + sleep_ms(500) + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(1, 0, 0) + sleep_ms(500) + + +start(setup, loop) From a7b258ae243771e45ca5e56b9315fbcab6907ed4 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 20 Nov 2024 17:53:29 +0100 Subject: [PATCH 15/23] feat: runtime examples line_follower --- examples/arduino-runtime/blink.py | 2 +- examples/arduino-runtime/line_follower.py | 60 +++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 examples/arduino-runtime/line_follower.py diff --git a/examples/arduino-runtime/blink.py b/examples/arduino-runtime/blink.py index 7626cdb..31936be 100644 --- a/examples/arduino-runtime/blink.py +++ b/examples/arduino-runtime/blink.py @@ -12,7 +12,7 @@ def setup(): def loop(): - print('loopy loop') + print('blinking LEDs') alvik.left_led.set_color(0, 0, 1) alvik.right_led.set_color(0, 0, 1) sleep_ms(500) diff --git a/examples/arduino-runtime/line_follower.py b/examples/arduino-runtime/line_follower.py new file mode 100644 index 0000000..b4293e6 --- /dev/null +++ b/examples/arduino-runtime/line_follower.py @@ -0,0 +1,60 @@ +from time import sleep_ms + +from arduino import start +from arduino_alvik import ArduinoAlvik + + +alvik = ArduinoAlvik() + + +def calculate_center(left: int, center: int, right: int): + centroid = 0 + sum_weight = left + center + right + sum_values = left + 2 * center + 3 * right + if sum_weight != 0: + centroid = sum_values / sum_weight + centroid = 2 - centroid + return centroid + + +def run_line_follower(alvik): + + kp = 50.0 + line_sensors = alvik.get_line_sensors() + print(f' {line_sensors}') + + error = calculate_center(*line_sensors) + control = error * kp + + if control > 0.2: + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(0, 0, 0) + elif control < -0.2: + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(0, 0, 0) + else: + alvik.left_led.set_color(0, 1, 0) + alvik.right_led.set_color(0, 1, 0) + + alvik.set_wheels_speed(30 - control, 30 + control) + sleep_ms(100) + + +def setup(): + alvik.begin() + alvik.left_led.set_color(0, 0, 1) + alvik.right_led.set_color(0, 0, 1) + + +def loop(): + while not alvik.get_touch_cancel(): + run_line_follower(alvik) + + while not alvik.get_touch_ok(): + alvik.left_led.set_color(0, 0, 1) + alvik.right_led.set_color(0, 0, 1) + alvik.brake() + sleep_ms(100) + + +start(setup, loop) From 9c4565612e5b431fef36c7625fa1674ff745243d Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 20 Nov 2024 18:02:26 +0100 Subject: [PATCH 16/23] feat: runtime examples read_tof --- examples/arduino-runtime/read_tof.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 examples/arduino-runtime/read_tof.py diff --git a/examples/arduino-runtime/read_tof.py b/examples/arduino-runtime/read_tof.py new file mode 100644 index 0000000..1dd87b9 --- /dev/null +++ b/examples/arduino-runtime/read_tof.py @@ -0,0 +1,22 @@ +from time import sleep_ms + +from arduino import start +from arduino_alvik import ArduinoAlvik + + +alvik = ArduinoAlvik() + + +def setup(): + alvik.begin() + + +def loop(): + L, CL, C, CR, R = alvik.get_distance() + T = alvik.get_distance_top() + B = alvik.get_distance_bottom() + print(f'T: {T} | B: {B} | L: {L} | CL: {CL} | C: {C} | CR: {CR} | R: {R}') + sleep_ms(100) + + +start(setup, loop) From 8246027aaf5b1faf1704bdea053d4d77ab4e3fef Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 22 Nov 2024 09:19:51 +0100 Subject: [PATCH 17/23] feat: runtime-examples --- examples/arduino-runtime/blink.py | 6 +++++- examples/arduino-runtime/line_follower.py | 6 +++++- examples/arduino-runtime/read_tof.py | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/examples/arduino-runtime/blink.py b/examples/arduino-runtime/blink.py index 31936be..920838f 100644 --- a/examples/arduino-runtime/blink.py +++ b/examples/arduino-runtime/blink.py @@ -21,4 +21,8 @@ def loop(): sleep_ms(500) -start(setup, loop) +def cleanup(): + alvik.stop() + + +start(setup=setup, loop=loop, cleanup=cleanup) diff --git a/examples/arduino-runtime/line_follower.py b/examples/arduino-runtime/line_follower.py index b4293e6..cff4631 100644 --- a/examples/arduino-runtime/line_follower.py +++ b/examples/arduino-runtime/line_follower.py @@ -57,4 +57,8 @@ def loop(): sleep_ms(100) -start(setup, loop) +def cleanup(): + alvik.stop() + + +start(setup=setup, loop=loop, cleanup=cleanup) diff --git a/examples/arduino-runtime/read_tof.py b/examples/arduino-runtime/read_tof.py index 1dd87b9..63fb76d 100644 --- a/examples/arduino-runtime/read_tof.py +++ b/examples/arduino-runtime/read_tof.py @@ -19,4 +19,8 @@ def loop(): sleep_ms(100) -start(setup, loop) +def cleanup(): + alvik.stop() + + +start(setup=setup, loop=loop, cleanup=cleanup) From 2c3ac21824629b7a413bc360e0f463e2e436cac0 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 22 Nov 2024 09:29:15 +0100 Subject: [PATCH 18/23] fix: examples demo and touch_move regression --- examples/demo/demo.py | 125 ++++++++++++------------------------ examples/demo/touch_move.py | 29 +++++---- 2 files changed, 57 insertions(+), 97 deletions(-) diff --git a/examples/demo/demo.py b/examples/demo/demo.py index f15c93d..3f01f1f 100644 --- a/examples/demo/demo.py +++ b/examples/demo/demo.py @@ -1,101 +1,60 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms +from line_follower import run_line_follower +from touch_move import run_touch_move +from hand_follower import run_hand_follower -def blink(alvik): - alvik.left_led.set_color(1, 0, 1) - alvik.right_led.set_color(1, 0, 1) - sleep_ms(200) - alvik.left_led.set_color(1, 0, 0) - alvik.right_led.set_color(1, 0, 0) +alvik = ArduinoAlvik() +alvik.begin() -def add_movement(alvik, movements): - if alvik.get_touch_up(): - movements.append('forward') - blink(alvik) - while alvik.get_touch_up(): - sleep_ms(100) - if alvik.get_touch_down(): - movements.append('backward') - blink(alvik) - while alvik.get_touch_down(): - sleep_ms(100) - if alvik.get_touch_left(): - movements.append('left') - blink(alvik) - while alvik.get_touch_left(): - sleep_ms(100) - if alvik.get_touch_right(): - movements.append('right') - blink(alvik) - while alvik.get_touch_right(): - sleep_ms(100) - if alvik.get_touch_cancel(): - movements = [] - for i in range(0, 3): - val = i % 2 - alvik.left_led.set_color(val, 0, 0) - alvik.right_led.set_color(val, 0, 0) - sleep_ms(200) - while alvik.get_touch_cancel(): - sleep_ms(100) +menu_status = 0 -def run_movement(alvik, movement): - if movement == 'forward': - alvik.move(10, blocking=False) - if movement == 'backward': - alvik.move(-10, blocking=False) - if movement == 'left': - alvik.rotate(90, blocking=False) - if movement == 'right': - alvik.rotate(-90, blocking=False) - while not alvik.get_touch_cancel() and not alvik.is_target_reached(): +def update_led_status(val): + if val == 0: + alvik.left_led.set_color(0, 0, 1) + alvik.right_led.set_color(0, 0, 1) + elif val == 1: + alvik.left_led.set_color(0, 1, 0) + alvik.right_led.set_color(0, 1, 0) + elif val == -1: alvik.left_led.set_color(1, 0, 0) alvik.right_led.set_color(1, 0, 0) - sleep_ms(100) - alvik.left_led.set_color(0, 0, 0) - alvik.right_led.set_color(0, 0, 0) - sleep_ms(100) -def run_touch_move(alvik) -> int: - movements = [] - while not (alvik.get_touch_ok() and len(movements) != 0): - if alvik.get_touch_cancel(): - movements.clear() - blink(alvik) - return -1 - alvik.left_led.set_color(1, 0, 0) - alvik.right_led.set_color(1, 0, 0) - alvik.brake() - add_movement(alvik, movements) - sleep_ms(100) - - alvik.left_led.set_color(0, 0, 0) - alvik.right_led.set_color(0, 0, 0) - for move in movements: - run_movement(alvik, move) - if alvik.get_touch_cancel(): - blink(alvik) - return -1 - return 1 +while True: + update_led_status(menu_status) -if __name__ == "__main__": - alvik = ArduinoAlvik() - alvik.begin() + try: - alvik.left_led.set_color(1, 0, 0) - alvik.right_led.set_color(1, 0, 0) + if alvik.get_touch_ok(): + while not alvik.get_touch_cancel(): + if menu_status == 0: + run_line_follower(alvik) + elif menu_status == 1: + run_hand_follower(alvik) + elif menu_status == -1: + if run_touch_move(alvik) < 0: + break + alvik.brake() - while True: - try: + if alvik.get_touch_up() and menu_status < 1: + menu_status += 1 + update_led_status(menu_status) + while alvik.get_touch_up(): + sleep_ms(100) + if alvik.get_touch_down() and menu_status > -1: + menu_status -= 1 + update_led_status(menu_status) + while alvik.get_touch_down(): + sleep_ms(100) - run_touch_move(alvik) + sleep_ms(100) - except KeyboardInterrupt as e: - print('over') - alvik.stop() - break + except KeyboardInterrupt as e: + print('over') + alvik.stop() + break diff --git a/examples/demo/touch_move.py b/examples/demo/touch_move.py index db61dec..f15c93d 100644 --- a/examples/demo/touch_move.py +++ b/examples/demo/touch_move.py @@ -2,7 +2,7 @@ from time import sleep_ms -def blink(): +def blink(alvik): alvik.left_led.set_color(1, 0, 1) alvik.right_led.set_color(1, 0, 1) sleep_ms(200) @@ -10,25 +10,25 @@ def blink(): alvik.right_led.set_color(1, 0, 0) -def add_movement(movements): +def add_movement(alvik, movements): if alvik.get_touch_up(): movements.append('forward') - blink() + blink(alvik) while alvik.get_touch_up(): sleep_ms(100) if alvik.get_touch_down(): movements.append('backward') - blink() + blink(alvik) while alvik.get_touch_down(): sleep_ms(100) if alvik.get_touch_left(): movements.append('left') - blink() + blink(alvik) while alvik.get_touch_left(): sleep_ms(100) if alvik.get_touch_right(): movements.append('right') - blink() + blink(alvik) while alvik.get_touch_right(): sleep_ms(100) if alvik.get_touch_cancel(): @@ -42,7 +42,7 @@ def add_movement(movements): sleep_ms(100) -def run_movement(movement): +def run_movement(alvik, movement): if movement == 'forward': alvik.move(10, blocking=False) if movement == 'backward': @@ -60,24 +60,27 @@ def run_movement(movement): sleep_ms(100) -def run_touch_move(alvik): +def run_touch_move(alvik) -> int: movements = [] while not (alvik.get_touch_ok() and len(movements) != 0): if alvik.get_touch_cancel(): movements.clear() - return + blink(alvik) + return -1 alvik.left_led.set_color(1, 0, 0) alvik.right_led.set_color(1, 0, 0) alvik.brake() - add_movement(movements) + add_movement(alvik, movements) sleep_ms(100) alvik.left_led.set_color(0, 0, 0) alvik.right_led.set_color(0, 0, 0) for move in movements: - run_movement(move) + run_movement(alvik, move) if alvik.get_touch_cancel(): - break + blink(alvik) + return -1 + return 1 if __name__ == "__main__": @@ -96,5 +99,3 @@ def run_touch_move(alvik): print('over') alvik.stop() break - - From 00e45c388028453fb4d1941a0b84cdaae5a496cc Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 22 Nov 2024 12:34:14 +0100 Subject: [PATCH 19/23] mod: demo example menu. led blinks when an example is chosen --- examples/demo/demo.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/demo/demo.py b/examples/demo/demo.py index 3f01f1f..3e26ad0 100644 --- a/examples/demo/demo.py +++ b/examples/demo/demo.py @@ -31,6 +31,9 @@ def update_led_status(val): try: if alvik.get_touch_ok(): + alvik.left_led.set_color(0, 0, 0) + alvik.right_led.set_color(0, 0, 0) + sleep_ms(500) while not alvik.get_touch_cancel(): if menu_status == 0: run_line_follower(alvik) From 02f4cfc2834e0dfb8608dd6b8bc04a8fcada2db4 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 22 Nov 2024 12:45:16 +0100 Subject: [PATCH 20/23] fix: line_follower starting too early fix: modulino example should raise ImportError when the lib is not available --- examples/arduino-runtime/line_follower.py | 6 +++--- examples/communication/modulino.py | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/arduino-runtime/line_follower.py b/examples/arduino-runtime/line_follower.py index cff4631..cf6c861 100644 --- a/examples/arduino-runtime/line_follower.py +++ b/examples/arduino-runtime/line_follower.py @@ -47,15 +47,15 @@ def setup(): def loop(): - while not alvik.get_touch_cancel(): - run_line_follower(alvik) - while not alvik.get_touch_ok(): alvik.left_led.set_color(0, 0, 1) alvik.right_led.set_color(0, 0, 1) alvik.brake() sleep_ms(100) + while not alvik.get_touch_cancel(): + run_line_follower(alvik) + def cleanup(): alvik.stop() diff --git a/examples/communication/modulino.py b/examples/communication/modulino.py index 6398c14..c8606f9 100644 --- a/examples/communication/modulino.py +++ b/examples/communication/modulino.py @@ -1,13 +1,12 @@ from arduino_alvik import ArduinoAlvik from time import sleep_ms -import sys try: from modulino import ModulinoPixels except ImportError as e: - print("ImportError: ModulinoPixels not installed") - sys.exit(-1) + print("\nImportError: ModulinoPixels not installed") + raise e alvik = ArduinoAlvik() alvik.begin() From c5a6920d8b33889480b09bc0d51de2d7c44134aa Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 22 Nov 2024 12:50:55 +0100 Subject: [PATCH 21/23] mod: demo blink on example exit --- examples/demo/demo.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/demo/demo.py b/examples/demo/demo.py index 3e26ad0..f1b283d 100644 --- a/examples/demo/demo.py +++ b/examples/demo/demo.py @@ -42,6 +42,9 @@ def update_led_status(val): elif menu_status == -1: if run_touch_move(alvik) < 0: break + alvik.left_led.set_color(0, 0, 0) + alvik.right_led.set_color(0, 0, 0) + sleep_ms(500) alvik.brake() if alvik.get_touch_up() and menu_status < 1: From d14964855fdf0e38711c786dbf5f646ea1fb2ce8 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 22 Nov 2024 15:59:35 +0100 Subject: [PATCH 22/23] mod: blinking nano while flashing FW --- arduino_alvik/arduino_alvik.py | 20 +++++++++++++++++++- arduino_alvik/stm32_flash.py | 4 +++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 0aade44..e80674f 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -2105,6 +2105,19 @@ def update_firmware(file_path: str): STM32_eraseMEM, STM32_writeMEM, ) + def flash_toggle(): + i = 0 + + while True: + if i == 0: + LEDR.value(1) + LEDG.value(0) + else: + LEDR.value(0) + LEDG.value(1) + i = (i + 1) % 2 + yield + if CHECK_STM32.value() is not 1: print("Turn on your Alvik to continue...") while CHECK_STM32.value() is not 1: @@ -2121,8 +2134,13 @@ def update_firmware(file_path: str): STM32_eraseMEM(0xFFFF) print("\nWRITING MEM") - STM32_writeMEM(file_path) + toggle = flash_toggle() + STM32_writeMEM(file_path, toggle) + print("\nDONE") print("\nLower Boot0 and reset STM32") + LEDR.value(1) + LEDG.value(1) + STM32_endCommunication() diff --git a/arduino_alvik/stm32_flash.py b/arduino_alvik/stm32_flash.py index 89522c6..5ec1724 100644 --- a/arduino_alvik/stm32_flash.py +++ b/arduino_alvik/stm32_flash.py @@ -296,7 +296,7 @@ def STM32_readMEM(pages: int): _incrementAddress(readAddress) -def STM32_writeMEM(file_path: str): +def STM32_writeMEM(file_path: str, toggle: "Generator" = None): with open(file_path, 'rb') as f: print(f"Flashing {file_path}\n") @@ -326,6 +326,8 @@ def STM32_writeMEM(file_path: str): sys.stdout.write(f"{int((i/file_pages)*100)}%") i = i + 1 _incrementAddress(writeAddress) + if toggle is not None: + next(toggle) def _STM32_standardEraseMEM(pages: int, page_list: bytearray = None): From af372f175f8c1033516e211ecf7d3b7a69a6da94 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 22 Nov 2024 16:50:20 +0100 Subject: [PATCH 23/23] mod: touch_move reset on 1st CANCEL btn press --- examples/demo/touch_move.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/demo/touch_move.py b/examples/demo/touch_move.py index f15c93d..728cc03 100644 --- a/examples/demo/touch_move.py +++ b/examples/demo/touch_move.py @@ -64,9 +64,10 @@ def run_touch_move(alvik) -> int: movements = [] while not (alvik.get_touch_ok() and len(movements) != 0): if alvik.get_touch_cancel(): + if len(movements) == 0: + return -1 movements.clear() blink(alvik) - return -1 alvik.left_led.set_color(1, 0, 0) alvik.right_led.set_color(1, 0, 0) alvik.brake() @@ -78,8 +79,9 @@ def run_touch_move(alvik) -> int: for move in movements: run_movement(alvik, move) if alvik.get_touch_cancel(): + movements.clear() blink(alvik) - return -1 + sleep_ms(100) return 1