Blame view

automatique.c 4.84 KB
1ce35c23   omahieux   Ajout code raspbe...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  /*-------------------------------------------------------------------------------
  --------------------------PROJET BALLON ATMOSPHERIQUE ---------------------------
  ----------------------Baptiste GRILLERE - Olivier MAHIEUX------------------------
  --------------------------------------IMA4---------------------------------------
  ---------------------------------------------------------------------------------
  --------------------------------PROGRAMME AUTOMATIQUE----------------------------
  ---------------------------------------------------------------------------------*/
  
  #include<stdlib.h>
  #include<stdio.h>
  #include<unistd.h>
  #include<fcntl.h>
  #include<termios.h>
  #include<string.h>
  #include<sys/types.h>
  #include<sys/ioctl.h>
  #include<linux/serial.h>
  
   //INITIALISATIONS
  
          //Variables pour le traitement de données GPS
          char data_gps[1024];
          char trame[69], header[64], time[64], lat[64], longi[64],alt[64], nord[64], est[64];
          char metres[64] = "metres";
          int retoursscanf = 0; //sert à déterminer si on lit une trame GPS complète
  
          //Variables pour la detection de la chute du ballon
          int alt_int_tmp = 50;
          int alt_int;
          int diff=0;
  
          //Descripteurs de fichier pour gestion des ports USB
          int portGPS;
          int portCAPTEUR;
          int portLORA;
          FILE* fp;
  
          char pression[64]; //donnée de pression en hPa
          char hp[16] = "hPa";
          //Le mode initial est "en ascension"
          int mode = 0;
  
  
  
  
  int init_serial(char*device,int speed){
          struct termios new;
          struct termios saveterm;
          int fd = open(device,O_NOCTTY|O_RDWR|O_NONBLOCK);
          if(fd<0){perror(device); exit(-1);}
          tcgetattr(fd,&saveterm); /* save current port settings */
          bzero(&new,sizeof(new));
          new.c_cflag=CLOCAL|CREAD|speed|CS8;
          new.c_iflag=0;
          new.c_oflag=0;
  new.c_lflag=0;  /* set input mode (non-canonical, no echo,...) */
          new.c_cc[VTIME]=0; /* inter-character timer unused */
          new.c_cc[VMIN]=1; /* blocking read until 1 char received */
          tcflush(fd, TCIFLUSH);
          tcsetattr(fd,TCSANOW,&new);
          return fd;
          }    
  
  
  void init_PORT(){
  
          portGPS = init_serial("/dev/ttyUSB0",B9600);
          portLORA = init_serial("/dev/ttyACM0",B9600);
          portCAPTEUR = init_serial("/dev/ttyACM1",B9600);
          fp=fdopen(portGPS,"r");
  
  }
  
  
  
  //Fonction stockant dans data_GPS les données provenant du GPS
  void lecture_GPS(){
  
   printf("lecture GPS....\n");
  
   while( (retoursscanf != 7) && ( memcmp(header,"$GPGGA",6) != 0) )
          {
                  //Acquisition trame GPS
                  fgets(data_gps,sizeof(data_gps),fp);
  
                  //traitement de la trame
                  retoursscanf = sscanf(data_gps,"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%*[^,],%*[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*s", header, time, lat, nord, longi, est, alt);
                  //printf("retour: %d\n",retoursscanf);
                  printf("header: %s\n",header);
          }
          printf("heure : %s\n", time);
          printf("header : %s\n", header);
          printf("latitude : %s\n", lat);
          printf("longitude : %s\n", longi);
          printf("altitude : %s\n", alt);
  
          alt_int = atoi(alt);
  
          //Envoi au sol des données traitées
          if(mode == 0)
                  write(portLORA,&time,sizeof(time));
          if(mode == 0 || mode == 1){
                  write(portLORA,&lat,sizeof(lat));
                  write(portLORA,&nord,sizeof(nord));
                  }
          if(mode == 0 || mode == 1){
                  write(portLORA,&longi,sizeof(longi));
                  write(portLORA,&est,sizeof(est));
                  }
          if(mode==0 || mode==1){
  	  write(portLORA,&alt,sizeof(alt));
  	  write(portLORA,&metres,sizeof(metres));
  	}
  }
  
  void lecture_capteur(){
          read(portCAPTEUR,&pression,sizeof(pression));
          printf("pression = %s\n",pression);
          write(portLORA,&pression,sizeof(pression));
          write(portLORA,&hp,sizeof(hp));
          sleep(1);
  
  }
  
  //MODE AUTOMATIQUE
  int main(void) {        
  
          init_PORT();
  
          while(1) {
                  //MODE ASCENSION
                  while(mode == 0){
                          printf("Mode ascension actif\n");
                          lecture_capteur();
                          alt_int_tmp = alt_int;
                          lecture_GPS();
                          diff = abs(alt_int - alt_int_tmp);
                          printf("diff: %d\n", diff);
                          printf("mode: %d\n",mode);
  
                          if(200 <= diff)
                                  mode = 1;
                          sleep(10);
                  }
  
                  //MODE CHUTE
                  while(mode == 1){
                          printf("Mode CHUTE\n");
                          lecture_GPS();
                          sleep(1);
                          }
  
          }
  
    return 0;
  }