From 3c3010d5908a478baa86bd23c559039a1102eaf7 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 25 Oct 2022 22:01:26 +0700 Subject: [PATCH] added environment sensing --- kuukar.py | 4 ++- kuukar_config.py | 2 ++ kuukar_cv.py | 6 ++++- kuukar_environment.py | 33 +++++++++++++++++++++++ kuukar_lcd.py | 7 ++++- kuukar_leds.py | 8 +++--- kuukar_motion.py | 62 ++++++++++++++++++++++--------------------- kuukar_sensors.py | 7 ++--- kuukar_temperature.py | 17 ------------ 9 files changed, 90 insertions(+), 56 deletions(-) create mode 100644 kuukar_environment.py delete mode 100644 kuukar_temperature.py diff --git a/kuukar.py b/kuukar.py index f513c19..ec45e84 100644 --- a/kuukar.py +++ b/kuukar.py @@ -1,5 +1,5 @@ from telemetrix_rpi_pico import telemetrix_rpi_pico -import kuukar_leds, kuukar_collision, kuukar_motion, kuukar_lcd, kuukar_sensors +import kuukar_leds, kuukar_collision, kuukar_motion, kuukar_lcd, kuukar_sensors, kuukar_environment from kuukar_config import SERIAL_AUX_BOARD, SERIAL_DRIVER_BOARD if __name__ == '__main__': @@ -8,4 +8,6 @@ if __name__ == '__main__': sensors = kuukar_sensors.sensors(aux_board,driver_board) leds = kuukar_leds.leds(aux_board=aux_board) lcd = kuukar_lcd.lcd() + environment = kuukar_environment.environment(lcd,leds,sensors) + collision = kuukar_collision.collision(aux_board,lcd,leds) motion = kuukar_motion.motion(driver_board=driver_board,leds=leds,sensors= sensors) \ No newline at end of file diff --git a/kuukar_config.py b/kuukar_config.py index d801b0a..3c70dc2 100644 --- a/kuukar_config.py +++ b/kuukar_config.py @@ -1,5 +1,6 @@ SERIAL_DRIVER_BOARD = "/dev/serial/by-id/..." +# HC Sonar Pins SONAR_1_TRIG_PIN = 0 SONAR_1_ECHO_PIN = 1 SONAR_2_TRIG_PIN = 2 @@ -7,6 +8,7 @@ SONAR_2_ECHO_PIN = 3 SONAR_3_TRIG_PIN = 4 SONAR_3_ECHO_PIN = 5 +# Motor Pins MOTOR_FL_F = 6 MOTOR_FL_R = 7 MOTOR_FR_F = 8 diff --git a/kuukar_cv.py b/kuukar_cv.py index 7b6a6ce..2aac7f5 100644 --- a/kuukar_cv.py +++ b/kuukar_cv.py @@ -1,14 +1,18 @@ from kuukar_lcd import lcd from kuukar_leds import leds + class cv: def __init__(self, lcd: lcd, leds: leds) -> None: self.lcd = lcd self.leds = leds self.noise = noise + def capture_image(self): pass + def detectFace(self, image): pass + def handle_face_detected(self): - pass \ No newline at end of file + pass diff --git a/kuukar_environment.py b/kuukar_environment.py new file mode 100644 index 0000000..3f46747 --- /dev/null +++ b/kuukar_environment.py @@ -0,0 +1,33 @@ +import threading +from time import sleep +from kuukar_lcd import lcd +from kuukar_leds import leds +from kuukar_sensors import sensors + + +class environment: + t_alerted = False + h_alerted = False + def __init__(self, lcd: lcd, leds: leds, sensors: sensors) -> None: + self.lcd = lcd + self.leds = leds + self.sensors = sensors + threading.Thread(target=self.__checker__).start() + + def __checker__(self): + while True: + if self.sensors.get_temperature() > 38.0: + if not self.t_alerted: + lcd.keke_died() + self.t_alerted = True + else: + self.t_alerted = False + + if self.sensors.get_humidity_pct() > 60.0: + if not self.h_alerted: + lcd.keke_cute_noise() + self.h_alerted = True + else: + self.h_alerted = False + + sleep(5) \ No newline at end of file diff --git a/kuukar_lcd.py b/kuukar_lcd.py index 415cd3b..11faf1b 100644 --- a/kuukar_lcd.py +++ b/kuukar_lcd.py @@ -5,9 +5,15 @@ class lcd: def keke_uwu(self): pass + def keke_cute_noise(self): + pass + def keke_hurt(self): pass + def keke_died(self): + pass + def takina_noise(self): pass @@ -20,7 +26,6 @@ class lcd: def resume_video(self) -> None: pass - def stop_video(self) -> None: pass \ No newline at end of file diff --git a/kuukar_leds.py b/kuukar_leds.py index 3734d41..a9c6278 100644 --- a/kuukar_leds.py +++ b/kuukar_leds.py @@ -1,10 +1,12 @@ from telemetrix_rpi_pico import telemetrix_rpi_pico from kuukar_config import LEDS_DATA_PIN, LEDS_NUM + + class leds: def __init__(self, aux_board: telemetrix_rpi_pico.TelemetrixRpiPico) -> None: self.aux_board = aux_board - self.aux_board.set_pin_mode_neopixel(LEDS_DATA_PIN,LEDS_NUM) + self.aux_board.set_pin_mode_neopixel(LEDS_DATA_PIN, LEDS_NUM) - def flash(self, r: int,g: int,b: int,duration_ms: int): - pass \ No newline at end of file + def flash(self, r: int, g: int, b: int, duration_ms: int): + pass diff --git a/kuukar_motion.py b/kuukar_motion.py index ae85b87..43a67b5 100644 --- a/kuukar_motion.py +++ b/kuukar_motion.py @@ -6,6 +6,7 @@ from kuukar_sensors import sensors from telemetrix_rpi_pico import telemetrix_rpi_pico import threading + class motion: roaming = False @@ -14,32 +15,33 @@ class motion: self.driver = driver_board self.leds = leds self.sensors = sensors - self.roam_thread = threading.Thread(target = self.roam_loop) + self.roam_thread = threading.Thread(target=self.roam_loop) self.roam_thread.start() - - drive_pins = [MOTOR_FL_F,MOTOR_FL_R,MOTOR_FR_F,MOTOR_FR_R,MOTOR_RL_F,MOTOR_RL_R,MOTOR_RR_F,MOTOR_RR_R] + + drive_pins = [MOTOR_FL_F, MOTOR_FL_R, MOTOR_FR_F, + MOTOR_FR_R, MOTOR_RL_F, MOTOR_RL_R, MOTOR_RR_F, MOTOR_RR_R] for drive_pin in drive_pins: self.driver.set_pin_mode_pwm_output(drive_pin) - def motor_write(self, forward_pin: int,reverse_pin: int, speed: int): + def motor_write(self, forward_pin: int, reverse_pin: int, speed: int): if speed < 0: - self.driver.pwm_write(forward_pin,0) - self.driver.pwm_write(reverse_pin,-speed) + self.driver.pwm_write(forward_pin, 0) + self.driver.pwm_write(reverse_pin, -speed) elif speed >= 0: - self.driver.pwm_write(forward_pin,speed) - self.driver.pwm_write(reverse_pin,0) + self.driver.pwm_write(forward_pin, speed) + self.driver.pwm_write(reverse_pin, 0) def drive(self, speed: int): - self.motor_write(MOTOR_FL_F,MOTOR_FL_R,speed) - self.motor_write(MOTOR_FR_F,MOTOR_FR_R,speed) - self.motor_write(MOTOR_RL_F,MOTOR_RL_R,speed) - self.motor_write(MOTOR_RR_F,MOTOR_RR_R,speed) - + self.motor_write(MOTOR_FL_F, MOTOR_FL_R, speed) + self.motor_write(MOTOR_FR_F, MOTOR_FR_R, speed) + self.motor_write(MOTOR_RL_F, MOTOR_RL_R, speed) + self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed) + def turn(self, speed: int, duration: float): - self.motor_write(MOTOR_FL_F,MOTOR_FL_R,speed) - self.motor_write(MOTOR_FR_F,MOTOR_FR_R,-speed) - self.motor_write(MOTOR_RL_F,MOTOR_RL_R,speed) - self.motor_write(MOTOR_RR_F,MOTOR_RR_R,-speed) + self.motor_write(MOTOR_FL_F, MOTOR_FL_R, speed) + self.motor_write(MOTOR_FR_F, MOTOR_FR_R, -speed) + self.motor_write(MOTOR_RL_F, MOTOR_RL_R, speed) + self.motor_write(MOTOR_RR_F, MOTOR_RR_R, -speed) time.sleep(duration) self.stop() @@ -60,29 +62,29 @@ class motion: def __roam_handle__(self): self.drive(50) f_dist = self.sensors.sonar_get_distance(2) - if f_dist > 0 and f_dist < 20: # Close to collision, turn the vehicle + if f_dist > 0 and f_dist < 20: # Close to collision, turn the vehicle l_dist = self.sensors.sonar_get_distance r_dist = self.sensors.sonar_get_distance - if l_dist < 20: #Left side is blocked - if r_dist < 20: #Both side are blocked, do a full 180deg turn - self.turn(100,2*TURN_TIME_FS_90DEG_MS) + if l_dist < 20: # Left side is blocked + if r_dist < 20: # Both side are blocked, do a full 180deg turn + self.turn(100, 2*TURN_TIME_FS_90DEG_MS) self.drive(50) time.sleep(3) else: - #Right Side is Free, turn right - self.turn(100,TURN_TIME_FS_90DEG_MS) + # Right Side is Free, turn right + self.turn(100, TURN_TIME_FS_90DEG_MS) self.drive(50) time.sleep(3) - elif r_dist < 20: #Right side is blocked, left side is free, turn left - self.turn(-100,TURN_TIME_FS_90DEG_MS) - else: #Both Side are free, randomize a direction - right: bool = randint(0,1) + elif r_dist < 20: # Right side is blocked, left side is free, turn left + self.turn(-100, TURN_TIME_FS_90DEG_MS) + else: # Both Side are free, randomize a direction + right: bool = randint(0, 1) if right: - self.turn(100,TURN_TIME_FS_90DEG_MS) + self.turn(100, TURN_TIME_FS_90DEG_MS) self.drive(50) time.sleep(3) else: - self.turn(-100,TURN_TIME_FS_90DEG_MS) + self.turn(-100, TURN_TIME_FS_90DEG_MS) self.drive(50) time.sleep(3) - time.sleep(0.5) \ No newline at end of file + time.sleep(0.5) diff --git a/kuukar_sensors.py b/kuukar_sensors.py index 62adbaa..1fa61c7 100644 --- a/kuukar_sensors.py +++ b/kuukar_sensors.py @@ -5,7 +5,8 @@ from kuukar_config import DHT22_PIN, SONAR_1_ECHO_PIN, SONAR_1_TRIG_PIN, SONAR_2 class sensors: - __sonar_trig_pins__ = [SONAR_1_TRIG_PIN, SONAR_2_TRIG_PIN, SONAR_3_TRIG_PIN] + __sonar_trig_pins__ = [SONAR_1_TRIG_PIN, + SONAR_2_TRIG_PIN, SONAR_3_TRIG_PIN] __sonar_distances__ = [0, 0, 0] __temperature__ = 0 __humidity__ = 0 @@ -36,8 +37,8 @@ class sensors: def get_humidity_pct(self) -> float: return self.__humidity__ - def __dht22_callback__(self,data): + def __dht22_callback__(self, data): humidity = data[3] temperature = data[4] self.__humidity__ = humidity - self.__temperature__ = temperature \ No newline at end of file + self.__temperature__ = temperature diff --git a/kuukar_temperature.py b/kuukar_temperature.py deleted file mode 100644 index d9ce096..0000000 --- a/kuukar_temperature.py +++ /dev/null @@ -1,17 +0,0 @@ -import threading -from time import sleep -from kuukar_lcd import lcd -from kuukar_leds import leds -from kuukar_sensors import sensors - - -class temperature: - def __init__(self, lcd: lcd, leds: leds, sensors: sensors) -> None: - self.lcd = lcd - self.leds = leds - self.sensors = sensors - threading.Thread(target=self.temperature_checker).start() - - def temperature_checker(self): - while True: - sleep(5) \ No newline at end of file