Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compass: from 359 to 0 degrees

I am trying to move a robot using a compass. We use the compass to make the robot move in straight line, it uses 2 wheels and they move a bit different. So we set a value between 0 and 359 as direction, and then check the current direction, calculate the error and fix it. Like error = current_direction - actual direction.

The problem is that if for example our init direction is 90 degrees and our robot is at 45, the error will be 45 and it will fix it. If it is 0, the error will be 90 and it will fix it. The problem is that if it moves a bit more than 0 and it goes for example to 359, the error will be -269 so instead of moving 90 in one direction it will move -269 in the other.

I use the sign of the error to decide which wheel to move to fix the direction. any idea how to fix it?

like image 760
Dr Sokoban Avatar asked Apr 05 '11 12:04

Dr Sokoban


People also ask

How to calculate degree compass?

How to Calculate Bearing. A bearing angle is determined by measuring the clockwise angle between two points. Measure clockwise from the northern point on a compass to the point at which the point in question rests. If the angle is between north and east on the compass, it'll measure between 0 degrees and 90 degrees.

What are the degrees of a compass?

The compass has three basic parts: the base plate, rotating housing, and the magnetic needle. The housing rotates so you can set the degree dial where you need it. There is 360 degrees on the dial. North is 0 or 360 degrees and south is 180 degrees.

How do you read a compass degree and minutes?

Degrees, minutes and seconds are denoted by the symbols °, ', ". e.g. 10° 33' 19" means an angle of 10 degrees, 33 minutes and 19 seconds . A degree is divided into 60 minutes (of arc), and each minute is divided into 60 seconds (of arc).


2 Answers

if (error > 180) {
   error -= 360;
}

if (error < -180) {
   error += 360;
}
like image 135
Piskvor left the building Avatar answered Sep 24 '22 07:09

Piskvor left the building


if your error is greater than 180°, you should rack it from 360 and invert the sign. Doing so, you can be sure your robot will always move in the shortest direction.

like image 45
Tokk Avatar answered Sep 20 '22 07:09

Tokk