pre-hc-adj

This commit is contained in:
Siwat Sirichai 2022-11-04 11:58:20 +07:00
parent 0ea7ede2be
commit f7014f5fef
11 changed files with 121 additions and 83 deletions

26
app.py
View File

@ -1,3 +1,4 @@
import time
from telemetrix_rpi_pico import telemetrix_rpi_pico from telemetrix_rpi_pico import telemetrix_rpi_pico
import kuukar.kuukar_leds as kuukar_leds import kuukar.kuukar_leds as kuukar_leds
import kuukar.kuukar_collision as kuukar_collision import kuukar.kuukar_collision as kuukar_collision
@ -7,19 +8,27 @@ import kuukar.kuukar_sensors as kuukar_sensors
import kuukar.kuukar_environment as kuukar_environment import kuukar.kuukar_environment as kuukar_environment
from flask import Flask, request from flask import Flask, request
from kuukar.kuukar_config import SERIAL_AUX_BOARD, SERIAL_DRIVER_BOARD from kuukar.kuukar_config import SERIAL_MCU
app = Flask(__name__) app = Flask(__name__)
aux_board = telemetrix_rpi_pico.TelemetrixRpiPico(com_port=SERIAL_AUX_BOARD) mcu = telemetrix_rpi_pico.TelemetrixRpiPico(com_port=SERIAL_MCU)
driver_board = telemetrix_rpi_pico.TelemetrixRpiPico(com_port=SERIAL_DRIVER_BOARD) sensors = kuukar_sensors.sensors(mcu)
sensors = kuukar_sensors.sensors(aux_board, driver_board) leds = kuukar_leds.leds(mcu)
leds = kuukar_leds.leds(aux_board=aux_board) motion = kuukar_motion.motion(mcu, leds, sensors)
lcd = kuukar_lcd.lcd() lcd = kuukar_lcd.lcd(motion, sensors)
environment = kuukar_environment.environment(lcd, leds, sensors) environment = kuukar_environment.environment(lcd, leds, sensors)
collision = kuukar_collision.collision(aux_board, lcd, leds) collision = kuukar_collision.collision(mcu, lcd, leds)
motion = kuukar_motion.motion(driver_board=driver_board, leds=leds, sensors=sensors) motion.turn(30,3000)
while True:
temp = sensors.get_temperature()
humid = sensors.get_humidity_pct()
dist1 = sensors.sonar_get_distance(0)
dist2 = sensors.sonar_get_distance(1)
dist3 = sensors.sonar_get_distance(2)
print(f'temp: {temp}, humidity: {humid}, dist1: {dist1}, dist2: {dist2}, dist3: {dist3}')
time.sleep(1)
@app.route('/drive') @app.route('/drive')
def drive(): def drive():
@ -54,4 +63,3 @@ def set_mode():
motion.roam_start() motion.roam_start()
elif mode == "MANUAL": elif mode == "MANUAL":
motion.roam_stop() motion.roam_stop()

View File

@ -1,17 +1,17 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico from telemetrix_rpi_pico import telemetrix_rpi_pico
from kuukar.kuukar_config import COLLISION_DETECTOR_PIN from kuukar.kuukar_config import COLLISION_DETECTOR_PIN
from kuukar_lcd import lcd from kuukar.kuukar_lcd import lcd
from kuukar_leds import leds from kuukar.kuukar_leds import leds
class collision: class collision:
def __init__(self, aux: telemetrix_rpi_pico.TelemetrixRpiPico, lcd: lcd, leds: leds) -> None: def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico, lcd: lcd, leds: leds) -> None:
self.aux = aux self.mcu = mcu
self.lcd = lcd self.lcd = lcd
self.leds = leds self.leds = leds
aux.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]
if val == 1: 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") self.lcd.play_video("keke_hurt")

View File

