This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
motors-scanner-test-1 [2012/10/06 16:40] 85.218.109.130 |
motors-scanner-test-1 [2012/10/08 16:45] (current) 85.218.109.130 |
||
---|---|---|---|
Line 20: | Line 20: | ||
# this file is parsed by the csv module to find the number of data packets that have passed through the network since the last check | # this file is parsed by the csv module to find the number of data packets that have passed through the network since the last check | ||
- | listlasted=subprocess.Popen("ls -ct1 /home/gordo/rknfg/*csv | head -1", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | + | listlasted=subprocess.Popen("ls -ct1 $HOME/rknfg/*csv | head -1", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
lasted=listlasted.communicate()[0].strip() | lasted=listlasted.communicate()[0].strip() | ||
print lasted | print lasted | ||
# reading the scan log from a csv file | # reading the scan log from a csv file | ||
+ | |||
+ | lookup_key=' # IV' | ||
def lookup(dump): | def lookup(dump): | ||
+ | datalist=[] | ||
scan=open(dump, "rU") | scan=open(dump, "rU") | ||
next(scan) | next(scan) | ||
scanDict=csv.DictReader((line.replace('\0','') for line in scan), delimiter=',') | scanDict=csv.DictReader((line.replace('\0','') for line in scan), delimiter=',') | ||
for adict in scanDict: | for adict in scanDict: | ||
- | if adict.has_key(' # IV'): | + | if adict.has_key(lookup_key) and adict.get(lookup_key)!=None: |
- | data=adict.get(' # IV') | + | try: |
- | return data | + | datalist.append(int(adict.get(lookup_key))) |
- | + | except: | |
- | packets=int(lookup(lasted)) | + | pass |
+ | return sum(datalist) | ||
+ | |||
+ | packets=lookup(lasted) | ||
print 'packets', packets | print 'packets', packets | ||
# moving part ------------------- | # moving part ------------------- | ||
- | + | ||
- | portz=glob.glob("/dev/tty*")[0] | + | portz=glob.glob("/dev/ttyUSB*")[0] |
#print portz | #print portz | ||
- | + | ||
my_board=Arduino(portz) | my_board=Arduino(portz) | ||
my_board.output([9, 10]) | my_board.output([9, 10]) | ||
Line 58: | Line 64: | ||
# thus a dictionary of compatible values tupples is used to make sure the two motors are always in the same position | # thus a dictionary of compatible values tupples is used to make sure the two motors are always in the same position | ||
- | |||
positions = {0:[(72,87)],1:[(73,88)],2:[(74,88)],3:[(75,89)],4:[(76,90)],5:[(77,90)],6:[(78,91)],7:[(82,92)],8:[(83,92)],9:[(84,92)],10:[(86,94)],11:[(88,95)],12:[(90,96)],13:[(92,97)],14:[(93,98)],15:[(95,99)],16:[(98,100)],17:[(100,101)],18:[(102,102)],19:[(103,103)],20:[(104,104)],21:[(106,105)],22:[(107,106)],23:[(109,107)],24:[(110,108)],25:[(114,109)],26:[(117,110)],27:[(120,111)],28:[(122,112)],29:[(124,113)],30:[(125,114)],31:[(128,115)],32:[(129,116)],33:[(130,117)],34:[(132,118)],35:[(135,119)],36:[(137,120)],37:[(139,121)]} | positions = {0:[(72,87)],1:[(73,88)],2:[(74,88)],3:[(75,89)],4:[(76,90)],5:[(77,90)],6:[(78,91)],7:[(82,92)],8:[(83,92)],9:[(84,92)],10:[(86,94)],11:[(88,95)],12:[(90,96)],13:[(92,97)],14:[(93,98)],15:[(95,99)],16:[(98,100)],17:[(100,101)],18:[(102,102)],19:[(103,103)],20:[(104,104)],21:[(106,105)],22:[(107,106)],23:[(109,107)],24:[(110,108)],25:[(114,109)],26:[(117,110)],27:[(120,111)],28:[(122,112)],29:[(124,113)],30:[(125,114)],31:[(128,115)],32:[(129,116)],33:[(130,117)],34:[(132,118)],35:[(135,119)],36:[(137,120)],37:[(139,121)]} | ||
Line 68: | Line 73: | ||
max_pos=len(positions)-1 | max_pos=len(positions)-1 | ||
key_pos=max_pos | key_pos=max_pos | ||
- | | + | |
# put the motors in the minimum position | # put the motors in the minimum position | ||
linear_one.write(positions[key_pos][0][0]) | linear_one.write(positions[key_pos][0][0]) | ||
linear_two.write(positions[key_pos][0][1]) | linear_two.write(positions[key_pos][0][1]) | ||
time.sleep(5) | time.sleep(5) | ||
- | | + | |
while True: | while True: | ||
newPackets=int(lookup(lasted)) | newPackets=int(lookup(lasted)) | ||
Line 79: | Line 84: | ||
print 'packetsDiff', packetsDiff | print 'packetsDiff', packetsDiff | ||
# decide whether to stretch or shrink (when shrinking the height of the space increases): | # decide whether to stretch or shrink (when shrinking the height of the space increases): | ||
- | if packetsDiff>=10 and packetsDiff<50 and key_pos>0: | + | # case 1: motors will shrink for one position each; the initial key_pos value needs to be bigger than 0 |
+ | if packetsDiff>=8 and packetsDiff<50 and key_pos>0: | ||
new_key=key_pos-1 | new_key=key_pos-1 | ||
- | print 'linear1 will shrink',positions[new_key][0][0] | + | print 'case 1: new_key=', new_key, 'position:', positions[new_key] |
- | print 'linear2 will shrink',positions[new_key][0][1] | + | #print 'linear1 will shrink',positions[new_key][0][0] |
- | print 'current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) | + | #print 'linear2 will shrink',positions[new_key][0][1] |
+ | #print 'current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) | ||
linear_one.write(positions[new_key][0][0]) | linear_one.write(positions[new_key][0][0]) | ||
time.sleep(0.3) | time.sleep(0.3) | ||
Line 90: | Line 97: | ||
key_pos=new_key #update position | key_pos=new_key #update position | ||
- | elif packetsDiff>50 and key_pos>0: | + | # case 2: motors will shrink for 5 positions; the initial value must be bigger than 5 |
+ | elif packetsDiff>=50 and key_pos>0: | ||
new_key=key_pos-5 | new_key=key_pos-5 | ||
if new_key<=0: | if new_key<=0: | ||
+ | print 'case 2: but cannot shrink more' | ||
new_key=1 #reset position | new_key=1 #reset position | ||
key_pos=new_key #update position | key_pos=new_key #update position | ||
else: | else: | ||
- | print 'linear1 will shrink',positions[new_key][0][0] | + | print 'case 2: new_key=', new_key, 'position:', positions[new_key] |
- | print 'linear2 will shrink',positions[new_key][0][1] | + | #print 'linear1 will shrink',positions[new_key][0][0] |
+ | #print 'linear2 will shrink',positions[new_key][0][1] | ||
linear_one.write(positions[new_key][0][0]) | linear_one.write(positions[new_key][0][0]) | ||
time.sleep(0.3) | time.sleep(0.3) | ||
Line 103: | Line 113: | ||
time.sleep(0.3) | time.sleep(0.3) | ||
key_pos=new_key #update position | key_pos=new_key #update position | ||
+ | # case 3: motors will stretch for two positions; the initial value must not be bigger than max_pos | ||
elif packetsDiff<=0 and key_pos<max_pos: | elif packetsDiff<=0 and key_pos<max_pos: | ||
new_key=key_pos+2 | new_key=key_pos+2 | ||
- | print 'linear1 will stretch',positions[new_key][0][0] | + | if new_key>=max_pos: |
- | print 'linear2 will stretch',positions[new_key][0][1] | + | print 'case 3: but cannot stretch more' |
- | print 'current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) | + | new_key=max_pos |
- | linear_one.write(positions[new_key][0][0]) | + | key_pos=new_key #update position |
- | time.sleep(0.3) | + | else: |
- | linear_two.write(positions[new_key][0][1]) | + | print 'case 3: new_key=', new_key, 'position:', positions[new_key] |
- | time.sleep(0.3) | + | #print 'linear1 will stretch',positions[new_key][0][0] |
- | key_pos=new_key #update position | + | #print 'linear2 will stretch',positions[new_key][0][1] |
+ | #print 'current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) | ||
+ | linear_one.write(positions[new_key][0][0]) | ||
+ | time.sleep(0.3) | ||
+ | linear_two.write(positions[new_key][0][1]) | ||
+ | time.sleep(0.3) | ||
+ | key_pos=new_key #update position | ||
else: | else: | ||
print "else: packetDiff is too little", packetsDiff | print "else: packetDiff is too little", packetsDiff | ||
new_key=key_pos | new_key=key_pos | ||
- | print 'nothing changed, will stay in the position', positions[new_key] | + | #print 'nothing changed, will stay in the position', positions[new_key] |
time.sleep(3) | time.sleep(3) | ||
packets=newPackets #updates the packets | packets=newPackets #updates the packets | ||
+ | |||
</code> | </code> |