User Tools

Site Tools


quadricone-python-urbaines

quadricone urbaines py

import serial, subprocess, glob, time, csv, random


### assign arduino's serial port
usbport=glob.glob("/dev/tty*")[0]
### set up serial baud rate
ser = serial.Serial(usbport, 9600, timeout=1)


### 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):
            ser.write(chr(255))
            ser.write(chr(self.servoID))
            ser.write(chr(self.speed))
        else:
            print "Servo position must be an integer between 0 and 180.\n"

    def stop(self):
        self.move(90)
        print "stop"

    def down_slow(self):
        self.move(100)
        print "down slow"

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

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

## initiate the morots and put them all in the STOP (90) position
## alternative

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

random.shuffle(Servos)

how_many_times = [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
#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...'
    for i,servo in enumerate(Servos):
        print 'servo', servo
        packet_diff=new_packet_list[i]-packet_list[i]
        print 'packet_diff:', packet_diff
        if packet_diff==0:
            print 'packetdiff:', packet_diff, '; go back up'
            globals()[servo].stop()
            time.sleep(0.3)
            globals()[servo].stop()
        elif 0 < packet_diff < 7:
            if how_many_times[i]<=6:
                print 'packetdiff:', packet_diff, '; up, little'
                globals()[servo].down_slow()
                time.sleep(0.5)
                globals()[servo].stop()
                how_many_times[i]+=0.5
            else:
                globals()[servo].stop()
                time.sleep(0.3)
                how_many_times[i]-=2
        elif 40 > packet_diff >= 7:
            if how_many_times[i]<=6:
                print 'packetdiff:', packet_diff, '; down, little'
                globals()[servo].down()
                time.sleep(0.5)
                globals()[servo].stop()
                how_many_times[i]+=0.5
            else:
                globals()[servo].stop()
                time.sleep(0.3)
                how_many_times[i]-=2
        elif 90 > packet_diff >= 40:
            if how_many_times[i]<=6:
                print 'packetdiff:', packet_diff, '; go down, 2'
                globals()[servo].down()
                time.sleep(1)
                globals()[servo].stop()
                how_many_times[i]+=1
            else:
                globals()[servo].stop()
                time.sleep(0.3)
                how_many_times[i]-=2
        elif 130 > packet_diff >= 90:
            if how_many_times[i]<=6:
                print 'packetdiff:', packet_diff, '; go down, 3'
                globals()[servo].down()
                time.sleep(1.5)
                globals()[servo].stop()
                how_many_times[i]+=1.5
            else:
                globals()[servo].stop()
                time.sleep(0.3)
                how_many_times[i]-=2
        elif packet_diff >= 130:
            print 'packetdiff:', packet_diff, '; go down, 4'
            globals()[servo].down()
            time.sleep(2)
            globals()[servo].stop()
            how_many_times[i]+=2
    packet_list=new_packet_list # update packet list
    print "how many times?", how_many_times
    time.sleep(1)
quadricone-python-urbaines.txt · Last modified: 2013/04/22 17:09 by zoza