root/branch/MEMBRE/sp4a12/main.c @ 377
1 | jalaffon | #include <stdio.h>
|
|
#include <stdlib.h>
|
|||
377 | limembre | #include <strings.h>
|
|
#include <math.h>
|
|||
1 | 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",
|
|||
268 | limembre | 0};
|
|
247 | limembre | //Fontion trame_cmp
|
|
int trame_cmp(char * trame, char * type)
|
|||
{
|
|||
int i;
|
|||
int egalite = 1;
|
|||
1 | jalaffon | ||
247 | limembre | for (i=0;i<5;i++)
|
|
{
|
|||
if (trame[i+1]!= type[i])
|
|||
{
|
|||
egalite=0;
|
|||
}
|
|||
}
|
|||
return egalite;
|
|||
}
|
|||
268 | limembre | ||
257 | limembre | //Fonction decode_int
|
|
int decode_int(char c)
|
|||
{
|
|||
if ((c<'A') && (c>='0'))
|
|||
{
|
|||
c =c-48;
|
|||
}
|
|||
else
|
|||
{
|
|||
c=-1;
|
|||
}
|
|||
return c;
|
|||
}
|
|||
268 | limembre | ||
257 | limembre | //test decode_int
|
|
void test_decode_int(void)
|
|||
{
|
|||
if (decode_int('0')!=0){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
if (decode_int('1')!=1){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
if (decode_int('2')!=2){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
if (decode_int('3')!=3){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
if (decode_int('4')!=4){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
if (decode_int('5')!=5){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
if (decode_int('6')!=6){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
if (decode_int('7')!=7){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
if (decode_int('8')!=8){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
if (decode_int('9')!=9){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
if (decode_int('A')!=-1){
|
|||
268 | limembre | printf("Erreur test unitaire");
|
|
257 | limembre | exit(-1);
|
|
}
|
|||
}
|
|||
268 | limembre | ||
257 | limembre | //Fonction decode_nombre
|
|
int decode_nombre(char * ch, int n)
|
|||
{
|
|||
int p=0;
|
|||
int i;
|
|||
for(i=0;i<n;i++)
|
|||
{
|
|||
p= decode_int(ch[i]) + p*10;
|
|||
}
|
|||
return p;
|
|||
}
|
|||
259 | limembre | ||
//Test de decode_nombre
|
|||
int test_decode_nombre()
|
|||
{
|
|||
if (decode_nombre("123",3)!=123)
|
|||
{
|
|||
268 | limembre | printf("Erreur test nombre1");
|
|
259 | limembre | exit(-1);
|
|
}
|
|||
if (decode_nombre("987654321",2)!=98)
|
|||
{
|
|||
268 | limembre | printf("Erreur test nombre2");
|
|
259 | limembre | exit(-1);
|
|
}
|
|||
268 | limembre | }
|
|
//Fonction latitude en nombre flottant
|
|||
int latitude(char l[])
|
|||
{
|
|||
int n=0;
|
|||
int degree=0;
|
|||
int minutes=0;
|
|||
int lati=0;
|
|||
while (l[n]!='\0')
|
|||
{
|
|||
n=n+1;
|
|||
}
|
|||
if (n==9)
|
|||
{
|
|||
degree = ((l[0]-48)*10+(l[1]-48));
|
|||
356 | limembre | minutes = ((l[2]-48)*10+(l[3]-48)+(l[5]-48)*0.1+(l[6]-48)*0.01+(l[7]-48)*0.001+(l[8]-48)*0.0001);
|
|
lati = degree + minutes/60;
|
|||
268 | limembre | }
|
|
else
|
|||
{
|
|||
printf("Erreur de saisie");
|
|||
}
|
|||
return lati;
|
|||
}
|
|||
356 | limembre | ||
// Fonction longitude en nombre flotttant
|
|||
int longitude(char l[])
|
|||
{
|
|||
int n=0;
|
|||
int degree=0;
|
|||
int minutes=0;
|
|||
int longi=0;
|
|||
while (l[n]!='\0')
|
|||
{
|
|||
n=n+1;
|
|||
}
|
|||
if (n==10)
|
|||
{
|
|||
degree = ((l[0]-48)*100+(l[1]-48)*10+(l[2]-48));
|
|||
minutes = ((l[3]-48)*10+(l[4]-48)+(l[6]-48)*0.1+(l[7]-48)*0.01+(l[8]-48)*0.001+(l[9]-48)*0.0001);
|
|||
longi = degree + minutes/60;
|
|||
}
|
|||
else
|
|||
{
|
|||
printf("Erreur de saisie");
|
|||
}
|
|||
return longi;
|
|||
}
|
|||
359 | limembre | ||
//Fonction latitude/longitude nombre flottant
|
|||
float decode_lat_long (char c[]) {
|
|||
int n=0, degre=0,i=1,b;
|
|||
float min=0, secondes=0, res=0,p;
|
|||
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 {
|
|||
degre = (c[0]-48)*100+(c[1]-48)*10+(c[2]-48);
|
|||
min = (c[3]-48)*10+(c[4]-48);
|
|||
}
|
|||
n++;
|
|||
do {
|
|||
p=1;
|
|||
for (b=0;b<i;b++) {
|
|||
p=p*0.1;
|
|||
}
|
|||
secondes= secondes + (c[n]-48)*p;
|
|||
i++;
|
|||
n++;
|
|||
} while (c[n]!='\0');
|
|||
res = degre + (min+secondes)/60;
|
|||
return res;
|
|||
}
|
|||
268 | limembre | //Test de latitude
|
|
int test_latitude()
|
|||
{
|
|||
356 | limembre | if (latitude("3723.2475")-37.387458<0.000001&& 37.387458-latitude("3723.2475")<0.000001)
|
|
268 | limembre | {
|
|
printf("Erreur test latitude");
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
356 | limembre | ||
//Test de longitude
|
|||
int test_longitude()
|
|||
{
|
|||
if (longitude("00306.6036")-37.387458<0.000001 && 37.387458-latitude("00306.6036")<0.000001)
|
|||
{
|
|||
printf("Erreur test latitude");
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
359 | limembre | ||
//Test de latitude/longitude
|
|||
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");
|
|||
if (a*a > 0.000001){
|
|||
printf("erreur");
|
|||
exit(-1);
|
|||
}
|
|||
if (b*b > 0.000001){
|
|||
printf("erreur2");
|
|||
exit(-1);
|
|||
}
|
|||
if (c*c > 0.000001){
|
|||
printf("erreur2");
|
|||
exit(-1);
|
|||
}
|
|||
if (e*e > 0.000001){
|
|||
printf("erreur2");
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
268 | limembre | ||
1 | jalaffon | //Fonction ? modifier !!!!!
|
|
void traitement(char * trame)
|
|||
240 | limembre | {
|
|
static int cpt=0;
|
|||
253 | limembre | cpt++;
|
|
if (trame_cmp(trame,"GPGGA")==1)
|
|||
{
|
|||
printf ("> %s\n",trame);
|
|||
}
|
|||
213 | jalaffon | }
|
|
377 | limembre | ||
/* TP SP4A N?2 */
|
|||
typedef struct{
|
|||
float latitude;
|
|||
float longitude;
|
|||
} position;
|
|||
position p,p1,p2;
|
|||
//Fonction decode_tram v1 pas s?r
|
|||
/*int decode_trame_v1(char *trame, position p)
|
|||
{
|
|||
int i;
|
|||
if (trame_cmp(trame,"GPGGA")==1)
|
|||
{
|
|||
p.latitude=latitude(trame);
|
|||
p.longitude=longitude(trame);
|
|||
i=1;
|
|||
}
|
|||
else
|
|||
{
|
|||
i=0;
|
|||
}
|
|||
return i;
|
|||
}*/
|
|||
//Fonction decode_tram v2
|
|||
int decode_trame_v2(char *l, position *p)
|
|||
{
|
|||
float degree_latitue=0,minutes_latitude,res_latitude,degree_longitude=0,minutes_longitute=0,res_longitude=0;
|
|||
degree_longitude=decode_int(l[29])*100+decode_int(l[30])*10+decode_int(l[31]);
|
|||
minutes_longitute=(decode_int(l[32])*10+decode_int(l[33])+decode_int(l[35])*0.1+decode_int(l[36])*0.01+decode_int(l[37])*0.001+decode_int(l[38])*0.0001)/60;
|
|||
res_longitude=degree_longitude+minutes_longitute;
|
|||
degree_latitue=decode_int(l[17])*10+decode_int(l[18]);
|
|||
minutes_latitude=(decode_int(l[19])*10+decode_int(l[20])+decode_int(l[22])*0.1+decode_int(l[23])*0.01+decode_int(l[24])*0.001+decode_int(l[25])*0.0001)/60;
|
|||
res_latitude=degree_latitue+minutes_latitude;
|
|||
p ->longitude=res_longitude;
|
|||
p ->latitude=res_latitude;
|
|||
}
|
|||
//Test decode_trame
|
|||
/*void test_decode_tram (void)
|
|||
{
|
|||
decode_trame_v2("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D",&p1);
|
|||
if (p1.longitude-3.11006 < 0.000001 && 3.11006-p1.longitude< 0.000001)
|
|||
{
|
|||
printf("Erreur test longitude");
|
|||
exit(-1);
|
|||
}
|
|||
}
|
|||
*/
|
|||
//Fonction calcul_distance
|
|||
/*float clacul_distance(position *p1, position *p2)
|
|||
{
|
|||
float distance;
|
|||
distance=(2*3.14*6370/360)*(((p2.latitude-p1.latitude)*(p2.latitude-p1.latitude)+(p2.longitude-p1.longitude)*(p2.longitude-p1*longitude))**(1/2));
|
|||
return distance;
|
|||
}
|
|||
//Fonction calcul_vitesse
|
|||
float clacul_vitesse(position *p1, position *p2)
|
|||
{
|
|||
float vitesse;
|
|||
vitesse=calcul_distance(p1,p2);
|
|||
vitesse=vitesse/3600;
|
|||
return vitesse;
|
|||
}
|
|||
//Test calcul_vitesse
|
|||
float test_calcul_vitesse()
|
|||
{
|
|||
position p1,p2;
|
|||
float v;
|
|||
decode_tram_v1("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D", &p1);
|
|||
decode_tram_v1("$GPGGA,141915.00,4545.0242,N,00306.6039,E,1,05,3.4,499.5,M,,M,,*72", &p2);
|
|||
v=calcul_vitesse(p1,p2);
|
|||
}*/
|
|||
1 | jalaffon | ||
//Ajouter vos tests unitaires dans cette fonction.
|
|||
377 | limembre | void tests_unitaires(void){
|
|
position p2;
|
|||
1 | jalaffon | if (5!=5){
|
|
printf ("Erreur Test unitaire basique.\n");
|
|||
exit(-1);
|
|||
213 | jalaffon | }
|
|
247 | limembre | if (trame_cmp("$GPGGA suite chaine","GPGGA")!=1){
|
|
1 | jalaffon | printf ("Erreur Test unitaire trame_cmp.\n");
|
|
247 | limembre | 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);
|
|||
268 | limembre | }
|
|
/* test decode_int */
|
|||
if (decode_int('0')!=0){
|
|||
printf("Erreur test int0");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('1')!=1){
|
|||
printf("Erreur test int1");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('2')!=2){
|
|||
printf("Erreur test int2");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('3')!=3){
|
|||
printf("Erreur test int3");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('4')!=4){
|
|||
printf("Erreur test int4");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('5')!=5){
|
|||
printf("Erreur test int5");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('6')!=6){
|
|||
printf("Erreur test int6");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('7')!=7){
|
|||
printf("Erreur test int7");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('8')!=8){
|
|||
printf("Erreur test int8");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('9')!=9){
|
|||
printf("Erreur test int9");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('A')!=-1){
|
|||
printf("Erreur test intA");
|
|||
exit(-1);
|
|||
}
|
|||
/* decode _nombre */
|
|||
if (decode_nombre("123",3)!=123)
|
|||
{
|
|||
printf("Erreur test nombre1");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_nombre("987654321",2)!=98)
|
|||
{
|
|||
printf("Erreur test nombre2");
|
|||
exit(-1);
|
|||
}
|
|||
/* test latitude */
|
|||
if (latitude("3723.2475")-37.387458<0.000001 && 37.387458-latitude("3723.2475")<0.000001)
|
|||
{
|
|||
printf("Erreur test latitude");
|
|||
exit(-1);
|
|||
356 | limembre | }
|
|
/* test longitude */
|
|||
if (longitude("00306.6036")-37.387458<0.000001 && 37.387458-latitude("00306.6036")<0.000001)
|
|||
{
|
|||
printf("Erreur test latitude");
|
|||
exit(-1);
|
|||
359 | limembre | }
|
|
/* test latitude/longitude */
|
|||
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");
|
|||
if (a*a > 0.000001){
|
|||
printf("erreur");
|
|||
exit(-1);
|
|||
}
|
|||
if (b*b > 0.000001){
|
|||
printf("erreur2");
|
|||
exit(-1);
|
|||
}
|
|||
if (c*c > 0.000001){
|
|||
printf("erreur2");
|
|||
exit(-1);
|
|||
}
|
|||
if (e*e > 0.000001){
|
|||
printf("erreur2");
|
|||
exit(-1);
|
|||
377 | limembre | }
|
|
/*Test decode tram longitude */
|
|||
/*decode_trame_v2("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D",&p1);
|
|||
if (p1.longitude-3.11006 < 0.000001 && 3.11006-p1.longitude< 0.000001)
|
|||
{
|
|||
printf("Erreur test longitude");
|
|||
exit(-1);
|
|||
}*/
|
|||
decode_trame_v2("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D",&p2);
|
|||
/*if (p2.latitude-45.75 < 0.000001 && 45.75 -p2.latitude< 0.000001)
|
|||
{
|
|||
printf("Erreur test latitude");
|
|||
exit(-1);
|
|||
}*/
|
|||
printf("%f",p2.latitude);
|
|||
356 | limembre | }
|
|
268 | limembre | ||
1 | jalaffon | ||
// Ne pas modifier cette fonction
|
|||
int main(int argc,char ** argv)
|
|||
{
|
|||
tests_unitaires();
|
|||
// 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;
|
|||
}
|