From acaeeb9362c4f58b85e0c191f1862538eafa6992 Mon Sep 17 00:00:00 2001 From: Limao Chang <80520563+LimaoC@users.noreply.github.com> Date: Thu, 17 Oct 2024 21:23:22 +1000 Subject: [PATCH] refactor: remove proof_of_concept module --- client/proof_of_concept/__init__.py | 0 client/proof_of_concept/buttons.py | 112 -------- client/proof_of_concept/camera.py | 125 --------- client/proof_of_concept/cushion.py | 5 - client/proof_of_concept/display.py | 264 ------------------ .../flood_database_with_data.py | 59 ---- client/proof_of_concept/font-pet-me-128.dat | Bin 768 -> 0 bytes client/proof_of_concept/gpio.py | 57 ---- .../proof_of_concept/resources/hilarious.pbm | Bin 1034 -> 0 bytes .../resources/piicodev-logo.pbm | Bin 1080 -> 0 bytes client/proof_of_concept/servo_driver.py | 101 ------- 11 files changed, 723 deletions(-) delete mode 100644 client/proof_of_concept/__init__.py delete mode 100644 client/proof_of_concept/buttons.py delete mode 100644 client/proof_of_concept/camera.py delete mode 100644 client/proof_of_concept/cushion.py delete mode 100644 client/proof_of_concept/display.py delete mode 100644 client/proof_of_concept/flood_database_with_data.py delete mode 100644 client/proof_of_concept/font-pet-me-128.dat delete mode 100644 client/proof_of_concept/gpio.py delete mode 100644 client/proof_of_concept/resources/hilarious.pbm delete mode 100644 client/proof_of_concept/resources/piicodev-logo.pbm delete mode 100644 client/proof_of_concept/servo_driver.py diff --git a/client/proof_of_concept/__init__.py b/client/proof_of_concept/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/client/proof_of_concept/buttons.py b/client/proof_of_concept/buttons.py deleted file mode 100644 index c31dbab..0000000 --- a/client/proof_of_concept/buttons.py +++ /dev/null @@ -1,112 +0,0 @@ -""" -File: - sitting-desktop-garden/client/proof_of_concept/buttons.py - -Purpose: - Hardware test for the PiicoDev buttons. - -Author: - Gabriel Field (47484306) - -Source: - Largely based on https://core-electronics.com.au/guides/piicodev-button-getting-started-guide/#B62QRRH -""" - -## SECTION: Imports - -from PiicoDev_Switch import PiicoDev_Switch # Switch may be used for other types of PiicoDev Switch devices -from PiicoDev_Unified import sleep_ms - - - -## SECTION: main() - -def main(): - print("==== TEST: Chungus on one button ====") - button = PiicoDev_Switch(double_press_duration = 400) # Double-presses are those that happen with interval <= 400ms - basic_press_test(button) - led_test(button) - double_press_test(button) - - print(" Set button IDs to 0000 and 1110.") - print(" Do this by setting switches down for '0' and up for '1'.") - print(" You have 10 seconds...") - sleep_ms(1000 * 10) - print(" Assuming the button IDs have been set.") - - print("==== TEST: Chungi on two buttons ====") - button0000 = PiicoDev_Switch(id = [0, 0, 0, 0]) - button1110 = PiicoDev_Switch(id = [1, 1, 1, 0]) - two_chungi(button0000, button1110) - - print("==== TEST: COMPLETE! ====") - - - -## TEST: One-button - -def basic_press_test(button: PiicoDev_Switch): - print(" BEGIN basic_press_test()") - sleep_ms(2000) - cycle = 0 - press_count = 0 - while cycle < 10: - print("\tCycle: " + str(cycle)) - press_count += button.press_count - print("\t\tWas pressed: " + str(button.was_pressed)) - print("\t\tIs pressed: " + str(button.is_pressed)) - print("\t\tTotal press count: " + str(press_count)) - cycle += 1 - sleep_ms(1000) - print(" END basic_press_test()") - -def led_test(button): - print(" BEGIN led_test()") - sleep_ms(2000) - cycle = 0 - button.led = False - thinking_led = False - while cycle < 10: - print("\tCycle: " + str(cycle)) - button.led = not button.led - thinking_led = False - print("\tButton should be on: " + str(thinking_led)) - cycle += 1 - sleep_ms(1000) - print(" END led_test()") - -def double_press_test(button): - print(" BEGIN double_press_test()") - sleep_ms(2000) - cycle = 0 - while cycle < 10: - print("\tCycle: " + str(cycle)) - print("\tWas double-pressed: " + str(button.was_double_pressed)) - cycle += 1 - sleep_ms(1000) - print(" END double_press_test()") - - - -## TEST: Two buttons - -def two_chungi(button0000, button1110): - print(" BEGIN two_chungi()") - sleep_ms(2000) - cycle = 0 - button0000.press_count # Clear the count - button1110.press_count # Clear the count - while cycle < 10: - print("\tCycle: " + str(cycle)) - print("\tButton with id 0000 pressed this many times since last time: " + str(button0000.press_count)) - print("\tButton with id 1110 pressed this many times since last time: " + str(button1110.press_count)) - cycle += 1 - sleep_ms(1000) - print(" END two_chungi()") - - - -## LAUNCH - -if __name__ == "__main__": - main() diff --git a/client/proof_of_concept/camera.py b/client/proof_of_concept/camera.py deleted file mode 100644 index ece4377..0000000 --- a/client/proof_of_concept/camera.py +++ /dev/null @@ -1,125 +0,0 @@ -""" -File: - sitting-desktop-garden/client/proof_of_concept/buttons.py - -Purpose: - Hardware test for the PiicoDev buttons. - -Author: - Gabriel Field (47484306) - -Source: - Largely based on https://projects.raspberrypi.org/en/projects/getting-started-with-picamera/0 -""" - -## SECTION: Imports - -from picamera2 import Picamera2, Preview -from PiicoDev_Unified import sleep_ms -from time import time - - - -## SECTION: main() - -def main(): - print("==== TEST: Preview ====") - picam2 = Picamera2() - test_preview(picam2) - - print("==== TEST: Pics ====") - test_pics(picam2) - - print("==== TEST: Vids ====") - test_vids(picam2) - - print("==== TEST: COMPLETE! ====") - - - -## TEST: Preview - -def test_preview(picam2): - print(" BEGIN test_preview()") - sleep_ms(2000) - # Preview for 10 seconds - print("\tStarting preview for 10 seconds... Requires monitor!!") - picam2.start_preview(Preview.QTGL) - picam2.start() - sleep_ms(10000) - picam2.close() - print(" END test_preview()") - - - -## TEST: Pics - -def test_pics(picam2): - print(" BEGIN test_pics()") - sleep_ms(2000) - # Take a single picture - print("\tTaking a picture to ./camera_dump/picture-00.jpg") - start = time() - picam2.start_and_capture_file("./camera_dump/picture-00.jpg") - picam2.close() - end = time() - print("\t\t^done. Took " + str(end - start) + " seconds") - sleep_ms(5000) - # Take many pictures - print("\tTaking 10 pictures to ./camera_dump-pictures-?.jpg for ? = 0 .. 9, with delay 0.1 seconds") - start = time() - picam2.start_and_capture_files("./camera_dump/pictures-{:d}.jpg", num_files = 10, delay = 0.1) - picam2.close() - end = time() - print("\t\t^done. Took " + str(end - start) + " seconds") - sleep_ms(5000) - print(" END test_pics()") - - - -## TEST: Vids - -def test_vids(picam2): - print(" BEGIN test_vids()") - sleep_ms(2000) - # Take a single video for 10 seconds - print("\tTaking a 10-second video to ./camera_dump/video-00.jpg") - start = time() - picam2.start_and_record_video("./camera_dump/video-00.jpg", duration = 10, show_preview = False) - picam2.close() - end = time() - print("\t\t^done. Took " + str(end - start) + " seconds") - sleep_ms(5000) - # Taking many short videos - print("\tTaking ten 1-second videos to ./camera_dump/shorts-?.jpg for ? = 0 .. 9") - start = time() - for i in range(10): - picam2.start_and_record_video("./camera_dump/shorts-" + str(i) + ".jpg", duration = 1, show_preview = False) - picam2.close() - end = time() - print("\t\t^done. Took " + str(end - start) + " seconds") - # Taking many tiny videos - start = time() - for i in range(100): - print("\tTaking 100 0.1-second videos to ./camera_dump/tiny-%02f" % i, duration = 0.1, show_preview = False) - picam2.close() - end = time() - print("\t\t^done. Took " + str(end - start) + " seconds") - sleep_ms(5000) - # Take many short videos without .close() - print("\t Taking ten 1-second videos without .close() to ./camera_dump/open-?.jpg for ? = 0 .. 9") - start = time() - for i in range(10): - picam2.start_and_record_video("./camera_dump/open-" + str(i) + ".jpg", duration = 1, show_preview = False) - picam2.close() - end = time() - print("\t\t^done. Took " + str(end - start) + " seconds") - sleep_ms(5000) - print(" END test_vids()") - - - -## LAUNCH - -if __name__ == "__main__": - main() diff --git a/client/proof_of_concept/cushion.py b/client/proof_of_concept/cushion.py deleted file mode 100644 index 628ebf0..0000000 --- a/client/proof_of_concept/cushion.py +++ /dev/null @@ -1,5 +0,0 @@ -""" -I have no idea how this is gonna work. We'll need to do our own PWM or something ig. -Will only do this after I have the hardware to play with properly. -~Gabe -""" diff --git a/client/proof_of_concept/display.py b/client/proof_of_concept/display.py deleted file mode 100644 index bbf1c22..0000000 --- a/client/proof_of_concept/display.py +++ /dev/null @@ -1,264 +0,0 @@ -""" -File: - sitting-desktop-garden/client/proof_of_concept/display.py - -Purpose: - Hardware test for the PiicoDev display. - -Author: - Gabriel Field (47484306) - -Source: - Largely based on https://core-electronics.com.au/guides/raspberry-pi/piicodev-oled-ssd1306-raspberry-pi-guide/ - -NOTE: - Ensure that I2C is enabled on the RPi first! -""" - -## SECTION: Imports - -from PiicoDev_SSD1306 import * -from PiicoDev_Unified import sleep_ms # cross-platform compatible sleep function -from math import sin, cos, pi -import random - - - -## SECTION: How the .graph2D constructor works - -""" -PiicoDev_SSD1306 - .graph2D - .__init__( - self, - originX = 0, originY = HEIGHT-1, # SSD coordinates of BOTTOM-LEFT CORNER of the graph. Units: pixels - width = WIDTH, height = HEIGHT, # Width and height that the graph TAKES UP ON THE SSD. Units: pixels. - minValue = 0, maxValue = 255, # Minimum and maximum vertical value that can be dispalyed on the graph. Unitless. - # Note: horizontal values for graph are *literally* horizontal pixels. - c = 1, # Colour in which to draw graph. - bars = False # Use True for a bar chart. We'll probably never use this. - ) -""" - - - -## SECTION: main() - -def main(): - display = create_PiicoDev_SSD1306() - - print("Screen width is " + str(WIDTH)) - print("Screen height is " + str(HEIGHT)) - sleep_ms(2000) - - print("==== TEST: Basic functionality ====") - draw_lines(display) - draw_rectangles(display) - - print("==== TEST: Displaying text and images ====") - draw_text(display) - draw_images(display) - - print("==== TEST: Graph ====") - draw_graph(display) - - print("==== TEST: COMPLETE! ====") - - - -## TEST: Basic functionality - -def draw_lines(display): - print(" BEGIN draw_lines()") - sleep_ms(2000) - # Draw a line from (0, 0) to (WIDTH, HEIGHT) - print("\tDrawing line from (0, 0) to (WIDTH, HEIGHT)") - start_x = 0 - start_y = 0 - end_x = WIDTH - end_y = HEIGHT - colour = 1 # COLOURS: `0` for black or `1` for white - display.line(start_x, start_y, end_x, end_y, colour) # Logically draw this line - display.show() # Render all things logically drawn - sleep_ms(5000) - # Blank the screen - print("\tBlanking the screen") - display.fill(0) # Parameter is a COLOUR, either `0` for black or `1` for white. - display.show() # Render - sleep_ms(1000) - # Draw a funny thing - print("\tDrawing a funny shape") - points = [None] * 5 - for i in range(5): - points[i] = ( WIDTH / 2 + cos(2 * pi / 5 * i) * HEIGHT / 2 , # how tf do you continue a line again in python - HEIGHT / 2 + sin(2 * pi / 5 * i) * HEIGHT / 2 ) - for i in range(5): - display.line(points[i][0], points[i][1], points[(i + 2) % 5][0], points[(i + 2) % 5][1], 1) - display.line(points[i][0], points[i][1], points[(i + 3) % 5][0], points[(i + 3) % 5][1], 1) - display.show() # Render - sleep_ms(5000) - # Blank the screen - print("\tBlanking the screen") - display.fill(0) - display.show() - print(" END draw_lines()") - -def draw_rectangles(display): - print(" BEGIN draw_rectangles()") - sleep_ms(2000) - # Draw an unfilled rectangle - print("\tDrawing an unfilled rectangle") - start_x = 0 - start_y = 0 - end_x = WIDTH / 2 - end_y = HEIGHT / 2 - colour = 1 # white - display.rect(start_x, start_y, end_x, end_y, colour) # UNFILLED rectangle - display.show() # render - sleep_ms(5000) - # Blank screen - print("\tBlanking the screen") - display.fill(0) - display.show() - # Draw a filled rectangle - print("\tDrawing a filled rectangle") - display.fill_rect(start_x, start_y, end_x, end_y, colour) # FILLED rectangle - display.show() # render - sleep_ms(5000) - # Blank the screen - print("\tBlanking the screen") - display.fill(0) - display.show() - print(" END draw_rectangles()") - - - -## TEST: Text and images - -def draw_text(display): - print(" BEGIN draw_text()") - sleep_ms(2000) - - # This should link to the font-pet-me-128.dat file in the current directory. - # **I have no idea how to change the location where we store this file.** - - # Hello world - print("\tDrawing hello world") - text_to_display = "Hello, World!" - top_left_x = 0 - top_left_y = 0 - colour = 1 - display.text(text_to_display, top_left_x, top_left_y, colour) - display.show() # render - sleep_ms(5000) - # Long string - print("\tDrawing long string") - display.text("The quick brown fox jumped over the lazy dog", 0, 15, 1) - display.show() - sleep_ms(5000) - # Blank screen - print("\tBlanking screen") - display.fill(0) - display.show() - sleep_ms(5000) - # Goodbye world - print("\tDrawing goodbye world") - display.text("Goodbye, world T-T", 0, 0, 1) - display.show() - sleep_ms(5000) - # Blank the screen - print("\tBlanking the screen") - display.fill(0) - display.show() - print(" END draw_text") - -def draw_images(display): - print(" BEGIN draw_images()") - sleep_ms(2000) - # Images are drawn from .pbm (Portable BitMap image) files. - # They should have dimensions 128 x 64. - - # Draw PiicoDev logo - print("\tDisplaying PiicoDev logo") - colour = 1 - display.load_pbm("./resources/piicodev-logo.pbm", colour) - display.show() # render - sleep_ms(5000) - # Blank screen - print("\tBlanking screen") - display.fill(0) - display.show() - sleep_ms(5000) - # Draw hilarious image - print("\tDisplaying hilarious image") - display.load_pbm("./resources/hilarious.pbm", 1) - display.show() - sleep_ms(5000) - # Blank screen - print("\tBlanking screen") - display.fill(0) - display.show() - print(" END draw_images()") - - - -## TEST: Graph - -def draw_graph(display): - print(" BEGIN draw_graph()") - sleep_ms(2000) - - curve = display.graph2D(minValue = -1, maxValue = 1, height = HEIGHT, width = WIDTH) - # minValue= key is for the minimum value on the logical graph. This gets mapped into pixels automatically. - # maxValue= key is for the maximum value on the logical graph. This gets mapped into pixels automatically. - # height= key is for maximum display height. - # width= key is for maximum display width. - - # Display a sine curve - print("\tDisplaying a sine curve") - for x in range(WIDTH): - y_value = sin(2 * pi * x / WIDTH) - display.fill(0) # NOTE: I don't know if this is necessary or not. - display.updateGraph2D(curve, y_value) # Add to the graph - display.show() # render - sleep_ms(5000) - # Add to the same curve - print("\tAdding another period to that curve, slowly") - for x in range(WIDTH): - display.fill(0) # When you add to the curve, I reckon this will mean that the whole graph overwrites the previous one. Otherwise, you'd get white garbage all over the screen - display.updateGraph2D(curve, sin(2 * pi * x / WIDTH)) # idk what this will do - display.show() - sleep_ms(100) # wait 1/10 of a second so that we can see what this does - sleep_ms(5000) - # Use a new curve to plot random values - print("\tPlotting random values on a new curve, slowly") - curve = display.graph2D(minValue = 0, maxValue = 1) # omitted height= and width= keys default to HEIGHT and WIDTH - for _ in range(WIDTH * 2): - display.fill(0) - display.updateGraph2D(curve, random.random()) - display.show() - sleep_ms(100) - sleep_ms(5000) - # Plot two curves simultaneously - print("\tPlotting two curves simultaneously") - linear = display.graph2D(minValue = 0, maxValue = WIDTH) - sinusoidal = display.graph2D(minValue = -1, maxValue = 1) - for x in range(WIDTH): - display.fill(0) - display.updateGraph2D(linear, x) - display.updateGraph2D(sinusoidal, sin(2 * pi * x / WIDTH)) - display.show() - sleep_ms(5000) - # Blank display - print("\tBlanking display") - display.fill(0) - display.show() - print(" END draw_graph()") - - - -## LAUNCH - -if __name__ == "__main__": - main() - diff --git a/client/proof_of_concept/flood_database_with_data.py b/client/proof_of_concept/flood_database_with_data.py deleted file mode 100644 index 94592dc..0000000 --- a/client/proof_of_concept/flood_database_with_data.py +++ /dev/null @@ -1,59 +0,0 @@ -""" -File: - sitting-desktop-garden/client/proof_of_concept/flood_database_with_data.py - -Purpose: - Providing debug test data - -Author: - Gabriel Field (47484306) - -Source: - Largely based on https://projects.raspberrypi.org/en/projects/getting-started-with-picamera/0 -""" - -## SECTION: Imports - -from datetime import datetime, timedelta -import random - -import data.routines as db - - - -## SECTION: Moses - -NUMBER_OF_SECONDS_IN_AN_HOUR = 3600 -"""title""" - -def DEBUG_flood() -> None: - """ - Flood the database with debug test data, for user with ID 1, set up for THE NEXT HOUR. - The debug test data includes a random posture record for each SECOND of the hour. - """ - print(" Killing database") - db.destroy_database() - print(" Initialising database") - db.init_database() - print(" Making user") - user_id = db.create_user() - print(f" {user_id=}") - print(" Flooding with posture records...") - now = datetime.now() - for second in range(NUMBER_OF_SECONDS_IN_AN_HOUR): - db.save_posture(posture = db.Posture( - id_ = None, - user_id = user_id, - prop_good = random.random(), - prop_in_frame = random.random(), - period_start = now + timedelta(seconds = second), - period_end = now + timedelta(seconds = second + 1) - )) - print(" Database flooded. Exiting.") - return - - -## LAUNCH - -if __name__ == "__main__": - DEBUG_flood() diff --git a/client/proof_of_concept/font-pet-me-128.dat b/client/proof_of_concept/font-pet-me-128.dat deleted file mode 100644 index ad4d9e3d0514af3fb3010ed1d35af26321291aec..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 768 zcmX|9O>5gg5FJ}WUK3H5Vu}i)af4_}4nd?~oFMc;<+g$vH=sfTrYMnV4>5$c2D@bK z@z3a?ha7V1Z^$7(rHB3um-LMsI%4b(Wpk7xh;ddy|c5!2Snuy z$Np3j?ekzT`}z)%re)LsZdfLgn3SPQq;^Bs4R;kO6^2T|zxrLsv8qf~MIm!s_>OJW zDdeorA@(VA921>>JIaQyh<3^><1Ufov>!6xr)Qu0{lx)Ee0H%LUT$qIH4)#{gFh*EWWQ-l z{8ERx*{q>&oM#t@vR8^3LvB&svRH_#)Kdh-f=1ifCKne8)$ja^|`-+)JZ1pSN&#Ctr>SuU|q4S|s6FVmDfqQyey_-3{Gixu?sCMg`CjR9IazN0m86EF3JXTA2kz`;ESp0{30oM}QI zy&2{Cv_qyDaXRi01v-v(faewzMc@K8`qwU{lU~L1^ZN2q^rYkI=q-5h{$|w=pxax} Sqy1U+9_vQU!1(ok^Zf(-nxWPJ diff --git a/client/proof_of_concept/gpio.py b/client/proof_of_concept/gpio.py deleted file mode 100644 index a79a37b..0000000 --- a/client/proof_of_concept/gpio.py +++ /dev/null @@ -1,57 +0,0 @@ -""" -File: - sitting-desktop-garden/client/proof_of_concept/gpio.py - -Purpose: - Hardware test for the GPIO pins, interfacing through RPi.GPIO. - -Author: - Gabriel Field (47484306) - -Source: - Largely based on https://elinux.org/RPi_GPIO_Code_Samples#RPi.GPIO - See also https://elinux.org/RPi_Low-level_peripherals#General_Purpose_Input.2FOutput_.28GPIO.29 -""" - -## SECTION: Imports - -import RPi.GPIO as GPIO # WARNING: This should be installed by default on the RPi, - # but not on your local machine. - # I haven't `poetry add`ed it (I tried, but it failed >:( ). -#from PiicoDev_Unified import sleep_ms -from time import sleep - - - -## SECTION: main() - -def main(): - print(" BEGIN main()...") - GPIO.setmode(GPIO.BCM) # use pin numbering convention as per the PiicoDev header - - # Set data directions - GPIO.setup(7, GPIO.IN) - GPIO.setup(8, GPIO.OUT) - - print(" Will read from pin 7 into code; will write to pin 8.") - print(" Doing that in 5 seconds...") - sleep(5) - - input_value = GPIO.input(7) - print(" Read " + str(input_value) + " from pin 7") - GPIO.output(8, GPIO.HIGH) - print(" Wrote high to pin 8") - - print(" Writing low to pin 8 in 2 seconds") - sleep(2) - GPIO.output(8, GPIO.LOW) - - print(" END main()") - - - -## LAUNCH - -if __name__ == "__main__": - main() - diff --git a/client/proof_of_concept/resources/hilarious.pbm b/client/proof_of_concept/resources/hilarious.pbm deleted file mode 100644 index 01006c996ee90f35d07d4083f92dd208e78152df..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1034 zcmZ|Ozl+pB6bJC{WOA8fFEA-1EMzL#GRh$-7H|Czwh(O>E6X&(70YFog|m{Y?5`9A zZ?UqRy~S26t`v=IAy|!Fie<;i4>w8S3xs^$ym^_Kkm>2*!NWv6IvwCYaoowr5b((% zp<<#C(1WFLW(Wx!Rj2HDj*x;}*OsxnLQHWoYsLi)5IgWi?OQzvaq%9bN$tI01zvoY z(`juLyFQ-W$Lipr59MLs)GKl9LAqqQs_Q55$RX0D_}VxaQ3BtSqOsjhX~eb5JAvJ> zD4&=qV-LS^R@tZ!RXs80>dK~cQ%{Xy<&>SBTFz%?@7SDefYcJ)7_VyCIBM!m>#CoT zb^SQbUi0EiXa9_dF~wD+9;bYr59i>FNqZ@y&+^iYF;LL8(V5PZIjFS*y8Wq(bDhjN zWgfd^i>=anoF?;V$vpmzueUPI$LDG7U1hUND!bhJmrSkm!}vy~8pFn4_+le{YWQhLwNK^wuJC;*&!w&-t-*cZPzNshpuIS zPd&%D>zLSIoV^{KP54ioA@6<%Sol3GVROxP5v^n)%|f~#oEx4h07*I~{TYWPcV!)mv` z6kHSE@jIO?g;s>A$oZF+#=p*Fl+R=CpQ27ODt$K}Ow7C)6~3FFl2kJd;=sO7G09wh kl{(JnS(?2#CMGajMZN_W`4_B;{ygko|F6*Z-+vSM2A>|mL;wH) diff --git a/client/proof_of_concept/servo_driver.py b/client/proof_of_concept/servo_driver.py deleted file mode 100644 index 2d93049..0000000 --- a/client/proof_of_concept/servo_driver.py +++ /dev/null @@ -1,101 +0,0 @@ -""" -File: - sitting-desktop-garden/client/proof_of_concept/servo_driver.py - -Purpose: - Hardware test for the PiicoDev servos and server driver. - -Author: - Gabriel Field (47484306) - -Source: - Largely based on https://core-electronics.com.au/guides/piicodev/piicodev-servo-driver-pca9685-getting-started-guide/#JEFNE3E -""" - -## SECTION: Imports - -from PiicoDev_Servo import PiicoDev_Servo, PiicoDev_Servo_Driver -from PiicoDev_Unified import sleep_ms -from math import sin, pi - - - -## SECTION: main() - -def main(): - print("==== TEST: Basic use ====") - controller = PiicoDev_Servo_Driver() # Initialise the Servo Driver Module - servo = PiicoDev_Servo(controller, 1) # Simple setup: Attach a servo to channel 1 of the controller with default properties - test_step(servo) - continuous_servo = PiicoDev_Servo(controller, 1, midpoint_us=1500, range_us=1800) # Connect a 360° servo to channel 2 - test_spinny(continuous_servo) - - print(" Hook up two servos, with channels 1 and 2") - print(" You have 10 seconds") - sleep_ms(10000) - print(" Assuming they're allg") - - print("==== TEST: Multiple servos ====") - - - print("==== TEST: COMPLETE! ====") - servo1 = PiicoDev_Servo(controller, 1) - servo2 = PiicoDev_Servo(controller, 2) - test_multiple(servo1, servo2) - - -## TEST: Basic use - -def test_step(servo): - print(" BEGIN test_step()") - sleep_ms(2000) - print("\tStepping the servo") - servo.angle = 0 - sleep_ms(1000) - servo.angle = 90 - print(" END test_step()") - -def test_spinny(continuous_servo): - print(" BEGIN test_spinny()") - sleep_ms(2000) - print("\tStopping") - continuous_servo.speed = 0 - sleep_ms(5000) - print("\tGoing full-speed forwards") - continuous_servo.speed = 1 - sleep_ms(5000) - print("\tGoing half-speed forwards") - continuous_servo.speed = 0.5 - sleep_ms(5000) - print("\tGoing half-speed backwards") - continuous_servo.speed = -0.5 - sleep_ms(5000) - print("\tGoing full-speed backwards") - continuous_servo.speed = -1 - sleep_ms(5000) - print("\tStopping") - continuous_servo.speed = 0 - sleep_ms(5000) - print(" END test_spinny()") - - - -## TEST: Multiple servos - -def test_multiple(servo1, servo2): - print(" BEGIN test_multiple()") - sleep_ms(2000) - # This takes 18 seconds - for i in range(1800): - servo1.angle = 90 + 90 * sin(i * 2 * pi / 1800) - servo2.angle = 90 - 90 * sin(i * 2 * pi / 1800) - sleep_ms(10) - print(" END test_multiple()") - - - - -## LAUNCH - -if __name__ == "__main__": - main()