Projet

Général

Profil

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};
typedef struct
{
float latitude;
float longitude;
}Position;

430 haelmaadi
typedef struct
{
Position rpos;
float vitmax;
}Zone;
422 haelmaadi
432 haelmaadi
Zone zones[]=
{
{{44.788762, -3.012},50},
{{44.788762, -3.013},70},
422 haelmaadi
432 haelmaadi
};

422 haelmaadi
419 haelmaadi
int trame_cmp(char *trame,char *type)
194 haelmaadi
{
int i;
int res=1;
for (i=0;i<5;i++)
{
if(trame[i+1]!=type[i])
{
res=0;
}
}
return res;
199 haelmaadi
}

int decode_int(char c)
{
int val;
val = c-48;
if (val>9)
{
val=-1;
}
return val;
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
}
float latitude (char *ch)
{
float degre, min1, min2, result;
degre=(float)decode_nombre(&ch[0],2);
min1=(float)decode_nombre(&ch[0+2],2);
min2=(float)decode_nombre(&ch[0+5],4);
result=degre+(min1/60)+(min2/600000);
return result;
283 haelmaadi
}
284 haelmaadi
float longitude (char *ch)
{
float degre, min1, min2, result;
degre=(float)decode_nombre(&ch[0],3);
min1=(float)decode_nombre(&ch[0+3],2);
min2=(float)decode_nombre(&ch[0+6],4);
result=degre+(min1/60)+(min2/600000);
return result;
}
419 haelmaadi
float latitude_longitude (char * ch)
{
float result;
if(ch[4]==46)
{
result=latitude(ch);
}
else
{
result=longitude(ch);
}
return result;
422 haelmaadi
}

float decode_trame(char *trame, Position *p)
{
p->latitude=latitude(trame);
p->longitude=longitude(trame);
}
430 haelmaadi
431 haelmaadi
float calcule_distance(Position p_1, Position p_2)
430 haelmaadi
{
float dist;
dist=((2*3.14*6370)/360)*sqrt(pow((p_2.latitude-p_1.latitude),2)+pow((p_2.longitude-p_1.longitude),2));
return dist;
431 haelmaadi
}

float calcule_vitesse(Position p_1, Position p_2)
{
float vit;
vit=(calcule_distance(p_1,p_2)*3600);
return vit;
}

432 haelmaadi
int distance_a_la_plus_proche_zone(Position p, Zone r[], int nb_zones, float *d)
{
int i,j;
float D0;
D0=calcule_distance(p, r[0].rpos);
if (nb_zones>0)
{
for(i=1;i>nb_zones;i++)
{
*d=calcule_distance(p,r[i].rpos);
if(*d<D0)
{
j=i;
D0=*d;
}
}
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"))
{
printf ("> %s\n",trame);
433 haelmaadi
}*/
Position P,P0;
float vit, dist;
int zone_nb, trameOK, alarme=0, Dmax=1;
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){
199 haelmaadi
if (5!=5)
200 haelmaadi
{
printf ("Erreur Test unitaire basique.\n");
exit(-1);
}
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);
}
199 haelmaadi
if (decode_int('2')!=2)
200 haelmaadi
{
printf ("Erreur Test unitaire decode_int.\n");
exit(-1);
}
if (decode_nombre("1234",2)!=12)
{
212 haelmaadi
printf ("Erreur Test unitaire decode_nombre.\n");
200 haelmaadi
exit(-1);
212 haelmaadi
}
283 haelmaadi
if (latitude("3723.2475")-37.387458>0.0001)
212 haelmaadi
{
printf ("Erreur Test unitaire latitude.\n");
exit(-1);
}
284 haelmaadi
if (longitude("00306.6043")-3.110071667>0.01)
{
printf ("Erreur Test unitaire longitude.\n");
exit(-1);
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
}
432 haelmaadi
/*Position p;
422 haelmaadi
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 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));*/
int nb_zone;
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;
}