Help in converting a few lines of C code to PBasic for use with BS2
weg
Posts: 2
Hi all,
I am having trouble getting around the integer math limitations of the BS2, but I know there are ways to do so.· I was hoping someone (with a lot more BS2 experience than me) can take a look at the C code below and suggest modifications that would yield the same result in Basic (or something close as I know there will be rounding errors).· The values I get when compiling the code below are: distance=2144 and angle=66.· I really appreciate everyone's time!!!
Thanks in advance,
weg
// declare variables
float degrees_LAT1, minutes_LAT1, seconds_LAT1,·degrees_LONG1, minutes_LONG1, seconds_LONG1;
float degrees_LAT2, minutes_LAT2, seconds_LAT2,·degrees_LONG2, minutes_LONG2, seconds_LONG2;
float LAT_1, LAT_2, LONG_1, LONG_2;
float D=0, courseAngle=0;
// initialize variables
degrees_LAT1 = 33.0;·minutes_LAT1 = 57.0;·seconds_LAT1 = 30.0;
degrees_LONG1 = 118.0;·minutes_LONG1 = 24.0;·seconds_LONG1 = 50.0;
degrees_LAT2 = 40.0;·minutes_LAT2 = 38.0;·seconds_LAT2 = 55.0;
degrees_LONG2 = 73.0;·minutes_LONG2 = 47.0;·seconds_LONG2 = 35.0;
// code in C I need to mimic in Basic...
LAT_1 = (degrees_LAT1 + minutes_LAT1/60 + seconds_LAT1/3600) * (Pi/180);
LONG_1 = (degrees_LONG1 + minutes_LONG1/60 + seconds_LONG1/3600) * (Pi/180);
LAT_2 = (degrees_LAT2 + minutes_LAT2/60 + seconds_LAT2/3600) * (Pi/180);
LONG_2 = (degrees_LONG2 + minutes_LONG2/60 + seconds_LONG2/3600) * (Pi/180);
D = acos(sin(LAT_1)*sin(LAT_2) + cos(LAT_1)*cos(LAT_2)*cos(LONG_1-LONG_2));
courseAngle = acos((sin(LAT_2)-sin(LAT_1)*cos(D))/(sin(D)*cos(LAT_1)));
D = D*60*180/Pi;
printf("distance = %f\n", D);
printf("angle = %f\n\n", courseAngle*180/Pi);
I am having trouble getting around the integer math limitations of the BS2, but I know there are ways to do so.· I was hoping someone (with a lot more BS2 experience than me) can take a look at the C code below and suggest modifications that would yield the same result in Basic (or something close as I know there will be rounding errors).· The values I get when compiling the code below are: distance=2144 and angle=66.· I really appreciate everyone's time!!!
Thanks in advance,
weg
// declare variables
float degrees_LAT1, minutes_LAT1, seconds_LAT1,·degrees_LONG1, minutes_LONG1, seconds_LONG1;
float degrees_LAT2, minutes_LAT2, seconds_LAT2,·degrees_LONG2, minutes_LONG2, seconds_LONG2;
float LAT_1, LAT_2, LONG_1, LONG_2;
float D=0, courseAngle=0;
// initialize variables
degrees_LAT1 = 33.0;·minutes_LAT1 = 57.0;·seconds_LAT1 = 30.0;
degrees_LONG1 = 118.0;·minutes_LONG1 = 24.0;·seconds_LONG1 = 50.0;
degrees_LAT2 = 40.0;·minutes_LAT2 = 38.0;·seconds_LAT2 = 55.0;
degrees_LONG2 = 73.0;·minutes_LONG2 = 47.0;·seconds_LONG2 = 35.0;
// code in C I need to mimic in Basic...
LAT_1 = (degrees_LAT1 + minutes_LAT1/60 + seconds_LAT1/3600) * (Pi/180);
LONG_1 = (degrees_LONG1 + minutes_LONG1/60 + seconds_LONG1/3600) * (Pi/180);
LAT_2 = (degrees_LAT2 + minutes_LAT2/60 + seconds_LAT2/3600) * (Pi/180);
LONG_2 = (degrees_LONG2 + minutes_LONG2/60 + seconds_LONG2/3600) * (Pi/180);
D = acos(sin(LAT_1)*sin(LAT_2) + cos(LAT_1)*cos(LAT_2)*cos(LONG_1-LONG_2));
courseAngle = acos((sin(LAT_2)-sin(LAT_1)*cos(D))/(sin(D)*cos(LAT_1)));
D = D*60*180/Pi;
printf("distance = %f\n", D);
printf("angle = %f\n\n", courseAngle*180/Pi);
Comments
Doing this in CORDIC isn't trivial, and I doubt someone will sit down to work through all of it for you. So I suggest reading as many guides on CORDIC on the internet as possible (especially Tracy's link he already provided), sitting down with pen and paper, and work through example coordinates in his example code step by step, confirming your calculations are correct with a calculator. Then once you get a feel how his algorithm works, and how CORDIC works in general, expand his code to work with the range of values you need for you application.
Or buy the math cooprocessor chip Parallax sells, and have it do the computations for you.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1+1=10