@ -1,4 +1,4 @@
SERIAL_DRIVER_BOARD = "/dev/serial/by-id/..." SERIAL_MCU = "COM4"
# HC Sonar Pins # HC Sonar Pins
SONAR_1_TRIG_PIN = 0 SONAR_1_TRIG_PIN = 0
@ -19,21 +19,21 @@ MOTOR_RR_F = 13
MOTOR_RR_R = 12 MOTOR_RR_R = 12
# Full Speed 90 Degrees Turn Time # Full Speed 90 Degrees Turn Time
TURN_TIME_FS_90DEG_MS = 1500 TURN_TIME_FS_90DEG_MS = 3000.0
SERIAL_AUX_BOARD = "/dev/serial/by-id/..." BEEPER_PIN = 15
COLLISION_DETECTOR_PIN = 2 COLLISION_DETECTOR_PIN = 16
LEDS_DATA_PIN = 3 LEDS_DATA_PIN = 17
LEDS_NUM = 15 LEDS_NUM = 15
HEADLIGHT_LEDS = [5, 10] HEADLIGHT_LEDS = [5, 10]
LEFT_SIGNAL_LEDS = [6, 3] LEFT_SIGNAL_LEDS = [6, 3]
RIGHT_SIGNAL_LEDS = [7, 4] RIGHT_SIGNAL_LEDS = [7, 4]
REVERSE_LEDS = [1, 2] REVERSE_LEDS = [1, 2]
LIGHT_ANALOG_PIN = 0 LIGHT_ANALOG_PIN = 2
DHT22_PIN = 5 DHT22_PIN = 14
SERIAL_LCD = "COM9" SERIAL_LCD = "COM9"

View File

@ -18,14 +18,14 @@ 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:
lcd.play_video("keke_died") self.lcd.play_video("keke_died")
self.t_alerted = True self.t_alerted = True
else: else:
self.t_alerted = False self.t_alerted = False
if self.sensors.get_humidity_pct() > 60.0: if self.sensors.get_humidity_pct() > 60.0:
if not self.h_alerted: if not self.h_alerted:
lcd.play_video("keke_cute_noise") self.lcd.play_video("keke_cute_noise")
self.h_alerted = True self.h_alerted = True
else: else:
self.h_alerted = False self.h_alerted = False

View File

@ -1,14 +1,16 @@
from time import sleep from time import sleep
from socket import gethostname, gethostbyname from socket import gethostname, gethostbyname
import kuukar.kuukar_nextion as kuukar_nextion import kuukar.kuukar_nextion as kuukar_nextion
import kuukar.kuukar_motion as kuukar_motion
import kuukar.kuukar_sensors as kuukar_sensors
class lcd: class lcd:
def __init__(self) -> None: def __init__(self, motion: kuukar_motion.motion, sensors: kuukar_sensors.sensors) -> None:
self.nextion = kuukar_nextion.nextion() self.nextion = kuukar_nextion.nextion()
sleep(1) sleep(1)
self.nextion.send_command("page home") self.nextion.send_command("page home")
self.nextion.send_command("ipTXT.txt=\""+str(gethostbyname(gethostname()))+"\"") self.nextion.send_command("ipTXT.txt=\"" + str(gethostbyname(gethostname())) + "\"")
self.nextion.external_touch_handler = self.touch_handler self.nextion.external_touch_handler = self.touch_handler
def play_video(self, filename: str) -> None: def play_video(self, filename: str) -> None:
@ -37,6 +39,9 @@ class lcd:
if component_id == 2 and press_type == '00': if component_id == 2 and press_type == '00':
x = self.nextion.get_attribute("joystick.x") x = self.nextion.get_attribute("joystick.x")
y = self.nextion.get_attribute("joystick.y") y = self.nextion.get_attribute("joystick.y")
speed = 100-(y-60)/(205-60)*200 speed = 100 - (y - 60) / (205 - 60) * 200
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}")
def _sensor_screen_updator(self):
pass

View File

