kuukar-rpi/kuukar/kuukar_motion.py

175 lines
6.1 KiB
Python
Raw Permalink 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-12-08 14:39:18 +00:00
def __init__(self, mcu: telemetrix_rpi_pico.TelemetrixRpiPico, leds: leds, sensors: sensors) -> None:
2022-11-04 04:58:20 +00:00
self.mcu = mcu
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-12-08 14:39:18 +00:00
self.mcu.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-12-08 14:39:18 +00:00
self.mcu.pwm_write(forward_pin, 0)
self.mcu.pwm_write(reverse_pin, -int(speed))
elif speed >= 0:
2022-12-08 14:39:18 +00:00
self.mcu.pwm_write(forward_pin, int(speed))
self.mcu.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)
lm_speed = (100+dir/2)*speed/100
2022-12-13 14:49:17 +00:00
if lm_speed > 99:
2022-11-09 15:59:21 +00:00
lm_speed = 99
2022-12-13 14:49:17 +00:00
elif lm_speed < -99:
2022-11-09 15:59:21 +00:00
lm_speed = -99
rm_speed = (100-dir/2)*speed/100
if rm_speed > 99:
rm_speed = 99
elif rm_speed < -99:
2022-12-13 14:49:17 +00:00
rm_speed = -99
2022-11-09 15:59:21 +00:00
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-12-10 10:09:04 +00:00
'''
2022-12-10 07:20:28 +00:00
def skt_drive(self, speed: float, turn: float, t=0):
speed *=100
turn *=100
leftSpeed = speed - turn
rightSpeed = speed + turn
2022-12-10 09:11:14 +00:00
if leftSpeed>99: leftSpeed=99
elif leftSpeed<-99: leftSpeed= -99
if rightSpeed>99: rightSpeed=99
elif rightSpeed<-99: rightSpeed= -99
if speed>99: speed=99
elif speed<-99: speed= -99
2022-12-10 09:30:42 +00:00
if (rightSpeed - leftSpeed) > 0:
2022-12-10 09:41:22 +00:00
##self.motor_write(MOTOR_FL_F, MOTOR_FL_R, -1*abs(leftSpeed))
2022-12-10 09:30:42 +00:00
self.motor_write(MOTOR_FR_F, MOTOR_FR_R, abs(rightSpeed))
2022-12-10 09:41:22 +00:00
##self.motor_write(MOTOR_RL_F, MOTOR_RL_R, speed)
2022-12-10 09:30:42 +00:00
self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed)
elif (rightSpeed - leftSpeed) < 0:
self.motor_write(MOTOR_FL_F, MOTOR_FL_R, abs(leftSpeed))
2022-12-10 09:41:22 +00:00
##self.motor_write(MOTOR_FR_F, MOTOR_FR_R, -1*abs(rightSpeed))
2022-12-10 09:30:42 +00:00
self.motor_write(MOTOR_RL_F, MOTOR_RL_R, speed)
2022-12-10 09:41:22 +00:00
##self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed)
2022-12-10 09:30:42 +00:00
else:
self.motor_write(MOTOR_FL_F, MOTOR_FL_R, abs(leftSpeed))
self.motor_write(MOTOR_FR_F, MOTOR_FR_R, abs(rightSpeed))
self.motor_write(MOTOR_RL_F, MOTOR_RL_R, speed)
self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed)
2022-12-10 10:09:04 +00:00
'''
def skt_drive(self, speed: float, turn: float, t=0):
2022-12-13 14:49:17 +00:00
speed *= 100
turn *= 100
2022-12-10 10:09:04 +00:00
leftSpeed = speed - turn
rightSpeed = speed + turn
2022-12-13 14:49:17 +00:00
if leftSpeed > 99:
leftSpeed = 99
elif leftSpeed < -99:
leftSpeed = -99
if rightSpeed > 99:
rightSpeed = 99
elif rightSpeed < -99:
rightSpeed = -99
if speed > 99:
speed = 99
elif speed < -99:
speed = -99
2022-12-10 10:09:04 +00:00
self.motor_write(MOTOR_FL_F, MOTOR_FL_R, abs(leftSpeed))
self.motor_write(MOTOR_FR_F, MOTOR_FR_R, abs(rightSpeed))
2022-12-12 09:10:23 +00:00
#self.motor_write(MOTOR_RL_F, MOTOR_RL_R, speed)
#self.motor_write(MOTOR_RR_F, MOTOR_RR_R, speed)
2022-12-11 07:51:17 +00:00
time.sleep(t)
2022-12-10 07:20:28 +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)
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-12-13 14:45:14 +00:00
time.sleep(0.1)
2022-10-28 07:14:00 +00:00
def __roam_handle(self):
2022-12-13 14:49:17 +00:00
if self.sensors.sonar_get_distance() < 900:
self.turn(32, 2600)
2022-12-12 09:18:59 +00:00
self.drive(25)
2022-12-13 14:45:14 +00:00
2022-11-09 16:43:20 +00:00
if not self.roaming:
2022-12-13 14:49:17 +00:00
self.stop()