2022-10-25 14:53:05 +00:00
|
|
|
from random import randint
|
2022-10-30 08:57:00 +00:00
|
|
|
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
|
2022-10-25 07:49:19 +00:00
|
|
|
import time
|
2022-10-30 08:57:00 +00:00
|
|
|
from kuukar.kuukar_leds import leds
|
|
|
|
from kuukar.kuukar_sensors import sensors
|
2022-10-25 07:49:19 +00:00
|
|
|
from telemetrix_rpi_pico import telemetrix_rpi_pico
|
|
|
|
import threading
|
2022-10-23 06:49:59 +00:00
|
|
|
|
2022-10-25 15:01:26 +00:00
|
|
|
|
2022-10-23 06:49:59 +00:00
|
|
|
class motion:
|
2022-10-25 07:49:19 +00:00
|
|
|
|
|
|
|
roaming = False
|
|
|
|
|
|
|
|
def __init__(self, driver_board: telemetrix_rpi_pico.TelemetrixRpiPico, leds: leds, sensors: sensors) -> None:
|
|
|
|
self.driver = driver_board
|
2022-10-23 06:49:59 +00:00
|
|
|
self.leds = leds
|
|
|
|
self.sensors = sensors
|
2022-10-30 08:57:00 +00:00
|
|
|
self.roam_thread = threading.Thread(target=self.__roam_looper)
|
2022-10-25 07:49:19 +00:00
|
|
|
self.roam_thread.start()
|
2022-10-25 15:01:26 +00:00
|
|
|
|
|
|
|
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]
|
2022-10-23 06:49:59 +00:00
|
|
|
for drive_pin in drive_pins:
|
2022-10-25 07:49:19 +00:00
|
|
|
self.driver.set_pin_mode_pwm_output(drive_pin)
|
|
|
|
|
2022-10-25 15:01:26 +00:00
|
|
|
def motor_write(self, forward_pin: int, reverse_pin: int, speed: int):
|
2022-10-25 07:49:19 +00:00
|
|
|
if speed < 0:
|
2022-10-25 15:01:26 +00:00
|
|
|
self.driver.pwm_write(forward_pin, 0)
|
|
|
|
self.driver.pwm_write(reverse_pin, -speed)
|
2022-10-25 07:49:19 +00:00
|
|
|
elif speed >= 0:
|
2022-10-25 15:01:26 +00:00
|
|
|
self.driver.pwm_write(forward_pin, speed)
|
|
|
|
self.driver.pwm_write(reverse_pin, 0)
|
2022-10-25 07:49:19 +00:00
|
|
|
|
|
|
|
def drive(self, speed: int):
|
2022-10-25 15:01:26 +00:00
|
|
|
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)
|
|
|
|
|
2022-10-25 07:49:19 +00:00
|
|
|
def turn(self, speed: int, duration: float):
|
2022-10-28 07:14:00 +00:00
|
|
|
if speed > 0:
|
|
|
|
self.leds.set_right_signal_led(True)
|
|
|
|
elif speed < 0:
|
|
|
|
self.leds.set_left_signal_led(True)
|
2022-10-25 15:01:26 +00:00
|
|
|
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)
|
2022-10-25 07:49:19 +00:00
|
|
|
time.sleep(duration)
|
|
|
|
self.stop()
|
2022-10-30 08:57:00 +00:00
|
|
|
|
|
|
|
def turn_cont(self, speed: int):
|
2022-10-28 07:14:00 +00:00
|
|
|
if speed > 0:
|
2022-10-30 08:57:00 +00:00
|
|
|
self.leds.set_right_signal_led(True)
|
2022-10-28 07:14:00 +00:00
|
|
|
elif speed < 0:
|
2022-10-30 08:57:00 +00:00
|
|
|
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)
|
2022-10-25 07:49:19 +00:00
|
|
|
|
2022-10-23 06:49:59 +00:00
|
|
|
def stop(self):
|
2022-10-28 07:14:00 +00:00
|
|
|
self.leds.set_left_signal_led(False)
|
|
|
|
self.leds.set_right_signal_led(False)
|
|
|
|
self.leds.set_reverse_led(False)
|
2022-10-25 07:49:19 +00:00
|
|
|
self.drive(0)
|
|
|
|
|
2022-10-23 06:49:59 +00:00
|
|
|
def roam_start(self):
|
2022-10-25 07:49:19 +00:00
|
|
|
self.roaming = True
|
|
|
|
|
2022-10-23 06:49:59 +00:00
|
|
|
def roam_stop(self):
|
2022-10-25 07:49:19 +00:00
|
|
|
self.roaming = False
|
|
|
|
|
2022-10-28 07:14:00 +00:00
|
|
|
def __roam_looper(self):
|
2022-10-25 07:49:19 +00:00
|
|
|
while True:
|
|
|
|
if self.roaming:
|
2022-10-28 07:14:00 +00:00
|
|
|
self.__roam_handle()
|
2022-10-25 07:49:19 +00:00
|
|
|
|
2022-10-28 07:14:00 +00:00
|
|
|
def __roam_handle(self):
|
2022-10-25 14:53:05 +00:00
|
|
|
self.drive(50)
|
|
|
|
f_dist = self.sensors.sonar_get_distance(2)
|
2022-10-28 07:14:00 +00:00
|
|
|
if 0 < f_dist < 20: # Close to collision, turn the vehicle
|
2022-10-25 14:53:05 +00:00
|
|
|
l_dist = self.sensors.sonar_get_distance
|
|
|
|
r_dist = self.sensors.sonar_get_distance
|
2022-10-25 15:01:26 +00:00
|
|
|
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)
|
2022-10-25 14:53:05 +00:00
|
|
|
self.drive(50)
|
|
|
|
time.sleep(3)
|
|
|
|
else:
|
2022-10-25 15:01:26 +00:00
|
|
|
# Right Side is Free, turn right
|
|
|
|
self.turn(100, TURN_TIME_FS_90DEG_MS)
|
2022-10-25 14:53:05 +00:00
|
|
|
self.drive(50)
|
|
|
|
time.sleep(3)
|
2022-10-25 15:01:26 +00:00
|
|
|
elif r_dist < 20: # Right side is blocked, left side is free, turn left
|
|
|
|
self.turn(-100, TURN_TIME_FS_90DEG_MS)
|
|
|
|
else: # Both Side are free, randomize a direction
|
|
|
|
right: bool = randint(0, 1)
|
2022-10-25 14:53:05 +00:00
|
|
|
if right:
|
2022-10-25 15:01:26 +00:00
|
|
|
self.turn(100, TURN_TIME_FS_90DEG_MS)
|
2022-10-25 14:53:05 +00:00
|
|
|
self.drive(50)
|
|
|
|
time.sleep(3)
|
|
|
|
else:
|
2022-10-25 15:01:26 +00:00
|
|
|
self.turn(-100, TURN_TIME_FS_90DEG_MS)
|
2022-10-25 14:53:05 +00:00
|
|
|
self.drive(50)
|
|
|
|
time.sleep(3)
|
2022-10-25 15:01:26 +00:00
|
|
|
time.sleep(0.5)
|