Hi all, a quick question.
How can I map 360 degrees?
So saying that X = 10 and Y = 20
How can I get the output of X-Y= 350 degrees?
Thanks a bunch
Hi all, a quick question.
How can I map 360 degrees?
So saying that X = 10 and Y = 20
How can I get the output of X-Y= 350 degrees?
Thanks a bunch
Have a look at the modulo operator (%) https://www.arduino.cc/en/Reference/Modulo
Different languages do different things when taking the modulo of a negative number. Make sure you test it to understand what it actually does in your system.
int X = 20;
int Y = 30;
int result = normalize(X-Y);
int normalize(int result) {
while (result < 0)
result += 360; // Take care of negative results
result %= 360;
result -= 360; // Normalize positive results
return result;
}
Cheers MorganS, Never even knew modulo existed.
John, I have tried to implement your code but I'm still getting a result of "-10". What am I missing?
int X = 20;
int Y = 30;
void setup() {
Serial.begin(9600);
}
void loop() {
int result = normalize(X-Y);
Serial.println(result);
}
int normalize(int result) {
while (result < 0)
result += 360; // Take care of negative results
result %= 360;
result -= 360; // Normalize positive results
return result;
}
Thanks!
Assuming that both x and y are always positive then this is what I would do
int result = (x - y + 360 ) % 360;
Oops. In my editing I left in a line that I should have deleted:
result -= 360; // Normalize positive results
Thanks guys, the code works seamlessly and I'm beginning to understand it.
I have yet stumbled across another problem.
I am working with a compass.
I want to be able to define a condition in this way.
int MyHeading; // Say its "North West 45degrees
int DestinationHeading;
If(MyHeading < DestinationHeading){
//Some Condition
}
else If(MyHeading > DestinationHeading){
//Some Other Condition
}
Now my problem is we are mapping 360 degrees, so 45 degrees anticlockwise to 270 degrees is still technically "<", but the code wont recognize this for obvious reasons.
Is there any way around this?
Cheers guys!
Is there any way around this?
Yes. Use a range of -180 to 180.
jstone:
Hi all, a quick question.How can I map 360 degrees?
So saying that X = 10 and Y = 20
How can I get the output of X-Y= 350 degrees?
Thanks a bunch
X - Y + 360?
There's an infinite number of ways to do what you have asked. What if X and Y aren't 10 and 20? What do you want X-Y to be equal to then?
PaulMurrayCbr:
What do you want X-Y to be equal to then?
X-Y must always subtract to be a true representation of subtracting degrees from a compass.
@johnwasser & @stowite's code works brilliantly.
PaulS has also showed that I can map 360 degrees to a range for the "<,>" operators to become effective.
I think I'm all set to write up my sketch and give it a shot.
Thank for the help everyone!
The modulo operator in most programming languages is broken by design I'm afraid - mathematically
a modulus is non-negative, but due to early (and bad) decisions in computer design the modulus operator
is as it is. You can fix this:
int modulus (a, b)
{
int result = a % b ;
return result < 0 ? result + b : result ;
}
For the +/- 180 range this can be done similarly:
int angle_diff (a, b)
{
int result = (a - b) % 360 ;
if (result < -180)
return result + 360 ;
if (result > 180)
return result - 360 ;
return result ;
}
You might want to disallow one of +180 and -180 as possible return values though, depending on the use.
MarkT:
The modulo operator in most programming languages is broken by design I'm afraid - mathematically
a modulus is non-negative, but due to early (and bad) decisions in computer design the modulus operator
is as it is.
The modulus operation is defined such that
a == (a/b) + (a%b);
That is, it's not a 'modulus' but a 'remainder'. Same thing, until you start working with negative numbers.
PaulMurrayCbr:
The modulus operation is defined such that [...]
The C/C++ modulus operation is... just to clarify.
Hi MarkT,
When i try to run your code:
MarkT:
For the +/- 180 range this can be done similarly:int angle_diff (a, b)
{
int result = (a - b) % 360 ;
if (result < -180)
return result + 360 ;
if (result > 180)
return result - 360 ;
return result ;
}
You might want to disallow one of +180 and -180 as possible return values though, depending on the use.
I get an error:
expression list treated as compound expression in initializer [-fpermissive]
Do you know what im getting wrong?
int a = 350;
int b = 30;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(result);
}
int anglediff (a, b)
{
int result = (a - b) % 360 ;
if (result < -180){
return result + 360 ;}
if (result > 180)
return result - 360 ;
return result ;
}
Cheers
In the definition of the anglediff function no types are specified for the parameters
In any case, you are not calling the anglediff() function in your program.
Hi UKHeliBob,
UKHeliBob:
In the definition of the anglediff function no types are specified for the parametersIn any case, you are not calling the anglediff() function in your program.
I have fixed the errors that you pointed out, to what i think should work but im still not error free.
int a = 350;
int b = 30;
void setup() {
Serial.begin(9600);
}
void loop() {
int result = anglediff(a-b);
Serial.println(result);
}
int anglediff (int a, int b)
{
int result = (a - b) % 360 ;
if (result < -180){
return result + 360 ;}
if (result > 180)
return result - 360 ;
return result ;
}
error:
too few arguments to function 'int anglediff(int, int)'
And even so this is what im trying to achieve, but cant see how the code will achieve this..
int MyHeading; // Say its "North West 45degrees
int DestinationHeading;
If(MyHeading < DestinationHeading){
//Some Condition
}
else If(MyHeading > DestinationHeading){
//Some Other Condition
}
Any ideas mate?
Cheers
Anglediff() expects two parameters. You have given it only one: the result of the expression a-b.
I think what you want is:
int MyHeading; // Say its "North West 45degrees
int DestinationHeading;
int headingDifference = anglediff(MyHeading, DestinationHeding);
if(headingDifference < 0) {
// Some Condition, like Turn Left
}
else if(headingDifference > 0) {
// Some Other Condition, like Turn Right
} else {
// Third condition because you are on the right heading already so stop turning!
}