From 767a4c4ad59d29d16baa3f5f93893aee79e80c72 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 25 Oct 2022 14:49:19 +0700 Subject: [PATCH] update code to change driver board to RPi Pico --- kuukar.py | 11 ++++++-- kuukar_collision.py | 20 +++++++++++--- kuukar_config.py | 15 ++++++---- kuukar_cv.py | 3 +- kuukar_leds.py | 11 ++++++-- kuukar_motion.py | 64 ++++++++++++++++++++++++++++++++----------- kuukar_nextion.py | 0 kuukar_noise.py | 2 -- kuukar_sensors.py | 7 +++-- kuukar_temperature.py | 4 +-- kuukar_voice.py | 3 +- requirements.txt | 1 - 12 files changed, 98 insertions(+), 43 deletions(-) create mode 100644 kuukar_nextion.py delete mode 100644 kuukar_noise.py diff --git a/kuukar.py b/kuukar.py index 49e3500..f513c19 100644 --- a/kuukar.py +++ b/kuukar.py @@ -1,4 +1,11 @@ from telemetrix_rpi_pico import telemetrix_rpi_pico +import kuukar_leds, kuukar_collision, kuukar_motion, kuukar_lcd, kuukar_sensors + +from kuukar_config import SERIAL_AUX_BOARD, SERIAL_DRIVER_BOARD if __name__ == '__main__': - board = telemetrix_rpi_pico.TelemetrixRpiPico() - pass \ No newline at end of file + 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) + leds = kuukar_leds.leds(aux_board=aux_board) + lcd = kuukar_lcd.lcd() + motion = kuukar_motion.motion(driver_board=driver_board,leds=leds,sensors= sensors) \ No newline at end of file diff --git a/kuukar_collision.py b/kuukar_collision.py index 5978fbc..01e1d2b 100644 --- a/kuukar_collision.py +++ b/kuukar_collision.py @@ -1,5 +1,17 @@ +from telemetrix_rpi_pico import telemetrix_rpi_pico +from kuukar_config import COLLISION_DETECTOR_PIN + +from kuukar_lcd import lcd +from kuukar_leds import leds class collision: - def __init__(self, board) -> None: - self.board = board - def collision_handle(self): - pass \ No newline at end of file + def __init__(self, aux: telemetrix_rpi_pico.TelemetrixRpiPico, lcd: lcd, leds: leds) -> None: + self.aux = aux + self.lcd = lcd + self.leds = leds + aux.set_pin_mode_digital_input(COLLISION_DETECTOR_PIN, self.collision_handle) + + def collision_handle(self, data): + val = data[2] + if val == 1: + self.leds.flash(r=255,g=0,b=0,duration_ms=250) + self.lcd.keke_hurt() \ No newline at end of file diff --git a/kuukar_config.py b/kuukar_config.py index 1a564b2..1b55413 100644 --- a/kuukar_config.py +++ b/kuukar_config.py @@ -1,12 +1,15 @@ SERIAL_DRIVER_BOARD = "/dev/serial/by-id/..." -MOTOR_FL_F = 4 -MOTOR_FL_R = 5 -MOTOR_FR_F = 6 -MOTOR_FR_R = 7 -MOTOR_R_F = 8 -MOTOR_R_R = 9 +MOTOR_FL_F = 6 +MOTOR_FL_R = 7 +MOTOR_FR_F = 8 +MOTOR_FR_R = 9 +MOTOR_RL_F = 10 +MOTOR_RL_R = 11 +MOTOR_RR_F = 12 +MOTOR_RR_R = 13 SERIAL_AUX_BOARD = "/dev/serial/by-id/..." COLLISION_DETECTOR_PIN = 2 LEDS_DATA_PIN = 3 DHT22_PIN = 5 +LEDS_NUM = 15 \ No newline at end of file diff --git a/kuukar_cv.py b/kuukar_cv.py index 16893f7..7b6a6ce 100644 --- a/kuukar_cv.py +++ b/kuukar_cv.py @@ -1,9 +1,8 @@ from kuukar_lcd import lcd from kuukar_leds import leds -from kuukar_noise import noise class cv: - def __init__(self, lcd: lcd, leds: leds, noise: noise) -> None: + def __init__(self, lcd: lcd, leds: leds) -> None: self.lcd = lcd self.leds = leds self.noise = noise diff --git a/kuukar_leds.py b/kuukar_leds.py index a55140d..18803f0 100644 --- a/kuukar_leds.py +++ b/kuukar_leds.py @@ -1,3 +1,10 @@ +from telemetrix_rpi_pico import telemetrix_rpi_pico + +from kuukar_config import LEDS_DATA_PIN, LEDS_NUM class leds: - def __init__(self, board, led_pin: int) -> None: - self.board = board \ No newline at end of file + 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(r: int,g: int,b: int,duration_ms: int): + pass \ No newline at end of file diff --git a/kuukar_motion.py b/kuukar_motion.py index 134634e..c763493 100644 --- a/kuukar_motion.py +++ b/kuukar_motion.py @@ -1,29 +1,61 @@ -from kuukar_config import MOTOR_FL_F, MOTOR_FL_R, MOTOR_FR_F, MOTOR_FR_R, MOTOR_R_F, MOTOR_R_R +from kuukar_config import MOTOR_FL_F, MOTOR_FL_R, MOTOR_FR_F, MOTOR_FR_R, MOTOR_RL_F, MOTOR_RL_R, MOTOR_RR_F, MOTOR_RR_R +import time from kuukar_leds import leds from kuukar_sensors import sensors -from telemetrix import telemetrix +from telemetrix_rpi_pico import telemetrix_rpi_pico +import threading class motion: - def __init__(self, driver: telemetrix.Telemetrix, led: leds, sensors: sensors) -> None: - self.driver = driver + + roaming = False + + def __init__(self, driver_board: telemetrix_rpi_pico.TelemetrixRpiPico, leds: leds, sensors: sensors) -> None: + self.driver = driver_board self.leds = leds self.sensors = sensors + 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_R_F,MOTOR_R_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_digital_output(drive_pin) + self.driver.set_pin_mode_pwm_output(drive_pin) + + 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) + elif speed >= 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) + + 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) + time.sleep(duration) + self.stop() - def forward(self, speed: int): - self.driver.digital_write(MOTOR_FL_F,1) - def turn(self, speed: int, angle: float): - pass - def backward(self, speed: int): - pass def stop(self): - pass + self.drive(0) + def roam_start(self): - pass + self.roaming = True + def roam_stop(self): - pass - def roam_loop(self): + self.roaming = False + + def __roam_looper__(self): + while True: + if self.roaming: + self.__roam_handle__() + + def __roam_handle__(self): + #TODO Implement Roaming Function pass \ No newline at end of file diff --git a/kuukar_nextion.py b/kuukar_nextion.py new file mode 100644 index 0000000..e69de29 diff --git a/kuukar_noise.py b/kuukar_noise.py deleted file mode 100644 index 1096cdd..0000000 --- a/kuukar_noise.py +++ /dev/null @@ -1,2 +0,0 @@ -class noise(): - pass \ No newline at end of file diff --git a/kuukar_sensors.py b/kuukar_sensors.py index 60b0af3..abc6c23 100644 --- a/kuukar_sensors.py +++ b/kuukar_sensors.py @@ -1,4 +1,5 @@ +from telemetrix_rpi_pico import telemetrix_rpi_pico class sensors: - def __init__(self, board, driver) -> None: - self.board = board - self.driver = driver \ No newline at end of file + def __init__(self, aux_board: telemetrix_rpi_pico.TelemetrixRpiPico, driver_board: telemetrix_rpi_pico.TelemetrixRpiPico) -> None: + self.aux = aux_board + self.driver = driver_board \ No newline at end of file diff --git a/kuukar_temperature.py b/kuukar_temperature.py index 049f130..d9ce096 100644 --- a/kuukar_temperature.py +++ b/kuukar_temperature.py @@ -2,16 +2,14 @@ import threading from time import sleep from kuukar_lcd import lcd from kuukar_leds import leds -from kuukar_noise import noise from kuukar_sensors import sensors class temperature: - def __init__(self, lcd: lcd, leds: leds, sensors: sensors, noise: noise) -> None: + def __init__(self, lcd: lcd, leds: leds, sensors: sensors) -> None: self.lcd = lcd self.leds = leds self.sensors = sensors - self.noise = noise threading.Thread(target=self.temperature_checker).start() def temperature_checker(self): diff --git a/kuukar_voice.py b/kuukar_voice.py index 7af04df..8cdb489 100644 --- a/kuukar_voice.py +++ b/kuukar_voice.py @@ -4,9 +4,8 @@ from kuukar_noise import noise class voice: - def __init__(self, lcd: lcd, leds: leds, noise: noise) -> None: + def __init__(self, lcd: lcd, leds: leds) -> None: self.lcd = lcd self.leds = leds - self.noise = noise def handle_voice_prompt(self, command): pass \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 5021e60..195ca31 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ telemetrix-rpi-pico -telemetrix pyaudio \ No newline at end of file