From e117177b9b05090828969e277f15c8808beba754 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 28 Oct 2022 14:14:00 +0700 Subject: [PATCH] add leds signal and headlight handling --- .idea/.gitignore | 8 +++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/kuukar-rpi.iml | 10 +++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ kuukar.py | 16 +++-- kuukar_collision.py | 2 +- kuukar_config.py | 10 ++- kuukar_cv.py | 2 +- kuukar_environment.py | 10 ++- kuukar_leds.py | 71 +++++++++++++++---- kuukar_motion.py | 19 +++-- kuukar_nextion.py | 14 ++-- kuukar_sensors.py | 48 ++++++++----- 15 files changed, 178 insertions(+), 56 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/kuukar-rpi.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/kuukar-rpi.iml b/.idea/kuukar-rpi.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/.idea/kuukar-rpi.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..ccc85b6 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..40043b1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/kuukar.py b/kuukar.py index ec45e84..a269618 100644 --- a/kuukar.py +++ b/kuukar.py @@ -1,13 +1,19 @@ from telemetrix_rpi_pico import telemetrix_rpi_pico -import kuukar_leds, kuukar_collision, kuukar_motion, kuukar_lcd, kuukar_sensors, kuukar_environment +import kuukar_leds +import kuukar_collision +import kuukar_motion +import kuukar_lcd +import kuukar_sensors +import kuukar_environment from kuukar_config import SERIAL_AUX_BOARD, SERIAL_DRIVER_BOARD + if __name__ == '__main__': aux_board = telemetrix_rpi_pico.TelemetrixRpiPico(com_port=SERIAL_AUX_BOARD) driver_board = telemetrix_rpi_pico.TelemetrixRpiPico(com_port=SERIAL_DRIVER_BOARD) - sensors = kuukar_sensors.sensors(aux_board,driver_board) + 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 + 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) diff --git a/kuukar_collision.py b/kuukar_collision.py index 25a5dc6..3668c44 100644 --- a/kuukar_collision.py +++ b/kuukar_collision.py @@ -13,5 +13,5 @@ class collision: def collision_handle(self, data): val = data[2] if val == 1: - self.leds.__flasher__(r=255,g=0,b=0,duration=250) + self.leds.__flasher(r=255, g=0, b=0, duration=250) self.lcd.play_video("keke_hurt") \ No newline at end of file diff --git a/kuukar_config.py b/kuukar_config.py index 381c0b2..afcaeb9 100644 --- a/kuukar_config.py +++ b/kuukar_config.py @@ -18,7 +18,7 @@ MOTOR_RL_R = 11 MOTOR_RR_F = 12 MOTOR_RR_R = 13 -# Full Speed 90 Degress Turn Time +# Full Speed 90 Degrees Turn Time TURN_TIME_FS_90DEG_MS = 1500 SERIAL_AUX_BOARD = "/dev/serial/by-id/..." @@ -27,7 +27,13 @@ COLLISION_DETECTOR_PIN = 2 LEDS_DATA_PIN = 3 LEDS_NUM = 15 +HEADLIGHT_LEDS = [5, 10] +LEFT_SIGNAL_LEDS = [6, 3] +RIGHT_SIGNAL_LEDS = [7, 4] +REVERSE_LEDS = [1, 2] + +LIGHT_ANALOG_PIN = 0 DHT22_PIN = 5 -SERIAL_LCD = "/dev/serial/by-id/..." \ No newline at end of file +SERIAL_LCD = "/dev/serial/by-id/..." diff --git a/kuukar_cv.py b/kuukar_cv.py index ebd4ee2..56cbc64 100644 --- a/kuukar_cv.py +++ b/kuukar_cv.py @@ -10,7 +10,7 @@ class cv: def capture_image(self): pass - def detectFace(self, image): + def detect_face(self, image): pass def handle_face_detected(self): diff --git a/kuukar_environment.py b/kuukar_environment.py index d72dcc3..f355053 100644 --- a/kuukar_environment.py +++ b/kuukar_environment.py @@ -12,9 +12,9 @@ class environment: self.lcd = lcd self.leds = leds self.sensors = sensors - threading.Thread(target=self.__checker__).start() + threading.Thread(target=self.__checker).start() - def __checker__(self): + def __checker(self): while True: if self.sensors.get_temperature() > 38.0: if not self.t_alerted: @@ -30,4 +30,8 @@ class environment: else: self.h_alerted = False - sleep(5) \ No newline at end of file + if self.sensors.get_brightness_pct() < 50: + self.leds.set_headlights(True) + else: + self.leds.set_headlights(False) + sleep(1) \ No newline at end of file diff --git a/kuukar_leds.py b/kuukar_leds.py index ee2b654..914e926 100644 --- a/kuukar_leds.py +++ b/kuukar_leds.py @@ -2,36 +2,79 @@ import threading from time import perf_counter from telemetrix_rpi_pico import telemetrix_rpi_pico import time -from kuukar_config import LEDS_DATA_PIN, LEDS_NUM +from kuukar_config import LEDS_DATA_PIN, LEDS_NUM, HEADLIGHT_LEDS, LEFT_SIGNAL_LEDS, RIGHT_SIGNAL_LEDS, REVERSE_LEDS class leds: flashing = False + headlight = False + reverse_signal = False + left_signal = False + right_signal = False + ambient_light = [0, 0, 0] + start_time = perf_counter() + 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) - def flash(self,r: int, g: int, b: int, duration: int): + def set_headlights(self, state: bool): + headlight = True + self.__update_leds() + + def set_left_signal_led(self, state: bool): + self.left_signal = state + self.__update_leds() + + def set_right_signal_led(self, state: bool): + self.right_signal = state + self.__update_leds() + + def set_ambient_led(self, r: int, g: int, b: int): + self.ambient_light = [r, g, b] + self.__update_leds() + + def set_reverse_led(self, state: bool): + self.reverse_signal = True + self.__update_leds() + + def __update_leds(self): + self.aux_board.neopixel_fill(r=self.ambient_light[0], g=self.ambient_light[1], b=self.ambient_light[2]) + if self.headlight: + for led in HEADLIGHT_LEDS: + self.aux_board.neo_pixel_set_value(led, self.headlight * 255, self.headlight * 255, + self.headlight * 255) + if self.left_signal: + for led in LEFT_SIGNAL_LEDS: + self.aux_board.neo_pixel_set_value(led, 255, 0, 0) + if self.right_signal: + for led in RIGHT_SIGNAL_LEDS: + self.aux_board.neo_pixel_set_value(led, 255, 0, 0) + if self.reverse_signal: + for led in REVERSE_LEDS: + self.aux_board.neo_pixel_set_value(led, 255, 0, 0) + self.aux_board.neopixel_show() + + def flash(self, r: int, g: int, b: int, duration: int): if not self.flashing: self.flashing = True self.start_time = perf_counter() - threading.Thread(target=self.__flasher__,args=(r,g,b,duration)) - - + threading.Thread(target=self.__flasher, args=(r, g, b, duration)) + @staticmethod - def __blink_func__(t: float,dur: float): - if t>dur: + def __blink_func(t: float, dur: float): + if t > dur: return 0 - return (-1/(dur/2.0)**2)*(t-dur/2.0)**2+1 + return (-1 / (dur / 2.0) ** 2) * (t - dur / 2.0) ** 2 + 1 - def __get_time__(self) -> float: - return perf_counter()-self.start_time + def __get_time(self) -> float: + return perf_counter() - self.start_time - def __flasher__(self, r: int, g: int, b: int, duration: int): + def __flasher(self, r: int, g: int, b: int, duration: int): while True: - print(leds.__blink_func__(self.__get_time__(),duration)) + print(leds.__blink_func(self.__get_time(), duration)) time.sleep(0.05) - if self.__get_time__ > duration: + if self.__get_time > duration: flashing = False - break \ No newline at end of file + break diff --git a/kuukar_motion.py b/kuukar_motion.py index 43a67b5..a3b8f80 100644 --- a/kuukar_motion.py +++ b/kuukar_motion.py @@ -38,14 +38,25 @@ class motion: self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed) def turn(self, speed: int, duration: float): + if speed > 0: + self.leds.set_right_signal_led(True) + elif speed < 0: + self.leds.set_left_signal_led(True) 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() + if speed > 0: + self.leds.set_right_signal_led(False) + elif speed < 0: + self.leds.set_left_signal_led(False) def stop(self): + self.leds.set_left_signal_led(False) + self.leds.set_right_signal_led(False) + self.leds.set_reverse_led(False) self.drive(0) def roam_start(self): @@ -54,15 +65,15 @@ class motion: def roam_stop(self): self.roaming = False - def __roam_looper__(self): + def __roam_looper(self): while True: if self.roaming: - self.__roam_handle__() + self.__roam_handle() - def __roam_handle__(self): + 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 0 < 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 diff --git a/kuukar_nextion.py b/kuukar_nextion.py index 76b29db..e731ff9 100644 --- a/kuukar_nextion.py +++ b/kuukar_nextion.py @@ -19,9 +19,9 @@ class nextion: def __init__(self) -> None: self.device = serial.Serial(SERIAL_LCD, baudrate=19200, timeout=5) - self.__send_stop_bit__() + self.__send_stop_bit() self.reset_device() - threading.Thread(target=self.__serial_reader__).start() + threading.Thread(target=self.__serial_reader).start() threading.Thread(target=self.__serial_writer__).start() def send_command(self, command: str): @@ -31,22 +31,22 @@ class nextion: def __serial_writer__(self): if len(self.__commands_output_buffer__) > 0: self.device.write(self.__commands_output_buffer__.pop(0)) - self.__send_stop_bit__() + self.__send_stop_bit() def reset_device(self): self.send_command("rest") - def __get_stop_bit__(self) -> bytearray: + def __get_stop_bit(self) -> bytearray: stop_bit = bytearray() stop_bit.append(0xFF) stop_bit.append(0xFF) stop_bit.append(0XFF) return stop_bit - def __send_stop_bit__(self): - self.device.write(self.__get_stop_bit__()) + def __send_stop_bit(self): + self.device.write(self.__get_stop_bit()) - def __serial_reader__(self) -> None: + def __serial_reader(self) -> None: ser = self.device while True: if self.allow_serial_read: diff --git a/kuukar_sensors.py b/kuukar_sensors.py index 1fb07d4..d48be22 100644 --- a/kuukar_sensors.py +++ b/kuukar_sensors.py @@ -1,44 +1,54 @@ from telemetrix_rpi_pico import telemetrix_rpi_pico -from kuukar_config import DHT22_PIN, SONAR_1_ECHO_PIN, SONAR_1_TRIG_PIN, SONAR_2_ECHO_PIN, SONAR_2_TRIG_PIN, SONAR_3_ECHO_PIN, SONAR_3_TRIG_PIN +from kuukar_config import DHT22_PIN, SONAR_1_ECHO_PIN, SONAR_1_TRIG_PIN,\ + SONAR_2_ECHO_PIN, SONAR_2_TRIG_PIN, SONAR_3_ECHO_PIN, SONAR_3_TRIG_PIN,\ + LIGHT_ANALOG_PIN class sensors: - __sonar_trig_pins__ = [SONAR_1_TRIG_PIN, - SONAR_2_TRIG_PIN, SONAR_3_TRIG_PIN] - __sonar_distances__ = [0, 0, 0] - __temperature__ = 0 - __humidity__ = 0 + __sonar_trig_pins = [SONAR_1_TRIG_PIN, + SONAR_2_TRIG_PIN, SONAR_3_TRIG_PIN] + __sonar_distances = [0, 0, 0] + __temperature = 0 + __humidity = 0 + __brightness = 0 def __init__(self, aux_board: telemetrix_rpi_pico.TelemetrixRpiPico, driver_board: telemetrix_rpi_pico.TelemetrixRpiPico) -> None: self.aux = aux_board self.driver = driver_board self.driver.set_pin_mode_sonar( - SONAR_1_TRIG_PIN, SONAR_1_ECHO_PIN, self.__sonar_callback__) + SONAR_1_TRIG_PIN, SONAR_1_ECHO_PIN, self.__sonar_callback) self.driver.set_pin_mode_sonar( - SONAR_2_TRIG_PIN, SONAR_2_ECHO_PIN, self.__sonar_callback__) + SONAR_2_TRIG_PIN, SONAR_2_ECHO_PIN, self.__sonar_callback) self.driver.set_pin_mode_sonar( - SONAR_3_TRIG_PIN, SONAR_3_ECHO_PIN, self.__sonar_callback__) - self.driver.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback__) + SONAR_3_TRIG_PIN, SONAR_3_ECHO_PIN, self.__sonar_callback) + self.driver.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback) + self.aux.set_pin_mode_analog_input(LIGHT_ANALOG_PIN, 0, self.__light_sensor_callback()) def sonar_get_distance(self, id: int) -> float: - return self.__sonar_distances__[id] + return self.__sonar_distances[id] - def __sonar_callback__(self, data): + def __sonar_callback(self, data): pin = data[1] distance = data[2] - sonar_id = self.__sonar_trig_pins__.index(pin) - self.__sonar_distances__[sonar_id] = distance + sonar_id = self.__sonar_trig_pins.index(pin) + self.__sonar_distances[sonar_id] = distance + + def get_brightness_pct(self) -> float: + return self.__brightness def get_temperature(self) -> float: - return self.__temperature__ + return self.__temperature def get_humidity_pct(self) -> float: - return self.__humidity__ + return self.__humidity - def __dht22_callback__(self, data): + def __light_sensor_callback(self, data): + self.__brightness = data[2] + + def __dht22_callback(self, data): humidity = data[3] temperature = data[4] - self.__humidity__ = humidity - self.__temperature__ = temperature + self.__humidity = humidity + self.__temperature = temperature