November 26th, 2018, 04:11 PM  #1 
Member Joined: Jan 2016 From: Uk Posts: 93 Thanks: 2  X,Y into 360 Degrees
Hi, I'm trying to write eight Octants for a program written in Oshonsoft BASIC for a PIC chip that READs a Magnetometer X and Y, but I'm stuck. Here are eight Octants, copied and pasted from the first one. (They are in the correct format for OSH BASIC) The first one works and is not COMMENTED OUT with ' I may have edited the second one ok, I think. Can someone edit the remaining six for me please? Camerart. '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''' '044 If y_c > 0 Then If x_c > 0 Then If x_c / y_c < 1 Then deg = Arctan(x_c / y_c) * 57.3 Endif Endif Endif '4589 'If y_c > 0 Then 'If x_c > 0 Then 'If x_c / y_c > 1 Then 'deg = 90  Arctan(y_c / x_c) / 57.3 'Endif 'Endif 'Endif '90134 'If y_c > 0 Then 'If x_c > 0 Then 'If x_c / y_c > 1 Then 'deg = 90  Arctan(y_c / x_c) / 57.3 'Endif 'Endif 'Endif '135179 'If y_c > 0 Then 'If x_c > 0 Then 'If x_c / y_c > 1 Then 'deg = 90  Arctan(y_c / x_c) / 57.3 'Endif 'Endif 'Endif '180224 'If y_c > 0 Then 'If x_c > 0 Then 'If x_c / y_c > 1 Then 'deg = 90  Arctan(y_c / x_c) / 57.3 'Endif 'Endif 'Endif '225269 'If y_c > 0 Then 'If x_c > 0 Then 'If x_c / y_c > 1 Then 'deg = 90  Arctan(y_c / x_c) / 57.3 'Endif 'Endif 'Endif '270314 'If y_c > 0 Then 'If x_c > 0 Then 'If x_c / y_c > 1 Then 'deg = 90  Arctan(y_c / x_c) / 57.3 'Endif 'Endif 'Endif '315359 'If y_c > 0 Then 'If x_c > 0 Then 'If x_c / y_c > 1 Then 'deg = 90  Arctan(y_c / x_c) / 57.3 'Endif 'Endif 'Endif 
November 26th, 2018, 05:15 PM  #2 
Senior Member Joined: Sep 2015 From: USA Posts: 2,296 Thanks: 1221 
we can't really help w/o specifications on how the BASIC Arctan function operates. What is it's domain? What is it's range? 
November 27th, 2018, 12:43 AM  #3  
Member Joined: Jan 2016 From: Uk Posts: 93 Thanks: 2  Quote:
This is almost a mystery to me, but I've just remembered that the X and Y are in the format of the attached image: If I understand correctly the range of each signwave is +32760 to 32760 The DATA is in 2's compliment, which is also almost a mystery. C. Last edited by camerart; November 27th, 2018 at 12:49 AM.  
November 27th, 2018, 05:11 AM  #4 
Global Moderator Joined: Dec 2006 Posts: 20,262 Thanks: 1958 
What did you mean by "the first one works"? What angle are you calculating?

November 27th, 2018, 06:01 AM  #5  
Member Joined: Jan 2016 From: Uk Posts: 93 Thanks: 2  Quote:
Note the ' before each line in the 1st 0 to 44 octant. This means that I have all of the TXT in a program, with 0 to 44 DEG working, all other Octants are commented ' out, so pending. I've since found out that Octants are for X Y Z DATA from a Magnetometer. As I only need X and Y, only Quadrants are needed. I want the program to convert the Magnetometer X and Y DATA, which is 2's compliment, and convert it into 0 to 359Deg as I rotate the Magnetometer. C.  
November 27th, 2018, 07:41 AM  #6 
Global Moderator Joined: Dec 2006 Posts: 20,262 Thanks: 1958 
How did you check whether the results for the first octant are correct? As you rotate it in which direction? You don't check whether the value you use in the denominator is zero. You don't need to use 90. Is the angle you wish to calculate the same as the angle represented on the horizontal axis of the graph you posted? 
November 27th, 2018, 08:15 AM  #7  
Member Joined: Jan 2016 From: Uk Posts: 93 Thanks: 2  Quote:
1/2 By rotating it CCW, then I added a negate into the program, so now I can turn it CW. The results from the first Octantis incorrect, as zero isn't zero, and 0 to 44 shows 0 to 41DEG. 3/ I don't understand denominator. I've learnt that Octant is for the Z component, an as I don't need this at the moment, Quadrans are fine. The graph was in response to another question, which explains what the X an Y are. I want the module to act as a Compass, so as it rotates, it gives 0 to 360. C.  
November 27th, 2018, 10:27 AM  #8 
Member Joined: Jan 2016 From: Uk Posts: 93 Thanks: 2 
Hi, Here is a post from an electronic forum I'm on, which looks interesting. (again note, this is a bit advanced for me) C. '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''' " I found an approximation of arctan, which works in entire quadrant. ( 090 degrees ) https://www.embedded.com/design/othe...approximation "There are rational approximations that are defined in an entire quadrant. Although they are more complex, their advantage is that they not require to discriminate the octant. This is for x in [0, Infinity). When x is negative just use atan_approx(x). Second order: atan_approx(x) = (Pi/2)*(b*x + x*x)/(1 + 2*b*x + x*x) where b = 0.596227, with a maximum approximation error of 0.1620º " 
November 27th, 2018, 10:31 PM  #9 
Global Moderator Joined: Dec 2006 Posts: 20,262 Thanks: 1958  The denominator of x_c / y_c is y_c. The denominator of y_c / x_c is x_c. You cannot divide by zero, so you need to ensure that the denominator isn't zero. I suggest you use Arctan(y_c / x_c) * 57.3 consistently, but if x_c is zero, use 90. You may need to use Arctan(y_c / x_c) * 57.3 consistently to make the result consistent with the graph, but do not use 90  Arctan(y_c / x_c) * 57.3. You may need to add 180 or 360 to the initial result, depending on the signs of x_c and y_c. 
November 28th, 2018, 01:22 AM  #10  
Member Joined: Jan 2016 From: Uk Posts: 93 Thanks: 2  Quote:
I can see that you have given a good explanation, but I'm afraid my with limitations I won't be able to actually write out each case, but thanks for your efforts. I will have to search for an existing sequence, I'm sure there are some. Cheers, C.  

Tags 
360, 360degrees, degrees 
Thread Tools  
Display Modes  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Why 79 degrees instead of 101 degrees?  LBOlson  Trigonometry  3  December 8th, 2015 02:03 PM 
dividing 3.14 (or pi) by 180 degrees  hansolo  Algebra  6  May 2nd, 2013 12:10 AM 
Cp at 15 million degrees  Setsuna  Physics  1  September 28th, 2012 08:35 AM 
degrees and bearings, am i right?  mathslog  Algebra  2  April 29th, 2012 12:25 PM 
Why 360 degrees is 2π?  Skyer  Algebra  2  September 12th, 2011 10:15 AM 