Shop OBEX P1 Docs P2 Docs Learn Events
More FPU questions — Parallax Forums

More FPU questions

halfblinddadohalfblinddado Posts: 59
edited 2009-02-19 18:34 in BASIC Stamp
Hello,

After a lot of mucking around I finally got this to work. However' In the code snippet below I get the an output of 135 degrees which is correct.

'
Variable Definitions
coord1········· VAR·· Word············· ' signed word variable
coord2········· VAR·· Word············· ' signed word variable
coord1To VAR Word
coord2To VAR Word
junk VAR Word
'
Generated Code
·· coord1To = 25··· 'y
·· coord2To = -25··· 'x
·· coord1 = 0····· 'y
·· coord2 = 0····· 'x
·· coord1 = coord1To - coord1
·· coord2 = coord2To - coord2
·· SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]SELECTA, heading, LEFT,
··· LOADWORD, coord1.HIGHBYTE, coord1.LOWBYTE, FSET0,
··· LOADWORD, coord2.HIGHBYTE, coord2.LOWBYTE, ATAN2, 0, RIGHT, FSET0]
·· SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]SELECTA, heading,
··· FWRITE0, $42, $65, $2E, $E1, FMUL0]
· ' Heading is output *180/Pi

· DEBUG CR, "Heading = "
· GOSUB Print_Float
· DEBUG CR, "Done.", CR
· END


However if I change the input to the following I get -135 degrees? Shouldn't I get 225 degrees?

·· coord1To = -25··· 'y
·· coord2To = -25··· 'x
·· coord1 = 0····· 'y
·· coord2 = 0····· 'x

As you can see I'm no math wiz but I would like to know how to get the degrees for the complete circle.

Thanks,

Mike

Comments

  • Carl HayesCarl Hayes Posts: 841
    edited 2009-02-16 05:49
    Notice that -135 degrees is the same direction as 225 degrees. If you want your answer always positive, simply add 360 if it's negative.

    If you really want a brain teaser, try to devise an algorithm for averaging several headings.· It's surprisingly hard to come up with a way that will work for any combination of headings.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net

    Post Edited (Carl Hayes) : 2/16/2009 5:55:01 AM GMT
  • halfblinddadohalfblinddado Posts: 59
    edited 2009-02-17 00:55
    Carl,

    Thanks for your reply. That works great. I knew it had to be something simple.

    Now if I can just get the fpu to determine if the output is a negative or positive number.

    Mike
  • halfblinddadohalfblinddado Posts: 59
    edited 2009-02-19 18:34
    Ok So now I am able to get angles in degrees I modified the code and was able to find the heading from the google campus to hangar 1 at moffet field on Google maps. I took out the degrees and just used the fractional minutes.

    Can someone tell me how to get the whole number, degrees and minutes such as -122.083893 into the FPU?

    Thanks,

    Mike

    ·· coord1 = 19036··· 'y
    ·· coord2 = -2653··· 'x
    ·· coord1To = 24785···· 'y Current position
    ·· coord2To = -3235····· 'x Current position
    ·· coord1 = coord1To - coord1
    ·· coord2 = coord2To - coord2
    ·· DEBUG DEC coord2.HIGHBYTE, CR
    ·· DEBUG DEC coord2.LOWBYTE, CR
    ·· SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]SELECTA, heading, LEFT,
    ··· LOADWORD, coord1.HIGHBYTE, coord1.LOWBYTE, FSET0,
    ··· LOADWORD, coord2.HIGHBYTE, coord2.LOWBYTE, ATAN2, 0, RIGHT, FSET0]
    ·· SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]FSTATUSA] '
    ·· GOSUB Fpu_Status····························· 'status 1 = negative 0 = positive
    ·· DEBUG ? status_Sign, CR······················ '
    ··· IF status_Sign = 0 THEN
    ····· SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]SELECTA, heading,
    ····· FWRITE0, $42, $65, $2E, $E1, FMUL0]
    ··· ELSE
    ····· SHIFTOUT FpuOut, FpuClk, MSBFIRST, [noparse][[/noparse]SELECTA, heading, LEFT, LEFT, FSET, heading,
    ····· FWRITE0, $42, $65, $2E, $E1, FMUL0, RIGHT, FSET0, LOADWORD, $01, $68, FADD0,
    ····· RIGHT, FSET0]
    ··· ENDIF
    · ' Heading is output *180/Pi. For negative numbers add 360

    · DEBUG CR, "Heading = "
    · GOSUB Print_Float
    · DEBUG CR, "Done.", CR
    · END
Sign In or Register to comment.