# Два наступних рядки істотні для роботи на ОС Windows. if __name__ == '__main__': freeze_support() # Об'єкт який дає доступ до API мотора та молоточків. device = Arduino() # Комунікаційна бібліотека ігнорує дані деякий час, тому даємо їй час на ініціалізацію. time.sleep(0.5) # Розпочати рух вперед. Платформа буде їхати вперед доки ми її не зупинимо. NUM_HAMMERS = 2 state = [0] * NUM_HAMMERS count1 = [0] * NUM_HAMMERS count2 = [0] * NUM_HAMMERS count3 = [0] * NUM_HAMMERS value = [0] * NUM_HAMMERS while True: hammers = device.getHammer() finished = 0 for i in range(NUM_HAMMERS): if state[i] == 0: value[i] = hammers[i] state[i] = 1 count1[i] = time.time() elif state[i] == 1: if value[i] != hammers[i]: count1[i] = time.time() - count1[i] state[i] = 2 value[i] = hammers[i] count2[i] = time.time() elif state[i] == 2: if value[i] != hammers[i]: count2[i] = time.time() - count2[i]
import pygame import sys from arduino import Arduino from multiprocessing import freeze_support if __name__ == '__main__': freeze_support() device = Arduino() pygame.init() pygame.display.set_mode((100, 100)) prev = pygame.time.get_ticks() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: device.close() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_f: device.forwardMotor() if event.key == pygame.K_b: device.backwardMotor() if event.type == pygame.KEYUP: if event.key == pygame.K_f or event.key == pygame.K_b: device.stopMotor() cur = pygame.time.get_ticks() if cur - prev > 1000: prev = cur print device.getHammer() print '=' * 20
freeze_support() device = Arduino() time.sleep(0.5) position = 0 intervals = [3.0, 4.0, 3.0, 4.0, 3.0, 2.0, 2.0, 1.0, 4.0, 1.0, 5.0] cur = 0.0 plan = [] for i in xrange(len(intervals)): plan.append(cur + intervals[i]) cur = plan[i] wait = False while True: h = device.getHammer() if h[4] == 1: wait = True if wait and h[4] == 0: break time.sleep(0.05) start = time.time() device.forwardMotor() while True: if time.time() - start > plan[position]: print 'reached', position, time.time() - start if position % 2 == 0: print 'stopping' device.stopMotor() else: