from random import randint 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 import time from kuukar.kuukar_leds import leds from kuukar.kuukar_sensors import sensors from telemetrix_rpi_pico import telemetrix_rpi_pico import threading class motion: roaming = False def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico, drv: telemetrix_rpi_pico.TelemetrixRpiPico, leds: leds, sensors: sensors) -> None: self.mcu = mcu self.drv = drv self.leds = leds self.sensors = sensors self.roam_thread = threading.Thread(target=self.__roam_looper) self.roam_thread.start() 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.drv.set_pin_mode_pwm_output(drive_pin) def motor_write(self, forward_pin: int, reverse_pin: int, speed: int): if speed < 0: self.drv.pwm_write(forward_pin, 0) self.drv.pwm_write(reverse_pin, -int(speed)) elif speed >= 0: self.drv.pwm_write(forward_pin, int(speed)) self.drv.pwm_write(reverse_pin, 0) def drive(self, speed: int): 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) def turn(self, speed: int, duration: float): if speed > 0: self.leds.set_left_signal_led(False) self.leds.set_right_signal_led(True) elif speed < 0: self.leds.set_left_signal_led(True) self.leds.set_right_signal_led(False) 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/1000.0) self.stop() def turn_cont(self, speed: int): if speed > 0: self.leds.set_right_signal_led(True) self.leds.set_left_signal_led(False) elif speed < 0: self.leds.set_right_signal_led(False) 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) def drive_skew(self, speed: float, dir: float): if dir > 0: self.leds.set_right_signal_led(True) self.leds.set_left_signal_led(False) elif dir < 0: self.leds.set_right_signal_led(False) self.leds.set_left_signal_led(True) lm_speed = (100+dir/2)*speed/100 if lm_speed>99: lm_speed = 99 elif lm_speed<-99: lm_speed = -99 rm_speed = (100-dir/2)*speed/100 if rm_speed > 99: rm_speed = 99 elif rm_speed < -99: rm_speed = -99; 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_RL_F, MOTOR_RL_R, lm_speed) self.motor_write(MOTOR_RR_F, MOTOR_RR_R, rm_speed) 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): self.drive(25) self.roaming = True def roam_stop(self): self.roaming = False time.sleep(0.5) self.stop() def is_roaming(self) -> bool: return self.roaming def __roam_looper(self): while True: if self.roaming: self.__roam_handle() time.sleep(0.3) def __roam_handle(self): sensitivity = 35 turn_speed = 40 drive_speed = 25 f_dist = self.sensors.sonar_get_distance(1) if 0 < f_dist < sensitivity: # Close to collision, turn the vehicle print("collision") self.drive(-drive_speed) time.sleep(0.75) self.stop() 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) self.drive(23) else: # Right Side is Free, turn right print("right turn") self.turn(turn_speed, 1) self.drive(drive_speed) elif r_dist < sensitivity: # Right side is blocked, left side is free, turn left print("right left") self.turn(-turn_speed, 1) self.drive(drive_speed) else: # Both Side are free, randomize a direction print("turn random") right: bool = randint(0, 1) if right: self.turn(turn_speed, 1) self.drive(drive_speed) else: self.turn(-turn_speed, 1) self.drive(drive_speed) if not self.roaming: self.stop()