def rotate_by_angle(self, angle, direction=MotorDriverInterface.Direction.CLOCKWISE, speed=100): """Rotate the motor through a given angle. Args: angle (float): The angle, in degrees, through which the motor is to be rotated. direction (MotorDriver.Direction): The direction of rotation of the motor. Default is clockwise. speed (float): The desired speed as a percentage of the max speed. Must be greater than 0. Default is 100. """ if angle < 0: angle = abs(angle) direction = direction.opposite() steps_to_perform = int(round( ULN2003.STEPS_PER_ROTATION * angle / 360)) delay = self._delay_for_speed(speed) for _ in range(steps_to_perform): self._perform_step(direction, delay) # reset pin states. output(ULN2003.OUTPUT_PINS, LOW) sleep(ULN2003.MIN_DELAY)
def draw_lcd(digit, lcd): active = [] if digit == 0: active = [0, 1, 2, 3, 4, 5] elif digit == 1: active = [0, 3] elif digit == 2: active = [1, 2, 3, 4, 6] elif digit == 3: active = [0, 1, 3, 4, 6] elif digit == 4: active = [0, 3, 6, 5] elif digit == 5: active = [0, 1, 4, 5, 6] elif digit == 6: active = [0, 1, 2, 4, 5, 6] elif digit == 7: active = [0, 3, 4] elif digit == 8: active = [0, 1, 2, 3, 4, 5, 6] elif digit == 9: active = [0, 3, 4, 5, 6] for segment in range(0, 7): if segment in active: output(lcd[segment], HIGH) else: output(lcd[segment], LOW)
def alarm() : '''Light up the LED On alarm, light up the led ''' output (PIN_LED, HIGH) print('pass') sleep (DELAY_ALARM) output (PIN_LED, LOW)
def alarm(): '''Light up the LED On alarm, light up the led ''' output(PIN_LED, HIGH) send_sms() sleep(DELAY_ALARM) output(PIN_LED, LOW)
def forward(duty=50): global isForward global isBackward isBackward = False if isForward: duty = 75 straight() output(20, LOW) output(22, HIGH) p.ChangeDutyCycle(float(duty)) isForward = True
def draw_number(number, row): binary = convert_to_binary(number) while len(binary) < len(row): binary = [0] + binary for i in range(0, len(row)): if binary[i] == 1: output(row[i], HIGH) else: output(row[i], LOW)
def _perform_step(self, direction=MotorDriverInterface.Direction.CLOCKWISE, delay=MIN_DELAY): """Perform 1 full step of the motor. Args: direction (MotorDriver.Direction): The direction of rotation of the motor in this step. delay (float): Time in seconds to wait between each update to motor controller pin states. """ for state in ULN2003.PIN_STATES[::direction.value]: output(ULN2003.OUTPUT_PINS, state) sleep(delay)
def turn(self, degrees, dir=True, speed=200, error=1): try: start_degrees = float(ser.readline().split()[0]) start_time = time() if dir: #True is right, false is left output(self.dir_pin, 1) else: output(self.dir_pin, 0) self.setSpeed(speed) while (abs(degrees - abs( abs(start_degrees) - abs(float(ser.readline().split()[0])))) > error): if time() - start_time > 1: self.setSpeed(0) return except: turn(degrees, dir, speed, error) self.setSpeed(0)
def setup_gpio(): setmode(BCM) setwarnings(False) for led in BIT_LEDS + [ONE_HUNDRED_LED, TWO_HUNDRED_LED]: setup(led, OUT) for segment in LSD + MSD: setup(segment, OUT) setup(BEEPER, OUT) setup(BUTTON, IN, pull_up_down=PUD_UP) cleanup() for segment in [0, 1, 2, 3, 4, 5]: output(LSD[segment], HIGH) output(MSD[segment], HIGH)
def loop(): '''Infinity loop Setup the trigger get the echo calculate and prints distance prints out of range if the distance is incorrect ''' pulse_start = 0 pulse_end = 0 while True: output(PIN_TRIGGER, False) sleep(TIMESTAMP_SETUP) output(PIN_TRIGGER, True) sleep(TIMESTAMP_EXEC) output(PIN_TRIGGER, False) while not input(PIN_ECHO): pulse_start = time() while input(PIN_ECHO): pulse_end = time() distance = (pulse_end - pulse_start) * CM_CONVERTOR distance = round(distance, 2) distance -= 0.5 if 2 < distance < 250: print("Distance: ", distance, "cm") else: print("Out Of Range (", distance, ")")
def get_dist() : '''Get the distance Returns: Distance in cm ''' output (PIN_TRIGGER, False) sleep (DELAY_SETUP) output (PIN_TRIGGER, True) sleep (DELAY_EXEC) output (PIN_TRIGGER, False) while not input (PIN_ECHO): pulse_start = time() while input (PIN_ECHO): pulse_end = time() distance = (pulse_end - pulse_start) * CM_CONVERTOR distance = round (distance, 2) if not MIN_DIST < distance < MAX_DIST: distance = 0 return distance
def cleanup(): for led in BIT_LEDS + [ONE_HUNDRED_LED, TWO_HUNDRED_LED]: output(led, LOW) for segment in LSD + MSD: output(segment, LOW) output(BEEPER, LOW)
def blink (iterations, delay) : for x in range (iterations): output (PIN_LED, HIGH) sleep (delay) output (PIN_LED, LOW) sleep (delay)
def off(): output(7, False)
def stop(duty=0): straight() output(20, LOW) output(22, LOW) return default_response()
def low(self): output(self.gpio, LOW)
def randomize(led): if random.randint(0, 1) == 1: output(led, HIGH) else: output(led, LOW)
def turnOn(self): output(ELEMENT_GPIO_PIN, HIGH)
def right(duty=100): output(5, HIGH) output(6, LOW) p2.ChangeDutyCycle(float(duty))
def on(): output(7, True)
''' import RPi.GPIO from RPi.GPIO import cleanup from RPi.GPIO import output from RPi.GPIO import setup from RPi.GPIO import setmode from RPi.GPIO import BCM from RPi.GPIO import HIGH from RPi.GPIO import LOW from RPi.GPIO import OUT import time from time import sleep DELAY = 1 PIN_LED = 18 if __name__ == '__main__': setmode(BCM) setup(PIN_LED, OUT) while True: try: output(PIN_LED, HIGH) sleep(DELAY) output(PIN_LED, LOW) sleep(DELAY) except (KeyboardInterrupt, SystemExit) as e: cleanup()
def turnOff(self): output(ELEMENT_GPIO_PIN, LOW)
def statusOff(self): output(STATUS_GPIO_PIN, LOW)
def draw(number): binary = convert_to_8_bit_binary(number) for i in range(0, 8): if binary[i] == 1: output(BIT_LEDS[i], HIGH) else: output(BIT_LEDS[i], LOW) numberstring = str(number).zfill(2) draw_lcd(int(numberstring[-1]), LSD) draw_lcd(int(numberstring[-2]), MSD) if len(numberstring) > 2: if numberstring[0] == "1": output(ONE_HUNDRED_LED, HIGH) output(TWO_HUNDRED_LED, LOW) elif numberstring[0] == "2": output(ONE_HUNDRED_LED, HIGH) output(TWO_HUNDRED_LED, HIGH) else: output(ONE_HUNDRED_LED, LOW) output(TWO_HUNDRED_LED, LOW)
def statusOn(self): output(STATUS_GPIO_PIN, HIGH)
from RPi.GPIO import setmode, setwarnings, setup, output, BCM, HIGH, LOW, OUT, cleanup from time import sleep pins = [17, 27, 22, 23] setmode(BCM) setwarnings(False) for i in pins: setup(i, OUT) output(i, 0) c = [ [1,0,0,0], [1,1,0,0], [0,1,0,0], [0,1,1,0], [0,0,1,0], [0,0,1,1], [0,0,0,1], [1,0,0,1] ] for i in range(512): for i in range(8): for j in range(4): output(pins[j], c[i][j]) sleep(0.01)
def left(duty=100): output(5, LOW) output(6, HIGH) p2.ChangeDutyCycle(float(duty)) return default_response()
def __init__(self): self.count = 0 def increment(self): if self.count < 255: self.count += 1 draw(self.count) def reset(self): self.count = 0 draw(self.count) if __name__ == "__main__": setup_gpio() try: counter = Counter() while True: wait_for_input() output(BEEPER, HIGH) counter.increment() time.sleep(0.2) output(BEEPER, LOW) except KeyboardInterrupt: cleanup()
def straight(duty=100): output(5, LOW) output(6, LOW)
setmode(BCM) # pins are called by the number on the pi ledRED = 17 ledGREEN = 16 # red led = pin 17 # green led = pin 16 setup(ledRED, OUT) setup(ledGREEN, OUT) # sets led pins as outputs count = 0 # counter starts at 0 while count < 10: # until each led has blinked 10 times output(ledRED, HIGH) output(ledGREEN, LOW) # turns the red led on and green one off sleep(1) output(ledRED, LOW) output(ledGREEN, HIGH) # turns the red led off and green one on sleep(1) count = count + 1 # adds one to the counter output(ledGREEN, LOW) # turns off the green led
def cleanup(): for led in LEDS: output(led, LOW)
txLedOn = 1 txLedOff = 0 else: print("No BPF") txLed = 27 txLedOn = 1 txLedOff = 0 # GPIO.setup(txLed, GPIO.OUT) # output(txLed, txLedOff) GPIO.setmode(GPIO.BCM) # Repeat to make LED work on Pi 4 GPIO.setwarnings(False) GPIO.setup(txLed, GPIO.OUT) output(txLed, txLedOn) sleep(1) output(txLed, txLedOff) # print(txLedOn) print(txLed) # GPIO.setup(27, GPIO.OUT) # GPIO.output(27, 0) debug_mode = 0 if __name__ == "__main__": if (len(sys.argv)) > 1: # print("There are arguments!") if (('d' == sys.argv[1]) or ('-d' in sys.argv[1])):
def on(): setup(7, OUT) output(7, True)