add leds signal and headlight handling

This commit is contained in:
Siwat Sirichai 2022-10-28 14:14:00 +07:00
parent e5864eeec5
commit e117177b9b
15 changed files with 178 additions and 56 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

10
.idea/kuukar-rpi.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (kuukar-rpi)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/kuukar-rpi.iml" filepath="$PROJECT_DIR$/.idea/kuukar-rpi.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,13 +1,19 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico
import kuukar_leds, kuukar_collision, kuukar_motion, kuukar_lcd, kuukar_sensors, kuukar_environment
import kuukar_leds
import kuukar_collision
import kuukar_motion
import kuukar_lcd
import kuukar_sensors
import kuukar_environment
from kuukar_config import SERIAL_AUX_BOARD, SERIAL_DRIVER_BOARD
if __name__ == '__main__':
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)
sensors = kuukar_sensors.sensors(aux_board, driver_board)
leds = kuukar_leds.leds(aux_board=aux_board)
lcd = kuukar_lcd.lcd()
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)
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)

View File

@ -13,5 +13,5 @@ class collision:
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

@ -18,7 +18,7 @@ MOTOR_RL_R = 11
MOTOR_RR_F = 12
MOTOR_RR_R = 13
# Full Speed 90 Degress Turn Time
# Full Speed 90 Degrees Turn Time
TURN_TIME_FS_90DEG_MS = 1500
SERIAL_AUX_BOARD = "/dev/serial/by-id/..."
@ -27,6 +27,12 @@ COLLISION_DETECTOR_PIN = 2
LEDS_DATA_PIN = 3
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
DHT22_PIN = 5

View File

@ -10,7 +10,7 @@ class cv:
def capture_image(self):
pass
def detectFace(self, image):
def detect_face(self, image):
pass
def handle_face_detected(self):

View File

@ -12,9 +12,9 @@ class environment:
self.lcd = lcd
self.leds = leds
self.sensors = sensors
threading.Thread(target=self.__checker__).start()
threading.Thread(target=self.__checker).start()
def __checker__(self):
def __checker(self):
while True:
if self.sensors.get_temperature() > 38.0:
if not self.t_alerted:
@ -30,4 +30,8 @@ class environment:
else:
self.h_alerted = False
sleep(5)
if self.sensors.get_brightness_pct() < 50:
self.leds.set_headlights(True)
else:
self.leds.set_headlights(False)
sleep(1)

View File

@ -2,36 +2,79 @@ import threading
from time import perf_counter
from telemetrix_rpi_pico import telemetrix_rpi_pico
import time
from kuukar_config import LEDS_DATA_PIN, LEDS_NUM
from kuukar_config import LEDS_DATA_PIN, LEDS_NUM, HEADLIGHT_LEDS, LEFT_SIGNAL_LEDS, RIGHT_SIGNAL_LEDS, REVERSE_LEDS
class leds:
flashing = False
headlight = False
reverse_signal = False
left_signal = False
right_signal = False
ambient_light = [0, 0, 0]
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 flash(self,r: int, g: int, b: int, duration: int):
def set_headlights(self, state: bool):
headlight = True
self.__update_leds()
def set_left_signal_led(self, state: bool):
self.left_signal = state
self.__update_leds()
def set_right_signal_led(self, state: bool):
self.right_signal = state
self.__update_leds()
def set_ambient_led(self, r: int, g: int, b: int):
self.ambient_light = [r, g, b]
self.__update_leds()
def set_reverse_led(self, state: bool):
self.reverse_signal = True
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])
if self.headlight:
for led in HEADLIGHT_LEDS:
self.aux_board.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)
if self.right_signal:
for led in RIGHT_SIGNAL_LEDS:
self.aux_board.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()
def flash(self, r: int, g: int, b: int, duration: int):
if not self.flashing:
self.flashing = True
self.start_time = perf_counter()
threading.Thread(target=self.__flasher__,args=(r,g,b,duration))
threading.Thread(target=self.__flasher, args=(r, g, b, duration))
@staticmethod
def __blink_func__(t: float,dur: float):
if t>dur:
def __blink_func(t: float, dur: float):
if t > dur:
return 0
return (-1/(dur/2.0)**2)*(t-dur/2.0)**2+1
return (-1 / (dur / 2.0) ** 2) * (t - dur / 2.0) ** 2 + 1
def __get_time__(self) -> float:
return perf_counter()-self.start_time
def __get_time(self) -> float:
return perf_counter() - self.start_time
def __flasher__(self, r: int, g: int, b: int, duration: int):
def __flasher(self, r: int, g: int, b: int, duration: int):
while True:
print(leds.__blink_func__(self.__get_time__(),duration))
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

@ -38,14 +38,25 @@ class motion:
self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed)
def turn(self, speed: int, duration: float):
if speed > 0:
self.leds.set_right_signal_led(True)
elif speed < 0:
self.leds.set_left_signal_led(True)
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()
if speed > 0:
self.leds.set_right_signal_led(False)
elif speed < 0:
self.leds.set_left_signal_led(False)
def stop(self):
self.leds.set_left_signal_led(False)
self.leds.set_right_signal_led(False)
self.leds.set_reverse_led(False)
self.drive(0)
def roam_start(self):
@ -54,15 +65,15 @@ class motion:
def roam_stop(self):
self.roaming = False
def __roam_looper__(self):
def __roam_looper(self):
while True:
if self.roaming:
self.__roam_handle__()
self.__roam_handle()
def __roam_handle__(self):
def __roam_handle(self):
self.drive(50)
f_dist = self.sensors.sonar_get_distance(2)
if f_dist > 0 and f_dist < 20: # Close to collision, turn the vehicle
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

View File

@ -19,9 +19,9 @@ class nextion:
def __init__(self) -> None:
self.device = serial.Serial(SERIAL_LCD, baudrate=19200, timeout=5)
self.__send_stop_bit__()
self.__send_stop_bit()
self.reset_device()
threading.Thread(target=self.__serial_reader__).start()
threading.Thread(target=self.__serial_reader).start()
threading.Thread(target=self.__serial_writer__).start()
def send_command(self, command: str):
@ -31,22 +31,22 @@ class nextion:
def __serial_writer__(self):
if len(self.__commands_output_buffer__) > 0:
self.device.write(self.__commands_output_buffer__.pop(0))
self.__send_stop_bit__()
self.__send_stop_bit()
def reset_device(self):
self.send_command("rest")
def __get_stop_bit__(self) -> bytearray:
def __get_stop_bit(self) -> bytearray:
stop_bit = bytearray()
stop_bit.append(0xFF)
stop_bit.append(0xFF)
stop_bit.append(0XFF)
return stop_bit
def __send_stop_bit__(self):
self.device.write(self.__get_stop_bit__())
def __send_stop_bit(self):
self.device.write(self.__get_stop_bit())
def __serial_reader__(self) -> None:
def __serial_reader(self) -> None:
ser = self.device
while True:
if self.allow_serial_read:

View File

@ -1,44 +1,54 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico
from kuukar_config import DHT22_PIN, SONAR_1_ECHO_PIN, SONAR_1_TRIG_PIN, SONAR_2_ECHO_PIN, SONAR_2_TRIG_PIN, SONAR_3_ECHO_PIN, SONAR_3_TRIG_PIN
from kuukar_config import DHT22_PIN, SONAR_1_ECHO_PIN, SONAR_1_TRIG_PIN,\
SONAR_2_ECHO_PIN, SONAR_2_TRIG_PIN, SONAR_3_ECHO_PIN, SONAR_3_TRIG_PIN,\
LIGHT_ANALOG_PIN
class sensors:
__sonar_trig_pins__ = [SONAR_1_TRIG_PIN,
__sonar_trig_pins = [SONAR_1_TRIG_PIN,
SONAR_2_TRIG_PIN, SONAR_3_TRIG_PIN]
__sonar_distances__ = [0, 0, 0]
__temperature__ = 0
__humidity__ = 0
__sonar_distances = [0, 0, 0]
__temperature = 0
__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(
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(
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(
SONAR_3_TRIG_PIN, SONAR_3_ECHO_PIN, self.__sonar_callback__)
self.driver.set_pin_mode_dht(DHT22_PIN, self.__dht22_callback__)
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())
def sonar_get_distance(self, id: int) -> float:
return self.__sonar_distances__[id]
return self.__sonar_distances[id]
def __sonar_callback__(self, data):
def __sonar_callback(self, data):
pin = data[1]
distance = data[2]
sonar_id = self.__sonar_trig_pins__.index(pin)
self.__sonar_distances__[sonar_id] = distance
sonar_id = self.__sonar_trig_pins.index(pin)
self.__sonar_distances[sonar_id] = distance
def get_brightness_pct(self) -> float:
return self.__brightness
def get_temperature(self) -> float:
return self.__temperature__
return self.__temperature
def get_humidity_pct(self) -> float:
return self.__humidity__
return self.__humidity
def __dht22_callback__(self, data):
def __light_sensor_callback(self, data):
self.__brightness = data[2]
def __dht22_callback(self, data):
humidity = data[3]
temperature = data[4]
self.__humidity__ = humidity
self.__temperature__ = temperature
self.__humidity = humidity
self.__temperature = temperature