Conversion to degrees help


Closed Thread
Results 1 to 19 of 19
  1. #1

    Default Conversion to degrees help

    Hi All,

    I'm trying to convert a 10bit or 12bit word "position" to 0-360 degrees with 2 or 3 digits after the decimal point

    I have this routine working for conversion to degrees without decimal point
    anyone who can help me with this mathematics:

    Scale = 1024 (might also be 4098 for 12bit)
    dummy = position * 64
    dummy = dummy *360
    result_64 = DIV32 scale
    degrees = result_64/64

    thanks

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Talking

    Hi,

    The answer is quite simple :

    360° + 3 decimals = 360 000 steps ... 19 bits !

    360° + 2 decimals = 36000 steps ... 16 bits

    How do you do that with a 10 or 12 bits ADC ???

    try to look at 16 to 20 bits ADCs.


    Now, JUST for calculations ...

    3 decimals ... need at least PBPL and a Pic 18 ... Why not.


    The final smile ... what about your position sensor precision and linearity ???

    Alain

    2 decimals Ok
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Thank you for your fast reply

    yes you are right, So 360 / 4096 = 0.087 deg

    The linearity is ok as it is an absolute magnetic encoder

    I would like to display the result on an LCD in a xxx.xx format or xxx.xxx
    even if it does not reach its final resolution and the last digits are not correct or rounded

    Any help welcome

    Walter

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5


    Did you find this post helpful? Yes | No

    Default

    thank you joe, but value's are getting to big to fit in a 16bit var

    I thnk I need help from the Mathematics guy's

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by RFsolution View Post
    thank you joe, but value's are getting to big to fit in a 16bit var

    I thnk I need help from the Mathematics guy's
    That is most positively not me
    Last edited by Archangel; - 4th September 2008 at 22:54.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  7. #7
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    but value's are getting to big to fit in a 16bit var
    Post a little code and an example of the values you are getting.
    And maybe think about upgrading to 2.5. The LONGS are cool.
    Dave
    Always wear safety glasses while programming.

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by RFsolution View Post
    Hi All,

    I'm trying to convert a 10bit or 12bit word "position" to 0-360 degrees with 2 or 3 digits after the decimal point

    I have this routine working for conversion to degrees without decimal point
    anyone who can help me with this mathematics:

    Scale = 1024 (might also be 4098 for 12bit)
    dummy = position * 64
    dummy = dummy *360
    result_64 = DIV32 scale
    degrees = result_64/64

    thanks
    This might get you 2 behind the point...
    Code:
    scale = 1024
    dummy = position << 6'wont overflow 16 bits, same as *64 but quicker
    dummy = dummy * 360 'will overflow 16 bits
    result_high = div32 scale 'but doesnt matter here
    degrees_whole = result_high >> 6 'same as divide by 64 but quicker
    
    scale = 1024
    dummy = position << 6 'wont overflow 16 bits, same as *64 but quicker
    dummy = dummy * 36000 'will overflow 16 bits
    result_low = div32 scale 'again, doesnt matter here
    degrees_fraction = result_low >> 6 'same as divide by 64 but quicker
    degrees_fraction = degrees_fraction // 100 'take out the -whole- degrees and leave the fraction
    
    lcdout $fe , 1 , "Degrees:" , DEC3 degrees_whole , ":" , DEC2 degrees_fraction
    This should work for a 10 bit ADC, but for a 12 bit ADC, the 2nd chunk will overflow. Changing the 36000 to 3600 and 100 to 10 should let the 2nd chunk work with one digit behind the decimal point, or change 36000 to 9000 and 100 to 25 and you'll get 2 digits behind the decimal point but in steps of .04

    And upgrade to PBP 2.50b
    Last edited by skimask; - 5th September 2008 at 05:31.

  9. #9
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by skimask View Post
    This might get you 2 behind the point...
    Code:
    scale = 1024
    dummy = position << 6'wont overflow 16 bits, same as *64 but quicker
    dummy = dummy * 360 'will overflow 16 bits
    result_high = div32 scale 'but doesnt matter here
    degrees_whole = result_high >> 6 'same as divide by 64 but quicker
    
    scale = 1024
    dummy = position << 6 'wont overflow 16 bits, same as *64 but quicker
    dummy = dummy * 36000 'will overflow 16 bits
    result_low = div32 scale 'again, doesnt matter here
    degrees_fraction = result_low >> 6 'same as divide by 64 but quicker
    degrees_fraction = degrees_fraction // 100 'take out the -whole- degrees and leave the fraction
    
    lcdout $fe , 1 , "Degrees:" , DEC3 degrees_whole , ":" , DEC2 degrees_fraction
    This should work for a 10 bit ADC, but for a 12 bit ADC, the 2nd chunk will overflow. Changing the 36000 to 3600 and 100 to 10 should let the 2nd chunk work with one digit behind the decimal point, or change 36000 to 9000 and 100 to 25 and you'll get 2 digits behind the decimal point but in steps of .04

    And upgrade to PBP 2.50b
    All this math makes my head hurt , couldn't you do the math twice with different multipliers, throw out the top numbers from one, and the bottom numbers from the other and display the results together separated by the decimal point ? Or is that what you just said?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  10. #10


    Did you find this post helpful? Yes | No

    Default

    Thank you skymath
    I will give it a try this evening
    I have PBP 2.50 need to patch to 2.50b, what is special in 2.50 regarding to
    my conversion problem, dont find anything in the changes log that points to that



    Quote Originally Posted by skimask View Post
    This might get you 2 behind the point...
    Code:
    scale = 1024
    dummy = position << 6'wont overflow 16 bits, same as *64 but quicker
    dummy = dummy * 360 'will overflow 16 bits
    result_high = div32 scale 'but doesnt matter here
    degrees_whole = result_high >> 6 'same as divide by 64 but quicker
    
    scale = 1024
    dummy = position << 6 'wont overflow 16 bits, same as *64 but quicker
    dummy = dummy * 36000 'will overflow 16 bits
    result_low = div32 scale 'again, doesnt matter here
    degrees_fraction = result_low >> 6 'same as divide by 64 but quicker
    degrees_fraction = degrees_fraction // 100 'take out the -whole- degrees and leave the fraction
    
    lcdout $fe , 1 , "Degrees:" , DEC3 degrees_whole , ":" , DEC2 degrees_fraction
    This should work for a 10 bit ADC, but for a 12 bit ADC, the 2nd chunk will overflow. Changing the 36000 to 3600 and 100 to 10 should let the 2nd chunk work with one digit behind the decimal point, or change 36000 to 9000 and 100 to 25 and you'll get 2 digits behind the decimal point but in steps of .04

    And upgrade to PBP 2.50b

  11. #11
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    LONGS!!!!
    Look in your manual on the different variable types. 2.5 can do 32 bit +numbers
    or 31 bit - numbers. Something like that. The negative takes up a bit.
    Dave
    Always wear safety glasses while programming.

  12. #12
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Lightbulb Don't forget the "*/" and "**" operators.

    OK, You want to scale a number by a constant factor. This is pretty easy using the "*/" and "**" operators. This way you can save some time since the calculations are pretty easy to do for the processor, no divisions only one multiplication. You can learn more about them here http://www.emesystems.com/BS2math1.htm . A short way of describing them is that "*/" makes an invisible division by 256 and "**" divides by 65536.

    Older versions(pre 2.50) can only use WORD sized(16 bits) variables, meaning your result must be 65535 or less.
    1024 to 360. Degrees = position */ 90 or Degrees = position ** 23040
    1024 to 3600. Degrees = position */ 900
    1024 to 36000. Degrees = position */ 9000

    4096 to 360. Degrees = position ** 5760
    4096 to 3600. Degrees = position */ 225 or Degrees = position ** 57600
    4096 to 36000. Degrees = position */ 2250

    If you have PBP2.50 you can have your result bigger than 16 bits. 360000 is then possible.
    1024 to 360000. Degrees = position */ 90000
    4096 to 360000. Degrees = position */ 22500

    /Ingvar

  13. #13


    Did you find this post helpful? Yes | No

    Default

    Thanks Ingvar or should I say "professor Math" !

    give it a try this evening

  14. #14


    Did you find this post helpful? Yes | No

    Default

    Thanks Dave,

    Can you point me how to use the LONGS because when reading the PBP manual (online version and PDF @ melabs) they say 16bit is the maximum
    If i try to compile more than 16bit it give's me an error (i upgraded to 250.B and use MPASM)
    Are we talking about the same PICbasic ? I use Picbasic PRO from melabs



    Quote Originally Posted by mackrackit View Post
    Post a little code and an example of the values you are getting.
    And maybe think about upgrading to 2.5. The LONGS are cool.

  15. #15
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Yes, we are talking about the same PBP. The online version of the manual is a few versions old.

    At the risk of saying you have an illegal copy, where is the printed manual you got when you upgraded?
    Dave
    Always wear safety glasses while programming.

  16. #16


    Did you find this post helpful? Yes | No

    Default

    Yes it is a risk !!!

    Manual is at home, i'm a registred user since the early beginning of PB long before PBP excists hihi

    I will have a look at home

  17. #17
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by RFsolution View Post
    Yes it is a risk !!!

    Manual is at home, i'm a registred user since the early beginning of PB long before PBP excists hihi

    I will have a look at home
    now I notice the date you joined this forum. Sorry. Never know though.
    Code:
    MYVAR  VAR  LONG
    Try this
    http://www.melabs.com/resources/articles/longs.pdf
    Dave
    Always wear safety glasses while programming.

  18. #18
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi RFS

    If i try to compile more than 16bit it give's me an error (i upgraded to 250.B and use MPASM)
    Are we talking about the same PICbasic ? I use Picbasic PRO from melabs
    you have to make a little change in MPLAB to have PBPLong to work ...

    you must select PBPL.exe as the "executable" in the "Language tools location" window, instead of PBPW ...


    Melabs SHOULD have provided a DLL to simplify that ... ( I asked them When, but got a "may be yes - may be no" answer from them ...

    LAZY People ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  19. #19
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S. View Post
    All this math makes my head hurt , couldn't you do the math twice with different multipliers, throw out the top numbers from one, and the bottom numbers from the other and display the results together separated by the decimal point ? Or is that what you just said?
    You could do what you suggested, but I split it up to display the math involved a bit more clearly...
    If I was me as I usually am, I would've put something like this instead:
    Code:
    dummy=(position<<8)*90:result_high=div32 1024:degrees_whole = result_high >> 6
    dummy=(position<<8)*9000:result_low=div32 1024:degrees_fraction=(result_low>>6)//100
    lcdout $fe,1,"Degrees:",DEC3 degrees_whole,":",DEC2 degrees_fraction

Similar Threads

  1. A/D conversion with PIC18F67J50
    By ScaleRobotics in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 8th May 2009, 01:48
  2. A/D conversion problem in 18F2520, 2523, 2550 etc.
    By selimkara in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 10th March 2008, 16:26
  3. Conversion problem
    By eva in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 15th March 2007, 18:21
  4. Help for decimal conversion
    By eva in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th March 2007, 18:20
  5. 16bit variable and degrees conversion
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 2nd May 2005, 17:27

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts