Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
You have commented out the function abs ( ....Hi J,
I've made a TEST program:
C.
Hi J,You have commented out the function abs ( ....
Variable arg belongs to abs function, don't use it outside the function.
Hi J,Abs is not needed now, but do you want to calculate the angles with words or longs ?
dim sx,sy,comp as byte
dim ux,uy,degrees as word
if x_raw>32767 then
ux=65535-x_raw
ux=ux+1
sx=1
else
ux=x_raw
sx=0
endif
if y_raw>32767 then
uy=65535-y_raw
uy=uy+1
sy=1
else
uy=y_raw
sy=0
endif
if ux>uy then
degrees=uy*45/ux
oct=1
else
degrees=ux*45/uy
oct=0
endif
comp=0
if degrees>22
if degrees<44
comp=comp+1
endif
if degrees<41
comp=comp+1
endif
if degrees<37
comp=comp+1
endif
if degrees<32
comp=comp+1
endif
else
if degrees>2
comp=comp+1
endif
if degrees>6
comp=comp+1
endif
if degrees>10
comp=comp+1
endif
if degrees>15
comp=comp+1
endif
endif
degrees=degrees+comp
if oct>0
degrees=90-degrees
endif
if sy>0
if sx>0
degrees=180+degrees
else
degrees=180-degrees
endif
else
if sx>0
degrees=360-degrees
endif
endif
Ok, lets try again.
Assuming x_raw and y_raw are 16 bit variables read directly from the compass then we need to split them into their component parts.
sx=1 if x_raw was negative else 0 and ux is the positive value of x_raw. Ditto y values.
Now we have these parts we can use them to calculate an angle.
This produces a word variable between 0 and 359.
After the above degrees will contain the answer.
Mike.
Edit, the above code is not self explanatory. It is written this way to avoid any floating point numbers and trigonometry functions.
Hi M,Don't have time to check it now but looks like I messed up the quadrant part of the code. Will check tomorrow - Saturday night here, time to party.
I have a GY-271 board and, if it's flat, X and Y give the compass bearing.
Mike.
Edit, just noticed ux & uy are bytes - they should be words.
In the initial code I posted I multiply by 32 and adjust so a full circle is 256 degrees. The ux, sx bit is due to the limitations of oshonsoft - it can't handle negative integers!!
Due to your comment I've just realised that an overflow may be taking place (even more so if times 128). My compass module never returns a value greater than 1000 so overflow isn't a problem, the OPs compass may be different and cause errors due to overflow. Cam, do you have actual values from your compass?
Mike.
Hi B,Pommie,
Nice code !! .... I'm working on something very similar in Assembly
in your initial stage ux, and uy can be determined by XORing the MSB with a "1"
in your second stage, instead of using 45 to multiply the lowest number and dividing by the highest number, I am multiplying by 128 which is a simple shift left in Assembly language. Division is done by subtraction until there is an underflow. The result is used against a lookup table which effectively does what you have accomplished in a series of IF statements. The lookup table allows for at least 1/2 a Deg resolution.