Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import pygame
- import gc
- import sys
- import select
- import tty
- import termios
- import serial
- import time
- import os
- import datetime
- from pygame.locals import *
- #pygame.init()
- s = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=0.1)
- s.open
- #screen = pygame.display.set_mode((640,480))
- #font = pygame.font.Font(None,36)
- resultstring = 'Init'
- rcv = 'init'
- enc1 = 0
- enc2 = 0
- encH = False
- encV = False
- Vlowlimit = 0
- Vhighlimit = 0
- Hlowlimit = 0
- Hhighlimit = 0
- VlowSet = False
- VhighSet = False
- HlowSet = False
- HhighSet = False
- LimitsSet = False
- prevupdate = 0
- pointstaken = [[False for i in range(4096)] for i in range(4096)]
- datetimez = str(datetime.datetime.now().strftime('%Y%m%d%H%M'))
- fileopened = False
- Hmotorstage = 0
- Vmotorstage = 0
- Motorbyte = 0
- Started = False
- Finished = False
- VerticalDirection = 0
- def keycheck():
- return select.select([sys.stdin],[],[],0)==([sys.stdin],[],[])
- def motor(motorz, dir):
- global Hmotorstage
- global Vmotorstage
- global s
- global Motorbyte
- if motorz == 1:
- if dir == 1:
- if Hmotorstage == 0:
- Motorbyte = (Motorbyte & 15) + 80
- Hmotorstage = 1
- elif Hmotorstage == 1:
- Motorbyte = (Motorbyte & 15) + 144
- Hmotorstage = 2
- elif Hmotorstage == 2:
- Motorbyte = (Motorbyte & 15) + 208
- Hmotorstage = 3
- elif Hmotorstage == 3:
- Motorbyte = (Motorbyte & 15) + 224
- Hmotorstage = 4
- elif Hmotorstage == 4:
- Motorbyte = (Motorbyte & 15) + 240
- Hmotorstage = 5
- elif Hmotorstage == 5:
- Motorbyte = (Motorbyte & 15) + 176
- Hmotorstage = 6
- elif Hmotorstage == 6:
- Motorbyte = (Motorbyte & 15) + 112
- Hmotorstage = 7
- elif Hmotorstage == 7:
- Motorbyte = (Motorbyte & 15) + 96
- Hmotorstage = 0
- elif dir == 2:
- if Hmotorstage == 0:
- Motorbyte = (Motorbyte & 15) + 112
- Hmotorstage = 7
- elif Hmotorstage == 1:
- Motorbyte = (Motorbyte & 15) + 96
- Hmotorstage = 0
- elif Hmotorstage == 2:
- Motorbyte = (Motorbyte & 15) + 80
- Hmotorstage = 1
- elif Hmotorstage == 3:
- Motorbyte = (Motorbyte & 15) + 144
- Hmotorstage = 2
- elif Hmotorstage == 4:
- Motorbyte = (Motorbyte & 15) + 208
- Hmotorstage = 3
- elif Hmotorstage == 5:
- Motorbyte = (Motorbyte & 15) + 224
- Hmotorstage = 4
- elif Hmotorstage == 6:
- Motorbyte = (Motorbyte & 15) + 240
- Hmotorstage = 5
- elif Hmotorstage == 7:
- Motorbyte = (Motorbyte & 15) + 176
- Hmotorstage = 6
- elif dir == 3:
- Motorbyte = 170
- s.write(chr(Motorbyte))
- time.sleep(0.01)
- if motorz == 2:
- if dir == 1:
- if Vmotorstage == 0:
- Motorbyte = (Motorbyte & 240) + 5
- Vmotorstage = 1
- elif Vmotorstage == 1:
- Motorbyte = (Motorbyte & 240) + 9
- Vmotorstage = 2
- elif Vmotorstage == 2:
- Motorbyte = (Motorbyte & 240) + 13
- Vmotorstage = 3
- elif Vmotorstage == 3:
- Motorbyte = (Motorbyte & 240) + 14
- Vmotorstage = 4
- elif Vmotorstage == 4:
- Motorbyte = (Motorbyte & 240) + 15
- Vmotorstage = 5
- elif Vmotorstage == 5:
- Motorbyte = (Motorbyte & 240) + 11
- Vmotorstage = 6
- elif Vmotorstage == 6:
- Motorbyte = (Motorbyte & 240) + 7
- Vmotorstage = 7
- elif Vmotorstage == 7:
- Motorbyte = (Motorbyte & 240) + 6
- Vmotorstage = 0
- elif dir == 2:
- if Vmotorstage == 0:
- Motorbyte = (Motorbyte & 240) + 7
- Vmotorstage = 7
- elif Vmotorstage == 1:
- Motorbyte = (Motorbyte & 240) + 6
- Vmotorstage = 0
- elif Vmotorstage == 2:
- Motorbyte = (Motorbyte & 240) + 5
- Vmotorstage = 1
- elif Vmotorstage == 3:
- Motorbyte = (Motorbyte & 240) + 9
- Vmotorstage = 2
- elif Vmotorstage == 4:
- Motorbyte = (Motorbyte & 240) + 13
- Vmotorstage = 3
- elif Vmotorstage == 5:
- Motorbyte = (Motorbyte & 240) + 14
- Vmotorstage = 4
- elif Vmotorstage == 6:
- Motorbyte = (Motorbyte & 240) + 15
- Vmotorstage = 5
- elif Vmotorstage == 7:
- Motorbyte = (Motorbyte & 240) + 11
- Vmotorstage = 6
- elif dir == 3:
- Motorbyte = 170
- s.write(chr(Motorbyte))
- time.sleep(0.01)
- return
- def convert(rcvstring):
- resultstring = ""
- ok = True
- for i in range(len(rcvstring)):
- if i > 6 and i < 12:
- if i == 9:
- resultstring = resultstring + '.'
- if ord(rcvstring[i]) == 207:
- resultstring = resultstring + '0'
- elif ord(rcvstring[i]) == 223:
- resultstring = resultstring + '0'
- elif ord(rcvstring[i]) == 6:
- resultstring = resultstring + '1'
- elif ord(rcvstring[i]) == 22:
- resultstring = resultstring + '1'
- elif ord(rcvstring[i]) == 107:
- resultstring = resultstring + '2'
- elif ord(rcvstring[i]) == 123:
- resultstring = resultstring + '2'
- elif ord(rcvstring[i]) == 47:
- resultstring = resultstring + '3'
- elif ord(rcvstring[i]) == 63:
- resultstring = resultstring + '3'
- elif ord(rcvstring[i]) == 166:
- resultstring = resultstring + '4'
- elif ord(rcvstring[i]) == 182:
- resultstring = resultstring + '4'
- elif ord(rcvstring[i]) == 173:
- resultstring = resultstring + '5'
- elif ord(rcvstring[i]) == 189:
- resultstring = resultstring + '5'
- elif ord(rcvstring[i]) == 237:
- resultstring = resultstring + '6'
- elif ord(rcvstring[i]) == 253:
- resultstring = resultstring + '6'
- elif ord(rcvstring[i]) == 14:
- resultstring = resultstring + '7'
- elif ord(rcvstring[i]) == 30:
- resultstring = resultstring + '7'
- elif ord(rcvstring[i]) == 239:
- resultstring = resultstring + '8'
- elif ord(rcvstring[i]) == 255:
- resultstring = resultstring + '8'
- elif ord(rcvstring[i]) == 175:
- resultstring = resultstring + '9'
- elif ord(rcvstring[i]) == 191:
- resultstring = resultstring + '9'
- else:
- if i <> 7:
- ok = False
- return resultstring;
- print 'initz'
- s.flushOutput()
- motor(1,3)
- motor(2,3)
- while 1:
- try:
- s.flushInput()
- while 1:
- rcv = s.read(1)
- if ord(rcv[0]) == 128:
- rcv = s.read(15)
- break
- except Exception, e1:
- print str(e1)
- s.close()
- s.open()
- if len(rcv) > 11:
- if ord(rcv[6]) == ord(rcv[13]) and ord(rcv[14]) == 128:
- enc1 = (ord(rcv[1]) + (ord(rcv[2]) * 256)) % 4096
- enc2 = 4096 - ((ord(rcv[3]) + (ord(rcv[4]) * 256)) % 4096)
- if ord(rcv[5]) == 1 or ord(rcv[5]) == 3:
- encH = True
- if ord(rcv[5]) >= 2 or ord(rcv[5]) == 3:
- encV = True
- if ord(rcv[6]) <> prevupdate and ord(rcv[6]) == ord(rcv[13]):
- prevupdate = ord(rcv[6])
- resultstring = ""
- resultstring = convert(rcv)
- if encH == False or encV == False or LimitsSet == False:
- os.system('clear')
- print 'Laser:',resultstring
- print 'Vertical:',enc2
- print 'Horizontal:',enc1
- if encH == False:
- print 'Horizontal Encoder Not Referenced'
- if encV == False:
- print 'Vertical Encoder Not Referenced'
- if LimitsSet == False:
- print 'Limit Set, type ? to refresh current values'
- if VlowSet == False:
- rinput = raw_input("Vertical Low Limit:")
- if rinput <> '?':
- Vlowlimit = int(rinput)
- VlowSet = True
- if rinput == '?':
- continue
- else:
- print 'Vertical Low Limit:', Vlowlimit
- if VhighSet == False:
- rinput = raw_input("Vertical High Limit:")
- if rinput <> '?':
- Vhighlimit = int(rinput)
- VhighSet = True
- if rinput == '?':
- continue
- else:
- print 'Vertical High Limit:', Vhighlimit
- if HlowSet == False:
- rinput = raw_input("Horizontal Low Limit:")
- if rinput <> '?':
- Hlowlimit = int(rinput)
- HlowSet = True
- if rinput == '?':
- continue
- else:
- print 'Horizontal Low Limit:', Hlowlimit
- if HhighSet == False:
- rinput = raw_input("Horizontal High Limit:")
- if rinput <> '?':
- Hhighlimit = int(rinput)
- HhighSet = True
- if rinput == '?':
- continue
- if VhighSet == True and VlowSet == True and HhighSet == True and HlowSet == True:
- LimitsSet = True
- if encH == True and encV == True and resultstring <> '.' and resultstring <> '0.000' and resultstring <> 'init' and pointstaken[enc1][enc2] == False and LimitsSet == True:
- if Started == False:
- os.system('clear')
- print 'Returning to Start'
- print 'Horizontal Start', Hlowlimit
- print 'Vertical Start', Vlowlimit
- print 'Vertical:',enc2
- print 'Horizontal:',enc1
- if enc1 > Hlowlimit:
- motor(1,2)
- if enc2 > Vlowlimit:
- motor(2,2)
- if enc1 <= Hlowlimit and enc2 <= Vlowlimit:
- Started = True
- if fileopened == False and Started == True:
- f = open(datetimez + '.bls','w')
- print 'file opened'
- old_settings=termios.tcgetattr(sys.stdin)
- tty.setcbreak(sys.stdin.fileno())
- fileopened = True
- if Started == True:
- pointstaken[enc1][enc2] = True
- print enc1, enc2, resultstring
- f.write(str(enc1) + ' ' + str(enc2) + ' ' + resultstring + '\n')
- if Started == True:
- if VerticalDirection == 0:
- if enc2 < Vhighlimit:
- motor(2,1)
- else:
- VerticalDirection = 1
- motor(1,1)
- elif VerticalDirection == 1:
- if enc2 > Vlowlimit:
- motor(2,2)
- else:
- VerticalDirection = 0
- motor(1,1)
- if enc1 > Hhighlimit:
- print 'Finished'
- break
- if keycheck():
- c = sys.stdin.read(1)
- if c == '\x1b':
- break
- # pygame.draw.rect(screen, (0,0,255),(0,0,640,480),0)
- # text = font.render(str(enc1), 1, (255,255,255))
- # screen.blit(text,(0,0))
- # text = font.render(str(enc2), 1, (255,255,255))
- # screen.blit(text,(0,40))
- # text = font.render(resultstring, 1, (255,255,255))
- # screen.blit(text,(0,80))
- # if encH == False:
- # text = font.render('Horizontal No Ref', 1, (255,255,255))
- # screen.blit(text,(0,120))
- # if encV == False:
- # text = font.render('Vert No Ref', 1, (255,255,255))
- # screen.blit(text,(0,160))
- # pygame.display.flip()
- # pygame.event.pump()
- # keyz = pygame.key.get_pressed()
- # if keyz[pygame.K_ESCAPE]:
- # break
- pass
- motor(1,3)
- motor(2,3)
- f.close()
- termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement