You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.7KB

  1. import serial
  2. import numpy as np
  3. import os
  4. import time
  5. import bin.functions as fc
  6. import bin.data as data
  7. import bin.figures as fig
  8. #Variables principales
  9. parameters_path = os.path.abspath(os.path.dirname(__file__)) + "/parameters.txt"
  10. data_nb = 10
  11. captors_path = os.path.abspath(os.path.dirname(__file__)) + "/captors.txt"
  12. serial_path = fc.read_line_file(parameters_path)
  13. #Variables cachees
  14. flag_data = False
  15. data_buff_max = 10
  16. data_buff_nb = 0
  17. data_buff = ""
  18. pressed_key = ''
  19. flag_state = 1 # etat neutre
  20. #Programme principal
  21. #init
  22. try:
  23. arduino_ser = serial.Serial(serial_path, 9600) #communication avec Arduino
  24. except serial.serialutil.SerialException:
  25. print("SerialException : la carte Arduino n'est pas connectee ou est connectee sur au autre port (actuel : "+ serial_path+").")
  26. flag_state = -1
  27. exit()
  28. else:
  29. arduino_data = data.Arduino_data()
  30. arduino_data.add_captors_from_file(captors_path)
  31. print("PoDoCor est pret a enregistrer les donnees")
  32. while(flag_state > 0):
  33. if(flag_state == 1): #afficher le menu
  34. 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")
  35. flag_state = 4
  36. time.sleep(2) # attente de la bonne communication avec Arduino
  37. elif(flag_state == 4): # appui clavier pour les choix du menu principal
  38. flag_state = fc.press_key('XPN', [0, 2, 3], flag_state) #detection de touche pressee (demarrage procedure acquisition)
  39. elif(flag_state == 3): # afficher le ss-menu capteurs
  40. 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")
  41. flag_state = 5
  42. elif(flag_state == 5): # apui clavier pour les choix du ss-enu capteurs
  43. flag_state = fc.press_key('AETUQ', [31, 32, 33, 34, 1], flag_state) #detection de touche pressee (demarrage procedure acquisition)
  44. if(flag_state == 31):
  45. arduino_data.add_captor_manually(captors_path)
  46. flag_state = 3
  47. elif(flag_state == 32):
  48. arduino_data.del_captor(captors_path)
  49. flag_state = 3
  50. elif(flag_state == 33):
  51. arduino_data.change_captors_order(captors_path)
  52. flag_state = 3
  53. elif(flag_state == 34):
  54. arduino_data.captors_print_list(captors_path)
  55. flag_state = 3
  56. elif(flag_state == 2): # enregistrement des données
  57. titles_list = []
  58. for i in range(arduino_data.nb_captor_signals):
  59. titles_list.append(arduino_data.captors[i].captor_name)
  60. figures = fig.Figures(arduino_data.nb_captor_signals)
  61. figures.create_figure(titles_list)
  62. data_buff_nb = 0
  63. arduino_data.create_data_file()
  64. time_0 = time.time()-1
  65. while(flag_state == 2): #boucle de reception des donnees
  66. time_0 = time_0+1
  67. time_1 = time_0+0.6
  68. arduino_ser.write(bytes('1','utf-8')) #envoi de '1' a l'Arduino (acquisition donnees)
  69. time.sleep(max(time_1-time.time(), 0))
  70. arduino_ser.write(bytes('2','utf-8')) #envoi de '2' (réception donnees)
  71. time.sleep(0.05)
  72. raw_data_txt = arduino_ser.readline() #format des donnees: data1,data2,data3\n
  73. #print(str(raw_data_txt)[2:-5])
  74. if(raw_data_txt == ''): # si aucune donnée reçue
  75. print("Arduino n'envoie aucune donnee, veuillez verifier le code televerse.")
  76. flag_state = -1
  77. data_buff_nb = 0
  78. else:
  79. data_buff_nb +=1
  80. data_buff = data_buff + str(raw_data_txt)[2:-5] + "\n" #add 1 data line in buffer
  81. #procedure extraction donnees pour affichage
  82. arduino_data.extract_data_to_array(str(raw_data_txt)[2:-5])
  83. if(data_buff_nb == data_buff_max):
  84. # procedure d'ecriture fichier
  85. arduino_data.append_data_to_file(data_buff)
  86. data_buff = "" #remise a zero
  87. data_buff_nb = 0
  88. # plot
  89. figures.plot_data(arduino_data.data_array)
  90. flag_state = fc.press_key_timeout('P', [1], flag_state)
  91. time.sleep(max(time_0+1-time.time(), 0))
  92. # while end
  93. if(data_buff_nb > 0): #vidage du buffer
  94. arduino_data.append_data_to_file(data_buff)
  95. data_buff = ""
  96. data_buff_nb = 0
  97. figures.reinit()
  98. elif(flag_state == 0):
  99. break
  100. arduino_ser.close()
  101. print("Fermeture du programme.")