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

@ -1,3 +1,4 @@
from time import perf_counter
import cv2 import cv2
from kuukar.kuukar import kuukar from kuukar.kuukar import kuukar
from LaneDetectionModule import getLaneCurve from LaneDetectionModule import getLaneCurve
@ -5,7 +6,6 @@ from utils import initializeTrackbars
import WebcamModule import WebcamModule
from threading import Thread from threading import Thread
global speed, sen, car, delay global speed, sen, car, delay
from time import perf_counter
sen = 1 sen = 1
speed = 0 speed = 0
delay = 0.15 delay = 0.15
@ -14,27 +14,34 @@ car = kuukar()
################################################## ##################################################
############### MAIN ROBOT LANE ################## ############### MAIN ROBOT LANE ##################
################################################## ##################################################
def main(): def main():
global speed, sen, car, delay global speed, sen, car, delay
while True: while True:
stt = perf_counter() stt = perf_counter()
img = WebcamModule.getImg() img = WebcamModule.getImg()
curveVal= getLaneCurve(img,2) curveVal = getLaneCurve(img, 2)
maxVAl= 1.0 # MAX SPEED maxVAl = 1.0 # MAX SPEED
if curveVal>maxVAl:curveVal = maxVAl if curveVal > maxVAl:
if curveVal<-maxVAl: curveVal =-maxVAl curveVal = maxVAl
#print(curveVal) if curveVal < -maxVAl:
if curveVal>0: curveVal = -maxVAl
sen =1.7 # print(curveVal)
if curveVal<0.05: curveVal=0 if curveVal > 0:
sen = 1.7
if curveVal < 0.05:
curveVal = 0
else: else:
if curveVal>-0.08: curveVal=0 if curveVal > -0.08:
car.motion.skt_drive(speed,-curveVal*sen,delay) curveVal = 0
car.motion.skt_drive(speed, -curveVal*sen, delay)
turn_value = -curveVal*sen turn_value = -curveVal*sen
#print("Curve Value : " + str(curveVal) + " Turn Value : "+ str(round(turn_value,2))) #print("Curve Value : " + str(curveVal) + " Turn Value : "+ str(round(turn_value,2)))
#print(f"looptime: {perf_counter()-stt}s") #print(f"looptime: {perf_counter()-stt}s")
cv2.waitKey(1) cv2.waitKey(1)
if __name__ == '__main__': if __name__ == '__main__':
initialTrackBarVals = [107, 134, 89, 221] initialTrackBarVals = [107, 134, 89, 221]
initializeTrackbars(initialTrackBarVals) initializeTrackbars(initialTrackBarVals)
@ -43,7 +50,3 @@ if __name__ == '__main__':
speed = float(input("speed: ")) speed = float(input("speed: "))
sen = float(input("sen: ")) sen = float(input("sen: "))
delay = float(input("delay: ")) delay = float(input("delay: "))

21
app.py
View File

@ -1,24 +1,5 @@
import time
# from telemetrix_rpi_pico import telemetrix_rpi_pico
# import kuukar.kuukar_leds as kuukar_leds
# import kuukar.kuukar_collision as kuukar_collision
# import kuukar.kuukar_motion as kuukar_motion
# import kuukar.kuukar_lcd as kuukar_lcd
# import kuukar.kuukar_sensors as kuukar_sensors
# import kuukar.kuukar_environment as kuukar_environment
# from kuukar.kuukar_config import SERIAL_MCU, SERIAL_DRV
from kuukar.kuukar import kuukar from kuukar.kuukar import kuukar
import time
# mcu = telemetrix_rpi_pico.TelemetrixRpiPico(com_port=SERIAL_MCU, sleep_tune=0.001)
# drv = telemetrix_rpi_pico.TelemetrixRpiPico(com_port=SERIAL_DRV, sleep_tune=0.001)
# sensors = kuukar_sensors.sensors(mcu)
# leds = kuukar_leds.leds(mcu)
# motion = kuukar_motion.motion(mcu, drv, leds, sensors)
# lcd = kuukar_lcd.lcd(motion, sensors)
# environment = kuukar_environment.environment(lcd, leds, sensors)
# collision = kuukar_collision.collision(mcu, lcd, leds)
car = kuukar() car = kuukar()
while True: while True:

View File

@ -22,4 +22,4 @@ class kuukar:
self.lcd, self.leds, self.sensors) self.lcd, self.leds, self.sensors)
self.collision = kuukar_collision.collision( self.collision = kuukar_collision.collision(
self.mcu, self.lcd, self.leds) 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,13 +3,16 @@ from kuukar.kuukar_config import COLLISION_DETECTOR_PIN, COLLISION_ENABLE
from kuukar.kuukar_lcd import lcd from kuukar.kuukar_lcd import lcd
from kuukar.kuukar_leds import leds from kuukar.kuukar_leds import leds
class collision: class collision:
def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico, lcd: lcd, leds: leds) -> None: def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico, lcd: lcd, leds: leds) -> None:
self.mcu = mcu self.mcu = mcu
self.lcd = lcd self.lcd = lcd
self.leds = leds self.leds = leds
if COLLISION_ENABLE: 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): def collision_handle(self, data):
val = data[2] val = data[2]

