|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <strings.h>
|
|
#include "trame.h"
|
|
#include <math.h>
|
|
|
|
typedef struct {
|
|
float latitude;
|
|
float longitude;
|
|
} Position ;
|
|
|
|
typedef struct {
|
|
Position rpos;
|
|
float vitmax;
|
|
} Zone ;
|
|
|
|
//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};
|
|
int trame_cmp(char * trame, char * type)
|
|
{
|
|
int i=0;
|
|
int b=0;
|
|
int c=0;
|
|
int ok;
|
|
|
|
while (trame[b]!='\0')
|
|
{
|
|
b=b+1;
|
|
}
|
|
while (type[c]!='\0')
|
|
{
|
|
c=c+1;
|
|
}
|
|
for(i=0; i<=c-1; i++)
|
|
{
|
|
|
|
{
|
|
if (trame[i+1]==type[i])
|
|
{
|
|
ok=1;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
return ok;
|
|
}
|
|
int decode_int (char c)
|
|
{
|
|
int i=c-48;
|
|
int ok=-1;
|
|
if (i>=0 && i<=9){
|
|
ok=i;
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
int decode_nombre(char *ch,int n)
|
|
{
|
|
int b=0;
|
|
int a=0;
|
|
int c=0;
|
|
int i;
|
|
for (i=0; i<=n-1; i++){
|
|
|
|
a=decode_int(ch[i]);
|
|
c=c*10+a;
|
|
}
|
|
|
|
return c;
|
|
}
|
|
|
|
|
|
float convertisseur_latitude (char *ch)
|
|
{
|
|
|
|
float lat=0.0;
|
|
float degre=(float)decode_nombre(ch,2);
|
|
float decim1=(float)decode_nombre(ch,4)-degre*pow(10,2);
|
|
|
|
char chaine[50];
|
|
int i;
|
|
for (i=5; i<9; i++)
|
|
{
|
|
chaine[i-5]=ch[i];
|
|
}
|
|
float decim2=(float)decode_nombre(chaine, 4); //ok
|
|
decim1=decim1/60.0;
|
|
decim2=decim2/600000.0;
|
|
lat=degre+decim1+decim2;
|
|
|
|
return lat;
|
|
}
|
|
float convertisseur_longitude (char *ch)
|
|
{
|
|
float longi=0.0;
|
|
float degre=(float)decode_nombre(ch,3);
|
|
float decim1=(float)decode_nombre(ch,5)-degre*pow(10,2);
|
|
|
|
char chaine[50];
|
|
int i;
|
|
for (i=6; i<10; i++)
|
|
{
|
|
chaine[i-6]=ch[i];
|
|
}
|
|
float decim2=(float)decode_nombre(chaine, 4); //ok
|
|
decim1=decim1/60.0;
|
|
decim2=decim2/600000.0;
|
|
longi=degre+decim1+decim2;
|
|
|
|
return longi;
|
|
}
|
|
|
|
float latoulong (char *ch)
|
|
{
|
|
int i;
|
|
i=strlen(ch);
|
|
if (i==9)
|
|
{
|
|
return convertisseur_latitude(ch);
|
|
}
|
|
else
|
|
{
|
|
return convertisseur_longitude(ch);
|
|
}
|
|
}
|
|
|
|
float calcul_distance(Position p_1, Position p_2)
|
|
{
|
|
float S;
|
|
float R=6371.00; //rayon de la Terre
|
|
float P=2*M_PI*R;
|
|
float c=P/360.00;
|
|
S=c*pow((pow((p_2.latitude-p_1.latitude),2)+pow((p_2.longitude-p_1.longitude),2)),0.5);
|
|
return S;
|
|
}
|
|
|
|
float calcul_vitesse(Position p_1, Position p_2)
|
|
{
|
|
float vitesse;
|
|
float h=36000;
|
|
vitesse=calcul_distance(p_1, p_2)*36000;
|
|
return vitesse;
|
|
}
|
|
|
|
|
|
int decode_trame(char* t, Position* p){
|
|
int ok,i;
|
|
char latitudecopie[9]; // chaine qui va contenir la latitude extraite de la trame
|
|
char longitudecopie[10]; //chaine qui va contenir la longitude extraite de la trame
|
|
|
|
ok=trame_cmp(t,"GPGGA"); // comparaison pour voir si la trame est valide
|
|
if(ok==1)
|
|
{
|
|
for(i=0;i<9;++i)
|
|
{
|
|
latitudecopie[i]=t[17+i]; //on a un tableau contenant la latitude en cha?ne de caract?res
|
|
}
|
|
(*p).latitude=convertisseur_latitude(latitudecopie);
|
|
|
|
for(i=0;i<10;++i)
|
|
{
|
|
longitudecopie[i]=t[29+i]; //on a un tableau contenant la longitude en cha?ne de caract?res
|
|
|
|
}
|
|
(*p).longitude=convertisseur_longitude(longitudecopie);
|
|
|
|
}else{
|
|
exit(-1);
|
|
} }
|
|
|
|
int distance_a_la_plus_proche_zone(Position p, Zone r[],int nb_zones, float *d)
|
|
{ int i;
|
|
float distance;
|
|
distance=calcul_distance(p, r[0].rpos); //initialise la premiere distance ? comparer
|
|
int index; //variable que l'on retourne ? la fin de la fonction
|
|
|
|
for (i=0; i<nb_zones; i++)
|
|
|
|
{
|
|
*d=calcul_distance(p, r[i].rpos);
|
|
if (*d<=distance) //si la distance entre la position p et celle du radar ? la i-?me position recens?e est inf?rieur ? la premi?re distance calcul?e, distance prends cette valeur. on retrouvera ainsi de suite la zone dangereuse la plus pr?s de la position de la voiture!
|
|
{
|
|
distance=*d;
|
|
index=i;
|
|
}
|
|
|
|
}
|
|
return index;
|
|
}
|
|
//Fonction ? modifier !!!!!
|
|
void traitement(char * trame)
|
|
{
|
|
static int cpt=0;
|
|
cpt++;
|
|
|
|
if((trame[5]==65)&&(trame[4]==71)&&(trame[3]==71))
|
|
{
|
|
printf ("> %s\n",trame);
|
|
}
|
|
|
|
}
|
|
|
|
//Ajouter vos tests unitaires dans cette fonction.
|
|
void tests_unitaires(void){
|
|
Position a={0.0, 1.0};
|
|
Position *p;
|
|
|
|
p=&a;
|
|
|
|
|
|
|
|
if (5!=5){
|
|
printf ("Erreur Test unitaire basique.\n");
|
|
exit(-1);
|
|
}
|
|
if (trame_cmp("$GPGGA,141922.00,4545.1810,N,00306.6046,E,1,05,3.4,500.6,M,,M,,*74","GPGGA")!=1){
|
|
printf ("Erreur 1 Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
if (trame_cmp("$GPRMC suite chaine","GPGGA")!=0){
|
|
printf ("Erreur 2 Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
if (trame_cmp("$GPRMC... ", "GPRMC" )!=1){
|
|
printf ("Erreur 3 Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
if (trame_cmp("$APRMC...", "GPGGA")!=0){
|
|
printf ("Erreur 4 Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|
}
|
|
if(decode_int('G')!=-1){
|
|
printf("Erreur 1 test unitaire decode_int.\n");
|
|
}
|
|
if(decode_int('6')!=6){
|
|
printf("Erreur 2 test unitaire decode_int.\n");
|
|
}
|
|
if(decode_int('B')!=(-1)){
|
|
printf("Erreur 3 test unitaire decode_int.\n");
|
|
}
|
|
if(decode_int('4')!=4){
|
|
printf("Erreur 4 test unitaire decode_int.\n");
|
|
}
|
|
if(decode_nombre("789654",3)!=789){
|
|
printf("Erreur sur decode_nombre.\n");
|
|
}
|
|
if(decode_nombre("789654",4)!=7896){
|
|
printf("Erreur sur decode_nombre.\n");
|
|
}
|
|
if(convertisseur_latitude("4545.1810")<45.7000 || convertisseur_latitude("4545.1810")>45.8000 ) //comme c'est des float on ne peut que encadrer la valeur
|
|
{
|
|
printf("Erreur sur le convertisseur latitude");
|
|
}
|
|
if(convertisseur_longitude("00306.6046")<03.0000 || convertisseur_latitude("00306.6046")>03.4000 )
|
|
{
|
|
printf("Erreur sur le convertisseur longitude");
|
|
}
|
|
if (decode_trame("$GPGGA,141922.00,4545.1810,N,00306.6046,E,1,05,3.4,500.6,M,,M,,*74",p)==1) {
|
|
printf("Erreur sur decode_trame");
|
|
}
|
|
|
|
//tests de certaines fonction
|
|
|
|
Position pos;
|
|
pos.latitude=1;
|
|
pos.longitude=1;
|
|
Position *posi;
|
|
posi=&pos;
|
|
|
|
|
|
decode_trame("$GPGGA,141914.00,4545.0000,N,00306.6036,E,1,05,3.4,499.3,M,,M,,*7D", posi);
|
|
printf("position latitude: %f \n", (*posi).latitude);
|
|
printf("position longtitude: %f", (*posi).longitude);
|
|
|
|
Position position1, position2, position3, position4, position5,position6;
|
|
|
|
//paris
|
|
position2.latitude=48.85666666666667;
|
|
position2.longitude=2.3519444444444444;
|
|
|
|
//Rouen
|
|
position1.latitude=49.443888888888885;
|
|
position1.longitude=1.1033333333333335;
|
|
|
|
//lille
|
|
position3.latitude=50.63722222222222;
|
|
position3.longitude=3.063333333333333;
|
|
|
|
//chalons
|
|
position4.latitude=48.9575;
|
|
position4.longitude=4.364999999999999;
|
|
|
|
//nice
|
|
position5.latitude=43.7;
|
|
position5.longitude=7.35;
|
|
|
|
|
|
printf("fonction test distance\n");
|
|
printf("Distance Paris-Rouen=%f\n", calcul_distance(position2, position1)); //Paris-Rouen = 135 km
|
|
printf("Distance Paris-Lille=%f\n", calcul_distance(position2, position3)); //Paris-Lille
|
|
printf("D2=%f\n", calcul_distance(position2, position4));
|
|
printf("D2=%f\n", calcul_distance(position2, position5));
|
|
|
|
|
|
Zone zones[] = {{{44.7887762, -3.012}, 50},{{44.7891220, -3,013}, 70}};
|
|
float dist;
|
|
float *d;
|
|
d=&dist;
|
|
int c;
|
|
c=distance_a_la_plus_proche_zone(position1, zones, 2, d);
|
|
printf("zone dangereuse : %i \n", a);
|
|
|
|
|
|
}
|
|
|
|
// 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;
|
|
}
|