|
|
View previous topic :: View next topic |
Author |
Message |
ronnarone.r
Joined: 23 Dec 2008 Posts: 12
|
ocean-server compass |
Posted: Thu Jun 03, 2010 10:29 pm |
|
|
Code: |
#include <18F8722.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlibm.h>
#include <string.h>
#include <math.h>
#fuses HS,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock = 10000000)
#use rs232(baud =38400,xmit=pin_C6,rcv=pin_C7,PARITY=N,bits =8,RESTART_WDT,stream=Ocs,errors) //ocean_server
#use rs232(baud =9600,xmit=pin_G1,rcv=pin_G2,PARITY=N,bits =8,RESTART_WDT,stream=GPS,errors) //LEA-4S
#use rs232(baud =38400,xmit=pin_E0,rcv=pin_E1,PARITY=N,bits =8,stream=HOSTPC,errors)//Computer
#define N 1
#define S -1
#define E 1
#define W -1
#define f 1/297
#define lat_1 13.50 //Decimal Degree
#define lon_1 100.30 //Decimal Degree
char NmeaSentence[100];
char Fixsentence[]="GPRMC";
char k ; // Character in NMEA
char NMEA ;
int i=0;
/*...........................................*/
double lat1 ;
double lon1 ;
/*...........................................*/
char latitude_longitude_LEA_4S[26];
char lat_dd_LEA_4S[3];
char lat_mm_LEA_4S[10];
char lon_dd_LEA_4S[4];
char lon_mm_LEA_4S[10];
double lat_d_LEA_4S ;
double lat_m_LEA_4S ;
double lon_d_LEA_4S;
double lon_m_LEA_4S;
double lat_2;
double lon_2;
double lat2 ; //Rad
double lon2 ; //Rad
/**************************************************************/
int16 f_4 ; //T. Vincenty " Inverse Formula " Alfa_1
double U_1 ;
double U_2 ;
/********************************************************/
char compass_raw[7];
char buffer_ocs[80];
int16 compass;
int select;
signed int16 Azimuth;
void readMessage_Ocs(void);
void calulation(void);
void readMessage_Ocs()
{
fgets(buffer_ocs,Ocs);
for(select=3; buffer_ocs[select] != 'P'; select++)
{
compass_raw[select-3] = buffer_ocs[select];
fprintf(HOSTPC,"%c",compass_raw[select-3]);
bit_clear(buffer_ocs[80],31);
bit_set(buffer_ocs[80],31);
}
fprintf(HOSTPC,"\n\r");
compass = atol(compass_raw);
fprintf(HOSTPC,"compass=%lu\n\r",compass);
}
void calulation()
{
lat1 = (lat_1)*(pi/180)*(N);
lon1 = (lon_1)*(pi/180)*(E);
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 ;
}
for ( i= 12; i<= 37;i++ ) //1239.60366,N,10057.25350,E
{
latitude_longitude_LEA_4S[i-12]= NmeaSentence[i];
fprintf(HOSTPC,"%c" ,latitude_longitude_LEA_4S[i-12] );
}
fprintf(HOSTPC,"\n\r");
for ( i= 0 ; i<2;i++ )
{
lat_dd_LEA_4S[i]= NmeaSentence[i+12];
}
lat_d_LEA_4S = atof(lat_dd_LEA_4S);
fprintf(HOSTPC,"lat_d_LEA_4S=%f \n\r",lat_d_LEA_4S );
for ( i= 0 ; i<6;i++ )
{
lat_mm_LEA_4S[i]= NmeaSentence[i+14];
}
lat_m_LEA_4S = atof(lat_mm_LEA_4S)/60;
fprintf(HOSTPC,"lat_m_LEA_4S =%f \n\r" ,lat_m_LEA_4S );
lat_2 = (lat_d_LEA_4S + lat_m_LEA_4S)*N; //Decimal Degree
fprintf(HOSTPC,"lat_2 =%3.7f \n\r" ,lat_2 );
fprintf(HOSTPC,"lon_dd_LEA_4S[i]=" );
for ( i= 0 ; i<3;i++ )
{
lon_dd_LEA_4S[i]= NmeaSentence[i+25];
fprintf(HOSTPC,"%c" ,lon_dd_LEA_4S[i] );
}
fprintf(HOSTPC," \n\r");
lon_d_LEA_4S = atof(lon_dd_LEA_4S);
fprintf(HOSTPC,"lon_d_LEA_4S =%f \n\r" ,lon_d_LEA_4S );
for ( i= 0 ; i<6;i++ )
{
lon_mm_LEA_4S[i]= NmeaSentence[i+28];
}
lon_m_LEA_4S = atof(lon_mm_LEA_4S)/60;
fprintf(HOSTPC,"lon_m_LEA_4S =%f \n\r" ,lon_m_LEA_4S );
lon_2 = (lon_d_LEA_4S + lon_m_LEA_4S)*E ; //Decimal Degree
fprintf(HOSTPC,"lon_2=%3.7f \n\r" ,lon_2 );
}
void main ()
{
while(true)
{
readMessage_Ocs();
calulation();
}
}
|
Output for compass has error. But when I run only compass function, it works and I can not change baud rate. I want to know why ? Thank you. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jun 04, 2010 5:08 am |
|
|
Code: | bit_clear(buffer_ocs[80],31);
bit_set(buffer_ocs[80],31); | I don't know what you want to do here, but whatever it is it is a bug.
buffer_ocs has a length of 80, meaning the last item in the buffer is buffer_ocs[79]. Also, it is an 8-bit array so when setting bit 31 of buffer_ocs[80] you are actually writing to bit 8 of buffer_ocs[83].
You are writing outside the array, corrupting other variables in RAM with unpredictable results.
Code: | for ( i= 0 ; i<6;i++ )
{
lat_mm_LEA_4S[i]= NmeaSentence[i+14];
}
lat_m_LEA_4S = atof(lat_mm_LEA_4S)/60;
| You have this construction several times in your code. atof() expects a string as input, i.e. a null terminated string of characters. This zero terminator is missing causing unpredictable output for atof().
Code: | fgets(buffer_ocs,Ocs);
for(select=3; buffer_ocs[select] != 'P'; select++)
| This will fail in two situations:
1) When the string is shorter than 3 characters
2) When the string does not contain a 'P' character |
|
|
|
|
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
|