Пример #1
0
def print_out_received_data(mavfile, args = False):
    loop = True;
    while not command.end_entered() and loop:
        
        try:
            msg_type = sys.argv[1]
            loop = False
        except:
            pass
        if loop == True:
            print('----------------------------')
            list_mav_msg.print_list()
            msg_type = raw_input('------------------------------------ \nEnter the MAV_MSG type (help/end/return): ')
        else:
            pass
        if '_' not in msg_type and not msg_type.isdigit():
            splitted = msg_type.split()
            name = ''
            for word in splitted:
                name += word.upper()+'_'
            msg_type = name[:-1] #removing the last _ from the string 
            print('Changed input to: ' + msg_type)
        else:
            pass
        id_not_found = False
        try:
            msg_type = int(msg_type)
            ID       = msg_type
            msg_type = list_mav_msg.list_mav_msg_working[str(msg_type)]
            print('--------------------------------\n ID '+ str(ID) + ' found: ' + msg_type + '\n-------------------------------')
        except:
            pass
        #print(msg_type)
        #print(type(msg_type))
        if msg_type == 'end':
            sys.exit()
        elif msg_type == 'help' or msg_type == 'ls':
            list_mav_msg.print_list()
        elif msg_type == 'PARAM_VALUE':
            print('-----------------------\nGenerating list of all Parameters')
            print('Bad implementation... First go to mavproxy. See how many parameters and hardcode the param_count value') 
            param_count = 403
            par_name = [0,]*param_count
            par_value = [0,]*param_count
            ls_name = [0,]*param_count
            ls_id = [0,]*param_count
            print('Reading out all param_ids')
            for i in range(0,param_count):
                #if i == 4:
                name, value, data = receive_msg(mavfile, 'PARAM_VALUE', True, param_id = i)
                par_name[i] = value[0]
                ls_name[i] = value[0]
                par_value[i] = value[1]
                ls_id[i] = str( value[4])
                print(i)
                
            par_data = zip (par_name, par_value)
            #Used to generate the Dictionary 'param_id_list'
            print('saving in params_id_list.py')
            ls = dict(zip(ls_id, ls_name))
            #print(ls)
            txt = open('param_id_list.py', 'w')
            txt.write('param_dic = {\n')
            for i in ls_id:
                txt.write('\''+ str(i)+ '\'' + ' : ' + '\''+ ls[str(i)]+ '\'' +', \n')
            txt.write('}')
            txt.close()
                
            print('saving in params.py')
            #for i in par_data:
                #print(i)
                
            txt=open('params.py', 'w')
            par_data_dic = dict(par_data)
            for i in par_name:
                s = '\'' + i + '\'' + ':' + '\'' + str(par_data_dic[i]) +'\'' + ','+ '\n'
                txt.write(s) 
            txt.close()
            

        else:
            if id_not_found:
                print('ID not found')
                break
            else:
                pass
            #plot_str =  raw_input ('------------------------------------\n Plot data? (y/n)')
            plot_str = 'n'
            if plot_str == 'y':
                plot = True
            else:
                plot = False
            if loop == True:
                log_str  = raw_input('-------------------------------------\n Log data? (y/n) ')
            else:
                log_str = 'n'
            log = False
            path = 'log_received_data.txt'
            if log_str == 'y':
                log = True
                cmt_log = raw_input('Add comment: ')
                cmt = 'Comment: ' +cmt_log + '\n---------------------------------------\n'
                path_input = raw_input('Add name for log file (Hit Enter for logging into log_received_data.txt: ')
                if path_input != '':
                    path = '../log/' + path_input + '.csv'

                else:
                    pass
            else:
                log = False
            #print("Not Receiving messages till terminated")
            #run_str = "n"
            if loop == True:
                run_str     = raw_input('--------------------------------------\n Receiving messages till terminated? (y/n): ')
            else:
                run_str  = 'n'
            if run_str == 'y':
                run         = True
                tol_value   = 5
                tol         = True
                print("no noise tolerance")
                tol_confirm = "n"
                #tol_confirm = raw_input('---------------------------------\n Tolerance (noise) in update? (y/n): ')

                if tol_confirm == 'y':
                    tol         = True
                    tol_value   = int(raw_input('----------------------------------\n Value of the tolerance: '))
                elif tol_confirm == 'n':
                    tol         = False
                else:
                    pass
            elif run_str == 'n':
                run = False
            else:
                print('Answer not accepted. Only receiving one message')
                run = False
                break
            if loop == True:
                slicing = "n"
                #slicing  = raw_input('-----------------------------------\n Slicing the list? (y/n) ')
            else:
                slicing = "n"

            if slicing == 'y':
                start = int(raw_input('Start: ')) -1
                end   = int(raw_input('End: '))
               

                name, value, data = receive_msg(mavfile, msg_type, True,  True, start, end)    
                if 'time' in name[0]:
                    #time_check = raw_input('Print time (for log purposes)? (y/n)')
                    time_check = 'y'
                    if time_check == 'y':
                        time = True
                        #Getting the data for the first time
                        name, value, data = receive_msg(mavfile, msg_type, time,  True, start, end)    
                    else:
                        time = False
                        #Getting the data for the first time
                        name, value, data = receive_msg(mavfile, msg_type, time,  True, start, end)    
                else:
                    time = False
                    pass
                
                if log == True:
                    #write_log_number_line(cmt)
                    log_data(name, path)
                    log_data(value, path)
                else:
                    pass
                for line in data:
                    print(line)
                while not command.end_entered() and run:
                    new_name, new_value, new_data = receive_msg(mavfile, msg_type,time,  True, start, end)
                    #Only print further data if it differs from the first one
                    if not matching(new_value,value, tol, tol_value):
                        if log == True:
                            log_data(new_value, path)
                        else: 
                            pass
                        print('Values changed:')
                        for line in new_data:
                            print(line)
                    value = new_value
            else:
                

                #print(msg_type)
                #print(type(msg_type))
                print('Getting ' + msg_type)
                sleep(1)
                time_start = time_now()
                name, value, data = receive_msg(mavfile, msg_type, True)    
                if 'time' in name[0]:
                    if loop == True:
                        print("Printing time for log purposes")
                        time_check = "y"
                    #time_check = raw_input('--------------------------------\n Print time (for log purposes)? (y/n)')
                    else:
                        time_check = 'n'
                    if time_check == 'y':
                        time = True
                        print('Printing time')
                        #Getting the data for the first time
                        name, value, data = receive_msg(mavfile, msg_type, time)    
                    else:
                        time = False
                        print('Not printing time')
                        #Getting the data for the first time
                        name, value, data = receive_msg(mavfile, msg_type, time)    
                else:
                    pass
                if log == True:
                    #write_log_number_line(cmt)
                    log_data(name, path)
                    log_data(value, path)
                else:
                    pass
                for line in data:
                    print(line)

                while not command.end_entered() and run:
                    new_name, new_value, new_data = receive_msg(mavfile, msg_type, time)
                    #Only print further data if it differs from the first one
                    if not matching(new_value,value, tol, tol_value):
                        if log == True:
                            log_data(new_value, path)
                        else:
                            pass
                        print('Values changed:')
                        for line in new_data:
                            print(line)
                    value= new_value
                    if time_now()-time_start > 120:
                        break
