|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <strings.h>
|
|
#include "trame.h"
|
|
#include <math.h>
|
|
|
|
typedef struct position
|
|
{
|
|
long latitude;
|
|
long longitude;
|
|
}position;
|
|
|
|
//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 decode_int(char c)
|
|
{
|
|
int a=0;
|
|
|
|
if(c>=48 && c<=57) //v?rifie que le caract?re est entre 0 et 9 compris
|
|
{
|
|
a=c-48; //donne la valeur en d?cimal du code ascii
|
|
}
|
|
else
|
|
{
|
|
a=-1; //renvoie -1 si autres caract?res que 0 ? 9
|
|
}
|
|
return a;
|
|
}
|
|
|
|
|
|
int decode_nombre(char *ch,int n)
|
|
{
|
|
int tab[n],i,retour=0,memoire=0;
|
|
|
|
for(i=0;i<n;i++)
|
|
{
|
|
tab[i]=decode_int(ch[i]);
|
|
|
|
if(tab[i]!= -1)
|
|
{
|
|
retour=(retour*10)+tab[i];
|
|
memoire++;
|
|
}
|
|
}
|
|
|
|
if((ch[memoire+2]=='S')||(ch[memoire+2]=='W'))
|
|
{
|
|
retour=retour*(-1);
|
|
}
|
|
|
|
return retour;
|
|
}
|
|
|
|
|
|
int conversion_sexa(char *latitude_longitude)
|
|
{
|
|
float reste,resultat;
|
|
int resultVF=0;
|
|
|
|
int tram=decode_nombre(latitude_longitude,12);
|
|
|
|
/* int degres=(tram/1000000); //on travailler en virgule flottante
|
|
reste=(tram-(degres*1000000));
|
|
reste=(reste/10000);
|
|
resultat=(degres+(reste/60));*/
|
|
|
|
int degres=(tram/1000000);
|
|
reste=(tram-(degres*1000000));
|
|
reste=(reste/10000);
|
|
resultVF=(degres*600000+reste*10000); //on travail en 1/10000('/?)
|
|
//On calcule un K pour passer en rad qui vaut 2.907718*10^-8
|
|
|
|
return resultVF;
|
|
}
|
|
|
|
|
|
int trame_cmp(char * trame, char * type)
|
|
{
|
|
int i=0, validation=0, j=0;
|
|
|
|
while(type[i]!='\0') //permet de connaitre la taille de la chaine
|
|
{
|
|
i++;
|
|
}
|
|
|
|
for(j=0;j<i;j++)
|
|
{
|
|
if(trame[j+1]==type[j]) //v?rifie si chaque caract?res sont ?gaux
|
|
{
|
|
validation=1;
|
|
}
|
|
else
|
|
{
|
|
validation=0;
|
|
j=i; //permet de pas recommencer une v?rif si un caract?re n'est pas retrouv?
|
|
}
|
|
}
|
|
return validation;
|
|
printf("%d\n", validation);
|
|
}
|
|
|
|
|
|
void traitement_trame(char *trame, position *p1)
|
|
{
|
|
char tab_latitude[13],tab_longitude[14];
|
|
int i,j;
|
|
|
|
for(i=0;i<11;i++)
|
|
{
|
|
tab_latitude[i]=trame[i+17];
|
|
}
|
|
p1->latitude=conversion_sexa(tab_latitude);
|
|
|
|
for(j=0;j<12;j++)
|
|
{
|
|
tab_longitude[j]=trame[j+29];
|
|
}
|
|
p1->longitude=conversion_sexa(tab_longitude);
|
|
|
|
printf ("> %s\n",trame);
|
|
}
|
|
|
|
|
|
int calcul_vitesse(position *p1, position *p2)
|
|
{
|
|
float distance, vitesse;
|
|
|
|
if((p2->latitude!=p1->latitude) || (p2->longitude!=p1->longitude))
|
|
{
|
|
distance=sqrt((p1->latitude - p2->latitude)*(p1->latitude - p2->latitude)+(p1->longitude - p2->longitude)*(p1->longitude - p2->longitude));
|
|
vitesse=distance*0.1852507138; //rayon de la terre * kradian = 0.1852507138 (vitesse en m/s)
|
|
vitesse=vitesse*3.6; //vitesse en km/h, 3.6=3600s/1000m
|
|
|
|
printf("vitesse=%f\n",vitesse);
|
|
|
|
printf("latitude=%d\n",p1->latitude);
|
|
printf("longitude=%d\n",p1->longitude);
|
|
printf("memolat=%d\n",p2->latitude);
|
|
printf("memolong=%d\n",p2->longitude);
|
|
}
|
|
p2->latitude=p1->latitude;
|
|
p2->longitude=p1->longitude;
|
|
}
|
|
|
|
|
|
//Fonction ? modifier !!!!!
|
|
void traitement(char * trame)
|
|
{
|
|
static int cpt=0;
|
|
cpt++ ;
|
|
|
|
position p1,p2;
|
|
|
|
if (trame_cmp(trame,"GPGGA")==1)
|
|
{
|
|
traitement_trame(trame,&p1);
|
|
calcul_vitesse(&p1,&p2);
|
|
}
|
|
}
|
|
|
|
|
|
//**************************************************************************************************************************************************************************
|
|
|
|
|
|
// 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;
|
|
}
|
|
|
|
|
|
//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")==0){
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
if (trame_cmp("$GPRMC suite chaine","GPGGA")==1){
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
if (trame_cmp("$GPRMC... ", "GPRMC" )==0){
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
if (trame_cmp("$APRMC...", "GPGGA")==1){
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
|
|
test_decode_int(); //test la fonction decode_int
|
|
test_decode_nombre(); //test la fonction decode_nombre
|
|
test_conversion_latitude_longitude(); //test la fonction conversion_char_latitude
|
|
}
|
|
|
|
|
|
void test_decode_int(void)
|
|
{
|
|
char i;
|
|
|
|
for(i=0;i<10;i++)
|
|
{
|
|
if (decode_int('0'+i)==i);
|
|
{
|
|
printf("GOOD pour le caractere: %hd\n",i); //test si la fonction renvoie bien la bonne valeur d?cimal
|
|
}
|
|
}
|
|
|
|
if(decode_int('A')==-1)
|
|
{
|
|
printf("GOOD pour le caractere A\n\n"); //test si la fonction renvoie -1 pour le caract?re A
|
|
}
|
|
}
|
|
|
|
|
|
void test_decode_nombre(void)
|
|
{
|
|
char *ch;
|
|
int n;
|
|
|
|
ch="246500";
|
|
n=3;
|
|
if(decode_nombre(ch,n)==246);
|
|
{
|
|
printf("decode nombre=%hd pour ch=%s et n=%d\n",decode_nombre(ch,n),ch,n);
|
|
}
|
|
|
|
ch="28600";
|
|
n=4;
|
|
if(decode_nombre(ch,n)==246);
|
|
{
|
|
printf("decode nombre=%hd pour ch=%s et n=%d\n\n",decode_nombre(ch,n),ch,n);
|
|
}
|
|
}
|
|
|
|
|
|
void test_conversion_latitude_longitude(void)
|
|
{
|
|
char *trame_latitude_longitude;
|
|
trame_latitude_longitude="35723.2475,W";
|
|
int retour;
|
|
|
|
retour=conversion_sexa(trame_latitude_longitude);
|
|
//printf("retour=%ld",retour);
|
|
}
|
|
|
|
|