Getting a sense of direction

In order to navigate your robot around the environment, you will need to keep track of which way your robot is facing. You can estimate the angle that your robot turns at by measuring the angle that it turned at in a fixed time period. For wheeled robots, you can also measure the rotation of each wheel using a rotary encoder (a device that provides a count of the wheel's rotations). However, as you make the robot take multiple turns, the direction the robot is facing becomes more and more uncertain as differences in the surfaces and the grip of the wheels or legs cause differences in the angles that the robot is turning at.

Fortunately, we can use an electronic version of a compass; it allows us to determine the direction ...

Get Raspberry Pi for Python Programmers Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.