def motor_brake(self, motor): if motor == KMotor.MOTOR_1: pin8.write_digital(1) pin12.write_digital(1) else: pin0.write_digital(1) pin16.write_digital(1)
def motor_on(self, motor, direction, speed=100): """ Turn motor with the given direction and speed :param motor: KMotor.MOTOR1 or KMotor.Motor2 :param direction: KMotor.FORWARD or KMOTOR.REVERSE :param speed: 0 - 100 :return: """ # make sure the speed is within range if not 0 <= speed <= 100: # not display a "NO" and return display.show(Image.NO) return # speed needs to be scaled from 0-100 to 0-1023 speed = self._scale(speed) # Move Motor Forward if direction == KMotor.FORWARD: if motor == KMotor.MOTOR_1: pin8.write_analog(speed) pin12.write_digital(0) elif motor == KMotor.MOTOR_2: pin0.write_analog(speed) pin16.write_digital(0) # Move Motor In Reverse else: if motor == KMotor.MOTOR_1: pin12.write_analog(speed) pin8.write_digital(0) elif motor == KMotor.MOTOR_2: pin16.write_analog(speed) pin0.write_digital(0)
def motor_brake(self, motor): """ Brake the selected motor. :param motor: :return: """ if motor == KMotor.MOTOR_1: pin8.write_digital(1) pin12.write_digital(1) else: pin0.write_digital(1) pin16.write_digital(1)
def motor_on(self, motor, direction, speed=100): if not 0 <= speed <= 100: display.show(Image.NO) return speed = self._scale(speed) if direction == KMotor.FORWARD: if motor == KMotor.MOTOR_1: pin8.write_analog(speed) pin12.write_digital(0) elif motor == KMotor.MOTOR_2: pin0.write_analog(speed) pin16.write_digital(0) else: if motor == KMotor.MOTOR_1: pin12.write_analog(speed) pin8.write_digital(0) elif motor == KMotor.MOTOR_2: pin16.write_analog(speed) pin0.write_digital(0)
def get_sonar_distance(unit: int = SONAR_CM, timeout_us: int = 30000) -> float: """ Returns object distance from sonar module in given unit. If negative number is returned, the timeout was reached during waiting for echo. """ # trigger pin8.write_digital(0) sleep_us(5) pin8.write_digital(1) sleep_us(10) pin8.write_digital(0) # catch echo echo_time = time_pulse_us(pin12, 1, timeout_us) if echo_time < 0: return echo_time if unit == SONAR_CM: return (echo_time / 2) / 29.1 elif unit == SONAR_IN: return float((echo_time / 2) / 74) return -1.0
try: message = radio.receive() except ValueError: radio.off() sleep(100) radio.on() message = None direction = None if message is not None: data = message.split(":") if data[0] == "move": direction = data[1] if direction is not None: display.show(ARROWS[direction]) pins = DIRECTIONS[direction] # to avoid shorting motors # first turn everything off then turn on the ones that need to be for value in range(2): for index, pin in enumerate([pin0, pin1, pin2, pin8]): if pins[index] == value: pin.write_digital(value) sleep(50) else: pin0.write_digital(0) pin8.write_digital(0) pin1.write_digital(0) pin2.write_digital(0) display.show(Image.NO)
def set_leds(self, state): pin8.write_digital(state)