Projet

Général

Profil

1 jalaffon
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
261 clroche
#include "trame.h"
#include <math.h>
1 jalaffon
//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",
261 clroche
0};
355 clroche
typedef struct {
float latitude;
float longitude;
} Position ;
1 jalaffon
//Fonction ? modifier !!!!!
void traitement(char * trame)
236 clroche
{
357 clroche
//tests_unitaires();
//test_decode_int();
//test_decode_nombre();
//test_decode_latitude();
//test_decode_longitude();
//test_decode_lat_long();
362 clroche
//test_decode_trame();
//test_calcul_distance();
364 clroche
//test_calcul_vitesse();
357 clroche
Position P1,P2 ;

236 clroche
static int cpt=0;
245 clroche
cpt++;

if (trame_cmp(trame,"GPGGA") == 1) {
printf ("> %s\n",trame);
357 clroche
decode_trame(trame, &P1);
362 clroche
printf("\nLatitude : %f Longitude : %f\n\n", P1.latitude, P1.longitude);
245 clroche
}

243 clroche
}

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..’
return (res);
}
249 clroche
int decode_int(char c) {
int i;
i=c-48;
if (i>9) {
return (-1);
}
else {
return(i);
}
}
261 clroche
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;
}


269 clroche
float decode_lat_long (char c[]) {
275 clroche
int n=0, degre=0,i=1,b;
float min=0, secondes=0, res=0,p;
269 clroche

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 {
275 clroche
degre = (c[0]-48)*100+(c[1]-48)*10+(c[2]-48);
min = (c[3]-48)*10+(c[4]-48);
269 clroche
}

n++;
do {
275 clroche
p=1;
for (b=0;b<i;b++) {
p=p*0.1;
}
secondes= secondes + (c[n]-48)*p;
i++;
269 clroche
n++;
} while (c[n]!='\0');

res = degre + (min+secondes)/60;

return res;

}

355 clroche
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);
}
362 clroche
float calcule_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;
}
364 clroche
float calcule_vitesse(Position p_1,Position p_2) {
float res;
res = calcule_distance(p_1,p_2) * 3600;
return res;
}
1 jalaffon
//Ajouter vos tests unitaires dans cette fonction.
249 clroche
/*void tests_unitaires_trame_cmp(void){
243 clroche
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){
355 clroche
printf ("Erreur Testtest_decode_trame unitaire trame_cmp.\n");
243 clroche
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);
}
249 clroche
}*/

void test_decode_int(void) {
if (decode_int('0')!=0) {
printf("erreur");
261 clroche
exit(-1);
249 clroche
}
261 clroche
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);
}
249 clroche
if (decode_int('5')!=5) {
printf("erreur");
261 clroche
exit(-1);
249 clroche
}
261 clroche
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);
}
249 clroche
if (decode_int('A')!=-1) {
printf("erreur");
261 clroche
exit(-1);
249 clroche
}
243 clroche
}
261 clroche

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);
}
}
262 clroche

275 clroche
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");
262 clroche
275 clroche
if (a*a > 0.000001){
265 clroche
printf("erreur");
exit(-1);
}
275 clroche
if (b*b > 0.000001){
printf("erreur2");
269 clroche
exit(-1);
}
275 clroche
if (c*c > 0.000001){
printf("erreur2");
269 clroche
exit(-1);
}
275 clroche
if (e*e > 0.000001){
printf("erreur2");
exit(-1);
}
265 clroche
}
355 clroche
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);
}


}
362 clroche

void test_calcul_distance(void) {
Position P1,P2;
float a;
decode_trame("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*79",&P1);
decode_trame("$GPGGA,141915.00,4545.0242,N,00306.6039,E,1,05,3.4,499.5,M,,M,,*75",&P2);
a = 0.044932 - calcule_distance(P1,P2);

if (a*a > 0.000001) {
printf("erreur");
exit(-1);
}
}

364 clroche
void test_calcul_vitesse(void) {
Position P1,P2;
float a;
decode_trame("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*79",&P1);
decode_trame("$GPGGA,141915.00,4545.0242,N,00306.6039,E,1,05,3.4,499.5,M,,M,,*75",&P2);
a = 161.756485 - calcule_vitesse(P1,P2);
printf("%f",a);
362 clroche
364 clroche
if (a*a > 0.000001) {
printf("erreur");
exit(-1);
}
}

1 jalaffon
// Ne pas modifier cette fonction
int main(int argc,char ** argv)
355 clroche
{
1 jalaffon
// 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;
}