root/branch/aseck/sp4a12/main.c @ 749
5 | jalaffon | #include <stdio.h>
|
|
#include <stdlib.h>
|
|||
#include <strings.h>
|
|||
28 | abseck1 | #include "trame.h"
|
|
579 | abseck1 | #include <math.h>
|
|
28 | abseck1 | ||
typedef struct
|
|||
{
|
|||
float latitude;
|
|||
float longitude;
|
|||
} position;
|
|||
216 | abseck1 | typedef struct
|
|
{
|
|||
position rpos;
|
|||
float vitmax;
|
|||
} Zone;
|
|||
579 | abseck1 | #define Rayon 6378.1370
|
|
5 | 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",
|
|||
216 | abseck1 | 0};
|
|
5 | jalaffon | ||
216 | abseck1 | Zone zones[] = {
|
|
{{44.7887762, -3.012}, 50}, /* Descripteur de la premi?re zone */
|
|||
{{44.7891220, -3,013}, 70},
|
|||
581 | abseck1 | {{46.7501, 3.1101}, 50}
|
|
579 | abseck1 | };
|
|
216 | abseck1 | ||
28 | abseck1 | position test_position;
|
|
position pos1;
|
|||
position pos2;
|
|||
579 | abseck1 | int trame_cmp(char * trame, char * type) // declaration de la fonction de comparaison de deux chaines
|
|
{
|
|||
if (!strncmp(trame+1,type,strlen(type)))
|
|||
return 1;
|
|||
5 | jalaffon | return 0;
|
|
579 | abseck1 | ||
int i;
|
|||
for (i=0; i<strlen(type); i++) // parcours de la longueur du type
|
|||
28 | abseck1 | {
|
|
579 | abseck1 | if (trame[i+1] != type[i]) // inegalit? entre les caracteres
|
|
return 0; // renvoie 0
|
|||
28 | abseck1 | }
|
|
579 | abseck1 | return 1; // renvoie 1
|
|
5 | jalaffon | }
|
|
28 | abseck1 | ||
579 | abseck1 | int decode_int(char c) // declaration de la fonction qui renvoie le nombre associ? au caractere
|
|
{
|
|||
c=c-'0';
|
|||
return c;
|
|||
}
|
|||
28 | abseck1 | ||
int decode_nombre(char * ch, int n)
|
|||
{
|
|||
int i;
|
|||
int somme=0;
|
|||
for (i=0;i<n;i++)
|
|||
{
|
|||
somme=(somme*10) + decode_int(ch[i]);
|
|||
}
|
|||
return somme;
|
|||
579 | abseck1 | /*https://forge.clermont-universite.fr/svn/polytech-ge-sp4abc-2021*/
|
|
}
|
|||
float conversion_longitude(char * longi)
|
|||
28 | abseck1 | {
|
|
float result_convers,val_deci;
|
|||
579 | abseck1 | int val_ent_dec,val_ent_minut;
|
|
result_convers=decode_nombre(longi,3);// recuperation de la partie entiere
|
|||
val_ent_minut=decode_nombre(&longi[3],2);
|
|||
val_ent_dec=decode_nombre(&longi[6],4);
|
|||
val_deci=(val_ent_minut+(0.0001*val_ent_dec))/60; // recuperation de la valeur decimale en degr?
|
|||
result_convers=result_convers+val_deci;
|
|||
return result_convers;
|
|||
28 | abseck1 | ||
}
|
|||
579 | abseck1 | float conversion_latitude(char * lati) // fonction qui convertie une longitude en nombre flottant
|
|
{
|
|||
// 3723.2475 equivalent a 37 degr? 23.2475 / 60 degr?
|
|||
float result_convers,val_deci;
|
|||
int val_ent_dec,val_ent_minut;
|
|||
char minute[7];
|
|||
char min[4];
|
|||
int i;
|
|||
for (i=0;i<7; i++) minute[i]=lati[2+i];
|
|||
for (i=0;i<4; i++) min[i]=lati[5+i];
|
|||
result_convers=decode_nombre(lati,2);// recuperation de la partie entiere 37
|
|||
val_ent_minut=decode_nombre(minute,2); // 23
|
|||
val_ent_dec=decode_nombre(min,4); // 2475
|
|||
val_deci=(val_ent_minut+(val_ent_dec*0.0001))*0.0166667; // recuperation de la valeur decimale en degr?
|
|||
result_convers+=val_deci;
|
|||
return result_convers;
|
|||
}
|
|||
28 | abseck1 | ||
579 | abseck1 | ||
216 | abseck1 | int decode_trame (char * trame, position * pos)
|
|
{
|
|||
int i;
|
|||
579 | abseck1 | char ch[10];
|
|
if (trame_cmp(trame,"GPGGA"))
|
|||
216 | abseck1 | {
|
|
579 | abseck1 | for (i=0; i<10; i++) ch[i]=trame[29+i];
|
|
pos->latitude= conversion_latitude(trame);
|
|||
pos->longitude=conversion_longitude(ch);
|
|||
216 | abseck1 | return 1;
|
|
}
|
|||
return 0;
|
|||
}
|
|||
579 | abseck1 | ||
float conver_radian(float x) // fonction qui convertit un nombre flottant en flottaant
|
|||
{
|
|||
return (x*M_PI)/180;
|
|||
}
|
|||
float calcul_distance(position pos1,position pos2) // fonction qui calcule la distance entre deux positions
|
|||
{
|
|||
float result= (40000/360) * sqrt(((pos2.latitude - pos1.latitude)*(pos2.latitude - pos1.latitude))+((pos2.longitude - pos1.longitude)*(pos2.longitude - pos1.longitude))) ;
|
|||
return result;
|
|||
}
|
|||
float calcul_vitesse(position pos1,position pos2) // fonction qui calcule la distance entre deux positions
|
|||
{
|
|||
float vitesse;
|
|||
vitesse=3600*(calcul_distance(pos1,pos2)*Rayon);
|
|||
return vitesse;
|
|||
}
|
|||
581 | abseck1 | int distance_a_la_plus_proche_zone(position p,Zone r[],int nb_zones,float *d) // fonction qui calcule la distance la plus proche
|
|
579 | abseck1 | {
|
|
int index=0;
|
|||
int i;
|
|||
*d=fabs(calcul_distance(p,r[0].rpos)); // initialisation du minimum
|
|||
float t;
|
|||
if (nb_zones>0)
|
|||
{
|
|||
for (i=1;i<nb_zones;i++)
|
|||
{
|
|||
t=fabs(calcul_distance(p,r[i].rpos));
|
|||
if (t<*d)
|
|||
{
|
|||
*d=t;
|
|||
index=i;
|
|||
}
|
|||
}
|
|||
return index;
|
|||
}
|
|||
return -1;
|
|||
}
|
|||
void test_distance_plus_proche(void) //
|
|||
{
|
|||
position p;
|
|||
p.latitude =45.75;
|
|||
p.longitude=3.110060;
|
|||
int num_zone;
|
|||
float d ;
|
|||
num_zone=distance_a_la_plus_proche_zone(p,zones,sizeof(zones),&d);
|
|||
if (num_zone!=2 & fabs(d-44.85)>0.5)
|
|||
{
|
|||
printf(("erreur test distance la plus proche "));
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
float fonction_generique(char* ch) // fonction qui convertit la latitudes ou la longitude en flottant
|
|||
{
|
|||
float result_convers;
|
|||
if(ch[4]=='.')
|
|||
{
|
|||
result_convers=conversion_latitude(ch);
|
|||
}
|
|||
else if(ch[5]=='.')
|
|||
{
|
|||
result_convers=conversion_longitude(ch);
|
|||
}
|
|||
return result_convers;
|
|||
}
|
|||
5 | jalaffon | ||
//Fonction ? modifier !!!!!
|
|||
void traitement(char * trame)
|
|||
28 | abseck1 | {
|
|
static int cpt=0;
|
|||
cpt++;
|
|||
if(trame_cmp(trame,"GPGGA"))
|
|||
579 | abseck1 | printf ("> %s\n",trame);
|
|
position p;
|
|||
position p_init;
|
|||
p_init.latitude=45.750000;
|
|||
p_init.longitude= 3.110060;
|
|||
float distance;
|
|||
float vitesse;
|
|||
float seuil=1000;
|
|||
int index;
|
|||
if (decode_trame(trame,&p))
|
|||
{
|
|||
printf ("> %s\n",trame);
|
|||
printf("la latitude est %f \n la longitude est %f \n ",p.latitude,p.longitude);
|
|||
vitesse=calcul_vitesse(p_init,p);
|
|||
index=distance_a_la_plus_proche_zone(p,zones,sizeof(zones),&distance);
|
|||
if ((distance<seuil) && (vitesse>zones[index].vitmax))
|
|||
{
|
|||
printf(" alarme on \n");
|
|||
printf(" zone numero %d \n ",index);
|
|||
printf("la vitesse est %f\n ",vitesse);
|
|||
printf("la distance est %f \n ",distance);
|
|||
}
|
|||
else
|
|||
printf("Alarme off \n ");
|
|||
p_init = p;
|
|||
}
|
|||
}
|
|||
5 | jalaffon | ||
579 | abseck1 | ||
//Ajouter vos tests unitaires dans cette fonction.
|
|||
void test_decode_trame(void)
|
|||
{
|
|||
if (decode_trame("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D",&test_position)!=1)
|
|||
{
|
|||
printf("Erreur Test decode trame \n");
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
void test_decode_int(void)
|
|||
{
|
|||
if (decode_int('0')!=0)
|
|||
{
|
|||
printf("Erreur test unitaire decode int\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('1')!=1)
|
|||
{
|
|||
printf("Erreur test unitaire decode int\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('2')!=2)
|
|||
{
|
|||
printf("Erreur test unitaire decode int\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('3')!=3)
|
|||
{
|
|||
printf("Erreur test unitaire decode int\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('4')!=4)
|
|||
{
|
|||
printf("Erreur test unitaire decode int\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('5')!=5)
|
|||
{
|
|||
printf("Erreur test unitaire decode int\n");
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
void test_decode_nombre(void)
|
|||
{
|
|||
if (decode_nombre("7541",1)!=7)
|
|||
{
|
|||
printf("Erreur test unitaire decode nombre\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_nombre("7541",2)!=75)
|
|||
{
|
|||
printf("Erreur test unitaire decode nombre\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_nombre("7541",3)!=754)
|
|||
{
|
|||
printf("Erreur test unitaire decode nombre\n");
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
void test_fonction_generique(void)
|
|||
{
|
|||
if (fonction_generique("3223.2475") - 37.387459 > 0.0001 )
|
|||
{
|
|||
printf("Erreur test latitude !!! \n");
|
|||
exit(-1);
|
|||
}
|
|||
if (fonction_generique("00306.6036")-3.110060 > 0.0001)
|
|||
{
|
|||
printf("Erreur test longitude !!! \n");
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
void test_latitude(void)
|
|||
{
|
|||
if (conversion_latitude("3223.2475,N") - 37.387459 > 0.0001 )
|
|||
{
|
|||
printf("Erreur test latitude !!! \n");
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
void test_longitude(void)
|
|||
{
|
|||
if (conversion_longitude("00306.6036,E")-3.110060 > 0.0001)
|
|||
{
|
|||
printf("Erreur test longitude !!! \n");
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
5 | jalaffon | void tests_unitaires(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);
|
|||
581 | abseck1 | }//https:forge.clermont-universite.fr/svn/polytech-ge-sp4abc-2021
|
|
5 | jalaffon | }
|
|
// Ne pas modifier cette fonction
|
|||
int main(int argc,char ** argv)
|
|||
{
|
|||
579 | abseck1 | tests_unitaires();
|
|
int trame_valide;
|
|||
tests_unitaires();
|
|||
test_decode_int();
|
|||
test_decode_nombre();
|
|||
test_latitude();
|
|||
test_longitude();
|
|||
test_fonction_generique();
|
|||
test_decode_trame();
|
|||
test_distance_plus_proche();
|
|||
5 | jalaffon | ||
579 | abseck1 | ||
5 | 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;
|
|||
}
|