|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <strings.h>
|
|
#include <math.h>
|
|
#include "trame.h"
|
|
#define pi 3.141592
|
|
|
|
//Trames de tests ? modifier si n?cessaire.
|
|
char * trames[]= {"$GPGSV,3,2,10,15,03,077,,18,04,041,42,19,85,271,,20,08,214,*7C",
|
|
"$GPGSV,3,3,10,22,39,053,50,28,15,320,*7E",
|
|
"$GPRMC,141914.00,A,4545.6424,N,00306.6036,E,0.4,99.4,010206,,*0C",
|
|
"$GPGLL,4545.6424,N,00306.6036,E,141914.00,A*0E",
|
|
"$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D",
|
|
"$GPGSA,A,3,,03,,22,14,,01,,18,,,,3.9,3.4,1.9*39",
|
|
"$GPVTG,99.4,T,,M,0.4,N,0.7,K*57",
|
|
"$GPZDA,141914.00,01,02,2006,00,00*69",
|
|
0};
|
|
|
|
//Comparaison des trames avec le type choisi
|
|
int trame_cmp(char* trame,char* type){
|
|
int n=1,i;
|
|
for (i=0;i<4;i++){
|
|
if (trame[i+1]!=type[i])
|
|
n=0;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
//renvoie la valeur d?cimale associ?e ? un caract?re donn?
|
|
int decode_int(char c){
|
|
int n;
|
|
if (('0'<=c)&&('9'>=c)){
|
|
n=c-'0';
|
|
}
|
|
else
|
|
n=-1;
|
|
return n;
|
|
}
|
|
|
|
//renvoie la valeur d?cimale des n premiers caract?res de la cha?ne ch
|
|
int decode_nombre(char * ch, int n){
|
|
|
|
int i, res=0;
|
|
for(i=0; i<n; i++)
|
|
res += (pow(10, n-i-1)) * (decode_int(ch[i]));
|
|
return res;
|
|
}
|
|
|
|
//traite les trames GPGGA
|
|
void traitement(char * trame){
|
|
static int cpt=0 ;
|
|
cpt++;
|
|
if (trame_cmp(trame,"GPGGA"))
|
|
printf ("> %s\n",trame);
|
|
}
|
|
|
|
//Structure d'une position GPS
|
|
typedef struct {
|
|
float latitude;
|
|
float longitude;
|
|
} Position ;
|
|
|
|
//Conversion de la latitude/longitude en sexagesimal vers les degr?s flottants
|
|
//On consid?re que les points ont ?t? supprim? de la chapine de caract?re (cf decode trame)
|
|
float conversion(char * ch){
|
|
int i=0,j;
|
|
float deglat,deglong;
|
|
while(ch[i]!='\0'){
|
|
i++;
|
|
}
|
|
//Cas de la latitude
|
|
if (i==8){
|
|
deglat=decode_nombre(ch,2);
|
|
for (j=2;j<i+1;j++){
|
|
deglat+=(ch[j]-'0')*(pow(10,-j+2))*0.1666666666666;
|
|
}
|
|
return deglat;
|
|
}
|
|
//Cas de la longitude
|
|
if (i==9){
|
|
deglong=decode_nombre(ch,3);
|
|
for (j=3;j<i+1;j++){
|
|
deglong+=(ch[j]-'0')*(pow(10,-j+3))*0.1666666666666;
|
|
}
|
|
return deglong;
|
|
}
|
|
else
|
|
printf("Erreur dans la trame %i \n",i);
|
|
|
|
}
|
|
|
|
//Test pour d?coder la latitude et la longitude des trames "$GPGGA" en degr? d?cimaux
|
|
int decode_trame (char *trame,Position P){
|
|
int i,compteur_virgule=0,compteur_element=0,valide,virgule=0;
|
|
char memorisation_latitude[]={'0','0','0','0','0','0','0','0','\0'};
|
|
char memorisation_indic_latitude[]={'0','\0'};
|
|
char memorisation_longitude[]={'0','0','0','0','0','0','0','0','0','\0'};
|
|
char memorisation_indic_longitude[2]={'0','\0'};
|
|
|
|
if (trame_cmp(trame,"GPGGA")==1){
|
|
valide=1;
|
|
for (i=0;compteur_virgule<6;i++){
|
|
if (trame[i]==','){
|
|
compteur_element=0;
|
|
compteur_virgule++;
|
|
}
|
|
else{
|
|
switch(compteur_virgule){
|
|
case 2: if (trame[i]!='.'){
|
|
memorisation_latitude[compteur_element]=trame[i];
|
|
compteur_element++;
|
|
}
|
|
break;
|
|
case 3: memorisation_indic_latitude[compteur_element]=trame[i];
|
|
compteur_element++;
|
|
break;
|
|
case 4: if (trame[i]!='.'){
|
|
memorisation_longitude[compteur_element]=trame[i];
|
|
compteur_element++;
|
|
}
|
|
break;
|
|
case 5: memorisation_indic_longitude[compteur_element]=trame[i];
|
|
compteur_element++;
|
|
default:compteur_element++;
|
|
break;
|
|
|
|
}
|
|
}
|
|
}
|
|
//On d?code la longitude en degr?
|
|
P.latitude=conversion(memorisation_latitude);
|
|
P.longitude=conversion(memorisation_longitude);
|
|
|
|
//On d?code les notations S/N/E/O
|
|
if(memorisation_indic_latitude[0]=='S')
|
|
P.latitude = -P.latitude;
|
|
|
|
if(memorisation_indic_longitude[0]=='W')
|
|
P.longitude = -P.longitude;
|
|
|
|
}
|
|
else
|
|
valide=0;
|
|
return valide;
|
|
}
|
|
|
|
//Calcul de la distance entre deux positions GPS
|
|
float calcul_distance(Position p1,Position p2){
|
|
float r=6378.137;
|
|
return 2*r*asin(sqrt(pow(sin(pi/180*(p1.latitude-p2.latitude)/2),2)+cos(pi/180*p1.latitude)*cos(pi/180*p2.latitude)*pow(sin(pi/180*(p1.longitude-p2.longitude)/2),2)));
|
|
}
|
|
|
|
//Calcul de la vitesse entre 2 positions Gps
|
|
float calcul_vitesse(Position p1,Position p2){
|
|
float d;
|
|
d=calcul_distance(p1,p2);
|
|
return d*3600;
|
|
}
|
|
|
|
//Structure zone dangereuse
|
|
typedef struct {
|
|
Position rpos;
|
|
float vitmax;
|
|
} Zone;
|
|
|
|
//On donne la distance de la zone dangereuse la plus proche
|
|
int distance_la_plus_proche_zone(Position P,Zone r[],int nb_zones,float *d){
|
|
int i,index;
|
|
//initialisation de la distance zones dangereuse-Position
|
|
float d_min=calcul_distance(P,r[0].rpos);
|
|
|
|
//Calcul de la distance zones dangereuse-Position pour conna?tre la zone dangereuse la plus proche
|
|
for(i=1;i<nb_zones;i++){
|
|
*d=calcul_distance(P,r[i].rpos);
|
|
if (*d<=d_min){
|
|
index=i+1;
|
|
d_min=*d;
|
|
}
|
|
}
|
|
return index,d_min;
|
|
}
|
|
|
|
//Ajouter vos tests unitaires dans cette fonction.
|
|
void tests_unitaires(void){
|
|
if (5!=5){
|
|
printf ("Erreur Test unitaire basique.\n");
|
|
exit(-1);
|
|
}
|
|
|
|
if (trame_cmp("$GPGGA suite chaine","GPGGA")!=1){
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
if (trame_cmp("$GPRMC suite chaine","GPGGA")!=0){
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
if (trame_cmp("$GPRMC... ", "GPRMC" )!=1){
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
if (trame_cmp("$APRMC...", "GPGGA")!=0){
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
test_decode_int();
|
|
test_decode_nombre();
|
|
test_decode_trame();
|
|
}
|
|
|
|
void test_decode_int(void){
|
|
if(decode_int('1')!=1){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('2')!=2){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('3')!=3){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('4')!=4){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('5')!=5){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('6')!=6){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('7')!=7){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('8')!=8){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('9')!=9){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('A')!=-1){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('0')!=0){
|
|
printf("Erreur Test decode int decode_int.\n");
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
void test_decode_nombre(void){
|
|
if(decode_nombre("6782",2)!=67){
|
|
printf("Erreur Test decode nombre decode_nombre.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_nombre("1238",3)!=123){
|
|
printf("Erreur Test decode nombre decode_nombre.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_nombre("782926",4)!=7829){
|
|
printf("Erreur Test decode nombre decode_nombre.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_nombre("357",1)!=3){
|
|
printf("Erreur Test decode nombre decode_nombre.\n");
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
void test_decode_trame(void){
|
|
Position p;
|
|
if (decode_trame("$GPGLL,4545.6424,N,00306.6036,E,141914.00,A*0E",p)!=0){
|
|
printf("Erreur Test decode trame.\n");
|
|
exit(-1);
|
|
}
|
|
if (decode_trame("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D",p)!=1){
|
|
printf("Erreur Test decode trame.\n");
|
|
exit(-1);
|
|
}
|
|
if (decode_trame("$GPZDA,141925.00,01,02,2006,00,00*6B",p)!=0){
|
|
printf("Erreur Test decode trame.\n");
|
|
exit(-1);
|
|
}
|
|
if (decode_trame("$GPGSA,A,3,,03,,22,14,,01,,18,,,,3.9,3.4,1.9*39",p)!=0){
|
|
printf("Erreur Test decode tram.\n");
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
// Ne pas modifier cette fonction
|
|
int main(int argc,char ** argv)
|
|
{
|
|
|
|
tests_unitaires();
|
|
|
|
|
|
// Affichage des trames definies dans la table trames.
|
|
printf ("Trames de tests tableau trames:\n");
|
|
int i=0;
|
|
while (trames[i])
|
|
traitement(trames[i++]);
|
|
|
|
if (!trame_init())
|
|
exit(-1);
|
|
// Affichage des trames du fichier gps.log
|
|
char *trame;
|
|
printf ("Trames de tests du fichier gps.log\n");
|
|
while ((trame = trame_suivante()))
|
|
traitement(trame);
|
|
|
|
return 0;
|
|
}
|