I am working on a project where i have to translate the X and Y positions to a single rotation degree.
Thankfully there is a form of math atan2(x,y) developed for this, however i stumbled uppon a problem with this method on arduino.
I've written a basic code(which i will post bellow) to test this method but found out that it doesn't work the way i think it should work. If i turn my joystick around and insert their values (converted to -Pi to Pi) the end result of atan2(x,y) only results in either PI of 0, nothing inbetween or bellow.
I've tested the math but now with firmata on arduino and run it in processing and it works like a charm the way it should. So my best bet is that some floats/doubles get converted along the way somewhere in arduino, but i am not that familiour with the deeper code of arduino so I hope you guys can help me with that.
Why map the values to PI/-PI? the whole point of atan2 is that you don't need to do this - it works on the ratio between the two values. Just subtract 512 to get zero where it needs to be.
Ok let me completely reformulate the question because we are going on details while the problem remains:
Problem: When i use atan2(a,b) and insert floats i expect to get values that change as a and b change but instead they eighter give me 0 or PI nothing inbetween and nothing inbetween 0 and -PI either.
My Question: Is this the same with you guys? If you use a arduino and insert 2 potentiometers and add them both into a atan2 function what are your results?
No. When I call atan2() with good data, I get a good result.
That you don't seem to means that there is still a problem in your code. Prove that you've fixed the problems that have already been pointed out, that you dismissed as irrelevant. Prove that the data that you pass to atan2 IS good data. Prove that, given good data, atan2() returns bad results.
BartZuidervaart:
Ok let me completely reformulate the question because we are going on details while the problem remains:
Problem: When i use atan2(a,b) and insert floats i expect to get values that change as a and b change but instead they eighter give me 0 or PI nothing inbetween and nothing inbetween 0 and -PI either.
My Question: Is this the same with you guys? If you use a arduino and insert 2 potentiometers and add them both into a atan2 function what are your results?
You need to ISOLATE the problem.
You are getting garbage output and you think it's the fault of atan2(). I assure you, this is a classic case of GIGO (Garbage In. Garbage Out.).
You THINK you are getting good numbers from the pots. You think you are putting those numbers into Degrees properly. As was asked earlier, what is your serial.print giving you for Degrees[0] and Degrees[1], and are you putting those numbers into atan2() correctly (again, not Degrees[1] and Degrees[2], but Degrees[0] and Degrees[1])?
*I guarantee you are not putting the numbers in there you think you are. *
I am an idiot.
That was it guys; atan2(Degrees[2],Degrees[1]) should have been atan2(Degrees[1],Degrees[0]), thank you very much for your help.
Sorry for the stubborness.
BartZuidervaart:
I am an idiot.
That was it guys; atan2(Degrees[2],Degrees[1]) should have been atan2(Degrees[1],Degrees[0]), thank you very much for your help.
Sorry for the stubborness.
Do you understand why it's 0 and 1? In C++, array indexes are an offset from the start of the array. The first element in an array is at offset zero.
PaulMurrayCbr:
Do you understand why it's 0 and 1? In C++, array indexes are an offset from the start of the array. The first element in an array is at offset zero.
Yeah i understand, for some reason i was completely blind to the Degrees[2] standing there.