Пример #2
0
def print_out_received_data(mavfile, args = False):
    # Calibrate accelerometer if input is -ca
    try:
        if sys.argv[1] == '-ca':
            calibrate_acc(mavfile)
            sys.exit()
        else:
            pass
    except:
        pass
    # Calibrate compass if input is -cc
    try:
        if sys.argv[1] == '-cc':
            calibrate_mag(mavfile)
            sys.exit()
        else:
            pass
    except:
        pass
    # Looping in a while loop
    loop = True;
    # Duration while the received data is printed out
    time_run_duration = 60;
    while not command.end_entered() and loop:
        extern = False 
        try:
            # Extern means that '27' as input from extern was typed in
            if sys.argv[1] != '-ca' and sys.argv[1] != '-cc':
                msg_type = sys.argv[1]
                extern = True
            else:
                extern = False
                pass
        except:
            pass

        # Get a Prompt if no input from extern
        if loop == True and extern == False:
            print('----------------------------')
            list_mav_msg.print_list()
            msg_type = raw_input('------------------------------------ \nEnter the MAV_MSG type (help/end/return): ')
        else:
            pass
        if '_' not in msg_type and not msg_type.isdigit():
            splitted = msg_type.split()
            name = ''
            for word in splitted:
                name += word.upper()+'_'
            msg_type = name[:-1] #removing the last _ from the string 
            print('Changed input to: ' + msg_type)
        else:
            pass
        id_not_found = False
        try:
            msg_type = int(msg_type)
            ID       = msg_type
            msg_type = list_mav_msg.list_mav_msg_working[str(msg_type)]
            print('--------------------------------\n ID '+ str(ID) + ' found: ' + msg_type + '\n-------------------------------')
        except:
            pass
        if msg_type == 'end':
            sys.exit()
        elif msg_type == 'help' or msg_type == 'ls':
            list_mav_msg.print_list()
        elif msg_type == 'PARAM_VALUE':
            print('-----------------------\nGenerating list of all Parameters')
            print('Bad implementation... First go to mavproxy. See how many parameters and hardcode the param_count value') 
            # Hard coded amount of parameters. Each ID from 0 to param_count will be saved
            param_count = 403
            par_name = [0,]*param_count
            par_value = [0,]*param_count
            ls_name = [0,]*param_count
            ls_id = [0,]*param_count
            print('Reading out all param_ids')
            for i in range(0,param_count):
                name, value, data = receive_msg(mavfile, 'PARAM_VALUE', True, param_id = i)
                par_name[i] = value[0]
                ls_name[i] = value[0]
                par_value[i] = value[1]
                ls_id[i] = str( value[4])
                print(i)
                
            par_data = zip (par_name, par_value)
            #Used to generate the Dictionary 'param_id_list'
            print('saving in params_id_list.py')
            ls = dict(zip(ls_id, ls_name))
            txt = open('param_id_list.py', 'w')
            txt.write('param_dic = {\n')
            for i in ls_id:
                txt.write('\''+ str(i)+ '\'' + ' : ' + '\''+ ls[str(i)]+ '\'' +', \n')
            txt.write('}')
            txt.close()
                
            print('saving in params.py')
                
            txt=open('params.py', 'w')
            par_data_dic = dict(par_data)
            for i in par_name:
                s = '\'' + i + '\'' + ':' + '\'' + str(par_data_dic[i]) +'\'' + ','+ '\n'
                txt.write(s) 
            txt.close()
            

        else:
            if id_not_found:
                print('ID not found')
                break
            else:
                pass
            plot_str = 'n'
            if plot_str == 'y':
                plot = True
            else:
                plot = False
            if loop == True:
                log_str  = raw_input('-------------------------------------\n Log data? (y/n) ')
            else:
                log_str = 'n'
            log = False
            path = 'log_received_data.txt'
            if log_str == 'y':
                log = True
                time_run_duration = int(raw_input('Enter the duration of the logging: '))
                #cmt_log = raw_input('Add comment: ')
                cmt_log = ''
                cmt = 'Comment: ' +cmt_log + '\n---------------------------------------\n'
                path_input = raw_input('Add name for log file (Hit Enter for logging into log_received_data.txt: ')
                if path_input == 'gri' or path_input == 'gpi' or path_input == 'all':
                    path_input += get_pos_param(mavfile,'get')
                else:
                    pass


                if path_input != '':
                    path = '../log/' + path_input + '.csv'

                else:
                    pass
                name_counter = 1
                while os.path.isfile(path):
                    path = '../log/' + path_input +'_'+ str(name_counter) +'.csv'
            else:
                log = False
            #print("Not Receiving messages till terminated")
            #run_str = "n"
            if loop == True:
                #run_str     = raw_input('--------------------------------------\n Receiving messages till terminated? (y/n): ')
                run_str= 'y'
            else:
                run_str  = 'n'
            if run_str == 'y':
                run         = True
                tol_value   = 5
                tol         = True
                print("no noise tolerance")
                tol_confirm = "n"
                #tol_confirm = raw_input('---------------------------------\n Tolerance (noise) in update? (y/n): ')

                if tol_confirm == 'y':
                    tol         = True
                    tol_value   = int(raw_input('----------------------------------\n Value of the tolerance: '))
                elif tol_confirm == 'n':
                    tol         = False
                else:
                    pass
            elif run_str == 'n':
                run = False
            else:
                print('Answer not accepted. Only receiving one message')
                run = False
                break
            if loop == True:
                slicing = "n"
                #slicing  = raw_input('-----------------------------------\n Slicing the list? (y/n) ')
            else:
                slicing = "n"

            if slicing == 'y':
                start = int(raw_input('Start: ')) -1
                end   = int(raw_input('End: '))
               

                name, value, data = receive_msg(mavfile, msg_type, True,  True, start, end)    
                if 'time' in name[0]:
                    #time_check = raw_input('Print time (for log purposes)? (y/n)')
                    time_check = 'y'
                    if time_check == 'y':
                        time = True
                        #Getting the data for the first time
                        name, value, data = receive_msg(mavfile, msg_type, time,  True, start, end)    
                    else:
                        time = False
                        #Getting the data for the first time
                        name, value, data = receive_msg(mavfile, msg_type, time,  True, start, end)    
                else:
                    time = False
                    pass
                
                if log == True:
                    #write_log_number_line(cmt)
                    log_data(name, path)
                    log_data(value, path)
                else:
                    pass
                for line in data:
                    print(line)
                while not command.end_entered() and run:
                    new_name, new_value, new_data = receive_msg(mavfile, msg_type,time,  True, start, end)
                    #Only print further data if it differs from the first one
                    if not matching(new_value,value, tol, tol_value):
                        if log == True:
                            log_data(new_value, path)
                        else: 
                            pass
                        print('Values changed:')
                        for line in new_data:
                            print(line)
                    value = new_value
            else:
                
                if msg_type == 'ALL':
                    path_gpi = path.replace('.csv','_gpi.csv')
                    path_gri = path.replace('.csv','_gri.csv')
                    path_ri = path.replace('.csv','_ri.csv')
                    time_start = time_now()
                    gri_name, gri_value, gri_data = read_gri(mavfile)    
                    gpi_name, gpi_value, gpi_data = read_gpi(mavfile)    
                    ri_name, ri_value, ri_data = read_ri(mavfile)    

                    if log == True:
                        #write_log_number_line(cmt)
                        log_data(gpi_name, path_gpi)
                        log_data(gpi_value, path_gpi)
                        log_data(gri_name, path_gri)
                        log_data(gri_value, path_gri)
                        log_data(ri_name, path_ri)
                        log_data(ri_value, path_ri)
                    else:
                        pass

                    for line in gpi_data:
                        print(line)
                    print('---------------')
                    
                    for line in gri_data:
                        print(line)
                    print('---------------')

                    #for line in ri_data:
                        #print(line)
                    #print('---------------')

                    while not command.end_entered() and run:
                        ngri_name, ngri_value, ngri_data = read_gri(mavfile)    
                        ngpi_name, ngpi_value, ngpi_data = read_gpi(mavfile)    
                        nri_name, nri_value, nri_data = read_ri(mavfile)    

                        #if not matching(new_value,value, tol, tol_value):
                        if log == True:
                            #write_log_number_line(cmt)
                            log_data(ngpi_value, path_gpi)
                            log_data(ngri_value, path_gri)
                            log_data(nri_value, path_ri)
                        else:
                            pass

                        for line in gpi_data:
                            print(line)
                        print('---------------')
                        for line in gri_data:
                            print(line)
                        print('---------------')
                        #for line in ri_data:
                            #print(line)
                        #print('---------------')
                        gri_value = ngri_value
                        gpi_value = ngpi_value
                        ri_value = nri_value
                        if time_now()-time_start > time_run_duration:
                            break
                else:

                    #print(msg_type)
                    #print(type(msg_type))
                    print('Getting ' + msg_type)
                    time_start = time_now()
                    name, value, data = receive_msg(mavfile, msg_type, True)    
                    if 'lat' and 'lon' in name:
                        lat_idx = name.index('lat') 
                        lat_offset = value[lat_idx]
                        lon_idx = name.index('lon')
                        lon_offset = value[lon_idx]
                        offset = True
                    else:
                        offset = False
                        

                    if 'time' in name[0]:
                        if loop == True:
                            #print("Printing time for log purposes")
                            time_check = "y"
                            #time_check = raw_input('--------------------------------\n Print time (for log purposes)? (y/n)')
                        else:
                            time_check = 'n'
                        if time_check == 'y':
                            time = True
                            print('Printing time')
                            #Getting the data for the first time
                            name, value, data = receive_msg(mavfile, msg_type, time)    
                        else:
                            time = False
                            print('Not printing time')
                            #Getting the data for the first time
                            name, value, data = receive_msg(mavfile, msg_type, time)    
                    else:
                        pass
                    if log == True:
                        #write_log_number_line(cmt)
                        log_data(name, path)
                        log_data(value, path)
                    else:
                        pass
                    for line in data:
                        print(line)
                    print(type(data))
                    time_compare = time_now()
                    while not command.end_entered() and run:
                        new_name, new_value, new_data = receive_msg(mavfile, msg_type, time)
                        #Only print further data if it differs from the first one
                        if not matching(new_value,value, tol, tol_value) and time_now()-time_compare > 0.5:
                        #if not matching(new_value,value, tol, tol_value):
                            if log == True:
                                log_data(new_value, path)
                            else:
                                pass
                            if offset == True:
                                new_value[lat_idx] = new_value[lat_idx]-lat_offset
                                new_value[lon_idx] = new_value[lon_idx]-lon_offset
                                new_data = zip(new_name, new_value)
                            else:
                                pass
                            print('Values changed:')
                            for line in new_data:
                                print(line)
                            time_compare = time_now()
                        value= new_value
                        if time_now()-time_start > time_run_duration:
                            break