|
|
View previous topic :: View next topic |
Author |
Message |
ronnarone.r
Joined: 23 Dec 2008 Posts: 12
|
how to convert to float ? |
Posted: Thu Dec 24, 2009 10:39 pm |
|
|
I use gps and want to convert array for ( i= 13 ; i<= 36; i++ ) to float.
Output for ( i= 13 ; i<= 36; i++ ) : 1350.6955,N,10034.1684,E
I want to convert (float) :13.506955,n,100.341684,e
My code:
Code: |
#include <18F8722.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlibm.h>
#include <string.h>
#include <math.h>
#fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock = 10000000)
#use rs232(baud = 38400,xmit=pin_G1,rcv=pin_G2,stream=GPS,errors) //FV-M8(Station )
#use rs232(baud =38400,xmit=pin_E0,rcv=pin_E1,stream=HOSTPC,errors) //Computer
char NmeaSentence[80];
char Fixsentence[]="GPRMC";
char k ; // Character in NMEA
char NMEA ;
int i; // Sequence
/*................... From FV - M8 GPS ..... ......................*/
void fv_m8()
{
char latitude_longitude_1[35];
float lat_dd_1 ;
do
{
while ( fgetc(GPS) != '$' );
for (k=0;k<6;k++)
NmeaSentence[k]=fgetc(GPS);
}
while (strncmp(NmeaSentence,Fixsentence,5)!=0);
k = 0 ;
NMEA = 0 ;
while (NmeaSentence[k] !='*' && k<79 )
{
NMEA = fgetc(GPS);
NmeaSentence[k++] = NMEA ;
}
fprintf(HOSTPC,"latitude_longitude_1=");
for ( i= 13 ; i<= 36; i++ )
{
latitude_longitude_1[i-13]= NmeaSentence[i];
fprintf(HOSTPC,"%c", latitude_longitude_1[i-13] );
}
fprintf(HOSTPC,"\n\r");
} |
|
|
|
Ttelmah Guest
|
|
Posted: Fri Dec 25, 2009 6:10 am |
|
|
The first value won't be 13.506955....
The numbers being given, are in DM.mm format. 13 degrees, 50.6955 _minutes_. So, 13.844925 degrees.
Normally the GPS, will justify the values to the same length, so they are relatively easy to convert.
So, something like:
Code: |
int16 a_to_parti(char * locn,int8 numdig){
//Read 'numdig' digits from a text string, and return the integer
//value - no error checking...
int16 val=0;
int8 index=0;
while (numdig-- >0) {
val*=10;
val+=(locn[index] - '0');
}
return val;
}
//Then with the data in 'array', something like....
float northing,easting;
northing=a_to_parti(array+13,2);
northing+=a_to_parti(array+15,2)/60.0; //Add minutes
northing+=a_to_parti(array+18,4)/600000.0; //Add decimal minutes
if (array[23]=='S' || array[23]=='s') northing=-northing;
easting=a_to_parti(array+25,3);
easting+=a_to_parti(array+28,2)/60.0;
easting+=a_to_parti(array+31,4)/600000.0;
if (array[36]=='W' || array[36]=='w') easting=-easting;
|
Personally, I'd probably add checking that the commas are where expected etc., before using this.
No guarantees.....
Best Wishes |
|
|
guest1 Guest
|
Re: how to convert to float ? |
Posted: Wed Dec 30, 2009 3:02 am |
|
|
ronnarone.r wrote: | I use gps and want to convert array for ( i= 13 ; i<= 36; i++ ) to float.
Output for ( i= 13 ; i<= 36; i++ ) : 1350.6955,N,10034.1684,E
I want to convert (float) :13.506955,n,100.341684,e
|
Why do you want to convert it to float? Store the data in hundredths or thousandth or ten thousandth of minutes. Thus 1350.6955 will be 8306955 of ten thousandth of minutes = 0x7ec10b.
If you'll need to display it later - just convert it back. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|