PDA

View Full Version : Conversion to degrees help



RFsolution
- 3rd September 2008, 18:05
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

Acetronics2
- 3rd September 2008, 18:13
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

RFsolution
- 3rd September 2008, 18:18
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

Archangel
- 3rd September 2008, 19:00
Hello Walter,
Look at this/these posts:
http://www.picbasic.co.uk/forum/showthread.php?t=9011
http://www.picbasic.co.uk/forum/showthread.php?t=8355

RFsolution
- 4th September 2008, 22:20
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

Archangel
- 4th September 2008, 22:39
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 :)

mackrackit
- 4th September 2008, 23:34
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. :)

skimask
- 5th September 2008, 05:26
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...


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

Archangel
- 5th September 2008, 06:49
This might get you 2 behind the point...


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?

RFsolution
- 5th September 2008, 09:37
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




This might get you 2 behind the point...


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

mackrackit
- 5th September 2008, 09:42
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.

Ingvar
- 5th September 2008, 12:16
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

RFsolution
- 5th September 2008, 14:38
Thanks Ingvar or should I say "professor Math" !

give it a try this evening

RFsolution
- 5th September 2008, 14:49
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




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. :)

mackrackit
- 5th September 2008, 17:02
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?

RFsolution
- 5th September 2008, 17:18
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

mackrackit
- 5th September 2008, 17:44
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.


MYVAR VAR LONG

Try this
http://www.melabs.com/resources/articles/longs.pdf

Acetronics2
- 5th September 2008, 17:59
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

skimask
- 5th September 2008, 18:03
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:


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