From db1b9673e1728b3a2b8b6537099b911492eff206 Mon Sep 17 00:00:00 2001 From: Limao Chang <80520563+LimaoC@users.noreply.github.com> Date: Thu, 17 Oct 2024 21:25:47 +1000 Subject: [PATCH] refactor: change prints to logger debug message --- client/drivers/camera_overlord.py | 4 +-- client/drivers/data_structures.py | 23 ++++++++------ client/drivers/pi_overlord.py | 53 ++++++++++++++++--------------- 3 files changed, 44 insertions(+), 36 deletions(-) diff --git a/client/drivers/camera_overlord.py b/client/drivers/camera_overlord.py index f55ea51..1e74c30 100644 --- a/client/drivers/camera_overlord.py +++ b/client/drivers/camera_overlord.py @@ -40,13 +40,13 @@ def handle_quit(signo, frame): f = open("/tmp/snapshot.jpg", "x") f.close() except FileExistsError: - print("Snapshot already exists") + logger.debug("Snapshot already exists") try: f = open("/tmp/big_brother.jpg", "x") f.close() except FileExistsError: - print("Big Brother already exists") + logger.debug("Big Brother already exists") while True: for _ in range(2 * 3): diff --git a/client/drivers/data_structures.py b/client/drivers/data_structures.py index dafc654..383e493 100644 --- a/client/drivers/data_structures.py +++ b/client/drivers/data_structures.py @@ -5,6 +5,7 @@ Gabriel Field (47484306), Mitchell Clark """ +import logging import time from datetime import datetime from math import pi, sin @@ -22,6 +23,8 @@ RIGHT_BUTTON = 1 DOUBLE_RIGHT_BUTTON = 2 +logger = logging.getLogger(__name__) + class ControlledData: """ @@ -80,7 +83,9 @@ def make_empty(cls, user_id: int) -> "ControlledData": return_me._last_snapshot_time = datetime.now() return_me._last_cushion_time = datetime.now() return_me._last_plant_time = datetime.now() - print(" Made a new empty ControlledData() with user_id", return_me._user_id) + logger.debug( + " Made a new empty ControlledData() with user_id %d", return_me._user_id + ) return return_me @classmethod @@ -156,9 +161,7 @@ def set_last_plant_time(self, time: datetime) -> None: """ self._last_plant_time = time - def accept_new_posture_data( - self, posture_data: List[float] - ) -> None: + def accept_new_posture_data(self, posture_data: List[float]) -> None: """ Update the internal store of posture data for the OLED display. @@ -166,7 +169,7 @@ def accept_new_posture_data( posture_data: new posture data to accept and merge with the current state of this object. """ - print(" accept_new_posture_data()") + logger.debug(" accept_new_posture_data()") for datum in posture_data: self._posture_data.put_nowait(datum) @@ -326,7 +329,9 @@ def unwind_plant(self) -> None: """ self.plant_mover.speed = self._FULL_SPEED_UPWARDS time.sleep(16 * self._PLANT_MOVER_PERIOD * self._PLANT_GEAR_RATIO / 1000) - self.plant_height = self._PLANT_SHAFT_TURNS - self._PLANT_SHAFT_SAFETY_BUFFER_TURNS + self.plant_height = ( + self._PLANT_SHAFT_TURNS - self._PLANT_SHAFT_SAFETY_BUFFER_TURNS + ) self.plant_mover.speed = 0 def wind_plant_safe(self) -> None: @@ -353,7 +358,7 @@ def set_plant_height(self, new_height: int) -> None: new_height: height to which to drive the I. Jensen Plant Mover 10000 """ self.plant_mover.speed = 0 - print(f" set_plant_height: {self.plant_height=}, {new_height=}") + logger.debug(f" set_plant_height: {self.plant_height=}, {new_height=}") distance = new_height - self.plant_height distance = distance if distance > 0 else (-1) * distance if new_height == self.plant_height: @@ -363,13 +368,13 @@ def set_plant_height(self, new_height: int) -> None: new_height > self._PLANT_SHAFT_TURNS - self._PLANT_SHAFT_SAFETY_BUFFER_TURNS - 1 ): - print( + logger.debug( " Plant mover not schmovin': can't get that high mate, that's just unsafe" ) self.plant_mover.speed = 0 return if new_height < 0: - print( + logger.debug( " Plant mover not schmovin': can't get that low mate, that's just dirty" ) self.plant_mover.speed = 0 diff --git a/client/drivers/pi_overlord.py b/client/drivers/pi_overlord.py index 94ad797..9f0f87d 100644 --- a/client/drivers/pi_overlord.py +++ b/client/drivers/pi_overlord.py @@ -44,14 +44,14 @@ NUM_DATA_POINTS_PER_TIMEOUT = 3 #: Minimum delay between consecutive uses of the vibration motor. Used in handle_feedback(). -HANDLE_CUSHION_FEEDBACK_TIMEOUT = timedelta(milliseconds=30000) +HANDLE_CUSHION_FEEDBACK_TIMEOUT = timedelta(milliseconds=30000) #: Length of time for which the vibration motor should vibrate. Used in handle_cushion_feedback(). CUSHION_ACTIVE_INTERVAL = timedelta(milliseconds=3000) #: Threshold for vibration cushion feedback. If the proportion of "good" sitting posture is below this, the cushion will vibrate. CUSHION_PROPORTION_GOOD_THRESHOLD = 0.5 #: Minimum delay between consecutive uses of the plant-controlling servos. Used in handle_feedback(). -HANDLE_PLANT_FEEDBACK_TIMEOUT = timedelta(milliseconds=15000) +HANDLE_PLANT_FEEDBACK_TIMEOUT = timedelta(milliseconds=15000) #: Threshold for I. Jensen Plant Mover 10000 feedback. If the proportion of "good" sitting posture is below this, #: the plant will move down. PLANT_PROPORTION_GOOD_THRESHOLD = 0.5 @@ -61,6 +61,7 @@ logger = logging.getLogger(__name__) + def main(): """ Entry point for the control program. @@ -129,7 +130,7 @@ def initialise_hardware() -> HardwareComponents: Object consisting of all hardware components connected to the Raspberry Pi. """ - print(" initialise_hardware()") + logger.debug(" initialise_hardware()") return_me = HardwareComponents.make_fresh() # Clear button queues return_me.button0.was_pressed @@ -140,7 +141,7 @@ def initialise_hardware() -> HardwareComponents: # Write low to stop buzzer from mistakenly buzzing, if necessary GPIO.output(CUSHION_GPIO_PIN, GPIO.LOW) - print(" initialise_hardware() FINISHED") + logger.debug(" initialise_hardware() FINISHED") return return_me @@ -189,7 +190,7 @@ def run_user_session(user: ControlledData) -> None: hardware.oled_display_text(LOGOUT_MESSAGE, 0, 0, 1) hardware.display.show() sleep_ms(LOGOUT_SUCCESS_DELAY) - print(" END run_user_session()") + logger.debug(" END run_user_session()") return # Run core functionality @@ -264,7 +265,9 @@ def handle_posture_graph(user: ControlledData) -> bool: # Exit if no data if len(recent_posture_data) == 0: - print(" Exiting handle_posture_monitoring_new() early: Not enough data") + logger.debug( + " Exiting handle_posture_monitoring_new() early: Not enough data" + ) return True # Exit if person not in frame enough @@ -272,7 +275,7 @@ def handle_posture_graph(user: ControlledData) -> bool: [posture.prop_in_frame for posture in recent_posture_data] ) / len(recent_posture_data) if average_prop_in_frame < PROPORTION_IN_FRAME_THRESHOLD: - print( + logger.debug( " Exiting handle_posturing_monitoring_new() early: Not in frame for a high enough proportion of time." ) user.set_last_snapshot_time(datetime.now()) @@ -285,8 +288,8 @@ def handle_posture_graph(user: ControlledData) -> bool: # Calculate total time span start_time = recent_posture_data[0].period_start - end_time = recent_posture_data[-1].period_end - total_time = end_time - start_time + end_time = recent_posture_data[-1].period_end + total_time = end_time - start_time # Calculate the interval length interval = total_time / NUM_DATA_POINTS_PER_TIMEOUT @@ -309,7 +312,7 @@ def handle_posture_graph(user: ControlledData) -> bool: for posture_list in split_posture_lists: if len(posture_list) == 0: continue - print(f" {posture_list=}") + logger.debug(f" {posture_list=}") average_prop_good = sum( [posture.prop_good for posture in posture_list] ) / len(posture_list) @@ -362,7 +365,7 @@ def handle_cushion_feedback(user: ControlledData) -> bool: Ensures: ! user.is_failed() """ - print(" handle_cushion_feedback()") + logger.debug(" handle_cushion_feedback()") # Load posture records within the last HANDLE_CUSHION_FEEDBACK_TIMEOUT now = datetime.now() @@ -375,7 +378,7 @@ def handle_cushion_feedback(user: ControlledData) -> bool: # Exit if no data if len(recent_posture_data) == 0: - print(" Exiting handle_cushion_feedback() early: No data") + logger.debug(" Exiting handle_cushion_feedback() early: No data") user.set_last_cushion_time(datetime.now()) return True # Exit if person not in frame enough @@ -383,30 +386,30 @@ def handle_cushion_feedback(user: ControlledData) -> bool: [posture.prop_in_frame for posture in recent_posture_data] ) / len(recent_posture_data) if average_prop_in_frame < PROPORTION_IN_FRAME_THRESHOLD: - print( + logger.debug( " Exiting handle_cushion_feedback() early: Not in frame for a high enough proportion of time." ) user.set_last_cushion_time(datetime.now()) return True - + # Get average proportion of good posture average_prop_good = sum( [posture.prop_good for posture in recent_posture_data] ) / len(recent_posture_data) if average_prop_good >= CUSHION_PROPORTION_GOOD_THRESHOLD: - print(" Exiting handle_cushion_feedback() early: You sat well :)") + logger.debug(" Exiting handle_cushion_feedback() early: You sat well :)") user.set_last_cushion_time(datetime.now()) return True # If posture not good enough, turn buzzer on buzzer_start_time = datetime.now() GPIO.output(CUSHION_GPIO_PIN, GPIO.HIGH) - print(" buzzer on") + logger.debug(" buzzer on") while datetime.now() < buzzer_start_time + CUSHION_ACTIVE_INTERVAL: sleep_ms(100) # Turn buzzer off GPIO.output(CUSHION_GPIO_PIN, GPIO.LOW) - print(" buzzer off") + logger.debug(" buzzer off") user.set_last_cushion_time(datetime.now()) return True @@ -428,7 +431,7 @@ def handle_plant_feedback(user: ControlledData) -> bool: Ensures: ! user.is_failed() """ - print(" handle_plant_feedback()") + logger.debug(" handle_plant_feedback()") now = datetime.now() @@ -443,7 +446,7 @@ def handle_plant_feedback(user: ControlledData) -> bool: # Exit if no data if len(recent_posture_data) == 0: - print(" Exiting handle_plant_feedback() early: No data") + logger.debug(" Exiting handle_plant_feedback() early: No data") user.set_last_plant_time(datetime.now()) return True @@ -452,7 +455,7 @@ def handle_plant_feedback(user: ControlledData) -> bool: [posture.prop_in_frame for posture in recent_posture_data] ) / len(recent_posture_data) if average_prop_in_frame < PROPORTION_IN_FRAME_THRESHOLD: - print( + logger.debug( " Exiting handle_plant_feedback() early: Not in frame for a high enough proportion of time." ) user.set_last_plant_time(datetime.now()) @@ -476,18 +479,18 @@ def handle_plant_feedback(user: ControlledData) -> bool: def _reset_garden() -> None: """Reset data, faces and hardware.""" - print(" Burning the garden to the ground...") + logger.debug(" Burning the garden to the ground...") global hardware destroy_database() - print("\t initialising database anew...") + logger.debug("\t initialising database anew...") init_database() - print("\t resetting face embeddings...") + logger.debug("\t resetting face embeddings...") reset_registered_face_embeddings() - print("\t initialising hardware...") + logger.debug("\t initialising hardware...") hardware = initialise_hardware() - print("\t Like a phoenix, the Sitting Desktop Garden rises anew") + logger.debug("\t Like a phoenix, the Sitting Desktop Garden rises anew") # LAUNCH