TRUE = 1
FALSE = 0
import debug
import MOD
import MDM
import GPIO
import morselib
 
#Reset GPS
def reset_gps():
    MDM.send('AT$GPSR=1\r', 0)
    temp = MDM.receive(1)
 
#Get Position (without using the GPS library)    
def getActualPosition():
    MDM.send('AT$GPSACP\r', 0)
    pos = MDM.receive(1)
    pos = pos.split('\r\n')
    return pos[1]
 
#GPS status
def gps_status(gpspos):
    debug.msg('Retrieving GPS status')
    gpspos_parts = gpspos.split(',')
    if ( (gpspos_parts[5] == '2') or (gpspos_parts[5] == '3') ): #2D or 3D fix
        debug.msg('GPS fix "' + gpspos_parts[5] + '" ie valid');
        status = TRUE
    else:
        debug.msg('GPS fix "' + gpspos_parts[5] + '" ie not valid');
        status = FALSE
    return status
 
#Check GSM Reception
def reception_check():
    MDM.send('AT+CSQ\r', 0)
    res = MDM.receive(10)#5sec
    rec_parts = res.split('\r\n')
    rec_parts2 = rec_parts[1].split(':')
    debug.msg('Reception: ' + rec_parts2[1])
    return rec_parts2[1]
 
#SMS Send
def sms_send(to, text):
    global status1
    debug.msg('Send SMS to: %s, MSG: %s' % (to, text))
 
    MDM.send('AT+CMGS="' + to + '"\r', 0)
    res = MDM.receive(50)#5 sec
    MOD.sleep(1)#wait 0.1sec
 
    #Check for SMS prompt
    if (res == '\r\n> '):
 
        #Send SMS message text
        MDM.send(text, 0)
 
        #End SMS
        MDM.sendbyte(0x1A, 0)
        res2 = MDM.receive(180)#5 sec
        MOD.sleep(1)#wait 0.1sec
 
        if (res2.find('\r\nOK\r\n') != -1):
            debug.msg('SMS sent')
            status1 = TRUE
        else:
            debug.msg('SMS Send: ' + res2)
            debug.msg('Did not get SMS sent confirmation')
            status1 = FALSE
    else:
        debug.msg('SMS Send: ' + res)
        debug.msg('Did not receive SMS prompt')
 
        #Abort SMS (just in case)
        MDM.sendbyte(0x1B, 0)
        MOD.sleep(1)#wait 0.1sec
        status1 = FALSE
 
    return status1
 
#Setup SMS
def sms_setup():
    MDM.send('AT+CMGF=1\r', 0)
    res = MDM.receive(50)#5 sec
    MOD.sleep(1)#wait 0.1sec
 
#Toggle a GPIO to take a picture
def camera():
        GPIO.setIOvalue(6, 1)
        MOD.sleep(10)
        GPIO.setIOvalue(6, 0)
 
#Toggle a GPIO to take activate cutdown device
def cutdown():
        debug.msg('CUTDOWN')
        GPIO.setIOvalue(2, 1)
        MOD.sleep(30)
        GPIO.setIOvalue(2, 0)
 
def log(data):
        file = open('data.txt','a')
        file.write(data)
        file.close()
 
def main():
    debug.msg('Running...')
    gpspos_lastvalid = ''
    reset_count = 0
    north_cut = 5240 #if it goes above this cut
    south_cut = 5150 #if it goes below this cut
    long_cut = 55 #if it goes above this cut 
    sms_setup()
    while 1:
        #Retrieve current position
        gpspos = getActualPosition()
        debug.msg('Position: %s' % gpspos)
        gpspos_split = gpspos.split(',')
 
        #Retrieve GPS fix status
        gps_statusnow = gps_status(gpspos)
 
        #Check if updated if TRUE record to last valid
        if ( (gps_statusnow == TRUE) or (gpspos_lastvalid == '') ):
            gpspos_lastvalid = gpspos
 
        #Check whether we are still within cutdown square
        if (gps_statusnow == TRUE):
            altitude = gpspos_split[4].split('.')
            int_altitude = int(altitude[0])
            debug.msg('Alt : %d' % int_altitude)
            if (int_altitude >23000):
                #log('Too High')
                cutdown()
            latitude = gpspos_split[1].split('.')
            longitude = gpspos_split[2].split('.')
            int_latitude = int(latitude[0])
            int_longitude = int(longitude[0])
            debug.msg('%d' % int_latitude)
            debug.msg('%d' % int_longitude)
            if (int_longitude > long_cut):
                #log('Too E/W')
                cutdown()
            elif (int_latitude > north_cut):
                #log('Too N')
                cutdown()
            elif (int_latitude < south_cut):
                #log('Too S')
                cutdown()
            else:
                debug.msg('Still inside flight zone')
 
        reception = reception_check()
 
        #If GPS position fix valid send sms of position if not then senf 'NF' and the last valid
        #if (gps_statusnow == TRUE):
            #sms_send('07748628528', gpspos)
        #else:
            #sms_send('07748628528', 'NF ' + gpspos_lastvalid)
 
        #Morse Code
        if (gps_statusnow == TRUE):
            morselib.radio_on_off('on')
            morselib.callsign()
            morselib.splitter(gpspos_split[1])
            MOD.sleep(10)
            morselib.splitter(gpspos_split[2])
            MOD.sleep(10)
            morselib.splitter(gpspos_split[4])
            morselib.radio_on_off('off')
        else:
            morselib.radio_on_off('on')
            morselib.callsign()
            morselib.radio_on_off('off')            
 
        #Check whether GPS needs to be reset
        if (gps_statusnow == FALSE):
            if (reset_count > 20):
                debug.msg('Reseting the GPS Module')
                reset_gps()
                reset_count = 0
            else:
                reset_count = reset_count + 1
        if (gps_statusnow == TRUE):
            reset_count = 0
 
        #Take Picture with Camera
        debug.msg('Take Picture')
        camera()
 
        #Log Position
        logdata = gpspos + ',' + reception
        #log(logdata)
 
        debug.msg('Power Saving')
        MOD.powerSaving(15)
 
missions/firefly/main7.py.txt · Last modified: 2008/02/07 11:16 by jamescoxon
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki