|
#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};
|
|
|
|
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=c-'0';
|
|
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 = 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);
|
|
}
|
|
|
|
typedef struct {
|
|
float latitude;
|
|
float longitude;
|
|
} Position ;
|
|
|
|
|
|
int decode_trame (char *trame,Position *P){
|
|
char *type="GPGGA";
|
|
int i,j,compteur_virgule=0,compteur_element=0,valide,virgule=0;
|
|
char memorisation_tps[11]={'0','0','0','0','0','0','0','0','0','0','\0'};
|
|
char memorisation_latitude[10]={'0','0','0','0','0','0','0','0','0','\0'};
|
|
char memorisation_indic_latitude[2]={'0','\0'};
|
|
char memorisation_longitude[11]={'0','0','0','0','0','0','0','0','0','0','\0'};
|
|
char memorisation_indic_longitude[2]={'0','\0'};
|
|
|
|
if (trame_cmp(trame,"GPGGA")){
|
|
valide=1;
|
|
for (i=6;compteur_virgule<6;i++){
|
|
if (trame[i]==','){
|
|
compteur_element=0;
|
|
compteur_virgule++;
|
|
}
|
|
else{
|
|
switch(compteur_virgule){
|
|
case 1: memorisation_tps[compteur_element]=trame[i];
|
|
compteur_element++;
|
|
break;
|
|
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:break;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
else
|
|
valide=0;
|
|
|
|
//On d?code les degr?s entiers
|
|
P->latitude=decode_nombre(memorisation_latitude,2);
|
|
P->longitude=decode_nombre(memorisation_longitude,3);
|
|
//On d?code les d?gr?s flottants
|
|
for (i=2;i<9;i++){
|
|
P->longitude+=memorisation_longitude[i+1]*1/6*pow(10,-i+2);
|
|
P->latitude+=(memorisation_latitude[i]-'0')*1/6*pow(10,-i+2);
|
|
}
|
|
|
|
//On transforme les notations S/N/E/O
|
|
if(memorisation_indic_latitude[0]=='S')
|
|
P->latitude = -P->latitude;
|
|
|
|
if(memorisation_indic_longitude[0]=='O')
|
|
P->longitude = -P->longitude;
|
|
|
|
return valide;
|
|
}
|
|
|
|
float calcul_distance(Position p_1,Position p_2){
|
|
float r=6378.137;
|
|
return 2*r*asin(sqrt(pow(sin(pi/180*(p_1.latitude-p_2.latitude)/2),2)+cos(pi/180*p_1.latitude)*cos(pi/180*p_2.latitude)*pow(sin(pi/180*(p_1.longitude-p_2.longitude)/2),2)));
|
|
}
|
|
|
|
//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();
|
|
}
|
|
|
|
void test_decode_int(void){
|
|
if(decode_int('5')!=5){
|
|
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('3')!=3){
|
|
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);
|
|
}
|
|
}
|
|
|
|
// 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;
|
|
}
|