User Tools

Site Tools


quadricone-python-zurich

Warning: Use of undefined constant PREG_PATTERN_VALID_LANGUAGE - assumed 'PREG_PATTERN_VALID_LANGUAGE' (this will throw an Error in a future version of PHP) in /var/www/kucjica/emperors-wiki/inc/parser/xhtml.php on line 633

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /var/www/kucjica/emperors-wiki/inc/parser/xhtml.php on line 633

quadricone zurich py

import serial, 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
'''

### 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.startPos() # when initiated, go to zero
 
    def move(self, position):
        self.position=position
        print self.servoID, "\'s given position is", position
        if (10 <= self.position <= 120):
            ser.write(chr(255))
            ser.write(chr(self.servoID))
            ser.write(chr(self.position))
            print "everything OK, will write", self.position
        elif 120 < self.position < 170:
            self.position=160
            ser.write(chr(255))
            ser.write(chr(self.servoID))
            ser.write(chr(self.position))
            print "will adjust to 160", self.position
        elif self.position>170:
            print "MAX", self.servoID, "is in maximum position"
            self.position=180
            ser.write(chr(255))
            ser.write(chr(self.servoID))
            ser.write(chr(self.position))
            print "MAX: Servo will now go to", self.position
        elif self.position<10:
            print "MIN", self.servoID, "is in minimum position"
            self.position=0
            ser.write(chr(255))
            ser.write(chr(self.servoID))
            ser.write(chr(self.position))
            print "MIN: Servo will now go to", self.position
        else:
            print "how did this happen?"
 

    def startPos(self):
        self.move(0)

## 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].startPos()
    
### 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)
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
        pos=Servos.get(servo)
        packet_diff=new_packet_list[i]-packet_list[i]
        print 'packet_diff:', packet_diff
        if packet_diff==0:
            new_pos=pos-10
            print 'go down', new_pos
            globals()[servo].move(new_pos)
            time.sleep(0.2)
            if new_pos<0:
                Servos[servo]=0
            else:
                Servos[servo]=new_pos
        elif 7 > packet_diff >= 1:
            new_pos=Servos.get(servo)+5
            if new_pos < 10: 
                new_pos=10
            print 'go up very small', new_pos
            globals()[servo].move(new_pos)
            time.sleep(0.2)
            Servos[servo]=new_pos # update key position for the next round
        elif 24 > packet_diff >= 7:
            new_pos=Servos.get(servo)+8
            if new_pos < 10:
                new_pos=10
            print 'go up small', new_pos
            globals()[servo].move(new_pos)
            time.sleep(0.2)
            Servos[servo]=new_pos # update key position for the next round
        elif 56 > packet_diff >= 24:
            new_pos=Servos.get(servo)+10
            print 'go up middle', new_pos
            globals()[servo].move(new_pos)
            time.sleep(0.2)
            Servos[servo]=new_pos # update key position for the next round
        elif 100 > packet_diff >= 58:
            new_pos=Servos.get(servo)+20
            print 'go up big', new_pos
            globals()[servo].move(new_pos)
            time.sleep(0.2)
            if new_pos > 180:
                Servos[servo]=180
            else:
                Servos[servo]=new_pos # update key position for the next round
        elif packet_diff >= 100:
            new_pos=Servos.get(servo)+50
            print 'go up very big', new_pos
            globals()[servo].move(new_pos)
            time.sleep(0.2)
            if new_pos > 180:
                Servos[servo]=180
            else:
                Servos[servo]=new_pos # update key position for the next round
        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(2)
quadricone-python-zurich.txt · Last modified: 2012/11/24 20:36 by 85.218.109.130