root/branch/faye/sp4a12/main.c @ 416
1 | jalaffon | #include <stdio.h>
|
|
#include <stdlib.h>
|
|||
#include <strings.h>
|
|||
385 | abfaye1 | #include "trame.h"
|
|
#include <math.h>
|
|||
#define PI 3.14159
|
|||
// Declaration des variables
|
|||
typedef struct {
|
|||
float latitude;
|
|||
float longitude;
|
|||
} Position;
|
|||
typedef struct{
|
|||
Position rpos;
|
|||
float vitmax;
|
|||
} Zone;
|
|||
Position pos_prec;
|
|||
Zone zones[] = {
|
|||
{{44.7887762, -3.012}, 50},
|
|||
{{44.7891220, -3.013}, 70},
|
|||
};
|
|||
1 | 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",
|
|||
0};
|
|||
39 | abfaye1 | int trame_cmp(char*trame,char*type){
|
|
int i = 1;
|
|||
int res = 1;
|
|||
while ((res == 1)&&(type[i-1] != '\0')){
|
|||
if (trame[i] != type[i-1]){
|
|||
res = 0;
|
|||
}
|
|||
i++;
|
|||
}
|
|||
return res;
|
|||
186 | abfaye1 | }
|
|
int decode_int(char c){
|
|||
if (c<'0' || c>'9'){
|
|||
return -1;
|
|||
}
|
|||
188 | abfaye1 | return c - '0';
|
|
}
|
|||
int decode_nombre(char * ch, int n){
|
|||
int i;
|
|||
int res = 0;
|
|||
for(i=0; i<n; i++){
|
|||
res *= 10;
|
|||
res += decode_int(ch[i]);
|
|||
}
|
|||
return res;
|
|||
207 | abfaye1 | }
|
|
416 | abfaye1 | float conv_lat(char *ch){ //conersion de la latitude
|
|
float res = 0; // declaration de la variable resultat
|
|||
385 | abfaye1 | int i;
|
|
416 | abfaye1 | float val_sexa = decode_nombre(ch,4); //decodage des quatres premiers caract?res
|
|
385 | abfaye1 | for (i=5; i<9; i++){
|
|
416 | abfaye1 | val_sexa += (decode_int(ch[i]))*pow(10,-i+4); // on decode la partie decimale
|
|
385 | abfaye1 | }
|
|
416 | abfaye1 | res = (int)(val_sexa/100);// on re?oit les degres
|
|
res = res + (val_sexa - res*100)/60; // on convertit les minutes en degres et on fait la somme
|
|||
385 | abfaye1 | return res;
|
|
}
|
|||
416 | abfaye1 | float conv_long(char *ch){ // conversion de la longitude
|
|
float res = 0; // variable contenant le resultat
|
|||
385 | abfaye1 | int i;
|
|
416 | abfaye1 | float val_sexa = decode_nombre(ch,5); // decodage des 5 premiers caracteres
|
|
385 | abfaye1 | for (i=6; i<10; i++){
|
|
416 | abfaye1 | val_sexa += (decode_int(ch[i]))*pow(10,-i+5); // on decode la partie decimale
|
|
385 | abfaye1 | }
|
|
416 | abfaye1 | res = (int)(val_sexa/100); // on prend les degres
|
|
res = res + (val_sexa - res*100)/60; // conversion des minutes en degres et ajout de la somme
|
|||
385 | abfaye1 | return res;
|
|
}
|
|||
float conversion(char *ch){
|
|||
int i = 0;
|
|||
float res = 0;
|
|||
while (ch[i] != '\0'){
|
|||
i++;
|
|||
}
|
|||
if (i == 9){
|
|||
res = conv_lat(ch);
|
|||
}
|
|||
else if (i == 10){
|
|||
res = conv_long(ch);
|
|||
}
|
|||
else{res = 1000.0;}
|
|||
return res;
|
|||
}
|
|||
int decode_trame(char * trame, Position *p){
|
|||
char longi[10];
|
|||
char lati[9];
|
|||
int cpt = 0;
|
|||
int i = 0;
|
|||
int j = 0;
|
|||
int y = 0;
|
|||
if (trame_cmp(trame,"GPGGA")==1){
|
|||
while (trame[i] != '\0'){
|
|||
if (trame[i] == ','){ //On compte le nombre de virgule
|
|||
cpt += 1;
|
|||
i ++;
|
|||
}
|
|||
if (cpt == 2) { //latitude
|
|||
lati[j] = trame[i]; //conversion latitude
|
|||
j ++;
|
|||
}
|
|||
if (cpt == 4) { //longitude
|
|||
longi[y] = trame[i]; //conversion longitude
|
|||
y ++;
|
|||
}
|
|||
i ++;
|
|||
207 | abfaye1 | }
|
|
385 | abfaye1 | p->latitude = conv_lat(lati);
|
|
p->longitude = conv_long(longi); //conversion dans structure position
|
|||
return 1;
|
|||
}
|
|||
return 0;
|
|||
}
|
|||
float calcul_distance(Position p_1, Position p_2){
|
|||
float rayon = 6378.14; //Rayon de la Terre (km).
|
|||
float DistAng = 0;
|
|||
DistAng = (PI/180)*acos((sin(p_1.latitude)*sin(p_2.latitude)+cos(p_1.latitude)*cos(p_2.latitude)*cos(p_2.longitude-p_1.longitude))); //Calcul distance angulaire
|
|||
return DistAng*rayon; //On renvoi le calcul de la distance : D (km) = DistanceAngulaire*R.
|
|||
}
|
|||
float calcul_vitesse(Position p_1, Position p_2){
|
|||
return calcul_distance(p_1,p_2)*3600; //On calcul la vitesse (en km) avec la formule v=d/t avec t=1s.
|
|||
}
|
|||
int distance_a_la_plus_poche_zone(Position p, Zone r[], int nb_zones, float *d){
|
|||
int index = 0;
|
|||
*d = calcul_distance(p, r[0].rpos); //On initialise une distance pour pouvoir ensuite la comparer et chercher le minimum.
|
|||
for (int i = 1; i<nb_zones; i++){ //On parcours la table de zone dangereuse.
|
|||
if (calcul_distance(p,r[i].rpos) < *d){
|
|||
index = i;
|
|||
*d = calcul_distance(p,r[i].rpos);
|
|||
207 | abfaye1 | }
|
|
}
|
|||
385 | abfaye1 | return index;
|
|
39 | abfaye1 | }
|
|
1 | jalaffon | //Fonction ? modifier !!!!!
|
|
void traitement(char * trame)
|
|||
23 | abfaye1 | {
|
|
static int cpt=0;
|
|||
cpt++;
|
|||
385 | abfaye1 | printf ("> %s\n",trame);
|
|
Position pos;
|
|||
int alarme;
|
|||
float vitesse;
|
|||
float seuil = 0.5;
|
|||
int index;
|
|||
float distance;
|
|||
if (trame_cmp(trame,"GPGGA")==1){
|
|||
printf ("> %s\n",trame);
|
|||
if (decode_trame(trame,&pos)==1){
|
|||
vitesse = calcul_vitesse(pos,pos_prec);
|
|||
index = distance_a_la_plus_poche_zone(pos,zones,2,&distance);
|
|||
if ((distance <= seuil) && (vitesse > zones[index].vitmax)){
|
|||
alarme = 1;
|
|||
printf("Alarme on \n");
|
|||
}
|
|||
alarme = 0;
|
|||
printf("Alarme off \n");
|
|||
}
|
|||
pos_prec.latitude = pos.latitude;
|
|||
pos_prec.longitude = pos.longitude;
|
|||
}
|
|||
1 | jalaffon | }
|
|
//Ajouter vos tests unitaires dans cette fonction.
|
|||
void tests_unitaires(void){
|
|||
if (5!=5){
|
|||
printf ("Erreur Test unitaire basique.\n");
|
|||
exit(-1);
|
|||
186 | abfaye1 | }
|
|
39 | abfaye1 | if (trame_cmp("$GPGGA suite chaine","GPGGA")!=1){
|
|
1 | jalaffon | printf ("Erreur Test unitaire trame_cmp.\n");
|
|
exit(-1);
|
|||
186 | abfaye1 | }
|
|
39 | abfaye1 | 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){
|
|||
exit(-1);
|
|||
}
|
|||
if (trame_cmp("$APRMC...", "GPGGA")!=0){
|
|||
printf ("Erreur Test unitaire trame_cmp.\n");
|
|||
exit(-1);
|
|||
}
|
|||
186 | abfaye1 | if (decode_int('0')!=0){
|
|
printf ("Erreur Test unitaire decode_int.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('7')!=7){
|
|||
printf ("Erreur Test unitaire decode_int.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_int('A')!=-1){
|
|||
printf ("Erreur Test unitaire decode_int.\n");
|
|||
exit(-1);
|
|||
}
|
|||
188 | abfaye1 | if (decode_nombre("123",3)!=123){
|
|
printf ("Erreur Test unitaire decode_nombre.\n");
|
|||
exit(-1);
|
|||
}
|
|||
if (decode_nombre("7541",2)!=75){
|
|||
printf ("Erreur Test unitaire decode_nombre.\n");
|
|||
exit(-1);
|
|||
}
|
|||
385 | abfaye1 | if (fabs(conv_lat("3723.2475")-37.387458) >= pow(10,-6)){
|
|
printf ("Erreur Test unitaire conv_lat.\n");
|
|||
207 | abfaye1 | exit(-1);
|
|
}
|
|||
385 | abfaye1 | if (fabs(conv_long("00306.6036")-3.11006) >= pow(10,-6)){
|
|
printf ("Erreur Test unitaire conv_long.\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);
|
|||
}
|
|||
1 | jalaffon | ||
385 | abfaye1 | }
|
|
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;
|
|||
}
|