Révision 368
Ajouté par Damien REGENT il y a presque 3 ans
main.c | ||
---|---|---|
#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",
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <strings.h>
|
||
#include "trame.h"
|
||
#include <math.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};
|
||
typedef struct {
|
||
float latitude;
|
||
float longitude;
|
||
} Position ;
|
||
|
||
typedef struct
|
||
//Fonction ? modifier !!!!!
|
||
void traitement(char * trame)
|
||
{
|
||
float Latitude;
|
||
float Longitude;
|
||
}Position;
|
||
//tests_unitaires();
|
||
//test_decode_int();
|
||
//test_decode_nombre();
|
||
//test_decode_latitude();
|
||
//test_decode_longitude();
|
||
//test_decode_lat_long();
|
||
test_decode_trame();
|
||
|
||
int trame_cmp(char * trame, char * type)
|
||
{
|
||
int i;
|
||
int verif=1;
|
||
Position P1,P2 ;
|
||
|
||
for(i=0;i<5;i++)
|
||
{
|
||
if(trame[i+1]!=type[i])//verifie que le debut de la trame correspond au caractere type
|
||
{
|
||
verif=0;
|
||
}
|
||
static int cpt=0;
|
||
cpt++;
|
||
|
||
if (trame_cmp(trame,"GPGGA") == 1) {
|
||
printf ("> %s\n",trame);
|
||
decode_trame(trame, &P1);
|
||
printf("\nLatitude : %f Longitude : %f\n\n", P1.latitude, P1.longitude);
|
||
}
|
||
return verif;
|
||
|
||
}
|
||
|
||
int decode_int(char c)//Fonction qui permet de passer d'un caratere a un entier
|
||
{
|
||
if(c>='0' && c<='9')
|
||
int trame_cmp(char * trame, char * type) {
|
||
int i=1; //Pour commencer après le $ de la trame
|
||
int res=1;
|
||
|
||
do
|
||
{
|
||
c-=48;
|
||
if (trame[i]==type[i-1]) {
|
||
i++;
|
||
}
|
||
else {
|
||
res=0;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return -1;
|
||
while ((res!=0) && (i<6)); //on analyse le premier GP..’
|
||
return (res);
|
||
}
|
||
|
||
int decode_int(char c) {
|
||
int i;
|
||
i=c-48;
|
||
if (i>9) {
|
||
return (-1);
|
||
}
|
||
else {
|
||
return(i);
|
||
}
|
||
}
|
||
|
||
int test_decode_int()
|
||
{
|
||
if(decode_int('0')!=0)
|
||
{
|
||
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;
|
||
}
|
||
|
||
|
||
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;
|
||
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
//Ajouter vos tests unitaires dans cette fonction.
|
||
/*void tests_unitaires_trame_cmp(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 Testtest_decode_trame 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);
|
||
}
|
||
}*/
|
||
|
||
void test_decode_int(void) {
|
||
if (decode_int('0')!=0) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
if(decode_int('1')!=1)
|
||
{
|
||
if (decode_int('1')!=1) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
if(decode_int('2')!=2)
|
||
{
|
||
if (decode_int('2')!=2) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
if(decode_int('3')!=3)
|
||
{
|
||
if (decode_int('3')!=3) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
if(decode_int('4')!=4)
|
||
{
|
||
if (decode_int('4')!=4) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
if(decode_int('5')!=5)
|
||
{
|
||
if (decode_int('5')!=5) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
if(decode_int('6')!=6)
|
||
{
|
||
if (decode_int('6')!=6) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
if(decode_int('7')!=7)
|
||
{
|
||
if (decode_int('7')!=7) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
if(decode_int('8')!=8)
|
||
{
|
||
if (decode_int('8')!=8) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
if(decode_int('9')!=9)
|
||
{
|
||
if (decode_int('9')!=9) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
int decode_nombre(char *ch, int n, int i)//Fonction qui permet de passer d'une chaine de caratere a un entier
|
||
{
|
||
int temp=0;
|
||
for(i;i<n;i++)
|
||
{
|
||
temp=(temp*10)+decode_int(ch[i]);
|
||
if (decode_int('A')!=-1) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
return temp;
|
||
}
|
||
|
||
int test_decode_nombre()
|
||
{
|
||
if(decode_nombre("758961",2,0)!=75)
|
||
{
|
||
printf("Erreur Test unitaire nombre \n");
|
||
|
||
void test_decode_nombre(void) {
|
||
|
||
if (decode_nombre("7541",2)!=75) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
if(decode_nombre("758961",5,0)!=75896)
|
||
{
|
||
printf("Erreur Test unitaire nombre \n");
|
||
if (decode_nombre("7541",3)!=754) {
|
||
printf("erreur");
|
||
exit(-1);
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
float decode_latitude(char *trame )
|
||
{
|
||
float degres,reste,flottant,res;
|
||
int i=17;
|
||
degres=decode_nombre(trame,i+2,i);
|
||
reste=decode_nombre(trame,i+4,i+2);
|
||
flottant=decode_nombre(trame,i+9,i+5);
|
||
|
||
res=degres+((reste/60+(flottant/10000)));
|
||
return res;
|
||
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);
|
||
}
|
||
|
||
}
|
||
|
||
int test_decode_latitude()
|
||
{
|
||
if((-0.00001<=decode_latitude("3723.2475")-37.387458)&&(decode_latitude("3723.2475")-37.387458<=0.00001))
|
||
{
|
||
printf("Erreur Test unitaire latitude \n");
|
||
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);
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
float decode_longitude(char *trame )
|
||
{
|
||
float degres,reste,flottant,res;
|
||
int i=30;
|
||
degres=decode_nombre(trame,i+2,i);
|
||
reste=decode_nombre(trame,i+3,i+2);
|
||
flottant=decode_nombre(trame,i+10,i+5);
|
||
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);
|
||
}
|
||
|
||
res=degres+((reste+(flottant/10000))/60);
|
||
return res;
|
||
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);
|
||
}
|
||
|
||
}
|
||
|
||
int test_decode_longitude()
|
||
{
|
||
if((-0.00001<=decode_latitude("00306.6036")-3.11006)&&(decode_latitude("00306.6036")-3.11006<=0.00001))
|
||
{
|
||
printf("Erreur Test unitaire latitude \n");
|
||
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);
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
float decode_position(char *trame,Position Pos)
|
||
{
|
||
Pos.Latitude=decode_latitude(trame);
|
||
Pos.Longitude=decode_longitude(trame);
|
||
|
||
}
|
||
|
||
float test_decode_position()
|
||
{
|
||
|
||
}
|
||
|
||
//Fonction ? modifier !!!!!
|
||
void traitement(char * trame)
|
||
|
||
// Ne pas modifier cette fonction
|
||
int main(int argc,char ** argv)
|
||
{
|
||
static int cpt=0 ;
|
||
if(trame_cmp(trame,"GPGGA")==1)
|
||
{
|
||
printf ("> %s\n",trame);
|
||
cpt++;
|
||
}
|
||
}
|
||
|
||
|
||
//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.\n");
|
||
exit(-1);
|
||
}
|
||
if (test_decode_int()!=1){
|
||
printf ("Erreur Test unitaire int.\n");
|
||
// 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);
|
||
}
|
||
if (test_decode_nombre()!=1){
|
||
printf ("Erreur Test unitaire nombre.\n");
|
||
exit(-1);
|
||
}
|
||
if (test_decode_latitude()!=1){
|
||
printf ("Erreur Test unitaire latitude.\n");
|
||
exit(-1);
|
||
}
|
||
if (test_decode_longitude()!=1){
|
||
printf ("Erreur Test unitaire longitude.\n");
|
||
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);
|
||
|
||
|
||
|
||
// 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;
|
||
}
|
||
return 0;
|
||
}
|
Formats disponibles : Unified diff
Modification des fonctions précédentes, codage de la fonction decode_trame et affichage des positions des trame GPGGA