|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <strings.h>
|
|
#include "trame.h"
|
|
#include <math.h>
|
|
|
|
/*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};
|
|
|
|
typedef struct
|
|
{
|
|
float latitude;
|
|
float longitude;
|
|
} Position ;
|
|
|
|
typedef struct
|
|
{
|
|
Position rpos;
|
|
float vitmax;
|
|
}zone;
|
|
|
|
/*Table des zones*/
|
|
|
|
zone zones[]=
|
|
{
|
|
{{44.788762, -3.012},50},
|
|
{{44.788762, -3.013},70},
|
|
{{45.75080886, 3.11006165}, 70},
|
|
{{45.752910, 3.110067}, 80},
|
|
{{46.753910, 4.618967}, 90},
|
|
};
|
|
|
|
int nombre_zone = 5;
|
|
|
|
Position pos, pos_prec;
|
|
|
|
|
|
int trame_cmp(char * trame, char * type)
|
|
{
|
|
int i=1; /*Pour commencer apr?s le $ de la trame*/
|
|
int res=1;
|
|
do
|
|
{
|
|
if (trame[i]==type[i-1])
|
|
{
|
|
i++;
|
|
}
|
|
else
|
|
{
|
|
res=0;
|
|
}
|
|
} while ((res!=0) && (i<6)); /*on analyse le premier GP est bien GPGGA*/
|
|
return (res);
|
|
}
|
|
|
|
int decode_int(char c)
|
|
{
|
|
int i;
|
|
i=c-48;
|
|
if (i>9)
|
|
{
|
|
return (-1);
|
|
}
|
|
else
|
|
{
|
|
return(i);
|
|
}
|
|
}
|
|
|
|
int decode_nombre(char * ch, int n)
|
|
{
|
|
int i=0,res=0,b,p;
|
|
for (i=0;i<n;i++)
|
|
{
|
|
p=1;
|
|
for(b=0;b<n-i-1;b++)
|
|
{
|
|
p=p*10;
|
|
}
|
|
res = res + decode_int(ch[i])*p;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
float decode_lat_long (char c[])
|
|
{
|
|
int n=0, degre=0,i=1,b;
|
|
float min=0, secondes=0, res=0,p;
|
|
do
|
|
{
|
|
n++;
|
|
} while (c[n]!='.');
|
|
|
|
if (n==4)
|
|
{
|
|
degre = (c[0]-48)*10+(c[1]-48);
|
|
min = (c[2]-48)*10+(c[3]-48);
|
|
}
|
|
else
|
|
{
|
|
degre = (c[0]-48)*100+(c[1]-48)*10+(c[2]-48);
|
|
min = (c[3]-48)*10+(c[4]-48);
|
|
}
|
|
n++;
|
|
do
|
|
{
|
|
p=1;
|
|
for (b=0;b<i;b++)
|
|
{
|
|
p=p*0.1;
|
|
}
|
|
secondes= secondes + (c[n]-48)*p;
|
|
i++;
|
|
n++;
|
|
} while (c[n]!='\0');
|
|
res = degre + (min+secondes)/60;
|
|
return res;
|
|
}
|
|
|
|
void decode_trame(char * trame, Position * P) {
|
|
int n=17,cpt=0; /*n=17 car le codage d'une latitude commence ? l'indice 17 ds la trame*/
|
|
char lat[10],lon[10];
|
|
do
|
|
{
|
|
lat[cpt]=trame[n];
|
|
n++;
|
|
cpt++;
|
|
} while (trame[n]!=',');
|
|
cpt=0; /*cpt qui balaye le char lat/long*/
|
|
n=29; /*codage d'une longitude commence ? l'indice 29 ds la trame*/
|
|
do
|
|
{
|
|
lon[cpt]=trame[n];
|
|
n++;
|
|
cpt++;
|
|
} while (trame[n]!=',');
|
|
|
|
(*P).latitude = decode_lat_long(lat); /*ne pas mettre de chiffre cela na aps de sens*/
|
|
(*P).longitude = decode_lat_long(lon);
|
|
}
|
|
|
|
|
|
float calcul_distance(Position p_1,Position p_2) {
|
|
float x,y,res;
|
|
x=(p_2.longitude - p_1.longitude)*cos((p_2.latitude + p_1.latitude) / 2);
|
|
y=(p_2.latitude - p_1.latitude);
|
|
res = (sqrt((x*x)+(y*y)))*1.852*60;
|
|
return res;
|
|
}
|
|
|
|
|
|
float calcul_vitesse(Position p_1, Position p_2)
|
|
{
|
|
float vitesse;
|
|
vitesse = calcul_distance(p_1, p_2)*3600;
|
|
return vitesse;
|
|
}
|
|
|
|
/*Ajouter vos tests unitaires dans cette fonction.*/
|
|
void tests_unitaires_trame_cmp(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);
|
|
}
|
|
}
|
|
|
|
void test_decode_int(void)
|
|
{
|
|
if (decode_int('0')!=0)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_int('1')!=1)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_int('2')!=2)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_int('3')!=3)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_int('4')!=4)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_int('5')!=5)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_int('6')!=6)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_int('7')!=7)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_int('8')!=8)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_int('9')!=9)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_int('A')!=-1)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
|
|
void test_decode_nombre(void)
|
|
{
|
|
|
|
if (decode_nombre("7541",2)!=75)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (decode_nombre("7541",3)!=754)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
|
|
void test_decode_lat_long(void)
|
|
{
|
|
float a,b,c,e;
|
|
a= 37.387458 - decode_lat_long("3723.2475");
|
|
c= 45.760703 - decode_lat_long("4545.6422");
|
|
b=3.11006 - decode_lat_long("00306.6036");
|
|
e= 3.110077 - decode_lat_long("00306.6046");
|
|
|
|
if (a*a > 0.000001)
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
if (b*b > 0.000001)
|
|
{
|
|
printf("erreur2");
|
|
exit(-1);
|
|
}
|
|
if (c*c > 0.000001)
|
|
{
|
|
printf("erreur2");
|
|
exit(-1);
|
|
}
|
|
if (e*e > 0.000001)
|
|
{
|
|
printf("erreur2");
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
void test_decode_trame(void)
|
|
{
|
|
float a,b;
|
|
Position P1;
|
|
decode_trame("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D",&P1);
|
|
a= 3.110061 - P1.longitude;
|
|
b= 45.75 - P1.latitude;
|
|
if ((a*a > 0.000001) && (b*b > 0.000001))
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
|
|
decode_trame("$GPGGA,141917.00,4545.0726,N,00306.6039,E,1,05,3.4,499.6,M,,M,,*73",&P1);
|
|
a= 3.110066 - P1.longitude;
|
|
b= 45.751209 - P1.latitude;
|
|
if ((a*a > 0.000001) && (b*b > 0.000001))
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
|
|
decode_trame("$GPGGA,141918.00,4545.0968,N,00306.6034,E,1,05,3.4,498.8,M,,M,,*7A",&P1);
|
|
a= 3.110057 - P1.longitude;
|
|
b= 45.751614 - P1.latitude;
|
|
if ((a*a > 0.000001) && (b*b > 0.000001))
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
|
|
|
|
decode_trame("$GPGGA,141919.00,4545.1210,N,00306.6040,E,1,05,3.4,500.2,M,,M,,*77",&P1);
|
|
a= 3.110067 - P1.longitude;
|
|
b= 45.752018 - P1.latitude;
|
|
if ((a*a > 0.000001) && (b*b > 0.000001))
|
|
{
|
|
printf("erreur");
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
|
|
int distance_a_la_plus_proche_zone(Position p, zone r[], int nb_zones, float *d)
|
|
{
|
|
int i,j;
|
|
float ref;
|
|
ref = calcul_distance(p,r[0].rpos); /*creation reference sur la premiere zone du tableau pour commencer la boucle*/
|
|
for(i=0; i<nb_zones; i++)
|
|
{
|
|
*d=calcul_distance(p,r[i].rpos);
|
|
if(*d<=ref)
|
|
{
|
|
j=i; /*changement de l'indice de la zone la plus proche*/
|
|
ref=*d; /*changement de la reference en distance de la zone la plus proche*/
|
|
}
|
|
}
|
|
return j;
|
|
}
|
|
|
|
void test_distance_a_la_plus_proche_zone(void)
|
|
{
|
|
Position pos1;
|
|
pos1.latitude = 45.752911;
|
|
pos1.longitude = 3.110066;
|
|
|
|
float dist=0.0;
|
|
if(distance_a_la_plus_proche_zone(pos1,zones,nombre_zone,&dist)!=3)
|
|
{
|
|
printf ("Erreur Test unitaire calcul distance_a_la_proche_zone.\n");
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
/*Fonction ? modifier !*/
|
|
void traitement(char * trame)
|
|
{
|
|
/*tests_unitaires();
|
|
/test_decode_int();
|
|
/test_decode_nombre();
|
|
/test_decode_latitude();
|
|
/test_decode_longitude();
|
|
/test_decode_lat_long();
|
|
/test_decode_trame();*/
|
|
|
|
float v, d;
|
|
int zone_nb,trame_ok;
|
|
int alarme=0;
|
|
int seuil_d=1; /*distance maximale pour que l'alarme se d?clenche*/
|
|
static int cpt=0;
|
|
|
|
trame_ok=1;
|
|
cpt++;
|
|
if (trame_cmp(trame,"GPGGA") == 1)
|
|
{
|
|
printf ("> %s\n",trame);
|
|
if(trame_ok==1)
|
|
{
|
|
trame_ok=0;
|
|
decode_trame(trame, &pos);
|
|
if(&pos) /*v?rifier si pos existe*/
|
|
{
|
|
v=calcul_vitesse(pos,pos_prec);
|
|
zone_nb=distance_a_la_plus_proche_zone(pos,zones,nombre_zone,&d); /*d?finir la zone la plus proche de la position actuelle pos*/
|
|
if((d<seuil_d)&(v>zones[zone_nb].vitmax)) /*verifier si la voiture est proche de la zone et si la vitesse du vehicule est trop ?lev?e pour cette zone*/
|
|
{
|
|
alarme=1;
|
|
}
|
|
if(alarme==1)
|
|
{
|
|
printf("Alarme ON\n");
|
|
}
|
|
else
|
|
{
|
|
printf("Alarme OFF\n");
|
|
}
|
|
pos_prec=pos; /*la position donn?e par la trame trait?e devient la position pr?c?dente pour le calcul de vitesse et de distance de la prochaine trame*/
|
|
}
|
|
printf("vitesse : %.2f distance : %.2f\n\n",v,d);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Ne pas modifier cette fonction*/
|
|
int main(int argc,char ** argv)
|
|
{
|
|
|
|
/* 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;
|
|
}
|