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
import kuukar.kuukar_leds as kuukar_leds
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
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__)
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()
mcu = telemetrix_rpi_pico.TelemetrixRpiPico(com_port=SERIAL_MCU)
sensors = kuukar_sensors.sensors(mcu)
leds = kuukar_leds.leds(mcu)
motion = kuukar_motion.motion(mcu, leds, sensors)
lcd = kuukar_lcd.lcd(motion, sensors)
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)
collision = kuukar_collision.collision(mcu, lcd, leds)
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')
def drive():
@ -54,4 +63,3 @@ def set_mode():
motion.roam_start()
elif mode == "MANUAL":
motion.roam_stop()

View File

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

View File

@ -1,4 +1,4 @@
SERIAL_DRIVER_BOARD = "/dev/serial/by-id/..."
SERIAL_MCU = "COM4"
# HC Sonar Pins
SONAR_1_TRIG_PIN = 0
@ -19,21 +19,21 @@ MOTOR_RR_F = 13
MOTOR_RR_R = 12
# 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
HEADLIGHT_LEDS = [5, 10]
LEFT_SIGNAL_LEDS = [6, 3]
RIGHT_SIGNAL_LEDS = [7, 4]
REVERSE_LEDS = [1, 2]
LIGHT_ANALOG_PIN = 0
LIGHT_ANALOG_PIN = 2
DHT22_PIN = 5
DHT22_PIN = 14
SERIAL_LCD = "COM9"

View File

@ -18,14 +18,14 @@ class environment:
while True:
if self.sensors.get_temperature() > 38.0:
if not self.t_alerted:
lcd.play_video("keke_died")
self.lcd.play_video("keke_died")
self.t_alerted = True
else:
self.t_alerted = False
if self.sensors.get_humidity_pct() > 60.0:
if not self.h_alerted:
lcd.play_video("keke_cute_noise")
self.lcd.play_video("keke_cute_noise")
self.h_alerted = True
else:
self.h_alerted = False

View File

@ -1,10 +1,12 @@
from time import sleep
from socket import gethostname, gethostbyname
import kuukar.kuukar_nextion as kuukar_nextion
import kuukar.kuukar_motion as kuukar_motion
import kuukar.kuukar_sensors as kuukar_sensors
class lcd:
def __init__(self) -> None:
def __init__(self, motion: kuukar_motion.motion, sensors: kuukar_sensors.sensors) -> None:
self.nextion = kuukar_nextion.nextion()
sleep(1)
self.nextion.send_command("page home")
@ -40,3 +42,6 @@ class lcd:
speed = 100 - (y - 60) / (205 - 60) * 200
turn = (x - 5) / (150 - 5) * 200 - 100
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()
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 __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
self.mcu = mcu
self.mcu.set_pin_mode_neopixel(LEDS_DATA_PIN, LEDS_NUM)
def set_headlights(self, state: bool):
headlight = True
@ -40,21 +40,21 @@ class leds:
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])
self.mcu.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.mcu.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)
self.mcu.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)
self.mcu.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()
self.mcu.neo_pixel_set_value(led, 255, 0, 0)
self.mcu.neopixel_show()
def flash(self, r: int, g: int, b: int, duration: int):
if not self.flashing:
@ -75,6 +75,6 @@ class leds:
while True:
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

View File

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

View File

@ -14,17 +14,16 @@ class sensors:
__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(
def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico) -> None:
self.mcu = mcu
self.mcu.set_pin_mode_sonar(
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)
self.driver.set_pin_mode_sonar(
self.mcu.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)
self.aux.set_pin_mode_analog_input(LIGHT_ANALOG_PIN, 0, self.__light_sensor_callback())
self.mcu.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback)
self.mcu.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]

View File

@ -1,10 +1,16 @@
import sys
import time
import kuukar.kuukar_lcd as kuukar_lcd
from time import sleep
lcd = kuukar_lcd.lcd()
print("Project KuuKar LCD Device Command Line Interface")
lcd = kuukar_lcd.lcd(None, None)
lcd.nextion.send_command("volume=100")
while True:
cmd = input("kuukar_lcd$ ")
lcd.nextion.send_command(cmd)
lcd.play_video("keke_hurt")
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
import kuukar.kuukar_motion as kuukar_motion
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
driver_board = telemetrix_rpi_pico.TelemetrixRpiPico()
motion = kuukar_motion.motion(driver_board, None, None)
mcu = telemetrix_rpi_pico.TelemetrixRpiPico()
motion = kuukar_motion.motion(mcu, None, None)
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)