Compass: 359 to 0 degrees

I am trying to move the robot with a compass. We use a compass to make the robot move in a straight line, it uses 2 wheels, and they are slightly different from each other. Therefore, we set the value from 0 to 359 in the direction, and then check the current direction, calculate the error and fix it. Like error = current_direction is the actual direction.

The problem is that if, for example, our init direction is 90 degrees, and our robot is 45, the error will be 45, and this 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 little more than 0, and, 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 error sign to decide which wheel to move to fix the direction. any idea how to fix it?

+6
robotics mindstorms compass-geolocation lego nxt
source share
4 answers
if (error > 180) { error -= 360; } if (error < -180) { error += 360; } 
+6
source share

if your error is greater than 180 °, you must set it to 360 and invert the sign. By doing this, you can be sure that your robot will always move in the shortest direction.

+2
source share

If your error is> 180 degrees, just switch your correction algorithm to calculate the correction, moving in the opposite direction. A simple if-else statement should do.

+1
source share

I know little about NXT and Mindstorm, but basically this is a common problem in circular motions. You could just use two different coordinate systems and translate between each other, this is the most elegant way. Otherwise, you can subtract 360 from your error if the sign is negative, but this is a hack, not an elegant way to solve the problem; -)

+1
source share

All Articles