From 5e838812b5dd5b7c4eb06a3bad9fa40e6d23f912 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 5 Mar 2024 12:27:19 +0100 Subject: [PATCH 01/26] examples with main --- examples/demo.py | 53 ++++++++++++++++++++++ examples/hand_follower.py | 37 ++++++++++----- examples/main.py | 1 + examples/touch_move.py | 95 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+), 11 deletions(-) create mode 100644 examples/demo.py create mode 100644 examples/main.py create mode 100644 examples/touch_move.py diff --git a/examples/demo.py b/examples/demo.py new file mode 100644 index 0000000..fb75c6a --- /dev/null +++ b/examples/demo.py @@ -0,0 +1,53 @@ +from arduino_alvik import ArduinoAlvik +from time import sleep_ms +import sys + +alvik = ArduinoAlvik() +alvik.begin() + +menu_status = 0 + + +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) + + +while True: + + update_led_status(menu_status) + + try: + + if alvik.get_touch_ok(): + if menu_status == 0: + import line_follower + elif menu_status == 1: + import hand_follower + elif menu_status == -1: + pass + + 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) + + sleep_ms(100) + + except KeyboardInterrupt as e: + print('over') + alvik.stop() + sys.exit() diff --git a/examples/hand_follower.py b/examples/hand_follower.py index 5a031f3..400f4ac 100644 --- a/examples/hand_follower.py +++ b/examples/hand_follower.py @@ -7,14 +7,29 @@ reference = 10.0 -while True: - try: - 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) - except KeyboardInterrupt as e: - print('over') - alvik.stop() - sys.exit() \ No newline at end of file +alvik.left_led.set_color(0, 1, 0) +alvik.right_led.set_color(0, 1, 0) + +while not alvik.get_touch_ok(): + sleep_ms(50) + +try: + while True: + 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() + sys.exit() diff --git a/examples/main.py b/examples/main.py new file mode 100644 index 0000000..7713957 --- /dev/null +++ b/examples/main.py @@ -0,0 +1 @@ +import demo \ No newline at end of file diff --git a/examples/touch_move.py b/examples/touch_move.py new file mode 100644 index 0000000..51078f8 --- /dev/null +++ b/examples/touch_move.py @@ -0,0 +1,95 @@ +from arduino_alvik import ArduinoAlvik +from time import sleep_ms +import sys + +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) + sleep_ms(200) + alvik.left_led.set_color(1, 0, 0) + alvik.right_led.set_color(1, 0, 0) + + +def add_movement(): + global movements + + if alvik.get_touch_up(): + movements.append('forward') + blink() + while alvik.get_touch_up(): + sleep_ms(100) + if alvik.get_touch_down(): + movements.append('backward') + blink() + while alvik.get_touch_down(): + sleep_ms(100) + if alvik.get_touch_left(): + movements.append('left') + blink() + while alvik.get_touch_left(): + sleep_ms(100) + if alvik.get_touch_right(): + movements.append('right') + blink() + 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 run_movement(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(): + sleep_ms(100) + + +while not alvik.get_touch_ok(): + add_movement() + sleep_ms(50) + +try: + while True: + 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 = [] + + while not alvik.get_touch_ok(): + 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() + sys.exit() + + From 11228e7283ff8e6202abad6d367234a3dad9b3b8 Mon Sep 17 00:00:00 2001 From: Giovanni Bruno Date: Tue, 5 Mar 2024 12:40:54 +0100 Subject: [PATCH 02/26] fixed touch move not called in menu --- examples/demo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/demo.py b/examples/demo.py index fb75c6a..931aa47 100644 --- a/examples/demo.py +++ b/examples/demo.py @@ -32,7 +32,7 @@ def update_led_status(val): elif menu_status == 1: import hand_follower elif menu_status == -1: - pass + import touch_move if alvik.get_touch_up() and menu_status < 1: menu_status += 1 From dad94c4d8c3117a9f0ac33c6ce31829118157b8a Mon Sep 17 00:00:00 2001 From: Giovanni Bruno Date: Tue, 5 Mar 2024 12:58:20 +0100 Subject: [PATCH 03/26] improved demo --- examples/hand_follower.py | 3 +++ examples/line_follower.py | 3 +++ examples/touch_move.py | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/examples/hand_follower.py b/examples/hand_follower.py index 400f4ac..fcdc41e 100644 --- a/examples/hand_follower.py +++ b/examples/hand_follower.py @@ -10,6 +10,9 @@ 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) diff --git a/examples/line_follower.py b/examples/line_follower.py index e50136f..041faf6 100644 --- a/examples/line_follower.py +++ b/examples/line_follower.py @@ -23,6 +23,9 @@ def calculate_center(left: int, center: int, right: int): 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) diff --git a/examples/touch_move.py b/examples/touch_move.py index 51078f8..ee29e19 100644 --- a/examples/touch_move.py +++ b/examples/touch_move.py @@ -63,8 +63,15 @@ def run_movement(movement): 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 alvik.get_touch_ok(): + sleep_ms(50) while not alvik.get_touch_ok(): add_movement() From f0d310c674c0ba17663f63327ac33faadfde2469 Mon Sep 17 00:00:00 2001 From: Giovanni Bruno Date: Tue, 5 Mar 2024 15:18:40 +0100 Subject: [PATCH 04/26] demo explanation in readme --- README.md | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/README.md b/README.md index 20abd34..eae435f 100644 --- a/README.md +++ b/README.md @@ -70,4 +70,79 @@ You can now use Arduino Lab for Micropython to run your examples remotely from t
+## Default Demo + +Use `mpremote` to copy following files from examples: +- `main.py`, the file which allows you to automatically start the demo +- `demo.py`, demo launcher +- `touch_move.py`, programming the robot movements via touch pads demo +- `line_follower.py`, black line follower demo +- `hand_follower.py`, hand following demo, the robot stays always at 10cm from an obstacle placed in front of it. + +When the robot is turned on, the demo launcher starts after Alvik boot. + +Blue leds on top turns on. + +By pressing up and down arrows, it is possible to select different demos recognized by different distinct colors (blue, green and red). + +Each color allows to run a different demo as following: +- `red` launchs touch move +- `green` launchs hand following +- `blue` launchs line follower. + +To run a demo, `ok touch pad` pressing is needed after selected the right color demo. + +To run a different demo, turn off and on the robot or reset the Arduino® Nano ESP32. + +### 1. Touch mode example (RED) +This example starts with red leds on. + +`directional touch pads` (up, down, left, right) program desired movements. + +Everytime a directional touch pads is pressed, a violet blink happens on leds. + +To clear the queue of commands, press the `cancel touch pad`. +A red blink happens on top leds. + +To start the sequence, press the `ok touch pad`. + +Everytime is possible to press `cancel touch pad` to stop the robot and reset the sequence. + +
+ +### 2. Hand follower example (GREEN) +This example starts with green leds on. + +Place an obstacle or the hand in front of the robot. + +To start the robot press the `ok touch pad`. + +The robot automatically moves itself to maintain 10 centimeters to the obstacle/hand. + +It is possible to stop the robot everytime by pressing the `cancel touch pad`. + +
+ +### 3. Line Follower example (BLUE) +This example starts with blue leds on. + +To run this example, a white board and black tape (2cm wide) is required. + +Place the robot at the center of the line and press `ok touch pad`. + +It is possible to stop the robot everytime by pressing the `cancel touch pad`. + + + + + + + + + + +
+
+
+ __Note: not open bin files with Arduino Lab for Micropython because they will be corrupted__ From eee527149f28edb70ab79537e527974c6f7fe99c Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 5 Mar 2024 15:42:46 +0100 Subject: [PATCH 05/26] readme corrections --- README.md | 57 ++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index eae435f..e4c8a66 100644 --- a/README.md +++ b/README.md @@ -72,72 +72,69 @@ You can now use Arduino Lab for Micropython to run your examples remotely from t ## Default Demo -Use `mpremote` to copy following files from examples: -- `main.py`, the file which allows you to automatically start the demo +Use `mpremote` to copy following the files from the examples folder: +- `main.py`, this file allows you to automatically start the demo - `demo.py`, demo launcher -- `touch_move.py`, programming the robot movements via touch pads demo +- `touch_move.py`, programming the robot movements via touchpads demo - `line_follower.py`, black line follower demo - `hand_follower.py`, hand following demo, the robot stays always at 10cm from an obstacle placed in front of it. When the robot is turned on, the demo launcher starts after Alvik boot. -Blue leds on top turns on. +Blue leds on top turn on. -By pressing up and down arrows, it is possible to select different demos recognized by different distinct colors (blue, green and red). +By pressing up and down arrows, it is possible to select different demos identified by different colors (blue, green and red). Each color allows to run a different demo as following: -- `red` launchs touch move -- `green` launchs hand following -- `blue` launchs line follower. +- `red` launches the touch-move demo +- `green` launches the hand following demo +- `blue` launches the line follower demo -To run a demo, `ok touch pad` pressing is needed after selected the right color demo. +To run a demo, press the `OK touchpad`, after selecting the right color demo. -To run a different demo, turn off and on the robot or reset the Arduino® Nano ESP32. +To run a different demo, turn the robot off and on again or reset the Arduino® Nano ESP32. ### 1. Touch mode example (RED) -This example starts with red leds on. +This example starts with the red leds on. -`directional touch pads` (up, down, left, right) program desired movements. +`directional touchpads` (up, down, left, right) program the desired movements. -Everytime a directional touch pads is pressed, a violet blink happens on leds. +Everytime a directional touchpads is pressed, the leds blink with a purple color indicating that the command has been registered. +- `UP touchpad` will register a 10 cm forward movement +- `DOWN touchpad` will register a 10 cm backward movement +- `LEFT touchpad` will register a 90° clockwise rotation movement +- `UP touchpad` will register a 90° counterclockwise rotation movement -To clear the queue of commands, press the `cancel touch pad`. -A red blink happens on top leds. +To clear the commands queue, press the `cancel touchpad`. +The leds will blink in red. To start the sequence, press the `ok touch pad`. -Everytime is possible to press `cancel touch pad` to stop the robot and reset the sequence. +Pressing the `cancel touchpad` at any time stops the robot and resets the sequence.
### 2. Hand follower example (GREEN) -This example starts with green leds on. +This example starts with the green leds on. -Place an obstacle or the hand in front of the robot. +Place an obstacle or your hand in front of the robot. To start the robot press the `ok touch pad`. -The robot automatically moves itself to maintain 10 centimeters to the obstacle/hand. +The robot automatically moves itself to keep a 10 centimeters distance from the obstacle/hand. -It is possible to stop the robot everytime by pressing the `cancel touch pad`. +It is possible to stop the robot at any time by pressing the `cancel touchpad`.
### 3. Line Follower example (BLUE) -This example starts with blue leds on. +This example starts with the blue leds on. To run this example, a white board and black tape (2cm wide) is required. -Place the robot at the center of the line and press `ok touch pad`. - -It is possible to stop the robot everytime by pressing the `cancel touch pad`. - - - - - - +Place the robot at the center of the line and press the `ok touchpad`. +It is possible to stop the robot at any time by pressing the `cancel touchpad`. From ae3d68ef65b924e68f438b4575cf53fe0324f051 Mon Sep 17 00:00:00 2001 From: Giovanni Bruno Date: Tue, 5 Mar 2024 15:48:20 +0100 Subject: [PATCH 06/26] typo --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e4c8a66..0f9f325 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ You can now use Arduino Lab for Micropython to run your examples remotely from t Use `mpremote` to copy following the files from the examples folder: - `main.py`, this file allows you to automatically start the demo - `demo.py`, demo launcher -- `touch_move.py`, programming the robot movements via touchpads demo +- `touch_move.py`, programming the robot movements via touch pads demo - `line_follower.py`, black line follower demo - `hand_follower.py`, hand following demo, the robot stays always at 10cm from an obstacle placed in front of it. @@ -90,27 +90,27 @@ Each color allows to run a different demo as following: - `green` launches the hand following demo - `blue` launches the line follower demo -To run a demo, press the `OK touchpad`, after selecting the right color demo. +To run a demo, press the `OK touch pad`, after selecting the right color demo. To run a different demo, turn the robot off and on again or reset the Arduino® Nano ESP32. ### 1. Touch mode example (RED) This example starts with the red leds on. -`directional touchpads` (up, down, left, right) program the desired movements. +`directional touch pads` (up, down, left, right) program the desired movements. -Everytime a directional touchpads is pressed, the leds blink with a purple color indicating that the command has been registered. -- `UP touchpad` will register a 10 cm forward movement -- `DOWN touchpad` will register a 10 cm backward movement -- `LEFT touchpad` will register a 90° clockwise rotation movement -- `UP touchpad` will register a 90° counterclockwise rotation movement +Everytime a directional touch pads is pressed, the leds blink with a purple color indicating that the command has been registered. +- `UP touch pad` will register a 10 cm forward movement +- `DOWN touch pad` will register a 10 cm backward movement +- `LEFT touch pad` will register a 90° clockwise rotation movement +- `UP touch pad` will register a 90° counterclockwise rotation movement -To clear the commands queue, press the `cancel touchpad`. +To clear the commands queue, press the `cancel touch pad`. The leds will blink in red. To start the sequence, press the `ok touch pad`. -Pressing the `cancel touchpad` at any time stops the robot and resets the sequence. +Pressing the `cancel touch pad` at any time stops the robot and resets the sequence.
@@ -123,7 +123,7 @@ To start the robot press the `ok touch pad`. The robot automatically moves itself to keep a 10 centimeters distance from the obstacle/hand. -It is possible to stop the robot at any time by pressing the `cancel touchpad`. +It is possible to stop the robot at any time by pressing the `cancel touch pad`.
@@ -132,9 +132,9 @@ This example starts with the blue leds on. To run this example, a white board and black tape (2cm wide) is required. -Place the robot at the center of the line and press the `ok touchpad`. +Place the robot at the center of the line and press the `ok touch pad`. -It is possible to stop the robot at any time by pressing the `cancel touchpad`. +It is possible to stop the robot at any time by pressing the `cancel touch pad`. From 995dcc03adb72eb26c58a9d07e03d66a30079b76 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 5 Mar 2024 15:55:52 +0100 Subject: [PATCH 07/26] readme corrections, more --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0f9f325..9084cd3 100644 --- a/README.md +++ b/README.md @@ -90,27 +90,27 @@ Each color allows to run a different demo as following: - `green` launches the hand following demo - `blue` launches the line follower demo -To run a demo, press the `OK touch pad`, after selecting the right color demo. +To run a demo, press the `OK touch button`, after selecting the right demo color. To run a different demo, turn the robot off and on again or reset the Arduino® Nano ESP32. ### 1. Touch mode example (RED) This example starts with the red leds on. -`directional touch pads` (up, down, left, right) program the desired movements. +`directional touch buttons` (UP, DOWN, LEFT, RIGHT) program the desired movements. -Everytime a directional touch pads is pressed, the leds blink with a purple color indicating that the command has been registered. -- `UP touch pad` will register a 10 cm forward movement -- `DOWN touch pad` will register a 10 cm backward movement -- `LEFT touch pad` will register a 90° clockwise rotation movement -- `UP touch pad` will register a 90° counterclockwise rotation movement +Everytime a `directional touch button` is pressed, the leds blink in a purple color indicating that the command has been registered. +- `UP touch button` will register a 10 cm forward movement +- `DOWN touch button` will register a 10 cm backward movement +- `LEFT touch button` will register a 90° clockwise rotation movement +- `UP touch button` will register a 90° counterclockwise rotation movement -To clear the commands queue, press the `cancel touch pad`. +To clear the commands queue, press the `CANCEL touch button`. The leds will blink in red. -To start the sequence, press the `ok touch pad`. +To start the sequence, press the `OK touch button`. -Pressing the `cancel touch pad` at any time stops the robot and resets the sequence. +Pressing the `CANCEL touch button` at any time stops the robot and resets the sequence.
@@ -119,11 +119,11 @@ This example starts with the green leds on. Place an obstacle or your hand in front of the robot. -To start the robot press the `ok touch pad`. +To start the robot press the `OK touch button`. -The robot automatically moves itself to keep a 10 centimeters distance from the obstacle/hand. +The robot will move to keep a 10 centimeters distance from the obstacle/hand. -It is possible to stop the robot at any time by pressing the `cancel touch pad`. +It is possible to stop the robot at any time by pressing the `CANCEL touch button`.
@@ -132,9 +132,9 @@ This example starts with the blue leds on. To run this example, a white board and black tape (2cm wide) is required. -Place the robot at the center of the line and press the `ok touch pad`. +Place the robot at the center of the line and press the `OK touch button`. -It is possible to stop the robot at any time by pressing the `cancel touch pad`. +It is possible to stop the robot at any time by pressing the `CANCEL touch button`. From 2ed7fd2def20abe80492dbdebe5a707600737596 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 5 Mar 2024 15:58:21 +0100 Subject: [PATCH 08/26] readme corrections ccw movement typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9084cd3..7083e47 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ Everytime a `directional touch button` is pressed, the leds blink in a purple co - `UP touch button` will register a 10 cm forward movement - `DOWN touch button` will register a 10 cm backward movement - `LEFT touch button` will register a 90° clockwise rotation movement -- `UP touch button` will register a 90° counterclockwise rotation movement +- `RIGHT touch button` will register a 90° counterclockwise rotation movement To clear the commands queue, press the `CANCEL touch button`. The leds will blink in red. From 8859de296b791237b0963ae61a9f4e2f2b6cf334 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 5 Mar 2024 16:01:37 +0100 Subject: [PATCH 09/26] readme corrections, minor --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7083e47..1e73a84 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ You can now use Arduino Lab for Micropython to run your examples remotely from t Use `mpremote` to copy following the files from the examples folder: - `main.py`, this file allows you to automatically start the demo - `demo.py`, demo launcher -- `touch_move.py`, programming the robot movements via touch pads demo +- `touch_move.py`, programming the robot movements via touch buttons demo - `line_follower.py`, black line follower demo - `hand_follower.py`, hand following demo, the robot stays always at 10cm from an obstacle placed in front of it. From 040dba0e38617902e6dd660ce1a8adc5fd8e8716 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 5 Mar 2024 16:04:17 +0100 Subject: [PATCH 10/26] readme corrections, LEFT and RIGHT inverted --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1e73a84..d7d8320 100644 --- a/README.md +++ b/README.md @@ -102,8 +102,8 @@ This example starts with the red leds on. Everytime a `directional touch button` is pressed, the leds blink in a purple color indicating that the command has been registered. - `UP touch button` will register a 10 cm forward movement - `DOWN touch button` will register a 10 cm backward movement -- `LEFT touch button` will register a 90° clockwise rotation movement -- `RIGHT touch button` will register a 90° counterclockwise rotation movement +- `RIGHT touch button` will register a 90° clockwise rotation movement +- `LEFT touch button` will register a 90° counterclockwise rotation movement To clear the commands queue, press the `CANCEL touch button`. The leds will blink in red. From 04ae20f95340e7667fe51f1abc070cd7e42a1acf Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 5 Mar 2024 16:33:32 +0100 Subject: [PATCH 11/26] impr: Alvik on/off text in REPL --- arduino_alvik/arduino_alvik.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 506295f..1d17dfc 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -131,7 +131,7 @@ def _idle(self, delay_=1, check_on_thread=False) -> None: LEDR.value(led_val) LEDG.value(1) led_val = (led_val + 1) % 2 - print("Alvik is on") + print("********** Alvik is on **********") except KeyboardInterrupt: self.stop() sys.exit() @@ -176,7 +176,7 @@ def begin(self) -> int: :return: """ if not self.is_on(): - print("\nTurn on your Arduino Alvik!\n") + print("\n********** Please turn on your Arduino Alvik! **********\n") sleep_ms(1000) self._idle(1000) self._begin_update_thread() From 5e8f6a53a094427bf8e4ecc784a3fcae355ddbf6 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Wed, 6 Mar 2024 12:07:46 +0100 Subject: [PATCH 12/26] feat: DL1 DL2 LEDs alias --- arduino_alvik/arduino_alvik.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 1d17dfc..074b3cc 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -31,9 +31,9 @@ def __init__(self): self.left_wheel = _ArduinoAlvikWheel(self._packeter, ord('L')) self.right_wheel = _ArduinoAlvikWheel(self._packeter, ord('R')) self._led_state = list((None,)) - self.left_led = _ArduinoAlvikRgbLed(self._packeter, 'left', self._led_state, + self.left_led = self.DL1 = _ArduinoAlvikRgbLed(self._packeter, 'left', self._led_state, rgb_mask=[0b00000100, 0b00001000, 0b00010000]) - self.right_led = _ArduinoAlvikRgbLed(self._packeter, 'right', self._led_state, + self.right_led = self.DL2 = _ArduinoAlvikRgbLed(self._packeter, 'right', self._led_state, rgb_mask=[0b00100000, 0b01000000, 0b10000000]) self._battery_perc = None self._touch_byte = None From b83e7e4578ba2701e10a7db789e24f50f857578a Mon Sep 17 00:00:00 2001 From: Giovanni Bruno Date: Thu, 7 Mar 2024 10:27:35 +0100 Subject: [PATCH 13/26] fixed install scripts --- install.bat | 1 + install.sh | 1 + 2 files changed, 2 insertions(+) diff --git a/install.bat b/install.bat index 4e17379..85801b2 100644 --- a/install.bat +++ b/install.bat @@ -17,6 +17,7 @@ if /i "%1"=="-h" ( :install +python -m mpremote %port_string% fs mkdir lib python -m mpremote %port_string% fs mkdir lib/arduino_alvik python -m mpremote %port_string% fs cp arduino_alvik/__init__.py :lib/arduino_alvik/__init__.py python -m mpremote %port_string% fs cp arduino_alvik/arduino_alvik.py :lib/arduino_alvik/arduino_alvik.py diff --git a/install.sh b/install.sh index 736166c..b0f9611 100644 --- a/install.sh +++ b/install.sh @@ -42,6 +42,7 @@ fi # Uncomment the following line on windows machines # python_command="python" +$python_command -m mpremote $connect_string fs mkdir lib $python_command -m mpremote $connect_string fs mkdir lib/arduino_alvik $python_command -m mpremote $connect_string fs cp arduino_alvik/__init__.py :lib/arduino_alvik/__init__.py $python_command -m mpremote $connect_string fs cp arduino_alvik/arduino_alvik.py :lib/arduino_alvik/arduino_alvik.py From d97e7554018a42ac6ec10a247fdff62fef1f1fd0 Mon Sep 17 00:00:00 2001 From: Giovanni Bruno Date: Thu, 7 Mar 2024 16:48:29 +0100 Subject: [PATCH 14/26] fix issue on touch_move --- examples/touch_move.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/touch_move.py b/examples/touch_move.py index ee29e19..b51e4ca 100644 --- a/examples/touch_move.py +++ b/examples/touch_move.py @@ -73,7 +73,7 @@ def run_movement(movement): while alvik.get_touch_ok(): sleep_ms(50) -while not alvik.get_touch_ok(): +while not (alvik.get_touch_ok() and len(movements) != 0): add_movement() sleep_ms(50) @@ -88,7 +88,7 @@ def run_movement(movement): movements = [] - while not alvik.get_touch_ok(): + 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() From f81d278a920ca8543510b1f482a18ea955aa3bc5 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 8 Mar 2024 16:19:16 +0100 Subject: [PATCH 15/26] mod: move and rotate with timeouts --- arduino_alvik/arduino_alvik.py | 21 +++++++++++++-------- arduino_alvik/robot_definitions.py | 2 ++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 074b3cc..b60e0a8 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -3,7 +3,7 @@ import struct from machine import I2C import _thread -from time import sleep_ms +from time import sleep_ms, ticks_ms, ticks_diff from ucPack import ucPack @@ -229,9 +229,14 @@ def _stop_update_thread(cls): """ cls._update_thread_running = False - def _wait_for_target(self): - while not self.is_target_reached(): - pass + def _wait_for_target(self, timeout): + start = ticks_ms() + while True: + if self.is_target_reached(): + print("ACK received") + if ticks_diff(ticks_ms(), start) >= timeout*1000: + print('timeout reached') + break def is_target_reached(self) -> bool: """ @@ -243,8 +248,8 @@ def is_target_reached(self) -> bool: sleep_ms(50) return False else: - self._packeter.packetC1B(ord('X'), ord('K')) - uart.write(self._packeter.msg[0:self._packeter.msg_size]) + # self._packeter.packetC1B(ord('X'), ord('K')) + # uart.write(self._packeter.msg[0:self._packeter.msg_size]) sleep_ms(200) return True @@ -270,7 +275,7 @@ def rotate(self, angle: float, unit: str = 'deg', blocking: bool = True): self._packeter.packetC1F(ord('R'), angle) uart.write(self._packeter.msg[0:self._packeter.msg_size]) if blocking: - self._wait_for_target() + self._wait_for_target(timeout=(angle/MOTOR_CONTROL_DEG_S)*1.05) def move(self, distance: float, unit: str = 'cm', blocking: bool = True): """ @@ -285,7 +290,7 @@ def move(self, distance: float, unit: str = 'cm', blocking: bool = True): self._packeter.packetC1F(ord('G'), distance) uart.write(self._packeter.msg[0:self._packeter.msg_size]) if blocking: - self._wait_for_target() + self._wait_for_target(timeout=(distance/MOTOR_CONTROL_MM_S)*1.05) def stop(self): """ diff --git a/arduino_alvik/robot_definitions.py b/arduino_alvik/robot_definitions.py index 142adfa..9a7a174 100644 --- a/arduino_alvik/robot_definitions.py +++ b/arduino_alvik/robot_definitions.py @@ -3,6 +3,8 @@ MOTOR_KI_DEFAULT = 450.0 MOTOR_KD_DEFAULT = 0.0 MOTOR_MAX_RPM = 70.0 +MOTOR_CONTROL_DEG_S = 40 +MOTOR_CONTROL_MM_S = 40 WHEEL_TRACK_MM = 89.0 # Wheels parameters From 2931b53998038dfe7197a91fa27142ac16fc10ce Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 8 Mar 2024 16:51:00 +0100 Subject: [PATCH 16/26] fix: not exiting on target reached --- arduino_alvik/arduino_alvik.py | 1 + 1 file changed, 1 insertion(+) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index b60e0a8..78274c2 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -234,6 +234,7 @@ def _wait_for_target(self, timeout): while True: if self.is_target_reached(): print("ACK received") + break if ticks_diff(ticks_ms(), start) >= timeout*1000: print('timeout reached') break From aa15ba8274f2a044812e60cf72bc630945950e1b Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 8 Mar 2024 16:59:12 +0100 Subject: [PATCH 17/26] fix: wait for tgt with sleep --- arduino_alvik/arduino_alvik.py | 1 + 1 file changed, 1 insertion(+) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 78274c2..cff8b09 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -232,6 +232,7 @@ def _stop_update_thread(cls): def _wait_for_target(self, timeout): start = ticks_ms() while True: + sleep_ms(2) if self.is_target_reached(): print("ACK received") break From 2081d21bf6cf8fcc56ee5c99a1782d9cadadd2a8 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 8 Mar 2024 17:39:29 +0100 Subject: [PATCH 18/26] mods not ok --- arduino_alvik/arduino_alvik.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index cff8b09..847f0c4 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -70,6 +70,8 @@ def __init__(self): self._last_ack = '' self._version = [None, None, None] self._touch_events = _ArduinoAlvikTouchEvents() + self._time_skip = ticks_ms() + self._TIMEOUT = 0 @staticmethod def is_on() -> bool: @@ -246,14 +248,17 @@ def is_target_reached(self) -> bool: It also responds with an ack received message :return: """ - if self._last_ack != ord('M') and self._last_ack != ord('R'): - sleep_ms(50) - return False - else: - # self._packeter.packetC1B(ord('X'), ord('K')) - # uart.write(self._packeter.msg[0:self._packeter.msg_size]) - sleep_ms(200) - return True + if ticks_diff(ticks_ms(), self._time_skip) > self._TIMEOUT: + + if self._last_ack != ord('M') and self._last_ack != ord('R'): + sleep_ms(50) + return False + else: + # self._packeter.packetC1B(ord('X'), ord('K')) + # uart.write(self._packeter.msg[0:self._packeter.msg_size]) + sleep_ms(200) + return True + return False def set_behaviour(self, behaviour: int): """ @@ -274,9 +279,11 @@ def rotate(self, angle: float, unit: str = 'deg', blocking: bool = True): """ angle = convert_angle(angle, unit, 'deg') sleep_ms(200) + self._time_skip = ticks_ms() self._packeter.packetC1F(ord('R'), angle) uart.write(self._packeter.msg[0:self._packeter.msg_size]) if blocking: + self._TIMEOUT = (angle/MOTOR_CONTROL_DEG_S)*1.05 self._wait_for_target(timeout=(angle/MOTOR_CONTROL_DEG_S)*1.05) def move(self, distance: float, unit: str = 'cm', blocking: bool = True): @@ -289,9 +296,11 @@ def move(self, distance: float, unit: str = 'cm', blocking: bool = True): """ distance = convert_distance(distance, unit, 'mm') sleep_ms(200) + self._time_skip = ticks_ms() self._packeter.packetC1F(ord('G'), distance) uart.write(self._packeter.msg[0:self._packeter.msg_size]) if blocking: + self._TIMEOUT = (distance/MOTOR_CONTROL_MM_S)*1.05 self._wait_for_target(timeout=(distance/MOTOR_CONTROL_MM_S)*1.05) def stop(self): From 3dfee9f5a1d27d9ec03a816d353e5a65804a83ce Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Fri, 8 Mar 2024 17:49:39 +0100 Subject: [PATCH 19/26] fix: timeout in millis --- arduino_alvik/arduino_alvik.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 847f0c4..5543522 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -283,7 +283,7 @@ def rotate(self, angle: float, unit: str = 'deg', blocking: bool = True): self._packeter.packetC1F(ord('R'), angle) uart.write(self._packeter.msg[0:self._packeter.msg_size]) if blocking: - self._TIMEOUT = (angle/MOTOR_CONTROL_DEG_S)*1.05 + self._TIMEOUT = 1000*(angle/MOTOR_CONTROL_DEG_S)*1.05 self._wait_for_target(timeout=(angle/MOTOR_CONTROL_DEG_S)*1.05) def move(self, distance: float, unit: str = 'cm', blocking: bool = True): @@ -300,7 +300,7 @@ def move(self, distance: float, unit: str = 'cm', blocking: bool = True): self._packeter.packetC1F(ord('G'), distance) uart.write(self._packeter.msg[0:self._packeter.msg_size]) if blocking: - self._TIMEOUT = (distance/MOTOR_CONTROL_MM_S)*1.05 + self._TIMEOUT = 1000*(distance/MOTOR_CONTROL_MM_S)*1.05 self._wait_for_target(timeout=(distance/MOTOR_CONTROL_MM_S)*1.05) def stop(self): From 5ffbbe8ef0bc72d60bd62f1d8b5515a3731811b7 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Mon, 11 Mar 2024 10:38:45 +0100 Subject: [PATCH 20/26] mod: revert to multiple acks. accepts acks only if waiting one. uses an idle_time to wait on target --- arduino_alvik/arduino_alvik.py | 48 ++++++++++++++++------------------ 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 5543522..23af177 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -68,10 +68,9 @@ def __init__(self): self._linear_velocity = None self._angular_velocity = None self._last_ack = '' + self._waiting_ack = None self._version = [None, None, None] self._touch_events = _ArduinoAlvikTouchEvents() - self._time_skip = ticks_ms() - self._TIMEOUT = 0 @staticmethod def is_on() -> bool: @@ -231,16 +230,14 @@ def _stop_update_thread(cls): """ cls._update_thread_running = False - def _wait_for_target(self, timeout): + def _wait_for_target(self, idle_time): start = ticks_ms() while True: - sleep_ms(2) - if self.is_target_reached(): - print("ACK received") - break - if ticks_diff(ticks_ms(), start) >= timeout*1000: - print('timeout reached') + if ticks_diff(ticks_ms(), start) >= idle_time*1000 and self.is_target_reached(): break + else: + print(self._last_ack) + sleep_ms(100) def is_target_reached(self) -> bool: """ @@ -248,16 +245,13 @@ def is_target_reached(self) -> bool: It also responds with an ack received message :return: """ - if ticks_diff(ticks_ms(), self._time_skip) > self._TIMEOUT: - - if self._last_ack != ord('M') and self._last_ack != ord('R'): - sleep_ms(50) - return False - else: - # self._packeter.packetC1B(ord('X'), ord('K')) - # uart.write(self._packeter.msg[0:self._packeter.msg_size]) - sleep_ms(200) - return True + if self._last_ack == self._waiting_ack: + self._packeter.packetC1B(ord('X'), ord('K')) + uart.write(self._packeter.msg[0:self._packeter.msg_size]) + sleep_ms(100) + self._last_ack = '' + self._waiting_ack = None + return True return False def set_behaviour(self, behaviour: int): @@ -279,12 +273,11 @@ def rotate(self, angle: float, unit: str = 'deg', blocking: bool = True): """ angle = convert_angle(angle, unit, 'deg') sleep_ms(200) - self._time_skip = ticks_ms() self._packeter.packetC1F(ord('R'), angle) uart.write(self._packeter.msg[0:self._packeter.msg_size]) + self._waiting_ack = ord('R') if blocking: - self._TIMEOUT = 1000*(angle/MOTOR_CONTROL_DEG_S)*1.05 - self._wait_for_target(timeout=(angle/MOTOR_CONTROL_DEG_S)*1.05) + self._wait_for_target(idle_time=(angle/MOTOR_CONTROL_DEG_S)) def move(self, distance: float, unit: str = 'cm', blocking: bool = True): """ @@ -296,12 +289,11 @@ def move(self, distance: float, unit: str = 'cm', blocking: bool = True): """ distance = convert_distance(distance, unit, 'mm') sleep_ms(200) - self._time_skip = ticks_ms() self._packeter.packetC1F(ord('G'), distance) uart.write(self._packeter.msg[0:self._packeter.msg_size]) + self._waiting_ack = ord('M') if blocking: - self._TIMEOUT = 1000*(distance/MOTOR_CONTROL_MM_S)*1.05 - self._wait_for_target(timeout=(distance/MOTOR_CONTROL_MM_S)*1.05) + self._wait_for_target(idle_time=(distance/MOTOR_CONTROL_MM_S)) def stop(self): """ @@ -626,7 +618,11 @@ def _parse_message(self) -> int: _, self._linear_velocity, self._angular_velocity = self._packeter.unpacketC2F() elif code == ord('x'): # robot ack - _, self._last_ack = self._packeter.unpacketC1B() + _, ack = self._packeter.unpacketC1B() + if self._waiting_ack is not None: + self._last_ack = ack + else: + self._last_ack = 0x00 elif code == ord('z'): # robot ack _, self._x, self._y, self._theta = self._packeter.unpacketC3F() From 5281368ebd1f5c7ff6134707c488f21dd375315c Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Mon, 11 Mar 2024 12:12:17 +0100 Subject: [PATCH 21/26] mod: _wait_for_target should not print --- 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 23af177..19413a0 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -236,7 +236,7 @@ def _wait_for_target(self, idle_time): if ticks_diff(ticks_ms(), start) >= idle_time*1000 and self.is_target_reached(): break else: - print(self._last_ack) + # print(self._last_ack) sleep_ms(100) def is_target_reached(self) -> bool: From 3b81094edb6ac3c48ab9708c6113f3db9ff2ce53 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Mon, 11 Mar 2024 12:47:56 +0100 Subject: [PATCH 22/26] fix: hanging on begin waiting for 1st ack 0x00 --- arduino_alvik/arduino_alvik.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 19413a0..0a49b94 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -67,7 +67,7 @@ def __init__(self): self._bottom_tof = None self._linear_velocity = None self._angular_velocity = None - self._last_ack = '' + self._last_ack = None self._waiting_ack = None self._version = [None, None, None] self._touch_events = _ArduinoAlvikTouchEvents() @@ -200,6 +200,7 @@ def _wait_for_ack(self) -> None: Waits until receives 0x00 ack from robot :return: """ + self._waiting_ack = 0x00 while self._last_ack != 0x00: sleep_ms(20) @@ -618,10 +619,10 @@ def _parse_message(self) -> int: _, self._linear_velocity, self._angular_velocity = self._packeter.unpacketC2F() elif code == ord('x'): # robot ack - _, ack = self._packeter.unpacketC1B() if self._waiting_ack is not None: - self._last_ack = ack + _, self._last_ack = self._packeter.unpacketC1B() else: + self._packeter.unpacketC1B() self._last_ack = 0x00 elif code == ord('z'): # robot ack From 66f391ccf0cd334754dfdf529be7177810b67fde Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Mon, 11 Mar 2024 12:49:18 +0100 Subject: [PATCH 23/26] mod: is_target_reached returns True if not waiting any ack and resets _last_ack to 0x00 on True condition --- arduino_alvik/arduino_alvik.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index 0a49b94..cd38ba9 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -246,11 +246,13 @@ def is_target_reached(self) -> bool: It also responds with an ack received message :return: """ + if self._waiting_ack is None: + return True if self._last_ack == self._waiting_ack: self._packeter.packetC1B(ord('X'), ord('K')) uart.write(self._packeter.msg[0:self._packeter.msg_size]) sleep_ms(100) - self._last_ack = '' + self._last_ack = 0x00 self._waiting_ack = None return True return False From ad2ed2fb0c48ca7e94730a4b98a21706c714127a Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 12 Mar 2024 19:00:45 +0100 Subject: [PATCH 24/26] mod: increased MOTOR_CONTROL speeds --- arduino_alvik/robot_definitions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arduino_alvik/robot_definitions.py b/arduino_alvik/robot_definitions.py index 9a7a174..26a2e51 100644 --- a/arduino_alvik/robot_definitions.py +++ b/arduino_alvik/robot_definitions.py @@ -3,8 +3,8 @@ MOTOR_KI_DEFAULT = 450.0 MOTOR_KD_DEFAULT = 0.0 MOTOR_MAX_RPM = 70.0 -MOTOR_CONTROL_DEG_S = 40 -MOTOR_CONTROL_MM_S = 40 +MOTOR_CONTROL_DEG_S = 100 +MOTOR_CONTROL_MM_S = 100 WHEEL_TRACK_MM = 89.0 # Wheels parameters From e832a0d07ed393445922b6bb34c40364fed77ac4 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 12 Mar 2024 19:28:30 +0100 Subject: [PATCH 25/26] mod: better color standard calibration --- arduino_alvik/arduino_alvik.py | 4 ++-- arduino_alvik/constants.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index cd38ba9..cb3580c 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -914,9 +914,9 @@ def hsv2label(h, s, v) -> str: label = 'GREEN' elif 170 <= h < 210: label = 'LIGHT BLUE' - elif 210 <= h < 260: + elif 210 <= h < 250: label = 'BLUE' - elif 260 <= h < 280: + elif 250 <= h < 280: label = 'VIOLET' else: # h<20 or h>=280 is more problematic if v < 0.5 and s < 0.45: diff --git a/arduino_alvik/constants.py b/arduino_alvik/constants.py index 1076df3..47a7c06 100644 --- a/arduino_alvik/constants.py +++ b/arduino_alvik/constants.py @@ -1,5 +1,5 @@ # COLOR SENSOR COLOR_FULL_SCALE = 4097 -WHITE_CAL = [444, 342, 345] -BLACK_CAL = [153, 135, 123] +WHITE_CAL = [450, 500, 510] +BLACK_CAL = [160, 200, 190] \ No newline at end of file From a1d6d4cb5e8e9186b45297e8f5700934047880f7 Mon Sep 17 00:00:00 2001 From: Lucio Rossi Date: Tue, 12 Mar 2024 20:07:31 +0100 Subject: [PATCH 26/26] mod: flash FW from arduino_alvik module ver 0.4.0 --- arduino_alvik/arduino_alvik.py | 52 +++++++++++++++++++++++++++---- arduino_alvik/firmware_updater.py | 24 -------------- examples/flash_firmware.py | 5 +++ install.bat | 1 + install.sh | 1 + package.json | 3 +- utilities/flash_firmware.bat | 7 +---- utilities/flash_firmware.sh | 7 +---- 8 files changed, 56 insertions(+), 44 deletions(-) delete mode 100644 arduino_alvik/firmware_updater.py create mode 100644 examples/flash_firmware.py diff --git a/arduino_alvik/arduino_alvik.py b/arduino_alvik/arduino_alvik.py index cb3580c..69c7e49 100644 --- a/arduino_alvik/arduino_alvik.py +++ b/arduino_alvik/arduino_alvik.py @@ -15,7 +15,6 @@ class ArduinoAlvik: - _update_thread_running = False _update_thread_id = None _touch_events_thread_running = False @@ -32,9 +31,9 @@ def __init__(self): self.right_wheel = _ArduinoAlvikWheel(self._packeter, ord('R')) self._led_state = list((None,)) self.left_led = self.DL1 = _ArduinoAlvikRgbLed(self._packeter, 'left', self._led_state, - rgb_mask=[0b00000100, 0b00001000, 0b00010000]) + rgb_mask=[0b00000100, 0b00001000, 0b00010000]) self.right_led = self.DL2 = _ArduinoAlvikRgbLed(self._packeter, 'right', self._led_state, - rgb_mask=[0b00100000, 0b01000000, 0b10000000]) + rgb_mask=[0b00100000, 0b01000000, 0b10000000]) self._battery_perc = None self._touch_byte = None self._behaviour = None @@ -234,7 +233,7 @@ def _stop_update_thread(cls): def _wait_for_target(self, idle_time): start = ticks_ms() while True: - if ticks_diff(ticks_ms(), start) >= idle_time*1000 and self.is_target_reached(): + if ticks_diff(ticks_ms(), start) >= idle_time * 1000 and self.is_target_reached(): break else: # print(self._last_ack) @@ -280,7 +279,7 @@ def rotate(self, angle: float, unit: str = 'deg', blocking: bool = True): uart.write(self._packeter.msg[0:self._packeter.msg_size]) self._waiting_ack = ord('R') if blocking: - self._wait_for_target(idle_time=(angle/MOTOR_CONTROL_DEG_S)) + self._wait_for_target(idle_time=(angle / MOTOR_CONTROL_DEG_S)) def move(self, distance: float, unit: str = 'cm', blocking: bool = True): """ @@ -296,7 +295,7 @@ def move(self, distance: float, unit: str = 'cm', blocking: bool = True): uart.write(self._packeter.msg[0:self._packeter.msg_size]) self._waiting_ack = ord('M') if blocking: - self._wait_for_target(idle_time=(distance/MOTOR_CONTROL_MM_S)) + self._wait_for_target(idle_time=(distance / MOTOR_CONTROL_MM_S)) def stop(self): """ @@ -1337,3 +1336,44 @@ def register_callback(self, event_name: str, callback: callable, args: tuple = N if event_name not in self.__class__.available_events: return super().register_callback(event_name, callback, args) + + +# UPDATE FIRMWARE METHOD # + +def update_firmware(file_path: str): + """ + + :param file_path: path of your FW bin + :return: + """ + + from sys import exit + from stm32_flash import ( + CHECK_STM32, + STM32_endCommunication, + STM32_startCommunication, + STM32_NACK, + STM32_eraseMEM, + STM32_writeMEM, ) + + if CHECK_STM32.value() is not 1: + print("Turn on your Alvik to continue...") + while CHECK_STM32.value() is not 1: + sleep_ms(500) + + ans = STM32_startCommunication() + if ans == STM32_NACK: + print("Cannot establish connection with STM32") + exit(-1) + + print('\nSTM32 FOUND') + + print('\nERASING MEM') + STM32_eraseMEM(0xFFFF) + + print("\nWRITING MEM") + STM32_writeMEM(file_path) + print("\nDONE") + print("\nLower Boot0 and reset STM32") + + STM32_endCommunication() diff --git a/arduino_alvik/firmware_updater.py b/arduino_alvik/firmware_updater.py deleted file mode 100644 index 0367704..0000000 --- a/arduino_alvik/firmware_updater.py +++ /dev/null @@ -1,24 +0,0 @@ -from sys import exit -from stm32_flash import * - -if CHECK_STM32.value() is not 1: - print("Turn on your Alvik to continue...") - while CHECK_STM32.value() is not 1: - sleep_ms(500) - -ans = STM32_startCommunication() -if ans == STM32_NACK: - print("Cannot establish connection with STM32") - exit(-1) - -print('\nSTM32 FOUND') - -print('\nERASING MEM') -STM32_eraseMEM(0xFFFF) - -print("\nWRITING MEM") -STM32_writeMEM("firmware.bin") -print("\nDONE") -print("\nLower Boot0 and reset STM32") - -STM32_endCommunication() diff --git a/examples/flash_firmware.py b/examples/flash_firmware.py new file mode 100644 index 0000000..e96a3cf --- /dev/null +++ b/examples/flash_firmware.py @@ -0,0 +1,5 @@ +# from machine import reset +from arduino_alvik import update_firmware + +update_firmware('./firmware.bin') +# reset() diff --git a/install.bat b/install.bat index 85801b2..efbd576 100644 --- a/install.bat +++ b/install.bat @@ -25,6 +25,7 @@ python -m mpremote %port_string% fs cp arduino_alvik/constants.py :lib/arduino_a python -m mpremote %port_string% fs cp arduino_alvik/conversions.py :lib/arduino_alvik/conversions.py python -m mpremote %port_string% fs cp arduino_alvik/pinout_definitions.py :lib/arduino_alvik/pinout_definitions.py python -m mpremote %port_string% fs cp arduino_alvik/robot_definitions.py :lib/arduino_alvik/robot_definitions.py +python -m mpremote %port_string% fs cp arduino_alvik/stm32_flash.py :lib/arduino_alvik/stm32_flash.py python -m mpremote %port_string% fs cp arduino_alvik/uart.py :lib/arduino_alvik/uart.py echo Installing dependencies diff --git a/install.sh b/install.sh index b0f9611..e606f57 100644 --- a/install.sh +++ b/install.sh @@ -50,6 +50,7 @@ $python_command -m mpremote $connect_string fs cp arduino_alvik/constants.py :li $python_command -m mpremote $connect_string fs cp arduino_alvik/conversions.py :lib/arduino_alvik/conversions.py $python_command -m mpremote $connect_string fs cp arduino_alvik/pinout_definitions.py :lib/arduino_alvik/pinout_definitions.py $python_command -m mpremote $connect_string fs cp arduino_alvik/robot_definitions.py :lib/arduino_alvik/robot_definitions.py +$python_command -m mpremote $connect_string fs cp arduino_alvik/stm32_flash.py :lib/arduino_alvik/stm32_flash.py $python_command -m mpremote $connect_string fs cp arduino_alvik/uart.py :lib/arduino_alvik/uart.py echo "Installing dependencies" diff --git a/package.json b/package.json index 90b6c77..1bb63ac 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,10 @@ ["arduino_alvik/pinout_definitions.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/pinout_definitions.py"], ["arduino_alvik/robot_definitions.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/robot_definitions.py"], ["arduino_alvik/uart.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/uart.py"], - ["arduino_alvik/firmware_updater.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/firmware_updater.py"], ["arduino_alvik/stm32_flash.py", "github:arduino/arduino-alvik-mpy/arduino_alvik/stm32_flash.py"] ], "deps": [ ["github:arduino/ucPack-mpy", "0.1.5"] ], - "version": "0.3.0" + "version": "0.4.0" } \ No newline at end of file diff --git a/utilities/flash_firmware.bat b/utilities/flash_firmware.bat index f883fee..8fc2d1b 100644 --- a/utilities/flash_firmware.bat +++ b/utilities/flash_firmware.bat @@ -24,11 +24,6 @@ if "%1"=="" ( set "filename=%1" ) -echo Installing flash firmware utilities... - -python -m mpremote %port_string% fs cp ../arduino_alvik/firmware_updater.py :firmware_updater.py -python -m mpremote %port_string% fs cp ../arduino_alvik/stm32_flash.py :stm32_flash.py - echo Uploading %filename% python -m mpremote %port_string% fs cp %filename% :firmware.bin @@ -36,7 +31,7 @@ python -m mpremote %port_string% fs cp %filename% :firmware.bin set /p userInput=Do you want to flash the firmware right now? (y/N): if /i "%userInput%"=="y" ( - python -m mpremote %port_string% run ../arduino_alvik/firmware_updater.py + python -m mpremote %port_string% run ../examples/flash_firmware.py ) else ( echo The firmware will not be written to the device. ) diff --git a/utilities/flash_firmware.sh b/utilities/flash_firmware.sh index c973ad8..81bd12d 100644 --- a/utilities/flash_firmware.sh +++ b/utilities/flash_firmware.sh @@ -51,11 +51,6 @@ fi # Uncomment the following line on windows machines # python_command="python" -echo "Installing flash firmware utilities..." - -$python_command -m mpremote $connect_string fs cp ../arduino_alvik/firmware_updater.py :firmware_updater.py -$python_command -m mpremote $connect_string fs cp ../arduino_alvik/stm32_flash.py :stm32_flash.py - echo "Uploading $filename..." $python_command -m mpremote $connect_string fs cp $filename :firmware.bin @@ -64,7 +59,7 @@ echo "Do you want to flash the firmware right now? (y/N)" read do_flash if [ "$do_flash" == "y" ] || [ "$do_flash" == "Y" ]; then - $python_command -m mpremote $connect_string run ../arduino_alvik/firmware_updater.py + $python_command -m mpremote $connect_string run ../examples/flash_firmware.py else echo "The firmware will not be written to the device." fi