root/branch/DEMAGALHAES/sp4a12/main.c @ 685
1 | jalaffon | #include <stdio.h>
|
|
#include <stdlib.h>
|
|||
#include <strings.h>
|
|||
#include "trame.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};
|
|||
377 | rademagalh | typedef struct{
|
|
float latitude;
|
|||
float longitude;
|
|||
}Position;
|
|||
typedef struct {
|
|||
Position rpos;
|
|||
float vitmax;
|
|||
} Zone;
|
|||
Zone zones[] = {
|
|||
{{44.7887762, -3.012}, 50}, /* Descripteur de la premi?re zone */
|
|||
{{44.7891220, -3.013}, 70},
|
|||
};
|
|||
3 | jalaffon | ||
185 | rademagalh | int trame_cmp(char * trame, char * type)
|
|
{
|
|||
int indice=1;
|
|||
char mot[10]="";
|
|||
//Extraction du mot entre le symbole $ et la virgule
|
|||
do
|
|||
{
|
|||
mot[indice-1]=trame[indice];
|
|||
indice++;
|
|||
}
|
|||
while (trame[indice]!=',');
|
|||
int longueurMot=0;
|
|||
int longueurType=0;
|
|||
char caratereCourrant=0;
|
|||
//Obtention de la longueur du Mot extrait entre le symbole $ et la virgule
|
|||
do
|
|||
{
|
|||
caratereCourrant=mot[longueurMot];
|
|||
longueurMot++;
|
|||
}
|
|||
while (caratereCourrant != '\0');
|
|||
longueurMot--;
|
|||
caratereCourrant=0;
|
|||
////Obtention de la longueur du type
|
|||
do
|
|||
{
|
|||
caratereCourrant=type[longueurType];
|
|||
longueurType++;
|
|||
}
|
|||
while (caratereCourrant != '\0');
|
|||
longueurType--;
|
|||
if (longueurType==longueurMot) //Verification si le type est de m?me taille que le mot extrait
|
|||
{
|
|||
int verificateur=0; //varaiable de validation
|
|||
for (int i = 0; i < longueurMot; i++)
|
|||
{
|
|||
if (mot[i]==type[i])
|
|||
{
|
|||
verificateur=1; // Valide la comparaison entre deux lettres identiques
|
|||
}
|
|||
else
|
|||
{
|
|||
verificateur=0; // Si la comparaison n'est pas bonne, on arr?te la comparaison
|
|||
break;
|
|||
}
|
|||
}
|
|||
if (verificateur==1 && type=="GPGGA")
|
|||
{
|
|||
// Condition de renvoie si le type est bien GPGGA
|
|||
return 1;
|
|||
}
|
|||
else
|
|||
{
|
|||
// return 0 pour tout les test qui ne seraient pas du type GPGGA
|
|||
return 0;
|
|||
}
|
|||
}
|
|||
else
|
|||
{
|
|||
// return 0 si aucune comparaison n'est bonne
|
|||
return 0;
|
|||
}
|
|||
3 | jalaffon | }
|
|
185 | rademagalh | ||
int decode_int(char c)
|
|||
{
|
|||
int code;
|
|||
char i;
|
|||
if(c >= '0' && c <= '9')
|
|||
// Verification que le caract?re 'c' est un caract?re entre 0 et 9 dans la table ascii
|
|||
{
|
|||
for (i= '0'; i <='9'; i++)
|
|||
//Parcourir la table ascii ? la recherche du carac. c
|
|||
{
|
|||
if (i==c)
|
|||
// Identification du carac. c dans la table ascii
|
|||
{
|
|||
code=i-48; // On ram?ne le code ascii de c entre 0 et 9
|
|||
return code;
|
|||
}
|
|||
}
|
|||
}
|
|||
else
|
|||
{
|
|||
// le caract?re 'c' n'est pas entre '0' et '9' dans la table ascii donc on renvoie -1
|
|||
return -1;
|
|||
}
|
|||
}
|
|||
int decode_nombre(char * ch, int n)
|
|||
{
|
|||
int longueurMot=0;
|
|||
char caratereCourrant=0;
|
|||
//Obtention de la longueur du Mot extrait entre le symbole $ et la virgule
|
|||
do
|
|||
{
|
|||
caratereCourrant=ch[longueurMot];
|
|||
longueurMot++;
|
|||
}
|
|||
while (caratereCourrant != '\0');
|
|||
longueurMot--;
|
|||
if (longueurMot < n ||n==0){
|
|||
return 0;
|
|||
}
|
|||
else{
|
|||
int j=0,sum=0;
|
|||
char nb[n];
|
|||
for(j=0; j<n; j++)
|
|||
{
|
|||
if(ch[j] < 48 || ch[j] > 57)
|
|||
{
|
|||
return 0;
|
|||
}
|
|||
else
|
|||
{
|
|||
sum = sum*10 + (ch[j] - 48);
|
|||
}
|
|||
}
|
|||
return sum;
|
|||
}
|
|||
}
|
|||
377 | rademagalh | float CharVersDegre(char* ch)
|
|
185 | rademagalh | {
|
|
376 | rademagalh | char * nombre=ch;
|
|
float dec;
|
|||
int compte=0,n,degre;
|
|||
float res = 0, fact = 1;
|
|||
if (*ch == '-')
|
|||
{
|
|||
ch++;
|
|||
fact = -1;
|
|||
}
|
|||
for (int virguleVu = 0; *ch; ch++)
|
|||
{
|
|||
compte++;
|
|||
if (*ch == '.')
|
|||
{
|
|||
virguleVu = 1;
|
|||
n=compte-1;
|
|||
continue;
|
|||
}
|
|||
int d = *ch - '0';
|
|||
if (d >= 0 && d <= 9)
|
|||
{
|
|||
if (virguleVu) fact /= 10.0;
|
|||
res = res * 10.0 + (float)d;
|
|||
}
|
|||
if (*ch == ',')
|
|||
{
|
|||
break;
|
|||
}
|
|||
}
|
|||
dec=(res * fact);
|
|||
if (n==4)
|
|||
{
|
|||
degre=decode_nombre(nombre,2);
|
|||
dec=(dec-degre*100)/60;
|
|||
}
|
|||
else if (n==5)
|
|||
{
|
|||
degre=decode_nombre(nombre,3);
|
|||
dec=(dec-degre*100)/60;
|
|||
}
|
|||
377 | rademagalh | return (degre+dec);
|
|
185 | rademagalh | }
|
|
374 | rademagalh | Position decode_trame(char * ch)
|
|
{
|
|||
Position pos;
|
|||
int i,indice=0;
|
|||
char longitude[10]="";
|
|||
char latitude[10]="";
|
|||
int compteur_de_virgule = 0;
|
|||
if (trame_cmp(ch,"GPGGA")==0)
|
|||
{
|
|||
exit(-1);
|
|||
}
|
|||
else
|
|||
{
|
|||
do
|
|||
{
|
|||
if (ch[indice]==',')
|
|||
{
|
|||
compteur_de_virgule++;
|
|||
}
|
|||
if (compteur_de_virgule==2)
|
|||
{
|
|||
for (int i = 0; i < 9; i++)
|
|||
{
|
|||
latitude[i]=ch[indice+1];
|
|||
indice++;
|
|||
}
|
|||
}
|
|||
if (compteur_de_virgule==4)
|
|||
{
|
|||
for (int i = 0; i < 9; i++)
|
|||
{
|
|||
longitude[i]=ch[indice+1];
|
|||
indice++;
|
|||
}
|
|||
break;
|
|||
}
|
|||
indice++;
|
|||
} while (ch[indice]!='\0');
|
|||
377 | rademagalh | pos.latitude = CharVersDegre(latitude);
|
|
pos.longitude = CharVersDegre(longitude);
|
|||
374 | rademagalh | }
|
|
return pos;
|
|||
}
|
|||
377 | rademagalh | ||
float calcul_distance(Position p1,Position p2)
|
|||
{
|
|||
float R_terre=6378.137;
|
|||
float distance = R_terre*(p1.latitude-p2.latitude)*(3.14/180);
|
|||
return distance;
|
|||
}
|
|||
float calcul_vitesse(float distance)
|
|||
{
|
|||
return distance*3600;
|
|||
}
|
|||
float distance_a_la_plus_proche_zone(Position pos_voiture, Zone r[], int nb_zones)
|
|||
{
|
|||
Position t[nb_zones];
|
|||
for (int i = 0; i < nb_zones; i++)
|
|||
{
|
|||
Position pos=r[i].rpos;
|
|||
t[i]=pos;
|
|||
}
|
|||
float d_min=calcul_distance(pos_voiture,t[1]);
|
|||
int indice_rechercher=0;
|
|||
for (int i = 0; i < nb_zones; i++)
|
|||
{
|
|||
float d=calcul_distance(pos_voiture,t[i]);
|
|||
if (d<d_min)
|
|||
{
|
|||
d_min=d;
|
|||
indice_rechercher=i;
|
|||
}
|
|||
if (nb_zones==0)
|
|||
{
|
|||
return -1;
|
|||
}
|
|||
}
|
|||
return d_min;
|
|||
}
|
|||
1 | jalaffon | ||
//Fonction ? modifier !!!!!
|
|||
void traitement(char * trame)
|
|||
144 | rademagalh | {
|
|
static int cpt=0;
|
|||
cpt++;
|
|||
2 | jalaffon | printf ("> %s\n",trame);
|
|
1 | jalaffon | }
|
|
//Ajouter vos tests unitaires dans cette fonction.
|
|||
185 | rademagalh | void tests_unitaires(void){
|
|
printf("Test de la fonction trame_cmp \n");
|
|||
1 | jalaffon | if (5!=5){
|
|
printf ("Erreur Test unitaire basique.\n");
|
|||
exit(-1);
|
|||
}
|
|||
185 | rademagalh | if (trame_cmp("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D","GPGGA")!=1){
|
|
printf ("Erreur Test unitaire trame_cmp. 1\n");
|
|||
1 | jalaffon | exit(-1);
|
|
}
|
|||
185 | rademagalh | if (trame_cmp("$GPRMC,141914.00,A,4545.6424,N,00306.6036,E,0.4,99.4,010206,,*0C","GPGGA")!=0){
|
|
printf ("Erreur Test unitaire trame_cmp. 2\n");
|
|||
1 | jalaffon | exit(-1);
|
|
185 | rademagalh | }
|
|
// Teste de la sp?cificit? : modifier votre programme pour qu'il n'affiche que les trames GPGGA
|
|||
if (trame_cmp("$GPRMC,141914.00,A,4545.6424,N,00306.6036,E,0.4,99.4,010206,,*0C", "GPRMC" )!=0){
|
|||
printf ("Erreur Test unitaire trame_cmp. 3\n");
|
|||
exit(-1);
|
|||
1 | jalaffon | }
|
|
185 | rademagalh | if (trame_cmp("$APRMC,141914.00,A,4545.6424,N,00306.6036,E,0.4,99.4,010206,,*0C", "GPGGA")!=0){
|
|
printf ("Erreur Test unitaire trame_cmp. 4\n");
|
|||
1 | jalaffon | exit(-1);
|
|
185 | rademagalh | }
|
|
else
|
|||
{
|
|||
printf("Pas d'erreur fonction potentiellement valide !\n");
|
|||
1 | jalaffon | }
|
|
}
|
|||
185 | rademagalh | ||
//Fonction de tests unitaires pour decode_int
|
|||
void test_decode_int(void){
|
|||
printf("Test de la fonction decode_int \n");
|
|||
if (5!=5){
|
|||
printf ("Erreur test_decode_int basique. n?=1 \n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('5')==-1){
|
|||
printf("Erreur test_decode_int. n?=2 \n");
|
|||
}
|
|||
if (decode_int('A')!=-1){
|
|||
printf("Erreur test_decode_int. n?=3 \n");
|
|||
}
|
|||
else
|
|||
{
|
|||
printf("Pas d'erreur fonction potentiellement valide ! \n");
|
|||
}
|
|||
}
|
|||
//Fonction de tests unitaires pour decode_nombre
|
|||
void test_decode_nombre(void){
|
|||
printf("Test de la fonction decode_nombre \n");
|
|||
if (5!=5){
|
|||
printf ("Erreur test_decode_int basique. n?=1 \n");
|
|||
exit(-1);
|
|||
}
|
|||
if(decode_nombre("7541",2)!=75){
|
|||
printf("Erreur test_decode_nombre n?=2 \n");
|
|||
exit(-1);
|
|||
}
|
|||
if(decode_nombre("7541",5)!=0){
|
|||
printf("Erreur test_decode_nombre n?=2 \n");
|
|||
exit(-1);
|
|||
}
|
|||
else
|
|||
{
|
|||
printf("Pas d'erreur fonction potentiellement valide ! \n");
|
|||
}
|
|||
}
|
|||
374 | rademagalh | void test_decode_trame(){
|
|
printf("Test de la fonction test_decode_trame \n");
|
|||
if (5!=5){
|
|||
printf ("Erreur Test decode_trame basique. n?1\n");
|
|||
exit(-1);
|
|||
}
|
|||
377 | rademagalh | Position pos1=decode_trame("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D");
|
|
Position pos2=decode_trame("$GPGGA,141925.00,4545.2410,N,00306.6046,E,1,05,3.4,501.4,M,,M,,*7D");
|
|||
printf("pos.longitude : %f deg \n",pos1.longitude);
|
|||
printf("pos.latitude : %f deg \n",pos1.latitude);
|
|||
printf("pos.longitude : %f deg \n",pos2.longitude);
|
|||
printf("pos.latitude : %f deg \n",pos2.latitude);
|
|||
printf("distance entre pos1 et pos2 : %f km \n",calcul_distance(pos2,pos1));
|
|||
printf("distance a la plus proche zone : %f km \n",distance_a_la_plus_proche_zone(pos1, zones, 2));
|
|||
374 | rademagalh | }
|
|
//Ne pas modifier cette fonction
|
|||
1 | jalaffon | int main(int argc,char ** argv)
|
|
{
|
|||
185 | rademagalh | tests_unitaires();
|
|
test_decode_int();
|
|||
test_decode_nombre();
|
|||
374 | rademagalh | test_decode_trame();
|
|
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;
|
|||
}
|