Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Calculating ARCTAN in Oshonsoft

Status
Not open for further replies.

camerart

Well-Known Member
Hi,
I've been given an equasion by JJW, and would like to try it in Oshonsoft:
'##############################################################################
You calculate angle in radians from x, y coordinates.
Angle in degrees = radians × 180 / Pi
~ radians × 57.3
Oshonsoft has arctan function for y / x < 1
You have to calculate in eight octants ( 45 degrees )
Octants can be found from signs of x and y and from
value of y / x if it is less or greater than one.

For example, if North is 0 degrees and clockwise direction:
0 - 45 degrees x / y < 1 and x > 0, y > 0
angle = arctan( x / y ) × 57.3

45 - 90 degrees x/y > 1 x >0 y>0
angle = 90 - arctan( y / x ) × 57.3
etc ...

Angle, x and y are floating point variables
'#################################################################################
Is this correct?

Between 0° to 45°
If x/y < 1 then
If x >0 then
If y > 0 then
angle = arctan( x / y ) × 57.3
ENDIF
ENDIF
ENDIF

Corrected.
Cheers, Camerart.
 
Last edited:
It is not correct.
Between 0-45 , x < y
Follow the example I gave.

The arctan function in Oshonsoft is not accurate.
The error is over 4 degrees near arctan(1)

You can use the approximation which was used in the thread 1 - 1.5 year ago:
Atn(x/y) = x/y / (1+0.285125(x/y)^2)
when x/y <= 1
Accuracy is better than 0.3 degrees
 
It is not correct.
Between 0-45 , x < y
Follow the example I gave.

The arctan function in Oshonsoft is not accurate.
The error is over 4 degrees near arctan(1)

You can use the approximation which was used in the thread 1 - 1.5 year ago:
Atn(x/y) = x/y / (1+0.285125(x/y)^2)
when x/y <= 1
Accuracy is better than 0.3 degrees
Hi J,
When you say "Follow the example I gave" I presume you mean the one in #1, is this correct?
If so, then that was my attempt to write the first line of it. I don't understand the language your example is written in.

You say I can use the approximation from 1.5 years ago, but it has ATN, will it work in Oshonsoft?

(I apologies for being slow on my understanding)

C.
 
It is OK now after the change x/y < 1
If x/y < 1 can be written if x<y

The first example 0-45 degrees.
x, y and angle are floating point variables

If x < y then
If x>0 AND y >0 then ''' have not tested if this is ok
angle = arctan(x/y)*57.3
endif
endif
 
Hi J,
When you say "Follow the example I gave" I presume you mean the one in #1, is this correct?
If so, then that was my attempt to write the first line of it. I don't understand the language your example is written in.

You say I can use the approximation from 1.5 years ago, but it has ATN, will it work in Oshonsoft?

(I apologies for being slow on my understanding)

C.
ATN is just a name for the approximation function, which is defined in your program.
You can use any name which is not a reserved word in Oshonsoft.
For example arcustanget or whatever.
 
It is OK now after the change x/y < 1
If x/y < 1 can be written if x<y

The first example 0-45 degrees.
x, y and angle are floating point variables

If x < y then
If x>0 AND y >0 then ''' have not tested if this is ok
angle = arctan(x/y)*57.3
endif
endif

Hi J,
Can this section be used independantly?
I tried it and it complies ok, but only gives '0'
I presume the section shown as 2'S COMPLIMENT CALC IS NEEDED?

Note: I've just seen your later comment below, so another reply on it's way.
C.
 

Attachments

  • 0 TO 45.jpg
    0 TO 45.jpg
    156.4 KB · Views: 327
Arctan() wants floating point ( single) variables.
Hi J,
Arctan is an Oshonsoft reserved word.
I'm using 'arctn' as suggested, which isn't a reserved word.

This line: azi = arctn(x_raw / y_raw) * picalc '57.3 'ARCTN Not reserved word
gives this error. Attached.

NOTE: Oshonsoft has floating point SINGLEs. I've DIMed 'arctn' and 'picalc' (57.3) as SINGLE.

C.
 

Attachments

  • Too many.jpg
    Too many.jpg
    58.6 KB · Views: 333
What is picalc?
Still x_raw, y_raw should be floating point.
Hi J,

Will you clarify my questions in #6 please?

azi, X, Z, Y_raw and picalc all DIMed as LONG

I haven't been able to add 57.3 into the program, so I tried 'picalc' = 57.3. It didn't work!
EDIT: ERROR!! LONG is a mistake SINGLE should be used in some of the cases.
C.
 
Last edited:
2's complement is needed.
Floating point for x and y is needed, because if you divide x by y and x < y the result is zero when x, y are integers.
 
Last edited:
2's complement is needed.
Floating point for x and y is needed, because of you divide x by y and x < y the result is zero when x, y are integers.
Hi J,
Ok, thanks.

I found that using 'Const pi = 3.14159' and 'DIM picalc as SINGLE' and 'picalc = 180/pi' Compiles.

At the moment this section 0 to 45 is compiling, but not giving sensible results, so I'll recheck everything.

Will this section 0 to 45° work on it's own?

C.
 
Hi J,
Ok, thanks.

I found that using 'Const pi = 3.14159' and 'DIM picalc as SINGLE' and 'picalc = 180/pi' Compiles.

At the moment this section 0 to 45 is compiling, but not giving sensible results, so I'll recheck everything.

Will this section 0 to 45° work on it's own?

C.
What kind of results you get ?
Give some examples with x, y and the result.
 
What kind of results you get ?
Give some examples with x, y and the result.
Hi J,
Here are the DIMs and the section of program.

Since changing X, Y_raw to SINGLEs from WORD, I'm getting AZIMUTH = 0.0 in all directions.

Looking at the 5110 screen, the YWORD shows nothing.

C.
 

Attachments

  • DIM.jpg
    DIM.jpg
    183.8 KB · Views: 306
  • COMP.jpg
    COMP.jpg
    171.7 KB · Views: 318
  • YWORD.jpg
    YWORD.jpg
    129.4 KB · Views: 315
Hi J,
I tried changing Y_RAW back to WORD, and it now shows correctly on 5110 Scn, and AZIMUTH now shows '-- numbers.

I can see the problem. There needs to be some kind of intermediate set of words, between WORDs and SINGLEs.
C.
 
I don't see any arctan function in the program?
x and y as arguments to the function should be singles unless you will calculate with longs or words and handle all scaling back and forth.
 
Convert the x_raw, y_raw and all variables needed in calculation of arctan to singles.
It is much easier to calculate with them.
 
You have defined x_raw, y_raw as single, but use x_raw.hb x_raw.lb ??
Use x_raw ... as word, x as single and after reading x_raw and making 2's complement
x = x_raw
same for y and then calculate arctan
 
Hi J,
I tried changing Y_RAW back to WORD, and it now shows correctly on 5110 Scn, and AZIMUTH now shows '-- numbers.

I can see the problem. There needs to be some kind of intermediate set of words, between WORDs and SINGLEs.
C.
No intermediates are needed.
 
Hi J,
Firstly (following #2 and#5) do I use 'arctn' which is my none Oshonsoft reserved word, or Arctan which is an Oshonsoft function?

This does compile: azi = Arctan(x_raw / y_raw) * picalc '180/pi

This doesn't compile: azi = arctn(x_raw / y_raw) * picalc '180/pi

The x and y_raw HB and LB are used because the Compass module is READ in 6x BYTEs, also I use them to show the BYTEs as they change on the screen.
EDITED.

C.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top