import math from .pca9685 import ServoController, map_range from .dc_motor_controller import DcMotor, setup_gpio try: from RPi import GPIO except ImportError: from .mockGpio import GPIO PIN_MOTOR_A_ENABLE = 6 PIN_MOTOR_A_REVERSE = 12 PIN_MOTOR_B_ENABLE = 0 PIN_MOTOR_B_REVERSE = 5 SERVO_ARM_L = 15 SERVO_ARM_R = 14 SERVO_EYE_L = 13 SERVO_EYE_R = 12 SERVO_NECK_ROTATE = 11 SERVO_NECK_TOP = 10 SERVO_NECK_BOTTOM = 9 class WallE: def __init__(self): setup_gpio() self.motor_a = DcMotor(PIN_MOTOR_A_ENABLE, PIN_MOTOR_A_REVERSE) self.motor_b = DcMotor(PIN_MOTOR_B_ENABLE, PIN_MOTOR_B_REVERSE) self.servo_controller = ServoController() self.servo_controller.setup() def set_movement(self, angle: float, force: float): if force > 1: force = 1.0 x = math.cos(angle) * force y = math.sin(angle) * force if y >= 0: n_mot_premix_l = 1.0 if x >= 0 else 1.0 + x n_mot_premix_r = 1.0 - x if x >= 0 else 1.0 else: n_mot_premix_l = 1.0 - x if x >= 0 else 1.0 n_mot_premix_r = 1.0 if x >= 0 else 1.0 + x n_mot_premix_l = n_mot_premix_l * y / 1.0 n_mot_premix_r = n_mot_premix_r * y / 1.0 n_piv_speed = x f_piv_scale = 0.0 if abs(y) > .4 else 1.0 - abs(y) / .4 mot_mix_l = (1.0 - f_piv_scale) * n_mot_premix_l + f_piv_scale * n_piv_speed mot_mix_r = (1.0 - f_piv_scale) * n_mot_premix_r + f_piv_scale * (-n_piv_speed) self.motor_a.set(mot_mix_l) self.motor_b.set(mot_mix_r) def set_eye_velocity(self, angle: float, distance: float): if distance > 1: distance = 1 up_down = distance * math.sin(angle) left_right = distance * math.cos(angle) min_arm_r = 60 max_arm_r = 160 # 80 min_arm_l = 20 max_arm_l = 100 # 80 self.servo_controller.write(SERVO_ARM_L, map_range(up_down, -1.0, 1.0, min_arm_l, max_arm_l)) self.servo_controller.write(SERVO_ARM_R, map_range(left_right, -1.0, 1.0, min_arm_r, max_arm_r))