automatique.c 4.84 KB
/*-------------------------------------------------------------------------------
--------------------------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;
}