User Tools

Site Tools


motors-scanner-test-1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
motors-scanner-test-1 [2012/09/11 21:45]
81.10.136.85
motors-scanner-test-1 [2012/10/06 16:40]
85.218.109.130
Line 1: Line 1:
 +====== salzamt, linz, 11. 09. 2012 ======
 +
 +
 airodump-ng is scanning the wifi airodump-ng is scanning the wifi
 +
 this code parses the .csv file airodump is writing to and uses the value of '#​IV'​s or the amount of data packets to send positional values to linear actuators. this code parses the .csv file airodump is writing to and uses the value of '#​IV'​s or the amount of data packets to send positional values to linear actuators.
  
 <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 8: Line 16:
 # scanning part ----------------- # scanning part -----------------
  
-if you want to run airodump ​from python; +# airodump-ng is running and logging the results into a csv file 
-disadvantage:​ doesn'​t run in the background +subprocess looks for the last edited csv file in current folder 
-#cmd='​airodump-ng --bssid 14:​D6:​4D:​B6:​97:​3C -w /​home/​selena/​doc/​synergia/​sator/​scanning/​salzamt1109 mon0'​ +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 
-#​cmd=subprocess.call(cmd.split())+
  
-def lookup(table): +listlasted=subprocess.Popen("​ls -ct1 /​home/​gordo/​rknfg/​*csv | head -1", shell=True, stdout=subprocess.PIPE,​ stderr=subprocess.PIPE) 
-    scan=open(table)+lasted=listlasted.communicate()[0].strip() 
 +print lasted 
 + 
 +# reading the scan log from a csv file 
 + 
 +def lookup(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:
-        data=adict.get(' # IV') +         ​if ​adict.has_key(' # IV'): 
-        ​if ​data!=None:+            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/​tty*")[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) +linear_one=my_board.attachServo(9) 
-lineartwo=my_board.attachServo(11)+linear_two=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)+
  
-packets=int(lookup(airodump)) +# |   ​linear one          | 
-print '​packets',​ packets +# 72 - - - - - - - - - - 139
-time.sleep(10)+
  
 +# |   ​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:​[(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)]}
 +
 +# the main loop -----------------
 +
 +# lookup function checks for difference in the number of packets
 +# it sends the motors to a new position accordingly
 +
 +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(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 ​(when shrinking the height of the space increases):  
-    if packetsDiff>​10:​ +    if packetsDiff>​=10 and packetsDiff<​50 and key_pos>​0
-        ​newPosition=position+(packetsDiff/​5) +        ​new_key=key_pos-1 
-        print 'should stretch', ​newPosition +        print 'linear1 will shrink',positions[new_key][0][0] 
-        ​if newPosition <=140: +        print 'linear2 ​will shrink',​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)+        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 
 + 
 +    elif packetsDiff>​50 and key_pos>​0:​ 
 +        new_key=key_pos-5 
 +        if new_key<​=0:​ 
 +            new_key=1 #reset position 
 +            key_pos=new_key #update position
         else:         else:
-            print 'already at maximum+            print 'linear1 will shrink',​positions[new_key][0][0] 
-            ​# reset +            ​print '​linear2 will shrink',​positions[new_key][0][1] 
-            ​position=120 +            ​linear_one.write(positions[new_key][0][0]) 
-    elif packetsDiff<​=0:​ +            time.sleep(0.3) 
-        ​newPosition=position-10 +            linear_two.write(positions[new_key][0][1]) 
-        print 'shrinking', ​newPosition +            time.sleep(0.3) 
-        ​if newPosition >= 40: +            key_pos=new_key #update position 
-            ​print '​will ​do+    elif packetsDiff<​=0 ​and key_pos<​max_pos
-            ​linearone.write(newPosition), lineartwo.write(newPosition+        ​new_key=key_pos+2 
-        else: +        print 'linear1 will stretch',positions[new_key][0][0] 
-            print '​already at minumum'​ +        print 'linear2 ​will stretch',​positions[new_key][0][1] 
-            ​position=50 +        print '​current key_pos: ' + str(new_key) + " .. was oldkey: " + str(key_pos) 
-    time.sleep(5+        linear_one.write(positions[new_key][0][0]) 
-    packets=newPackets +        time.sleep(0.3) 
-    position=newPosition+        linear_two.write(positions[new_key][0][1]
 +        ​time.sleep(0.3) 
 +        key_pos=new_key #update position 
 +    ​else: 
 +        print "else: packetDiff is too little",​ packetsDiff 
 +        ​new_key=key_pos 
 +        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....
 +
motors-scanner-test-1.txt · Last modified: 2012/10/08 16:45 by 85.218.109.130