added environment sensing

This commit is contained in:
Siwat Sirichai 2022-10-25 22:01:26 +07:00
parent 37d8285b97
commit 3c3010d590
9 changed files with 90 additions and 56 deletions

View File

@ -1,5 +1,5 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico 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 from kuukar_config import SERIAL_AUX_BOARD, SERIAL_DRIVER_BOARD
if __name__ == '__main__': if __name__ == '__main__':
@ -8,4 +8,6 @@ if __name__ == '__main__':
sensors = kuukar_sensors.sensors(aux_board,driver_board) sensors = kuukar_sensors.sensors(aux_board,driver_board)
leds = kuukar_leds.leds(aux_board=aux_board) leds = kuukar_leds.leds(aux_board=aux_board)
lcd = kuukar_lcd.lcd() 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) motion = kuukar_motion.motion(driver_board=driver_board,leds=leds,sensors= sensors)

View File

@ -1,5 +1,6 @@
SERIAL_DRIVER_BOARD = "/dev/serial/by-id/..." SERIAL_DRIVER_BOARD = "/dev/serial/by-id/..."
# HC Sonar Pins
SONAR_1_TRIG_PIN = 0 SONAR_1_TRIG_PIN = 0
SONAR_1_ECHO_PIN = 1 SONAR_1_ECHO_PIN = 1
SONAR_2_TRIG_PIN = 2 SONAR_2_TRIG_PIN = 2
@ -7,6 +8,7 @@ SONAR_2_ECHO_PIN = 3
SONAR_3_TRIG_PIN = 4 SONAR_3_TRIG_PIN = 4
SONAR_3_ECHO_PIN = 5 SONAR_3_ECHO_PIN = 5
# Motor Pins
MOTOR_FL_F = 6 MOTOR_FL_F = 6
MOTOR_FL_R = 7 MOTOR_FL_R = 7
MOTOR_FR_F = 8 MOTOR_FR_F = 8

View File

@ -1,14 +1,18 @@
from kuukar_lcd import lcd from kuukar_lcd import lcd
from kuukar_leds import leds from kuukar_leds import leds
class cv: class cv:
def __init__(self, lcd: lcd, leds: leds) -> None: def __init__(self, lcd: lcd, leds: leds) -> None:
self.lcd = lcd self.lcd = lcd
self.leds = leds self.leds = leds
self.noise = noise self.noise = noise
def capture_image(self): def capture_image(self):
pass pass
def detectFace(self, image): def detectFace(self, image):
pass pass
def handle_face_detected(self): def handle_face_detected(self):
pass pass

33
kuukar_environment.py Normal file
View File

@ -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)

View File

@ -5,9 +5,15 @@ class lcd:
def keke_uwu(self): def keke_uwu(self):
pass pass
def keke_cute_noise(self):
pass
def keke_hurt(self): def keke_hurt(self):
pass pass
def keke_died(self):
pass
def takina_noise(self): def takina_noise(self):
pass pass
@ -20,7 +26,6 @@ class lcd:
def resume_video(self) -> None: def resume_video(self) -> None:
pass pass
def stop_video(self) -> None: def stop_video(self) -> None:
pass pass

View File

@ -1,10 +1,12 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico from telemetrix_rpi_pico import telemetrix_rpi_pico
from kuukar_config import LEDS_DATA_PIN, LEDS_NUM from kuukar_config import LEDS_DATA_PIN, LEDS_NUM
class leds: class leds:
def __init__(self, aux_board: telemetrix_rpi_pico.TelemetrixRpiPico) -> None: def __init__(self, aux_board: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
self.aux_board = aux_board 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): def flash(self, r: int, g: int, b: int, duration_ms: int):
pass pass

View File

