root/branch/pelloux/sp4a12/main.c @ 892
5 | jalaffon | #include <stdio.h>
|
|
#include <stdlib.h>
|
|||
14 | nipelloux | #include <strings.h>
|
|
#include <math.h>
|
|||
5 | jalaffon | #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};
|
|||
279 | nipelloux | typedef struct
|
|
{
|
|||
float latitude;
|
|||
float longitude;
|
|||
} Position;
|
|||
typedef struct
|
|||
{
|
|||
Position rpos;
|
|||
float vitmax;
|
|||
}Zone;
|
|||
//Variables globales
|
|||
int nb_Zones = 4;
|
|||
float a =10;
|
|||
float b = (float) 1;
|
|||
float c = (float) 1/6;
|
|||
float d = (float) 1/60;
|
|||
float e = (float) 1/600;
|
|||
float f = (float) 1/6000;
|
|||
float g = (float) 1/60000;
|
|||
float h = (float) 1/600000;
|
|||
215 | nipelloux | int trame_cmp(char *trame, char *type)
|
|
{
|
|||
int i=0,res=1;
|
|||
do
|
|||
{
|
|||
if (trame[i+1]==type[i])
|
|||
{
|
|||
i++;
|
|||
14 | nipelloux | }
|
|
215 | nipelloux | else{res=0;}
|
|
}while ((type[i]!='\0')&&(res==1));
|
|||
return (res);
|
|||
5 | jalaffon | }
|
|
14 | nipelloux | ||
215 | nipelloux | int decode_int(char c)
|
|
{
|
|||
int res;
|
|||
res = c-48;
|
|||
if ((res>9)||(res<0))
|
|||
{
|
|||
279 | nipelloux | return -1;
|
|
}else
|
|||
{
|
|||
return (res);
|
|||
215 | nipelloux | }
|
|
14 | nipelloux | }
|
|
215 | nipelloux | int decode_nombre (char ch[], int x)
|
|
{
|
|||
int i,nb=0;
|
|||
for(i=0;i<x;i++)
|
|||
{
|
|||
nb=nb*10;
|
|||
nb=nb+decode_int(ch[i]);
|
|||
14 | nipelloux | }
|
|
215 | nipelloux | return nb;
|
|
14 | nipelloux | }
|
|
23 | nipelloux | float conv_latitude(char * ch){
|
|
float res=0;
|
|||
int i;
|
|||
215 | nipelloux | float val_sexa=decode_nombre(ch,4);
|
|
14 | nipelloux | ||
215 | nipelloux | for(i=5;i<9;i++){
|
|
23 | nipelloux | val_sexa+=(decode_int(ch[i]))*pow(10,-i+4);
|
|
}
|
|||
res=(int)(val_sexa/100);
|
|||
res=res+(val_sexa-res*100)/60;
|
|||
}
|
|||
float conv_longitude(char * ch){
|
|||
float res=0;
|
|||
int i;
|
|||
215 | nipelloux | float val_sexa=decode_nombre(ch,5);
|
|
23 | nipelloux | ||
for(i=6;i<10;i++){
|
|||
val_sexa+=(decode_int(ch[i]))*pow(10,-i+5);
|
|||
}
|
|||
res=(int)(val_sexa/100);
|
|||
res=res+(val_sexa-res*100)/60;
|
|||
}
|
|||
float conversion(char * ch){
|
|||
int i=0;
|
|||
float res=0;
|
|||
while (ch[i] != '\0'){
|
|||
i++;
|
|||
}
|
|||
if (i==9){
|
|||
res=conv_latitude(ch);
|
|||
}
|
|||
else if (i==10){
|
|||
res=conv_longitude(ch);
|
|||
}
|
|||
else{res=1000.0;}
|
|||
return res;
|
|||
}
|
|||
5 | jalaffon | //Fonction ? modifier !!!!!
|
|
void traitement(char * trame)
|
|||
14 | nipelloux | {
|
|
215 | nipelloux | static int cpt=0 ;
|
|
cpt++;
|
|||
279 | nipelloux | Position pos;
|
|
if (decode_trame(trame,&pos)==1)
|
|||
215 | nipelloux | {
|
|
279 | nipelloux | printf ("> %s\n lattitude:%f; Longitude:%f\n",trame,pos.latitude,pos.longitude);
|
|
}
|
|||
cpt++;
|
|||
5 | jalaffon | }
|
|
215 | nipelloux | ||
279 | nipelloux | int decode_trame(char *trame, Position *p)
|
|
215 | nipelloux | {
|
|
279 | nipelloux | if (trame_cmp(trame, "GPGGA"))
|
|
{
|
|||
int nb_Virgule = 0;
|
|||
int i =0;
|
|||
int k = 0;
|
|||
int j =0;
|
|||
char Lat[10];
|
|||
char Long[12];
|
|||
int debut_Lat;
|
|||
int debut_Long;
|
|||
215 | nipelloux | ||
279 | nipelloux | while (trame[i] !='\0')
|
|
{
|
|||
if (nb_Virgule ==2 && i!= 26)
|
|||
{
|
|||
Lat[k] = trame[i];
|
|||
k++;
|
|||
}
|
|||
if (nb_Virgule ==4 && i!= 39)
|
|||
{
|
|||
Long[j] = trame[i];
|
|||
j++;
|
|||
}
|
|||
if (trame[i] == ',')
|
|||
{
|
|||
nb_Virgule += 1;
|
|||
}
|
|||
i++;
|
|||
}
|
|||
p->latitude = conversion(Lat);
|
|||
p->longitude = conversion(Long);
|
|||
215 | nipelloux | ||
279 | nipelloux | return 1;
|
|
}
|
|||
else
|
|||
{
|
|||
return 0;
|
|||
}
|
|||
215 | nipelloux | ||
279 | nipelloux | }
|
|
215 | nipelloux | ||
279 | nipelloux | float calcule_distance(Position p_1, Position p_2)
|
|
{
|
|||
float rpt = 0.01745329251;
|
|||
float distance;
|
|||
float delta =p_1.longitude-p_2.longitude;
|
|||
float dist = 6378.1*acos(sin(p_1.latitude*rpt)*sin(p_2.latitude*rpt)+ cos(p_1.latitude*rpt)*cos(p_2.latitude*rpt)*cos(delta*rpt));
|
|||
return dist;
|
|||
215 | nipelloux | }
|
|
279 | nipelloux | float calcule_vitesse(Position p_1, Position p_2)
|
|
{
|
|||
float vitesse;
|
|||
float dist = calcule_distance(p_1,p_2);
|
|||
vitesse = dist*3600;
|
|||
}
|
|||
int distance_a_la_plus_proche_zone(Position p, Zone r[], int nb_zones, float *d)
|
|||
{
|
|||
if (nb_zones ==0)
|
|||
{
|
|||
return -1;
|
|||
}
|
|||
else
|
|||
{
|
|||
int i;
|
|||
int curseur =0;
|
|||
float distance_min = 10000;
|
|||
float distance;
|
|||
for (i=0;i<nb_zones; i++)
|
|||
{
|
|||
distance =calcule_distance(p, r[i].rpos);
|
|||
if (distance <= distance_min)
|
|||
{
|
|||
distance_min = distance;
|
|||
curseur = i;
|
|||
}
|
|||
}
|
|||
*d = distance_min;
|
|||
return curseur;
|
|||
}
|
|||
}
|
|||
5 | jalaffon | ||
//Ajouter vos tests unitaires dans cette fonction.
|
|||
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);
|
|||
14 | nipelloux | }
|
|
if (decode_int('5')!=5){
|
|||
printf ("Erreur Test unitaire decode_int.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('A')!=-1){
|
|||
printf ("Erreur Test unitaire decode_nbr.\n");
|
|||
exit(-1);
|
|||
}
|
|||
215 | nipelloux | if (decode_nombre("987654321",2)!=98){
|
|
14 | nipelloux | printf ("Erreur Test unitaire decode_nbr.\n");
|
|
exit(-1);
|
|||
}
|
|||
215 | nipelloux | if (decode_nombre("123",3)!=123){
|
|
14 | nipelloux | printf ("Erreur Test unitaire decode_nbr.\n");
|
|
215 | nipelloux | printf ("%d",decode_nombre("123",3));
|
|
14 | nipelloux | exit(-1);
|
|
}
|
|||
23 | nipelloux | if (fabs(conv_latitude("3723.2475")-37.387458)>=pow(10,-6)){
|
|
printf ("Erreur Test unitaire conv_latitude.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (fabs(conv_longitude("00306.6036")-3.11006)>=pow(10,-6)){
|
|||
printf ("Erreur Test unitaire conv_longitude.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (fabs(conversion("3723.2475")-37.387458)>=pow(10,-6)){
|
|||
printf ("Erreur Test unitaire conversion.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (fabs(conversion("00306.6036")-3.11006)>=pow(10,-6)){
|
|||
printf ("Erreur Test unitaire conversion.\n");
|
|||
exit(-1);
|
|||
}
|
|||
279 | nipelloux | ||
Position test;
|
|||
if (decode_trame("$GPGSV,3,3,10,22,39,053,50,28,15,320,*7E", &test) != 0){
|
|||
printf("Erreur test unitaire decode_trame.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_trame("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D", &test)!=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,,*7D", &test)!=1){
|
|||
printf("Erreur test unitaire decode_trame.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_trame("$GPRMC,141920.00,A,4545.6419,N,00306.6039,E,0.2,133.1,010206,,*38", &test) != 0){
|
|||
printf("Erreur test unitaire decode_trame.\n");
|
|||
exit(-1);
|
|||
}
|
|||
5 | jalaffon | }
|
|
// Ne pas modifier cette fonction
|
|||
int main(int argc,char ** argv)
|
|||
{
|
|||
23 | nipelloux | tests_unitaires();
|
|
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;
|
|||
}
|