![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Cend Guest
|
Navigation Program (Problem with Int32 and float) |
Posted: Mon Nov 13, 2006 9:48 pm |
|
|
My Project is about creating a navigation program which used UTM Coordinates to find Azimuth angle and range.
Example UTM Coordinate
Waypoint[0][0]=669694 m; (Destination Easting)
Waypoint[0][1]=1531197 m; (Destination Northing)
Easting =673147 m; (Current Easting)
Northing =1518363 m; (Current Northing)
Here are my function to find Azimuth and Range
Code: |
long NAV_Azimuth (int Waypointno)
{ long Azimuth;
int32 X,Y;
float Degree,Xf,Yf;
printf("WP Easting %ld \n\r",Waypoint[Waypointno][0]);
printf("Current Easting %ld \n\r",Easting);
printf("WP Northing %ld \n\r",Waypoint[Waypointno][1]);
printf("Current Northing %ld \n\r",Northing);
X=Waypoint[Waypointno][0]-Easting;
Y=Waypoint[Waypointno][1]-Northing;
printf("X= %ld \n\r",X);
printf("Y= %ld \n\r",Y);
Xf=(float)X;
Yf=(float)Y;
printf("Xf= %E \n\r",Xf);
printf("Yf= %E \n\r",Yf);
Degree=atan2(Xf,-Yf);
printf("Degree =%f Radians \n\r",Degree);
Azimuth=(Degree/3.1417)*1800;
return (Azimuth);
}
int32 NAV_Range (int Waypointno)
{ int32 Range,X,Y;
X=Waypoint[Waypointno][0]-Easting;
Y=Waypoint[Waypointno][1]-Northing;
Range=sqrt(pow((float)X,2)+pow((float)Y,2));
Return (Range);
}
|
The first function Problem is when convert int32 to float the value change.
The second function Problem is square of X and Y Value is too high for
int32.
Any suggestion would be very appreciated |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 14, 2006 2:17 pm |
|
|
Quote: | Problem is when convert int32 to float the value change |
Post a test program that shows the problem. Initialize an int32 variable
to a value. Convert it to float. Use printf to display it. Tell us what
value is displayed by printf.
Here is an example of a test program.
http://www.ccsinfo.com/forum/viewtopic.php?t=28780&start=1&highlight=test+program
Do the same thing, except post a program that shows your problem.
Also post the PIC that you are using and the compiler version. |
|
![](templates/subSilver/images/spacer.gif) |
Cend Guest
|
Sorry |
Posted: Tue Nov 14, 2006 8:29 pm |
|
|
sorry for not giving you guys info here it is
I'm using PCH 3.249
I'm testing with PIC18F458 using Hyperterm
the result are
Code: |
WP Easting 661101
Current Easting 673144
WP Northing 1515691
Current Northing 1518396
X= -12043
y= -2678
Xf= 4.294954E+09
Yf= 4.294963E+09
Degree =2.356196 radians
and Range =1779030016
|
the problem is Xf does not equal to X
and Range is wrong |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 14, 2006 11:12 pm |
|
|
I noticed that your values for X and Y are signed integers. In CCS,
an 'int32' is unsigned by default. This is different from normal C.
To declare a signed integer, you have to use 'signed int32'.
I made a test program as shown below, and declared X and Y as
signed int32 values. I compiled it with PCH vs. 3.249 and ran it
in the MPLAB simulator and used "UART1" for output.
Here are the results. It looks OK.
Code: |
X= -12043
Y= -2678
Xf= -1.204299E+04
Yf= -2.677999E+03
|
Code: |
#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//=====================================
void main()
{
signed int32 X,Y;
float Xf,Yf;
X= -12043;
y= -2678;
printf("X= %ld \n\r",X);
printf("Y= %ld \n\r",Y);
Xf = (float)X;
Yf = (float)Y;
printf("Xf= %E \n\r",Xf);
printf("Yf= %E \n\r",Yf);
while(1);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|