|
- import serial
- import numpy as np
- import os
- import time
- import bin.functions as fc
- import bin.data as data
- import bin.figures as fig
-
- #Variables principales
-
- parameters_path = os.path.abspath(os.path.dirname(__file__)) + "/parameters.txt"
- data_nb = 10
- captors_path = os.path.abspath(os.path.dirname(__file__)) + "/captors.txt"
- serial_path = fc.read_line_file(parameters_path)
-
- #Variables cachees
-
- flag_data = False
- data_buff_max = 10
- data_buff_nb = 0
- data_buff = ""
- pressed_key = ''
- flag_state = 1 # etat neutre
-
-
- #Programme principal
-
- #init
-
- try:
- arduino_ser = serial.Serial(serial_path, 9600) #communication avec Arduino
- except serial.serialutil.SerialException:
- print("SerialException : la carte Arduino n'est pas connectee ou est connectee sur au autre port (actuel : "+ serial_path+").")
- flag_state = -1
- exit()
- else:
- arduino_data = data.Arduino_data()
- arduino_data.add_captors_from_file(captors_path)
- print("PoDoCor est pret a enregistrer les donnees")
-
- while(flag_state > 0):
- if(flag_state == 1): #afficher le menu
- print("_________________________________\n\nMAJ + P : Enregistrement des donnees\nMAJ + N : Gerer les capteurs\nMAJ + X : Arrêt du programme (ne fonctionne pas pendant l'enregistrement)\n_________________________________\n")
- flag_state = 4
- time.sleep(2) # attente de la bonne communication avec Arduino
- elif(flag_state == 4): # appui clavier pour les choix du menu principal
- flag_state = fc.press_key('XPN', [0, 2, 3], flag_state) #detection de touche pressee (demarrage procedure acquisition)
-
- elif(flag_state == 3): # afficher le ss-menu capteurs
- print(" _________________________________\n MAJ + U : Lister les capteurs\n MAJ + A : Ajouter un capteur\n MAJ + E : Supprimer un capteur\n MAJ + T : Changer l'ordre des capteurs\n MAJ + Q : Menu principal\n _________________________________\n")
- flag_state = 5
- elif(flag_state == 5): # apui clavier pour les choix du ss-enu capteurs
- flag_state = fc.press_key('AETUQ', [31, 32, 33, 34, 1], flag_state) #detection de touche pressee (demarrage procedure acquisition)
- if(flag_state == 31):
- arduino_data.add_captor_manually(captors_path)
- flag_state = 3
- elif(flag_state == 32):
- arduino_data.del_captor(captors_path)
- flag_state = 3
- elif(flag_state == 33):
- arduino_data.change_captors_order(captors_path)
- flag_state = 3
- elif(flag_state == 34):
- arduino_data.captors_print_list(captors_path)
- flag_state = 3
-
- elif(flag_state == 2): # enregistrement des données
- titles_list = []
- for i in range(arduino_data.nb_captor_signals):
- titles_list.append(arduino_data.captors[i].captor_name)
- figures = fig.Figures(arduino_data.nb_captor_signals)
- figures.create_figure(titles_list)
- data_buff_nb = 0
- arduino_data.create_data_file()
- time_0 = time.time()-1
- while(flag_state == 2): #boucle de reception des donnees
- time_0 = time_0+1
- time_1 = time_0+0.6
- arduino_ser.write(bytes('1','utf-8')) #envoi de '1' a l'Arduino (acquisition donnees)
- time.sleep(max(time_1-time.time(), 0))
- arduino_ser.write(bytes('2','utf-8')) #envoi de '2' (réception donnees)
- time.sleep(0.05)
- raw_data_txt = arduino_ser.readline() #format des donnees: data1,data2,data3\n
- #print(str(raw_data_txt)[2:-5])
- if(raw_data_txt == ''): # si aucune donnée reçue
- print("Arduino n'envoie aucune donnee, veuillez verifier le code televerse.")
- flag_state = -1
- data_buff_nb = 0
- else:
- data_buff_nb +=1
- data_buff = data_buff + str(raw_data_txt)[2:-5] + "\n" #add 1 data line in buffer
- #procedure extraction donnees pour affichage
- arduino_data.extract_data_to_array(str(raw_data_txt)[2:-5])
-
-
- if(data_buff_nb == data_buff_max):
- # procedure d'ecriture fichier
- arduino_data.append_data_to_file(data_buff)
- data_buff = "" #remise a zero
- data_buff_nb = 0
- # plot
- figures.plot_data(arduino_data.data_array)
-
- flag_state = fc.press_key_timeout('P', [1], flag_state)
- time.sleep(max(time_0+1-time.time(), 0))
-
- # while end
- if(data_buff_nb > 0): #vidage du buffer
- arduino_data.append_data_to_file(data_buff)
- data_buff = ""
- data_buff_nb = 0
- figures.reinit()
-
-
-
- elif(flag_state == 0):
- break
- arduino_ser.close()
- print("Fermeture du programme.")
|