kuukar-rpi/kuukar/kuukar_motion.py

153 lines
5.5 KiB
Python
Raw Normal View History

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
import time
2022-10-30 08:57:00 +00:00
from kuukar.kuukar_leds import leds
from kuukar.kuukar_sensors import sensors
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:
roaming = False
2022-11-04 06:56:52 +00:00
def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico, drv: telemetrix_rpi_pico.TelemetrixRpiPico, leds: leds, sensors: sensors) -> None:
2022-11-04 04:58:20 +00:00
self.mcu = mcu
2022-11-04 06:56:52 +00:00
self.drv = drv
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)
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-11-04 06:56:52 +00:00
self.drv.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):
if speed < 0:
2022-11-04 06:56:52 +00:00
self.drv.pwm_write(forward_pin, 0)
2022-11-10 15:46:25 +00:00
self.drv.pwm_write(reverse_pin, -int(speed))
elif speed >= 0:
2022-11-10 15:46:25 +00:00
self.drv.pwm_write(forward_pin, int(speed))
2022-11-04 06:56:52 +00:00
self.drv.pwm_write(reverse_pin, 0)
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)
def turn(self, speed: int, duration: float):
2022-10-28 07:14:00 +00:00
if speed > 0:
2022-11-10 15:50:31 +00:00
self.leds.set_left_signal_led(False)
2022-10-28 07:14:00 +00:00
self.leds.set_right_signal_led(True)
elif speed < 0:
self.leds.set_left_signal_led(True)
2022-11-10 15:50:31 +00:00
self.leds.set_right_signal_led(False)
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-11-04 04:58:20 +00:00
time.sleep(duration/1000.0)
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-11-10 15:50:31 +00:00
self.leds.set_left_signal_led(False)
2022-10-28 07:14:00 +00:00
elif speed < 0:
2022-11-10 15:50:31 +00:00
self.leds.set_right_signal_led(False)
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-11-09 15:59:21 +00:00
def drive_skew(self, speed: float, dir: float):
if dir > 0:
self.leds.set_right_signal_led(True)
2022-11-10 15:50:31 +00:00
self.leds.set_left_signal_led(False)
2022-11-09 15:59:21 +00:00
elif dir < 0:
2022-11-10 15:50:31 +00:00
self.leds.set_right_signal_led(False)
2022-11-09 15:59:21 +00:00
self.leds.set_left_signal_led(True)
2022-11-10 15:50:31 +00:00
2022-11-09 15:59:21 +00:00
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)
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)
self.drive(0)
2022-10-23 06:49:59 +00:00
def roam_start(self):
2022-11-14 16:28:46 +00:00
self.drive(25)
self.roaming = True
2022-10-23 06:49:59 +00:00
def roam_stop(self):
self.roaming = False
2022-11-09 16:18:23 +00:00
time.sleep(0.5)
self.stop()
2022-11-09 15:59:21 +00:00
def is_roaming(self) -> bool:
return self.roaming
2022-10-28 07:14:00 +00:00
def __roam_looper(self):
while True:
if self.roaming:
2022-10-28 07:14:00 +00:00
self.__roam_handle()
2022-11-11 15:01:38 +00:00
time.sleep(0.3)
2022-10-28 07:14:00 +00:00
def __roam_handle(self):
2022-11-09 16:27:42 +00:00
sensitivity = 35
2022-11-09 16:34:26 +00:00
turn_speed = 40
2022-11-09 16:27:42 +00:00
drive_speed = 25
2022-11-09 16:25:05 +00:00
2022-11-04 04:58:20 +00:00
f_dist = self.sensors.sonar_get_distance(1)
if 0 < f_dist < sensitivity: # Close to collision, turn the vehicle
print("collision")
2022-11-09 16:34:26 +00:00
self.drive(-drive_speed)
time.sleep(0.75)
self.stop()
2022-11-04 04:58:20 +00:00
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")
2022-11-09 16:25:05 +00:00
self.turn(30, 2)
2022-11-04 04:58:20 +00:00
self.drive(23)
2022-10-25 14:53:05 +00:00
else:
2022-10-25 15:01:26 +00:00
# Right Side is Free, turn right
2022-11-04 04:58:20 +00:00
print("right turn")
2022-11-09 16:25:05 +00:00
self.turn(turn_speed, 1)
self.drive(drive_speed)
2022-11-04 04:58:20 +00:00
elif r_dist < sensitivity: # Right side is blocked, left side is free, turn left
print("right left")
2022-11-09 16:25:05 +00:00
self.turn(-turn_speed, 1)
self.drive(drive_speed)
2022-10-25 15:01:26 +00:00
else: # Both Side are free, randomize a direction
2022-11-04 04:58:20 +00:00
print("turn random")
2022-10-25 15:01:26 +00:00
right: bool = randint(0, 1)
2022-10-25 14:53:05 +00:00
if right:
2022-11-09 16:25:05 +00:00
self.turn(turn_speed, 1)
self.drive(drive_speed)
2022-10-25 14:53:05 +00:00
else:
2022-11-09 16:25:05 +00:00
self.turn(-turn_speed, 1)
self.drive(drive_speed)
2022-11-09 16:43:20 +00:00
if not self.roaming:
2022-11-11 15:01:38 +00:00
self.stop()