finalize code

This commit is contained in:
Siwat Sirichai 2022-12-13 21:49:17 +07:00
parent 387bbbb215
commit cc8b01d574
10 changed files with 66 additions and 72 deletions

View file

@ -22,4 +22,4 @@ class kuukar:
self.lcd, self.leds, self.sensors)
self.collision = kuukar_collision.collision(
self.mcu, self.lcd, self.leds)
self.cv = kuukar_cv.cv(self.lcd,self.leds)
self.cv = kuukar_cv.cv(self.lcd, self.leds, self.motion)

View file

@ -3,15 +3,18 @@ from kuukar.kuukar_config import COLLISION_DETECTOR_PIN, COLLISION_ENABLE
from kuukar.kuukar_lcd import lcd
from kuukar.kuukar_leds import leds
class collision:
def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico, lcd: lcd, leds: leds) -> None:
self.mcu = mcu
self.lcd = lcd
self.leds = leds
if COLLISION_ENABLE:
self.mcu.set_pin_mode_digital_input(COLLISION_DETECTOR_PIN, self.collision_handle)
self.mcu.set_pin_mode_digital_input(
COLLISION_DETECTOR_PIN, self.collision_handle)
def collision_handle(self, data):
val = data[2]
if val == 1:
self.lcd.play_video("keke_hurt")
self.lcd.play_video("keke_hurt")

View file

@ -31,4 +31,3 @@ REVERSE_LEDS = [11, 10, 9]
LIGHT_SENSOR_PIN = 0
DHT22_PIN = 3

View file

@ -1,34 +1,36 @@
from kuukar.kuukar_lcd import lcd
from kuukar.kuukar_leds import leds
from kuukar.kuukar_motion import motion
import threading
import multiprocessing
import cv2
class cv:
def __init__(self, lcd: lcd, leds: leds) -> None:
def __init__(self, lcd: lcd, leds: leds, motion: motion) -> None:
self.motion = motion
self.lcd = lcd
self.leds = leds
#self.camera = cv2.VideoCapture(0)
#threading.Thread(target=self._cv_loop).start()
self.camera = cv2.VideoCapture(0)
threading.Thread(target=self._cv_loop).start()
def _cv_loop(self):
while True:
img = self.capture_image()
##stop_sign = self.detect_sign(img)
stop_signs = self.detect_sign(img)
if len(stop_signs) >= 1: # Stop Sign Found~!
self.motion.stop()
def get_road_curve(self, frame):
pass
def capture_image(self):
ret, frame = self.camera.read()
return frame
def detect_sign(self, frame):
stopsign_cascade = cv2.CascadeClassifier('./stopsign_good.xml')
image = frame.array
gray_img=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
found_stopsigns=stopsign_cascade.detectMultiScale(gray_img,1.1,5)
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
found_stopsigns = stopsign_cascade.detectMultiScale(gray_img, 1.1, 5)
return found_stopsigns

View file

@ -8,17 +8,18 @@ from kuukar.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:
#self.lcd.play_video("keke_died")
self.lcd.play_video("keke_hurt")
self.t_alerted = True
else:
self.t_alerted = False
@ -34,4 +35,4 @@ class environment:
self.leds.set_headlights(True)
else:
self.leds.set_headlights(False)
sleep(1)
sleep(1)

View file

@ -51,7 +51,8 @@ class lcd:
turn = (x - 5) / (150 - 5) * 200 - 100
print(f"Setting speed to {speed} and turn angle to {turn}")
if not self.motion.is_roaming():
self.motion.drive_skew(speed,turn)
self.motion.drive_skew(speed, turn)
def _sensor_screen_updator(self):
while True:
self.nextion.send_command(

View file

@ -18,7 +18,7 @@ class leds:
def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
self.mcu = mcu
self.mcu.set_pin_mode_neopixel(LEDS_DATA_PIN, LEDS_NUM)
self.ambient_light = [2, 2, 10] #Keke Image Color
self.ambient_light = [2, 2, 10] # Keke Image Color
self.__update_leds()
def set_headlights(self, state: bool):

View file

@ -71,17 +71,16 @@ class motion:
self.leds.set_right_signal_led(False)
self.leds.set_left_signal_led(True)
lm_speed = (100+dir/2)*speed/100
if lm_speed>99:
if lm_speed > 99:
lm_speed = 99
elif lm_speed<-99:
elif lm_speed < -99:
lm_speed = -99
rm_speed = (100-dir/2)*speed/100
if rm_speed > 99:
rm_speed = 99
elif rm_speed < -99:
rm_speed = -99;
rm_speed = -99
self.motor_write(MOTOR_FL_F, MOTOR_FL_R, lm_speed)
self.motor_write(MOTOR_FR_F, MOTOR_FR_R, rm_speed)
@ -119,16 +118,22 @@ class motion:
'''
def skt_drive(self, speed: float, turn: float, t=0):
speed *=100
turn *=100
speed *= 100
turn *= 100
leftSpeed = speed - turn
rightSpeed = speed + turn
if leftSpeed>99: leftSpeed=99
elif leftSpeed<-99: leftSpeed= -99
if rightSpeed>99: rightSpeed=99
elif rightSpeed<-99: rightSpeed= -99
if speed>99: speed=99
elif speed<-99: speed= -99
if leftSpeed > 99:
leftSpeed = 99
elif leftSpeed < -99:
leftSpeed = -99
if rightSpeed > 99:
rightSpeed = 99
elif rightSpeed < -99:
rightSpeed = -99
if speed > 99:
speed = 99
elif speed < -99:
speed = -99
self.motor_write(MOTOR_FL_F, MOTOR_FL_R, abs(leftSpeed))
self.motor_write(MOTOR_FR_F, MOTOR_FR_R, abs(rightSpeed))
@ -136,7 +141,6 @@ class motion:
#self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed)
time.sleep(t)
def stop(self):
self.leds.set_left_signal_led(False)
self.leds.set_right_signal_led(False)
@ -162,9 +166,9 @@ class motion:
time.sleep(0.1)
def __roam_handle(self):
if self.sensors.sonar_get_distance()<900:
self.turn(32,2600)
if self.sensors.sonar_get_distance() < 900:
self.turn(32, 2600)
self.drive(25)
if not self.roaming:
self.stop()
self.stop()