@ -6,6 +6,7 @@ from kuukar_sensors import sensors
from telemetrix_rpi_pico import telemetrix_rpi_pico from telemetrix_rpi_pico import telemetrix_rpi_pico
import threading import threading
class motion: class motion:
roaming = False roaming = False
@ -14,32 +15,33 @@ class motion:
self.driver = driver_board self.driver = driver_board
self.leds = leds self.leds = leds
self.sensors = sensors 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() 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: for drive_pin in drive_pins:
self.driver.set_pin_mode_pwm_output(drive_pin) 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: if speed < 0:
self.driver.pwm_write(forward_pin,0) self.driver.pwm_write(forward_pin, 0)
self.driver.pwm_write(reverse_pin,-speed) self.driver.pwm_write(reverse_pin, -speed)
elif speed >= 0: elif speed >= 0:
self.driver.pwm_write(forward_pin,speed) self.driver.pwm_write(forward_pin, speed)
self.driver.pwm_write(reverse_pin,0) self.driver.pwm_write(reverse_pin, 0)
def drive(self, speed: int): def drive(self, speed: int):
self.motor_write(MOTOR_FL_F,MOTOR_FL_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_FR_F, MOTOR_FR_R, speed)
self.motor_write(MOTOR_RL_F,MOTOR_RL_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_RR_F, MOTOR_RR_R, speed)
def turn(self, speed: int, duration: float): def turn(self, speed: int, duration: float):
self.motor_write(MOTOR_FL_F,MOTOR_FL_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_FR_F, MOTOR_FR_R, -speed)
self.motor_write(MOTOR_RL_F,MOTOR_RL_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_RR_F, MOTOR_RR_R, -speed)
time.sleep(duration) time.sleep(duration)
self.stop() self.stop()
@ -60,29 +62,29 @@ class motion:
def __roam_handle__(self): def __roam_handle__(self):
self.drive(50) self.drive(50)
f_dist = self.sensors.sonar_get_distance(2) 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 l_dist = self.sensors.sonar_get_distance
r_dist = self.sensors.sonar_get_distance r_dist = self.sensors.sonar_get_distance
if l_dist < 20: #Left side is blocked if l_dist < 20: # Left side is blocked
if r_dist < 20: #Both side are blocked, do a full 180deg turn if r_dist < 20: # Both side are blocked, do a full 180deg turn
self.turn(100,2*TURN_TIME_FS_90DEG_MS) self.turn(100, 2*TURN_TIME_FS_90DEG_MS)
self.drive(50) self.drive(50)
time.sleep(3) time.sleep(3)
else: else:
#Right Side is Free, turn right # Right Side is Free, turn right
self.turn(100,TURN_TIME_FS_90DEG_MS) self.turn(100, TURN_TIME_FS_90DEG_MS)
self.drive(50) self.drive(50)
time.sleep(3) time.sleep(3)
elif r_dist < 20: #Right side is blocked, left side is free, turn left elif r_dist < 20: # Right side is blocked, left side is free, turn left
self.turn(-100,TURN_TIME_FS_90DEG_MS) self.turn(-100, TURN_TIME_FS_90DEG_MS)
else: #Both Side are free, randomize a direction else: # Both Side are free, randomize a direction
right: bool = randint(0,1) right: bool = randint(0, 1)
if right: if right:
self.turn(100,TURN_TIME_FS_90DEG_MS) self.turn(100, TURN_TIME_FS_90DEG_MS)
self.drive(50) self.drive(50)
time.sleep(3) time.sleep(3)
else: else:
self.turn(-100,TURN_TIME_FS_90DEG_MS) self.turn(-100, TURN_TIME_FS_90DEG_MS)
self.drive(50) self.drive(50)
time.sleep(3) time.sleep(3)
time.sleep(0.5) time.sleep(0.5)

View File

@ -5,7 +5,8 @@ from kuukar_config import DHT22_PIN, SONAR_1_ECHO_PIN, SONAR_1_TRIG_PIN, SONAR_2
class sensors: 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] __sonar_distances__ = [0, 0, 0]
__temperature__ = 0 __temperature__ = 0
__humidity__ = 0 __humidity__ = 0
@ -36,7 +37,7 @@ class sensors:
def get_humidity_pct(self) -> float: def get_humidity_pct(self) -> float:
return self.__humidity__ return self.__humidity__
def __dht22_callback__(self,data): def __dht22_callback__(self, data):
humidity = data[3] humidity = data[3]
temperature = data[4] temperature = data[4]
self.__humidity__ = humidity self.__humidity__ = humidity

View File

@ -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)