@ -15,9 +15,9 @@ class leds:
start_time = perf_counter() start_time = perf_counter()
def __init__(self, aux_board: telemetrix_rpi_pico.TelemetrixRpiPico) -> None: def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
self.aux_board = aux_board self.mcu = mcu
self.aux_board.set_pin_mode_neopixel(LEDS_DATA_PIN, LEDS_NUM) self.mcu.set_pin_mode_neopixel(LEDS_DATA_PIN, LEDS_NUM)
def set_headlights(self, state: bool): def set_headlights(self, state: bool):
headlight = True headlight = True
@ -40,21 +40,21 @@ class leds:
self.__update_leds() self.__update_leds()
def __update_leds(self): def __update_leds(self):
self.aux_board.neopixel_fill(r=self.ambient_light[0], g=self.ambient_light[1], b=self.ambient_light[2]) self.mcu.neopixel_fill(r=self.ambient_light[0], g=self.ambient_light[1], b=self.ambient_light[2])
if self.headlight: if self.headlight:
for led in HEADLIGHT_LEDS: for led in HEADLIGHT_LEDS:
self.aux_board.neo_pixel_set_value(led, self.headlight * 255, self.headlight * 255, self.mcu.neo_pixel_set_value(led, self.headlight * 255, self.headlight * 255,
self.headlight * 255) self.headlight * 255)
if self.left_signal: if self.left_signal:
for led in LEFT_SIGNAL_LEDS: for led in LEFT_SIGNAL_LEDS:
self.aux_board.neo_pixel_set_value(led, 255, 0, 0) self.mcu.neo_pixel_set_value(led, 255, 0, 0)
if self.right_signal: if self.right_signal:
for led in RIGHT_SIGNAL_LEDS: for led in RIGHT_SIGNAL_LEDS:
self.aux_board.neo_pixel_set_value(led, 255, 0, 0) self.mcu.neo_pixel_set_value(led, 255, 0, 0)
if self.reverse_signal: if self.reverse_signal:
for led in REVERSE_LEDS: for led in REVERSE_LEDS:
self.aux_board.neo_pixel_set_value(led, 255, 0, 0) self.mcu.neo_pixel_set_value(led, 255, 0, 0)
self.aux_board.neopixel_show() self.mcu.neopixel_show()
def flash(self, r: int, g: int, b: int, duration: int): def flash(self, r: int, g: int, b: int, duration: int):
if not self.flashing: if not self.flashing:
@ -75,6 +75,6 @@ class leds:
while True: while True:
print(leds.__blink_func(self.__get_time(), duration)) print(leds.__blink_func(self.__get_time(), duration))
time.sleep(0.05) time.sleep(0.05)
if self.__get_time > duration: if self.__get_time() > duration:
flashing = False flashing = False
break break

View File

@ -11,8 +11,8 @@ class motion:
roaming = False roaming = False
def __init__(self, driver_board: telemetrix_rpi_pico.TelemetrixRpiPico, leds: leds, sensors: sensors) -> None: def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico, leds: leds, sensors: sensors) -> None:
self.driver = driver_board self.mcu = mcu
self.leds = leds self.leds = leds
self.sensors = sensors self.sensors = sensors
self.roam_thread = threading.Thread(target=self.__roam_looper) self.roam_thread = threading.Thread(target=self.__roam_looper)
@ -21,15 +21,15 @@ class motion:
drive_pins = [MOTOR_FL_F, MOTOR_FL_R, MOTOR_FR_F, 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] 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.mcu.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.mcu.pwm_write(forward_pin, 0)
self.driver.pwm_write(reverse_pin, -speed) self.mcu.pwm_write(reverse_pin, -speed)
elif speed >= 0: elif speed >= 0:
self.driver.pwm_write(forward_pin, speed) self.mcu.pwm_write(forward_pin, speed)
self.driver.pwm_write(reverse_pin, 0) self.mcu.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)
@ -46,7 +46,7 @@ class motion:
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/1000.0)
self.stop() self.stop()
def turn_cont(self, speed: int): def turn_cont(self, speed: int):
@ -77,31 +77,35 @@ class motion:
self.__roam_handle() self.__roam_handle()
def __roam_handle(self): def __roam_handle(self):
self.drive(50) sensitivity = 35
f_dist = self.sensors.sonar_get_distance(2) self.drive(23)
if 0 < f_dist < 20: # Close to collision, turn the vehicle f_dist = self.sensors.sonar_get_distance(1)
l_dist = self.sensors.sonar_get_distance if 0 < f_dist < sensitivity: # Close to collision, turn the vehicle
r_dist = self.sensors.sonar_get_distance print("collision")
if l_dist < 20: # Left side is blocked l_dist = self.sensors.sonar_get_distance(0)
if r_dist < 20: # Both side are blocked, do a full 180deg turn r_dist = self.sensors.sonar_get_distance(2)
self.turn(100, 2*TURN_TIME_FS_90DEG_MS) if l_dist < sensitivity: # Left side is blocked
self.drive(50) if r_dist < sensitivity: # Both side are blocked, do a full 180deg turn
time.sleep(3) print("full turn")
self.turn(30, 2*TURN_TIME_FS_90DEG_MS)
self.drive(23)
else: else:
# Right Side is Free, turn right # Right Side is Free, turn right
self.turn(100, TURN_TIME_FS_90DEG_MS) print("right turn")
self.drive(50) self.turn(30, TURN_TIME_FS_90DEG_MS)
time.sleep(3) self.drive(23)
elif r_dist < 20: # Right side is blocked, left side is free, turn left elif r_dist < sensitivity: # Right side is blocked, left side is free, turn left
self.turn(-100, TURN_TIME_FS_90DEG_MS) print("right left")
self.turn(-30, TURN_TIME_FS_90DEG_MS)
self.drive(23)
else: # Both Side are free, randomize a direction else: # Both Side are free, randomize a direction
print("turn random")
right: bool = randint(0, 1) right: bool = randint(0, 1)
if right: if right:
self.turn(100, TURN_TIME_FS_90DEG_MS) self.turn(30, TURN_TIME_FS_90DEG_MS)
self.drive(50) self.drive(23)
time.sleep(3)
else: else:
self.turn(-100, TURN_TIME_FS_90DEG_MS) self.turn(-23, TURN_TIME_FS_90DEG_MS)
self.drive(50) self.drive(30)
time.sleep(3) self.stop()
time.sleep(0.5) print("stopping")

