Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
refactor: change prints to logger debug message
Browse files Browse the repository at this point in the history
  • Loading branch information
LimaoC committed Oct 17, 2024
1 parent acaeeb9 commit db1b967
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 36 deletions.
4 changes: 2 additions & 2 deletions client/drivers/camera_overlord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 14 additions & 9 deletions client/drivers/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Gabriel Field (47484306), Mitchell Clark
"""

import logging
import time
from datetime import datetime
from math import pi, sin
Expand All @@ -22,6 +23,8 @@
RIGHT_BUTTON = 1
DOUBLE_RIGHT_BUTTON = 2

logger = logging.getLogger(__name__)


class ControlledData:
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -156,17 +161,15 @@ 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.
Args:
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)

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand Down
53 changes: 28 additions & 25 deletions client/drivers/pi_overlord.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,6 +61,7 @@

logger = logging.getLogger(__name__)


def main():
"""
Entry point for the control program.
Expand Down Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -264,15 +265,17 @@ 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
average_prop_in_frame = sum(
[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())
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -375,38 +378,38 @@ 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
average_prop_in_frame = sum(
[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
Expand All @@ -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()

Expand All @@ -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

Expand All @@ -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())
Expand All @@ -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
Expand Down

0 comments on commit db1b967

Please sign in to comment.