This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
motors-scanner-test-1 [2012/09/24 22:01] 81.10.185.142 |
motors-scanner-test-1 [2012/10/06 16:23] 85.218.109.130 |
||
---|---|---|---|
Line 7: | Line 7: | ||
<code python> | <code python> | ||
+ | #!/usr/bin/python | ||
+ | |||
+ | # -*- coding: utf-8 -*- | ||
+ | |||
import subprocess, glob, time, csv | import subprocess, glob, time, csv | ||
from arduino import Arduino, Servo | from arduino import Arduino, Servo | ||
Line 14: | Line 18: | ||
# airodump-ng is running and logging the results into a csv file | # airodump-ng is running and logging the results into a csv file | ||
# subprocess looks for the last edited csv file in current folder | # subprocess looks for the last edited csv file in current folder | ||
- | # 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) | ||
Line 42: | Line 45: | ||
my_board=Arduino(portz) | my_board=Arduino(portz) | ||
my_board.output([9, 10]) | my_board.output([9, 10]) | ||
- | linearone=my_board.attachServo(9) | + | linear_one=my_board.attachServo(9) |
- | lineartwo=my_board.attachServo(10) | + | linear_two=my_board.attachServo(10) |
# !!!! motors are not in sync!!!! | # !!!! motors are not in sync!!!! | ||
# extreme positions of the motor one and two: | # extreme positions of the motor one and two: | ||
- | linearone_max = 139 | ||
- | lineartwo_max = 121 | ||
- | linearone_min = 72 | ||
- | lineartwo_min = 87 | ||
# | linear one | | # | linear one | | ||
Line 59: | Line 58: | ||
# 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:[(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 | + | 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)]} |
- | linear_one.write(positions[0][0][0]) | + | |
- | linear_two.write(positions[0][0][1]) | + | |
- | + | ||
- | time.sleep(10) | + | |
# the main loop ----------------- | # the main loop ----------------- | ||
Line 72: | Line 66: | ||
# it sends the motors to a new position accordingly | # it sends the motors to a new position accordingly | ||
- | key_pos=0 | ||
max_pos=len(positions)-1 | max_pos=len(positions)-1 | ||
+ | key_pos=max_pos | ||
+ | |||
+ | # put the motors in the minimum position | ||
+ | linear_one.write(positions[key_pos][0][0]) | ||
+ | linear_two.write(positions[key_pos][0][1]) | ||
+ | time.sleep(5) | ||
+ | | ||
while True: | while True: | ||
newPackets=int(lookup(lasted)) | 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 (when shrinking the height of the space increases): |
- | if packetsDiff>=10 and packetsDiff<50 and key_pos<max_pos: | + | if packetsDiff>=10 and packetsDiff<50 and key_pos>0: |
- | new_key=key_pos+2 | + | new_key=key_pos-1 |
- | print 'linear1 should stretch',positions[new_key][0][0] | + | print 'linear1 will shrink',positions[new_key][0][0] |
- | print 'linear2 should stretch',positions[new_key][0][1] | + | print 'linear2 will shrink',positions[new_key][0][1] |
print 'current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) | print 'current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) | ||
- | linearone.write(positions[new_key][0][0]) | + | linear_one.write(positions[new_key][0][0]) |
time.sleep(0.3) | time.sleep(0.3) | ||
- | lineartwo.write(positions[new_key][0][1]) | + | linear_two.write(positions[new_key][0][1]) |
time.sleep(0.3) | time.sleep(0.3) | ||
key_pos=new_key #update position | key_pos=new_key #update position | ||
- | | + | |
- | elif packetsDiff>50 and key_pos<max_pos: | + | elif packetsDiff>50 and key_pos>0: |
- | new_key=key_pos+5 | + | new_key=key_pos-5 |
- | print 'linear1 should stretch',positions[new_key][0][0] | + | if new_key<=0: |
- | print 'linear2 should stretch',positions[new_key][0][1] | + | new_key=1 #reset position |
- | linearone.write(positions[new_key][0][0]) | + | key_pos=new_key #update position |
- | 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 '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]) | ||
+ | time.sleep(0.3) | ||
+ | linear_two.write(positions[new_key][0][1]) | ||
+ | time.sleep(0.3) | ||
key_pos=new_key #update position | key_pos=new_key #update position | ||
- | elif packetsDiff<=0 and key_pos>0: | + | elif packetsDiff<=0 and key_pos<max_pos: |
- | new_key=key_pos-2 | + | new_key=key_pos+2 |
- | print 'linear1 should shrink',positions[new_key][0][0] | + | print 'linear1 will stretch',positions[new_key][0][0] |
- | print 'linear2 should shrink',positions[new_key][0][1] | + | print 'linear2 will stretch',positions[new_key][0][1] |
print 'current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) | print 'current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) | ||
- | linearone.write(positions[new_key][0][0]) | + | linear_one.write(positions[new_key][0][0]) |
time.sleep(0.3) | time.sleep(0.3) | ||
- | lineartwo.write(positions[new_key][0][1]) | + | linear_two.write(positions[new_key][0][1]) |
time.sleep(0.3) | time.sleep(0.3) | ||
key_pos=new_key #update position | key_pos=new_key #update position |