View File

@ -14,17 +14,16 @@ class sensors:
__humidity = 0 __humidity = 0
__brightness = 0 __brightness = 0
def __init__(self, aux_board: telemetrix_rpi_pico.TelemetrixRpiPico, driver_board: telemetrix_rpi_pico.TelemetrixRpiPico) -> None: def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
self.aux = aux_board self.mcu = mcu
self.driver = driver_board self.mcu.set_pin_mode_sonar(
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( self.mcu.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( self.mcu.set_pin_mode_sonar(
SONAR_3_TRIG_PIN, SONAR_3_ECHO_PIN, self.__sonar_callback) SONAR_3_TRIG_PIN, SONAR_3_ECHO_PIN, self.__sonar_callback)
self.driver.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback) self.mcu.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback)
self.aux.set_pin_mode_analog_input(LIGHT_ANALOG_PIN, 0, self.__light_sensor_callback()) self.mcu.set_pin_mode_analog_input(LIGHT_ANALOG_PIN, 0, self.__light_sensor_callback)
def sonar_get_distance(self, id: int) -> float: def sonar_get_distance(self, id: int) -> float:
return self.__sonar_distances[id] return self.__sonar_distances[id]

View File

@ -1,10 +1,16 @@
import sys import sys
import time
import kuukar.kuukar_lcd as kuukar_lcd import kuukar.kuukar_lcd as kuukar_lcd
from time import sleep from time import sleep
lcd = kuukar_lcd.lcd() lcd = kuukar_lcd.lcd(None, None)
print("Project KuuKar LCD Device Command Line Interface")
lcd.nextion.send_command("volume=100")
while True: while True:
cmd = input("kuukar_lcd$ ") lcd.play_video("keke_hurt")
lcd.nextion.send_command(cmd) time.sleep(1.5)
# print("Project KuuKar LCD Device Command Line Interface")
# while True:
# cmd = input("kuukar_lcd$ ")
# lcd.nextion.send_command(cmd)

View File

@ -3,11 +3,11 @@ import time
from telemetrix_rpi_pico import telemetrix_rpi_pico from telemetrix_rpi_pico import telemetrix_rpi_pico
import kuukar.kuukar_motion as kuukar_motion import kuukar.kuukar_motion as kuukar_motion
import kuukar.kuukar_sensors as kuukar_sensors import kuukar.kuukar_sensors as kuukar_sensors
from kuukar.kuukar_config import SERIAL_AUX_BOARD, SERIAL_DRIVER_BOARD from kuukar.kuukar_config import SERIAL_MCU
from kuukar.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, TURN_TIME_FS_90DEG_MS from kuukar.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, TURN_TIME_FS_90DEG_MS
driver_board = telemetrix_rpi_pico.TelemetrixRpiPico() mcu = telemetrix_rpi_pico.TelemetrixRpiPico()
motion = kuukar_motion.motion(driver_board, None, None) motion = kuukar_motion.motion(mcu, None, None)
motion.drive(30) motion.drive(30)

16
test_sensors.py Normal file
View File

@ -0,0 +1,16 @@
import time
from telemetrix_rpi_pico import telemetrix_rpi_pico
import kuukar.kuukar_sensors as kuukar_sensors
mcu = telemetrix_rpi_pico.TelemetrixRpiPico()
sensors = kuukar_sensors.sensors(mcu)
while True:
temp = sensors.get_temperature()
humid = sensors.get_humidity_pct()
dist1 = sensors.sonar_get_distance(0)
dist2 = sensors.sonar_get_distance(1)
dist3 = sensors.sonar_get_distance(2)
print(f'temp: {temp}, humidity: {humid}, dist1: {dist1}, dist2: {dist2}, dist3: {dist3}')
time.sleep(1)