Révision 571
Ajouté par Louis JACQUET il y a presque 3 ans
main.c | ||
---|---|---|
#include <math.h>
|
||
|
||
|
||
|
||
//Trames de tests ? modifier si n?cessaire.
|
||
//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",
|
||
... | ... | |
|
||
|
||
|
||
typedef struct { // Definition de la structure des positions
|
||
float latitude;
|
||
float longitude;
|
||
} Position ;
|
||
|
||
|
||
void traitement(char * trame)
|
||
{
|
||
static int cpt=0;
|
||
cpt++;
|
||
if (trame_cmp(trame,"GPGGA") == 1) {
|
||
printf ("> %s\n",trame);
|
||
}
|
||
}
|
||
typedef struct { //Definition de la structure des zones
|
||
Position rpos;
|
||
float vitmax;
|
||
} Zone;
|
||
|
||
|
||
Zone zones[] = { //Iniatlisation du tableau renvoyant a la structure des zones
|
||
{{45.752610, 3.465067}, 60},
|
||
{{45.75080886, 3.11006165}, 70}
|
||
};
|
||
|
||
|
||
int trame_cmp(char * trame, char * type) {
|
||
... | ... | |
int res=1;
|
||
do
|
||
{
|
||
if (trame[i]==type[i]) {
|
||
if (trame[i+1]==type[i]) {
|
||
i++;
|
||
}
|
||
else {
|
||
... | ... | |
}
|
||
|
||
|
||
|
||
|
||
int decode_int(char c) {
|
||
int i;
|
||
i=c-48;
|
||
if (i>9) {
|
||
i=c-48; //Passage du code ASCII a son entier correspondant
|
||
if (i>9) { //Vrification du char, si c'est bien un chiffre
|
||
return (-1);
|
||
}
|
||
else {
|
||
... | ... | |
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
int decode_nombre(char * ch, int n) {
|
||
int i=0,res=0,b,p;
|
||
int decode_nombre(char * ch, int n) { //Fonction similaire a decode_int mais pour les nombres
|
||
int i=0,res=0,p;
|
||
for (i=0;i<n;i++) {
|
||
res=10*res+decode_int(ch[i]);
|
||
}
|
||
... | ... | |
}
|
||
|
||
|
||
|
||
|
||
|
||
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);
|
||
if (n==4) { //Tri les lattitudes
|
||
degre = (c[0]-48)*10+(c[1]-48); //Passe les valeurs des trames en entier
|
||
min = (c[2]-48)*10+(c[3]-48);
|
||
} else {
|
||
} else { //Tri les longitudes
|
||
degre = (c[0]-48)*100+(c[1]-48)*10+(c[2]-48);
|
||
min = (c[3]-48)*10+(c[4]-48);
|
||
}
|
||
... | ... | |
do {
|
||
p=1;
|
||
for (b=0;b<i;b++) {
|
||
p=p*0.1;
|
||
p=p*0.1; //Exprime le nombre en forme 0.03 en 3E-2
|
||
}
|
||
secondes= secondes + (c[n]-48)*p;
|
||
i++;
|
||
n++;
|
||
} while (c[n]!='\0');
|
||
res = degre + (min+secondes)/60;
|
||
res = degre + (min+secondes)/60; //Calcule final + renvois de la valeur
|
||
return res;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* float decode_trame(char * trame) {
|
||
float decode_trame(char * trame) {
|
||
int n=17, cpt=0; // latitude commence indice 17 dans une trame}
|
||
char lat[10],lon[10];
|
||
Position P;
|
||
... | ... | |
cpt++;
|
||
}
|
||
while (trame[n]!=',');
|
||
cpt=0; //cpt balaye les longitudes et latitudes on let remets a z?ro pour commencer a travailler sur les longitudes.
|
||
cpt=0; //cpt balaye les longitudes et latitudes on let remets a zero pour commencer a travailler sur les longitudes.
|
||
n=29; //les longitudes commencent indice 29 dans la trame
|
||
do{
|
||
lon[cpt]=trame[n];
|
||
... | ... | |
P.latitude = decode_lat_long(lat);
|
||
P.longitude = decode_lat_long(lon);
|
||
|
||
return P.lattitude, P.longitude;
|
||
return P.latitude, P.longitude; //Renvois les latitudes et longitudes
|
||
}
|
||
|
||
|
||
|
||
float calcule_distannce(Position p_1, Position p_2) {
|
||
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)))*6371;
|
||
x=(p_2.longitude - p_1.longitude)*cos((p_2.latitude + p_1.latitude) / 2); //On simplifie a un simple-
|
||
y=(p_2.latitude - p_1.latitude); //-th?or?me de pythagore, approximation.
|
||
res = (sqrt((x*x)++(y*y)))*111,12 //1852*60=111,12 Conversion mille marin en km
|
||
return res;
|
||
|
||
}
|
||
|
||
|
||
float calcule_vitesse(position p_1, Position p_2) {
|
||
float res;
|
||
res = calcule_distance(p_1,p_2) * 3600;
|
||
res = calcule_distance(p_1,p_2) * 3600; // *3600 pour passer en km/h
|
||
return res;
|
||
|
||
}
|
||
|
||
*/
|
||
int distance_a_la_plus_proche_zone(Position P, Zone r[], int nb_zones, float *d) {
|
||
int i,n;
|
||
*d = calcule_distance(P, r[0].rpos); //recupere la distance entre P et la 1er zone rencontr?
|
||
nb_zones = 0;
|
||
n=sizeof(*r); //Valeur necessaire pour utiliser une boucle for
|
||
for(i=1;i<n;i++) {
|
||
if ( calcule_distance(P, r[i].rpos) < *d ) { //Si on trouve une valeur de d inferieur a sa valeur actuelle, une zone-
|
||
*d = calcule_distance(P, r[i].rpos); //-dangereuse plus proche est detecte
|
||
nb_zones = i;
|
||
}
|
||
}
|
||
return nb_zones;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
//Ajouter vos tests unitaires dans cette fonction.
|
||
/*void tests_unitaires_trame_cmp(void){
|
||
void tests_unitaires_trame_cmp(void){
|
||
if (5!=5){
|
||
printf ("Erreur Test unitaire basique.\n");
|
||
exit(-1);
|
||
... | ... | |
printf ("Erreur Test unitaire trame_cmp.\n");
|
||
exit(-1);
|
||
}
|
||
}*/
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
void test_decode_int(void) {
|
||
if (decode_int('0')!=0) {
|
||
printf("erreur");
|
||
... | ... | |
exit(-1);
|
||
}
|
||
}
|
||
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);
|
||
}*/
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
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);
|
||
}*/
|
||
void test_decode_lat_long(void) { //Test la precision de la fonction, a 10^-6 pret
|
||
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 > 10^-6){
|
||
printf("erreur2");
|
||
exit(-1);
|
||
}
|
||
if (c*c > 10^-6){
|
||
printf("erreur2");
|
||
exit(-1);
|
||
}
|
||
if (e*e > 10^-6){
|
||
printf("erreur2");
|
||
exit(-1);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
void test_decode_nombre(void){
|
||
char t[5];
|
||
t[0]='1';t[1]='2';t[2]='3';t[3]='4';
|
||
... | ... | |
}
|
||
}
|
||
|
||
void test_decode_tram(void) {
|
||
position P1;
|
||
decode_trame("$GPGGA,141914.00,4645.5624,N,00306.0000,E,1,05,3.4,499.3,M,,M,,*7D",&pos1);
|
||
if(fabs(fabs(P1.latitude)-46.759373 >10-6)){
|
||
printf ("Erreur Test unitaire coordonnees 1.\n");
|
||
exit(-1);
|
||
}
|
||
if(fabs(fabs(P1.longitude)-3.100000 >10-6)){
|
||
printf ("Erreur Test unitaire coordonnees 2.\n");
|
||
exit(-1);
|
||
}
|
||
}
|
||
|
||
|
||
/*declaration coordonn?es de 3 villes pour les tests de calcul_distance*/
|
||
/*Paris*/
|
||
position P2;
|
||
P2.latitude = 48.8588897;
|
||
P2.longitude = 2.320041;
|
||
/*Lyon*/
|
||
position P3;
|
||
P3.latitude = 45.7578137;
|
||
P3.longitude = 4.8320114;
|
||
|
||
|
||
void test_distance(void) {
|
||
if((calcul_distance(&P2,&P3)>385) & (calcul_distance(&P2,&P3)<400)){} //On prend la distance connu (392) vol-
|
||
else{ //-d'oiseau et on prend un peu de marge
|
||
printf ("Erreur Test unitaire calcul distance 1.\n");
|
||
exit(-1);
|
||
}
|
||
}
|
||
|
||
|
||
// 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;
|
||
position P4;
|
||
P4.latitude = 40.457543;
|
||
P4.longitude = 7.110066;
|
||
position P5;
|
||
P5.latitude = 40.476543;
|
||
P5.longitude = 7.110073;
|
||
|
||
|
||
// 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;
|
||
}
|
Formats disponibles : Unified diff
J'ai changé pas mal de choses sur mon pc chez moi, que j'ai reporté ensuite ici, j'ai toute mes fonctions, et il me manque 2 tests unitaires