View File

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

View File

@ -1,22 +1,25 @@
from kuukar.kuukar_lcd import lcd from kuukar.kuukar_lcd import lcd
from kuukar.kuukar_leds import leds from kuukar.kuukar_leds import leds
from kuukar.kuukar_motion import motion
import threading import threading
import multiprocessing import multiprocessing
import cv2 import cv2
class cv: 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.lcd = lcd
self.leds = leds self.leds = leds
#self.camera = cv2.VideoCapture(0) self.camera = cv2.VideoCapture(0)
#threading.Thread(target=self._cv_loop).start() threading.Thread(target=self._cv_loop).start()
def _cv_loop(self): def _cv_loop(self):
while True: while True:
img = self.capture_image() 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): def get_road_curve(self, frame):
pass pass
@ -28,7 +31,6 @@ class cv:
def detect_sign(self, frame): def detect_sign(self, frame):
stopsign_cascade = cv2.CascadeClassifier('./stopsign_good.xml') stopsign_cascade = cv2.CascadeClassifier('./stopsign_good.xml')
image = frame.array image = frame.array
gray_img=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
found_stopsigns=stopsign_cascade.detectMultiScale(gray_img,1.1,5) found_stopsigns = stopsign_cascade.detectMultiScale(gray_img, 1.1, 5)
return found_stopsigns return found_stopsigns

View File

@ -8,6 +8,7 @@ from kuukar.kuukar_sensors import sensors
class environment: class environment:
t_alerted = False t_alerted = False
h_alerted = False h_alerted = False
def __init__(self, lcd: lcd, leds: leds, sensors: sensors) -> None: def __init__(self, lcd: lcd, leds: leds, sensors: sensors) -> None:
self.lcd = lcd self.lcd = lcd
self.leds = leds self.leds = leds
@ -18,7 +19,7 @@ class environment:
while True: while True:
if self.sensors.get_temperature() > 38.0: if self.sensors.get_temperature() > 38.0:
if not self.t_alerted: if not self.t_alerted:
#self.lcd.play_video("keke_died") self.lcd.play_video("keke_hurt")
self.t_alerted = True self.t_alerted = True
else: else:
self.t_alerted = False self.t_alerted = False

View File

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

View File

@ -18,7 +18,7 @@ class leds:
def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None: def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
self.mcu = mcu self.mcu = mcu
self.mcu.set_pin_mode_neopixel(LEDS_DATA_PIN, LEDS_NUM) 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() self.__update_leds()
def set_headlights(self, state: bool): def set_headlights(self, state: bool):

View File

@ -71,17 +71,16 @@ class motion:
self.leds.set_right_signal_led(False) self.leds.set_right_signal_led(False)
self.leds.set_left_signal_led(True) self.leds.set_left_signal_led(True)
lm_speed = (100+dir/2)*speed/100 lm_speed = (100+dir/2)*speed/100
if lm_speed>99: if lm_speed > 99:
lm_speed = 99 lm_speed = 99
elif lm_speed<-99: elif lm_speed < -99:
lm_speed = -99 lm_speed = -99
rm_speed = (100-dir/2)*speed/100 rm_speed = (100-dir/2)*speed/100
if rm_speed > 99: if rm_speed > 99:
rm_speed = 99 rm_speed = 99
elif 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_FL_F, MOTOR_FL_R, lm_speed)
self.motor_write(MOTOR_FR_F, MOTOR_FR_R, rm_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): def skt_drive(self, speed: float, turn: float, t=0):
speed *=100 speed *= 100
turn *=100 turn *= 100
leftSpeed = speed - turn leftSpeed = speed - turn
rightSpeed = speed + turn rightSpeed = speed + turn
if leftSpeed>99: leftSpeed=99 if leftSpeed > 99:
elif leftSpeed<-99: leftSpeed= -99 leftSpeed = 99
if rightSpeed>99: rightSpeed=99 elif leftSpeed < -99:
elif rightSpeed<-99: rightSpeed= -99 leftSpeed = -99
if speed>99: speed=99 if rightSpeed > 99:
elif speed<-99: speed= -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_FL_F, MOTOR_FL_R, abs(leftSpeed))
self.motor_write(MOTOR_FR_F, MOTOR_FR_R, abs(rightSpeed)) 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) #self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed)
time.sleep(t) time.sleep(t)
def stop(self): def stop(self):
self.leds.set_left_signal_led(False) self.leds.set_left_signal_led(False)
self.leds.set_right_signal_led(False) self.leds.set_right_signal_led(False)
@ -162,8 +166,8 @@ class motion:
time.sleep(0.1) time.sleep(0.1)
def __roam_handle(self): def __roam_handle(self):
if self.sensors.sonar_get_distance()<900: if self.sensors.sonar_get_distance() < 900:
self.turn(32,2600) self.turn(32, 2600)
self.drive(25) self.drive(25)
if not self.roaming: if not self.roaming: