开发者

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 lin开发者_JS百科e, 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?


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

if (error < -180) {
   error += 360;
}


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.


If your error is > than 180 degree simply switch your correction algorithm to calculate the correction by moving in the opposite direction. A simple if-else statement should do.


I don't know much about NXT and Mindstorm, but basically it is a common problem in circular motions. You could simply use two different coordinate systems and translate between each other, thats the most elegant way. Otherwise you could subtract 360 from your error, if the sign is negative, but that's a hack and not an elegant way to solve the problem ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