User Tools

Site Tools


quadricone-python-urbaines-noservo

quadricone urbaines noservo py

import subprocess, glob, time, csv, random

''' some naming clarifications:
Servo is a class for motors, where movement is defined
Servos is a dictionary of servo names and positions
servo is used in the while loop to refer to an item of the Servos dictionary
'''

### the moving part -------------------

class Servo:
    def __init__(self, servoID):
        self.servoID=servoID
        self.stop() # when initiated, stop!
        
    def move(self, speed):
        self.speed=speed
        if (0 <= self.speed <= 180):
            print "everything allright, will write", self.speed
        else:
            print "Servo position must be an integer between 0 and 180.\n"
        
    def stop(self):
        self.move(90)

    def down_slow(self):
        self.move(100)

    def down(self):
        self.move(120)

    def up(self):
        self.move(80)


## initiate the morots and put them all in the start (0) position
## alternative

Servos={"raja":0, "gaja":0, "vlaja":0, "zlaja":0}
for i,servo in enumerate(Servos):
    globals()[servo]=Servo(i+1)
    globals()[servo].stop()

#key_pos=[0, 0, 0, 0]

### the scanning part -----------------

# airodump-ng is running and logging the results into a csv file
# with the following command: sudo airodump-ng -o csv -w manuf mon0
# 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 

csvs=subprocess.Popen("ls -t1 *csv | head -1", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
csv_last=csvs.communicate()[0].strip()

key_value=(' # IV') # look up this value in the scan dump

def lookup(dump):
    datas=[]
    results=[]
    scan=open(dump, "rU")
    next(scan)
    scanDict=csv.DictReader((line.replace('\0','') for line in scan), delimiter=',')
    for adict in scanDict:
        if adict.has_key(key_value) and adict.get(key_value)!=None:
            try:
                datas.append(int(adict.get(key_value)))
            except:
                pass
    datas.sort(reverse=True)
    for i in range(4):
        results.append(datas[i])    
    return results
### the main loop ----------------------
    
packet_list=lookup(csv_last)
print 'packet_list', packet_list
#random.shuffle(servo_list)
#print 'servo_list', servo_list
time.sleep(3)

while True:
    new_packet_list=lookup(csv_last)
    print "//////////////////////"
    print "..."
    print 'new_packet_list', new_packet_list
    print 'calculating the movements...'
    print "starting key positions: ", Servos
    for i,servo in enumerate(Servos):
        print "i!!!!i!!!!i!!!!!"
        print 'servo:', servo
        packet_diff=new_packet_list[i]-packet_list[i]
        print 'packet_diff:', packet_diff
        if packet_diff==0:
            print 'go back up'
            globals()[servo].up()
            time.sleep(1)
            globals()[servo].stop()
        elif 12 > packet_diff >= 1:
            print 'up, little'
            globals()[servo].up()
            time.sleep(0.5)
            globals()[servo].stop()
        elif 36 > packet_diff >= 12:
            print 'down, little'
            globals()[servo].down_slow()
            time.sleep(1)
            globals()[servo].stop()     
        elif 80 > packet_diff >= 36:
            print 'go down, big'
            globals()[servo].down()
            time.sleep(2)
            globals()[servo].stop()
        elif 180 > packet_diff >= 80:
            print 'go down, great'
            globals()[servo].down()
            time.sleep(3)
            globals()[servo].stop()
        elif packet_diff >= 180:
            print 'go down, really really great'
            globals()[servo].down()
            time.sleep(4)
            globals()[servo].stop()
        else:
            print 'key_pos:', Servos.get(servo) , 'packet_diff:', packet_diff, 'and i am out of options'
    print "!!!!!!!!!!!!!!!!!!!!"
    print "new list of key pos", Servos
    packet_list=new_packet_list # update packet list
    time.sleep(10)
quadricone-python-urbaines-noservo.txt · Last modified: 2012/11/24 20:48 by 85.218.109.130