可以沿北/南或东/西轴移动,因为旋转限制为90度。在
您可以观察到,任何移动都将具有北/南分量和东/西分量。在
如果你的动作一致,那么你离下一步只有90度的距离:1. turn east or west
2. move east or west
3. turn north or south
4. move north or south
5. You should be at your target
6. turn east or west
7. move east or west
8. turn north or south
9. move north or south
10. you should be at your (next) target
。。。等等
如果我们假设你的机器人开始朝北,那么你的回路应该先转向东/西,然后移动,然后转向北/南。在
开始吧。这可能是您的全局数据和主代码。在
^{pr2}$
如果我们假设x是东/西,y是北/南,那么对于move_robot,你可以得到这样的结果:def move_robot(new_pos):
"""
Move robot from ``Robot_pos`` to ``new_pos`` given. The robot
is assumed to be facing either north or south upon entry, so
east/west movement is done first.
"""
delta_x = ...
turn_robot(EAST or WEST)
forward( some amount )
# similarly for Y
你必须在你的turn_robot()代码中稍微聪明一点,以优化你开始时是朝着积极还是消极的方向转弯。不过,它应该总是一个90度的旋转。在