This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
motors-scanner-test-1 [2012/09/23 11:01] 85.218.109.130 |
motors-scanner-test-1 [2012/09/24 22:01] 81.10.185.142 |
||
---|---|---|---|
Line 16: | Line 16: | ||
# 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 *csv | head -1", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | listlasted=subprocess.Popen("ls -ct1 *csv | head -1", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
lasted=listlasted.communicate()[0].strip() | lasted=listlasted.communicate()[0].strip() | ||
+ | print lasted | ||
# reading the scan log from a csv file | # reading the scan log from a csv file | ||
def lookup(dump): | def lookup(dump): | ||
- | scan=open(dump) | + | scan=open(dump, "rU") |
next(scan) | next(scan) | ||
- | scanDict=csv.DictReader(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(' # IV'): | ||
data=adict.get(' # IV') | data=adict.get(' # IV') | ||
return data | return data | ||
- | + | ||
- | #airodump='/home/selena/doc/synergia/sator/scanning/salzamt1109-01.csv' | + | packets=int(lookup(lasted)) |
+ | print 'packets', packets | ||
# moving part ------------------- | # moving part ------------------- | ||
+ | |||
portz=glob.glob("/dev/ttyUSB*")[0] | portz=glob.glob("/dev/ttyUSB*")[0] | ||
#print portz | #print portz | ||
+ | |||
my_board=Arduino(portz) | my_board=Arduino(portz) | ||
- | my_board.output([9, 11]) | + | my_board.output([9, 10]) |
linearone=my_board.attachServo(9) | linearone=my_board.attachServo(9) | ||
- | lineartwo=my_board.attachServo(11) | + | lineartwo=my_board.attachServo(10) |
- | position=40 | + | # !!!! motors are not in sync!!!! |
- | linearone.write(position), lineartwo.write(position) | + | # extreme positions of the motor one and two: |
- | time.sleep(1) | + | linearone_max = 139 |
+ | lineartwo_max = 121 | ||
+ | linearone_min = 72 | ||
+ | lineartwo_min = 87 | ||
+ | |||
+ | # | linear one | | ||
+ | # 72 - - - - - - - - - - 139 | ||
+ | |||
+ | # | linear two | | ||
+ | # 87 - - - - - - - - - - 121 | ||
+ | |||
+ | # 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:[(88,95)],9:[(93,98)],10:[(100,101)],11:[(110,108)],12:[(114,109)],13:[(120,111)],14:[(124,113)],15:[(125,114)],16:[(128,115)],17:[(129,116)],18:[(130,117)],19:[(132,118)],20:[(135,119)],21:[(137,120)],22:[(139,121)]} | ||
+ | |||
+ | # put the motors in the minimum position | ||
+ | linear_one.write(positions[0][0][0]) | ||
+ | linear_two.write(positions[0][0][1]) | ||
- | packets=int(lookup(lasted)) | ||
- | print 'packets', packets | ||
time.sleep(10) | time.sleep(10) | ||
+ | |||
+ | # the main loop ----------------- | ||
+ | |||
+ | # lookup function checks for difference in the number of packets | ||
+ | # it sends the motors to a new position accordingly | ||
+ | |||
+ | key_pos=0 | ||
+ | max_pos=len(positions)-1 | ||
while True: | while True: | ||
- | newPackets=int(lookup(airodump)) | + | newPackets=int(lookup(lasted)) |
packetsDiff=newPackets-packets | packetsDiff=newPackets-packets | ||
print 'packetsDiff', packetsDiff | print 'packetsDiff', packetsDiff | ||
# decide whether to stretch or shrink: | # decide whether to stretch or shrink: | ||
- | if packetsDiff>10: | + | if packetsDiff>=10 and packetsDiff<50 and key_pos<max_pos: |
- | newPosition=position+(packetsDiff/5) | + | new_key=key_pos+2 |
- | print 'should stretch', newPosition | + | print 'linear1 should stretch',positions[new_key][0][0] |
- | if newPosition <=140: | + | print 'linear2 should stretch',positions[new_key][0][1] |
- | print 'and will do' | + | print 'current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) |
- | linearone.write(newPosition), lineartwo.write(newPosition) | + | linearone.write(positions[new_key][0][0]) |
+ | time.sleep(0.3) | ||
+ | lineartwo.write(positions[new_key][0][1]) | ||
+ | time.sleep(0.3) | ||
+ | key_pos=new_key #update position | ||
+ | |||
+ | elif packetsDiff>50 and key_pos<max_pos: | ||
+ | new_key=key_pos+5 | ||
+ | print 'linear1 should stretch',positions[new_key][0][0] | ||
+ | print 'linear2 should stretch',positions[new_key][0][1] | ||
+ | linearone.write(positions[new_key][0][0]) | ||
+ | time.sleep(0.3) | ||
+ | lineartwo.write(positions[new_key][0][1]) | ||
+ | time.sleep(0.3) | ||
+ | if key_pos>=max_pos: | ||
+ | key_pos=max_pos #reset position | ||
else: | else: | ||
- | print 'already at maximum' | + | key_pos=new_key #update position |
- | # reset | + | elif packetsDiff<=0 and key_pos>0: |
- | position=120 | + | new_key=key_pos-2 |
- | elif packetsDiff<=0: | + | print 'linear1 should shrink',positions[new_key][0][0] |
- | newPosition=position-10 | + | print 'linear2 should shrink',positions[new_key][0][1] |
- | print 'shrinking', newPosition | + | print 'current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) |
- | if newPosition >= 40: | + | linearone.write(positions[new_key][0][0]) |
- | print 'will do' | + | time.sleep(0.3) |
- | linearone.write(newPosition), lineartwo.write(newPosition) | + | lineartwo.write(positions[new_key][0][1]) |
- | else: | + | time.sleep(0.3) |
- | print 'already at minumum' | + | key_pos=new_key #update position |
- | position=50 | + | else: |
- | time.sleep(5) | + | print "else: packetDiff is too little", packetsDiff |
- | packets=newPackets #resets the packets | + | new_key=key_pos |
- | position=newPosition #resets the position (so the next time it will compare with the updated value) | + | print 'nothing changed, will stay in the position', positions[new_key] |
+ | time.sleep(3) | ||
+ | packets=newPackets #updates the packets | ||
</code> | </code> | ||
+ | |||
+ | TODO | ||
+ | |||
+ | * positions resolution | ||
+ | * main loop flow: first add or first decide on the limit | ||
+ | * exceptions, restart.... | ||
+ |