root/branch/ELMAADI/sp4a12/main.c
1 | jalaffon | #include <stdio.h>
|
|
#include <stdlib.h>
|
|||
#include <strings.h>
|
|||
212 | haelmaadi | #include "trame.h"
|
|
#include <math.h>
|
|||
430 | haelmaadi | ||
1 | jalaffon | ||
283 | haelmaadi | /*Trames de tests ? modifier si n?cessaire.*/
|
|
1 | jalaffon | 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",
|
|||
422 | haelmaadi | "$GPZDA,141914.00,01,02,2006,00,00*69",
|
|
0};
|
|||
434 | haelmaadi | /*D?claration des structure*/
|
|
422 | haelmaadi | typedef struct
|
|
{
|
|||
float latitude;
|
|||
float longitude;
|
|||
}Position;
|
|||
430 | haelmaadi | typedef struct
|
|
{
|
|||
Position rpos;
|
|||
float vitmax;
|
|||
}Zone;
|
|||
434 | haelmaadi | /*D?finition des zone dangereuses*/
|
|
432 | haelmaadi | Zone zones[]=
|
|
{
|
|||
{{44.788762, -3.012},50},
|
|||
{{44.788762, -3.013},70},
|
|||
422 | haelmaadi | ||
432 | haelmaadi | };
|
|
434 | haelmaadi | /*Fonction qui renvoie 1 si la trame commence par la cha?ne de caract?re type et z?ro dans les autres cas*/
|
|
419 | haelmaadi | int trame_cmp(char *trame,char *type)
|
|
194 | haelmaadi | {
|
|
int i;
|
|||
int res=1;
|
|||
for (i=0;i<5;i++)
|
|||
{
|
|||
434 | haelmaadi | if(trame[i+1]!=type[i]) /*V?rification si la trame commence par GPGGA */
|
|
194 | haelmaadi | {
|
|
res=0;
|
|||
}
|
|||
}
|
|||
return res;
|
|||
199 | haelmaadi | }
|
|
434 | haelmaadi | /*Fonction qui renvoie la valeur d?cimale associ?e ? un caract?re donn? en param?tre.*/
|
|
199 | haelmaadi | int decode_int(char c)
|
|
{
|
|||
int val;
|
|||
434 | haelmaadi | val = c-48;/* Convertir caract?re ASCII en entier*/
|
|
199 | haelmaadi | if (val>9)
|
|
{
|
|||
val=-1;
|
|||
}
|
|||
return val;
|
|||
200 | haelmaadi | }
|
|
434 | haelmaadi | /*Fonction qui renvoie la valeur d?cimale des n premiers caract?res de la cha?ne ch en utilisant decode_int()*/
|
|
200 | haelmaadi | int decode_nombre(char *ch, int n)
|
|
{
|
|||
int i;
|
|||
int S=0;
|
|||
for(i=0;i<n;i++)
|
|||
{
|
|||
S = (S*10)+ decode_int(ch[i]);
|
|||
}
|
|||
return S;
|
|||
212 | haelmaadi | }
|
|
434 | haelmaadi | ||
/*Fonction qui permet de convertir la cha?ne de caract?re contenant la latitude en nombre flottant*/
|
|||
212 | haelmaadi | float latitude (char *ch)
|
|
{
|
|||
float degre, min1, min2, result;
|
|||
434 | haelmaadi | degre=(float)decode_nombre(&ch[0],2); /* acc?der au caract?re qui nous int?resse*/
|
|
212 | haelmaadi | min1=(float)decode_nombre(&ch[0+2],2);
|
|
min2=(float)decode_nombre(&ch[0+5],4);
|
|||
434 | haelmaadi | result=degre+(min1/60)+(min2/600000);/* Additionner tout apr?s avoir tout converti en degr?*/
|
|
212 | haelmaadi | return result;
|
|
283 | haelmaadi | }
|
|
434 | haelmaadi | ||
/* Fonction qui permet de convertir la cha?ne de caract?re contenant la longitude en nombre flottant */
|
|||
284 | haelmaadi | float longitude (char *ch)
|
|
{
|
|||
float degre, min1, min2, result;
|
|||
434 | haelmaadi | degre=(float)decode_nombre(&ch[0],3);/*Idem*/
|
|
284 | haelmaadi | min1=(float)decode_nombre(&ch[0+3],2);
|
|
min2=(float)decode_nombre(&ch[0+6],4);
|
|||
434 | haelmaadi | result=degre+(min1/60)+(min2/600000);/*Idem*/
|
|
284 | haelmaadi | return result;
|
|
}
|
|||
434 | haelmaadi | ||
/* Fonction g?n?rique qui d?code indiff?remment une latitude ou une longitude*/
|
|||
419 | haelmaadi | float latitude_longitude (char * ch)
|
|
{
|
|||
float result;
|
|||
434 | haelmaadi | if(ch[4]==46)/*On determine la nature du carat?re 5 pour choisir la bonne op?ration ? faire*/
|
|
419 | haelmaadi | {
|
|
result=latitude(ch);
|
|||
}
|
|||
else
|
|||
{
|
|||
result=longitude(ch);
|
|||
}
|
|||
return result;
|
|||
422 | haelmaadi | }
|
|
434 | haelmaadi | /*Fonction qui d?termine la latitude et la lonngitude de la position*/
|
|
422 | haelmaadi | float decode_trame(char *trame, Position *p)
|
|
{
|
|||
p->latitude=latitude(trame);
|
|||
p->longitude=longitude(trame);
|
|||
}
|
|||
430 | haelmaadi | ||
434 | haelmaadi | float calcule_distance(Position p_1, Position p_2) /*Calcul de la distance entre deux positions*/
|
|
430 | haelmaadi | {
|
|
float dist;
|
|||
434 | haelmaadi | dist=((2*3.14*6370)/360)*sqrt(pow((p_2.latitude-p_1.latitude),2)+pow((p_2.longitude-p_1.longitude),2));/* 6370 est le rayon de la terre*/
|
|
430 | haelmaadi | return dist;
|
|
431 | haelmaadi | }
|
|
434 | haelmaadi | float calcule_vitesse(Position p_1, Position p_2)/*calcul de la vitesse ? l'aide de la distance entre deux positions */
|
|
431 | haelmaadi | {
|
|
float vit;
|
|||
vit=(calcule_distance(p_1,p_2)*3600);
|
|||
return vit;
|
|||
}
|
|||
434 | haelmaadi | /*Fonction qui renvoie le num?ro de la zone la plus proche du point p ainsi que sa distance.*/
|
|
432 | haelmaadi | int distance_a_la_plus_proche_zone(Position p, Zone r[], int nb_zones, float *d)
|
|
{
|
|||
int i,j;
|
|||
float D0;
|
|||
434 | haelmaadi | D0=calcule_distance(p, r[0].rpos); /*Initialisation d'une distance de r?f?rence qui est la distnace initiale*/
|
|
432 | haelmaadi | if (nb_zones>0)
|
|
{
|
|||
for(i=1;i>nb_zones;i++)
|
|||
{
|
|||
*d=calcule_distance(p,r[i].rpos);
|
|||
if(*d<D0)
|
|||
{
|
|||
434 | haelmaadi | j=i; /* changement de l'indice */
|
|
D0=*d; /* changement de la r?f?rence*/
|
|||
432 | haelmaadi | }
|
|
}
|
|||
return j;
|
|||
}
|
|||
else
|
|||
{
|
|||
return -1;
|
|||
}
|
|||
431 | haelmaadi | ||
432 | haelmaadi | ||
}
|
|||
431 | haelmaadi | ||
283 | haelmaadi | /*Fonction ? modifier !!!!!*/
|
|
1 | jalaffon | void traitement(char * trame)
|
|
191 | haelmaadi | {
|
|
433 | haelmaadi | /*static int cpt=0;
|
|
197 | haelmaadi | cpt++;
|
|
if (trame_cmp(trame,"GPGGA"))
|
|||
{
|
|||
434 | haelmaadi | printf ("> %s\n",trame); /*Afficher que les trames GPGGA*/
|
|
433 | haelmaadi | }*/
|
|
Position P,P0;
|
|||
float vit, dist;
|
|||
434 | haelmaadi | int zone_nb, trameOK, alarme=0, Dmax=1;/*Dmax:distance maximale pour d?clencher l'alarme*/
|
|
433 | haelmaadi | trameOK=1;
|
|
if (trame_cmp(trame,"GPGGA")==1)
|
|||
{
|
|||
printf("> %s\n",trame);
|
|||
if(trameOK==1)
|
|||
{
|
|||
trameOK=0;
|
|||
decode_trame(trame,&P);
|
|||
if(&P)
|
|||
{
|
|||
vit=calcule_vitesse(P,P0);
|
|||
zone_nb=distance_a_la_plus_proche_zone(P,zones,zone_nb,&dist);
|
|||
if((dist<Dmax)&(vit>zones[zone_nb].vitmax))
|
|||
{
|
|||
alarme=1;
|
|||
}
|
|||
if(alarme==1)
|
|||
{
|
|||
printf("alarme ON\n");
|
|||
}
|
|||
else
|
|||
{
|
|||
printf("alarme OFF\n");
|
|||
}
|
|||
P0=P;
|
|||
}
|
|||
printf("vitesse : %.2f distance : %.2f\n\n",vit,dist);
|
|||
}
|
|||
212 | haelmaadi | }
|
|
197 | haelmaadi | ||
185 | haelmaadi | }
|
|
1 | jalaffon | ||
283 | haelmaadi | /*Ajouter vos tests unitaires dans cette fonction.*/
|
|
185 | haelmaadi | void tests_unitaires(void){
|
|
434 | haelmaadi | ||
/*Test de v?rification du bon fonctionnement*/
|
|||
199 | haelmaadi | if (5!=5)
|
|
200 | haelmaadi | {
|
|
printf ("Erreur Test unitaire basique.\n");
|
|||
exit(-1);
|
|||
}
|
|||
434 | haelmaadi | ||
/*Tests de la fonction tram_cmp*/
|
|||
199 | haelmaadi | if (trame_cmp("$GPGGA suite chaine","GPGGA")!=1)
|
|
200 | haelmaadi | {
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|||
exit(-1);
|
|||
}
|
|||
199 | haelmaadi | if (trame_cmp("$GPRMC suite chaine","GPGGA")!=0)
|
|
200 | haelmaadi | {
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|||
exit(-1);
|
|||
}
|
|||
199 | haelmaadi | if (trame_cmp("$GPRMC... ", "GPRMC" )!=1)
|
|
200 | haelmaadi | {
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|||
exit(-1);
|
|||
}
|
|||
199 | haelmaadi | if (trame_cmp("$APRMC...", "GPGGA")!=0)
|
|
200 | haelmaadi | {
|
|
printf ("Erreur Test unitaire trame_cmp.\n");
|
|||
exit(-1);
|
|||
}
|
|||
434 | haelmaadi | ||
/*Test de la fonction decode_int*/
|
|||
199 | haelmaadi | if (decode_int('2')!=2)
|
|
200 | haelmaadi | {
|
|
printf ("Erreur Test unitaire decode_int.\n");
|
|||
exit(-1);
|
|||
}
|
|||
434 | haelmaadi | ||
/*Test de la fonction decode_nombre*/
|
|||
200 | haelmaadi | if (decode_nombre("1234",2)!=12)
|
|
{
|
|||
212 | haelmaadi | printf ("Erreur Test unitaire decode_nombre.\n");
|
|
200 | haelmaadi | exit(-1);
|
|
212 | haelmaadi | }
|
|
283 | haelmaadi | ||
434 | haelmaadi | /*Test de la fonction latitude*/
|
|
283 | haelmaadi | if (latitude("3723.2475")-37.387458>0.0001)
|
|
212 | haelmaadi | {
|
|
printf ("Erreur Test unitaire latitude.\n");
|
|||
exit(-1);
|
|||
}
|
|||
284 | haelmaadi | ||
434 | haelmaadi | /*Test de la fonction longitude */
|
|
284 | haelmaadi | if (longitude("00306.6043")-3.110071667>0.01)
|
|
{
|
|||
printf ("Erreur Test unitaire longitude.\n");
|
|||
exit(-1);
|
|||
419 | haelmaadi | }
|
|
434 | haelmaadi | /*Tests de la fonction latitude_longitude*/
|
|
419 | haelmaadi | if (latitude_longitude("00306.6043")-3.110071667>0.01)
|
|
{
|
|||
printf ("Erreur Test unitaire latitude_longitude.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (latitude_longitude("3723.2475")-37.387458>0.0001)
|
|||
{
|
|||
printf ("Erreur Test unitaire latitude_longitude.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (latitude_longitude("3452.1463")-34.869105>0.0001)
|
|||
{
|
|||
printf ("Erreur Test unitaire latitude_longitude.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (latitude_longitude("03815.1974")-38.25329>0.01)
|
|||
{
|
|||
printf ("Erreur Test unitaire latitude_longitude.\n");
|
|||
exit(-1);
|
|||
422 | haelmaadi | }
|
|
434 | haelmaadi | /*Position p;*/ /*Tests de la fonction decode_trame*/
|
|
/*if (decode_trame("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D",&p)!=1)
|
|||
422 | haelmaadi | {
|
|
printf ("Erreur Test unitaire decode_trame.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_trame("$GPGGA,141918.00,4545.0968,N,00306.6034,E,1,05,3.4,498.8,M,,M,,*7A",&p)!=1)
|
|||
{
|
|||
printf ("Erreur Test unitaire decode_trame.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_trame("$GPGGA,141925.00,4545.2410,N,00306.6046,E,1,05,3.4,501.4,M,,M,,*7C",&p)!=1)
|
|||
{
|
|||
printf ("Erreur Test unitaire decode_trame.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_trame("$GPGGA,141922.00,4545.1810,N,00306.6046,E,1,05,3.4,500.6,M,,M,,*77",&p)!=1)
|
|||
{
|
|||
printf ("Erreur Test unitaire decode_trame.\n");
|
|||
exit(-1);
|
|||
432 | haelmaadi | }*/
|
|
431 | haelmaadi | /*Test du calcul de la distance et de la vitesse pour un intervalle de 1s*/
|
|
432 | haelmaadi | /*Position p_1, p_2, p_3;
|
|
431 | haelmaadi | p_2.latitude=latitude("$GPGGA,141915.00,4545.0242,N,00306.6039,E,1,05,3.4,499.5,M,,M,,*75");
|
|
p_2.longitude=longitude("$GPGGA,141915.00,4545.0242,N,00306.6039,E,1,05,3.4,499.5,M,,M,,*75");
|
|||
p_1.latitude=latitude("$GPGGA,141917.00,4545.0726,N,00306.6039,E,1,05,3.4,499.6,M,,M,,*73");
|
|||
p_1.longitude=longitude("$GPGGA,141917.00,4545.0726,N,00306.6039,E,1,05,3.4,499.6,M,,M,,*73");
|
|||
printf("distance: %f\n",calcule_distance(p_1,p_2));
|
|||
432 | haelmaadi | printf("vitesse: %f\n",calcule_vitesse(p_1,p_2));*/
|
|
431 | haelmaadi | ||
/*Test pour calculer la distance entre deux villes*/
|
|||
/*Paris*/
|
|||
432 | haelmaadi | /*p_1.latitude = 48.8588897;
|
|
p_1.longitude = 2.320041;*/
|
|||
431 | haelmaadi | /*Lyon*/
|
|
432 | haelmaadi | /*p_2.latitude = 45.7578137;
|
|
p_2.longitude = 4.8320114;*/
|
|||
431 | haelmaadi | /*Toulouse*/
|
|
432 | haelmaadi | /*p_3.latitude = 43.7520597;
|
|
p_3.longitude = 1.2877314;
|
|||
431 | haelmaadi | printf("distance Paris-Lyon : %f\n",calcule_distance(p_1,p_2));
|
|
432 | haelmaadi | printf("distance Lyon-Toulouse : %f\n",calcule_distance(p_2,p_3));*/
|
|
434 | haelmaadi | int nb_zone; /*Test de la fonction distance_a_la_plus_proche_zone*/
|
|
432 | haelmaadi | float *d;
|
|
433 | haelmaadi | Position p,p1,p2;
|
|
432 | haelmaadi | nb_zone=distance_a_la_plus_proche_zone(p,zones,2,&d);
|
|
p1.latitude=44.7887762;
|
|||
p1.longitude=-3.012;
|
|||
p2.latitude=44.7891220;
|
|||
p2.longitude=-3.013;
|
|||
decode_trame("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D", &p);
|
|||
printf("D1 est : %f\n",calcule_distance(p, p1));
|
|||
printf("D2 est : %f\n",calcule_distance(p, p2));
|
|||
if(nb_zone!=0)
|
|||
{
|
|||
printf("Erreur dans la zone la plus proche");
|
|||
exit(-1);
|
|||
}
|
|||
431 | haelmaadi | ||
1 | jalaffon | }
|
|
283 | haelmaadi | /* Ne pas modifier cette fonction*/
|
|
1 | jalaffon | int main(int argc,char ** argv)
|
|
{
|
|||
tests_unitaires();
|
|||
283 | haelmaadi | /* Affichage des trames definies dans la table trames.*/
|
|
1 | jalaffon | printf ("Trames de tests tableau trames:\n");
|
|
194 | haelmaadi | int i=0;
|
|
1 | jalaffon | while (trames[i])
|
|
traitement(trames[i++]);
|
|||
if (!trame_init())
|
|||
exit(-1);
|
|||
283 | haelmaadi | /* Affichage des trames du fichier gps.log*/
|
|
1 | jalaffon | char *trame;
|
|
printf ("Trames de tests du fichier gps.log\n");
|
|||
while ((trame = trame_suivante()))
|
|||
traitement(trame);
|
|||
return 0;
|
|||
}
|