Shop OBEX P1 Docs P2 Docs Learn Events
Help in converting a few lines of C code to PBasic for use with BS2 — Parallax Forums

Help in converting a few lines of C code to PBasic for use with BS2

wegweg Posts: 2
edited 2006-04-11 20:30 in BASIC Stamp
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);

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-04-11 20:30
    Tracy provided the answer to your question in·your other post. You'll have to implement CORDIC routines for the stamp. Converting the coordinates into radians,·maximum value is 2π or 6.28318...., to express the integer portion would take 3 bits. One second, or 2π/(360*3600) is 0.000048481368 or ~(1/206265) which takes 18 bits to express, so you'd need 21 bits to express each value in fixed point format.·This would require double word precision CORDIC math. If you instead trucated the value to a word·precision fixed point math, youd get 3.13 (3 bits for the integer portion, 13 bits for the fractional portion). A 13 bit fraction would be roughly equivalent to 25 seconds.

    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
Sign In or Register to comment.