[Part 1 begins a look at how to interface with some common robotics-type sensors and actuators, and how these can be tied into an example system (in this case, the author's E-2 Autonomous Submarine Project). Part 2 considers the design of the circuit and coding for the project's stepper motor controller. Part 3 describes the development of the circuit and coding for the speed-controlled DC motor for the project's propulsion system.]
3.6 Two-Axis Attitude Sensor using MEMS Accelerometer
For a variety of reasons; navigation, hazard avoidance, and so on, it's desirable for a vehicle to be able to know its orientation with respect to the earth. A ship, submarine or airplane has six degrees of freedom (land-bound vehicles generally have fewer). Three of these are rotational: rotation around an imaginary line from bow to stern (roll), rotation around an imaginary line perpendicular to the bow-stern line and parallel to the Earth's surface (pitch), and rotation in a plane parallel to the Earth's surface (yaw; turning the bow of the vehicle to point towards a new destination). The other three are translational, along the same axes just mentioned; respectively, surge (movement forwards or backwards), sway (movement from side to side) and heave (movement up or down).
Yaw is relatively difficult to measure directly, so let's discuss it first. One approach is to use a flux-gate sensor; an electronic compass, essentially. The difficulty with this is that every spot on the Earth's surface has more or less interference from local metallic deposits and other geographical features, so a compass needle doesn't always point at a known reference point (magnetic north). Magnetic north also moves about, and it doesn't coincide with the true geographic north pole.
For short trips, a fixed variance setting can be looked up on a map of your area, and you can just ignore any errors caused by roaming about close to vast lodestone deposits! E-2's core electronic module doesn't directly measure yaw; it assumes that most of the vessel's motion vector is parallel to the bow-to-stern axis and hence uses GPS velocity data (while surfaced) to infer the direction the bow is pointing. If you want to try your hand at magnetic navigation methods, there are numerous kits containing flux-gate compass boards, intended for the hobbyist robotics market. Most of these incorporate some clever firmware to deal with variance issues.
Static roll and pitch, on the other hand, can easily be ascertained by measuring the gravity vector acting on the craft and comparing it to an imaginary reference vector at right angles to both the bow-stern and port-starboard axes of the vehicle. To perform this measurement task, we use an accelerometer.
At its simplest, an attitude or acceleration sensor is simply a pendulum. In fact, a reasonably useful two-dimensional attitude sensor can be constructed by simply taking a two-axis potentiometer assembly out of an off-the-shelf analog joystick, attaching a heavy weight to the joystick lever, and mounting the whole thing upsidedown so that the weighted joystick can swing around freely. (Pay attention to align the axes of the sensor with the axes around which the sensor is expected to rotate).
In cases where fine accuracy is not essential and it is desirable to connect this sensor directly to a PC, cannibalizing a joystick in this way is definitely the path of least resistance, not to mention an extremely fast way to construct a prototype. Apropos of the E-2 project, it is interesting to note that attitude and depth control in torpedoes of World War II vintage were actually controlled using a mechanically interlinked system of a pendulum and a manometer.
Despite its simplicity, there are a number of disadvantages to the simple pendulum method - it is bulky, and friction in the potentiometers and joystick bearings tends to makes the device insensitive to small accelerations. A better solution for some applications is to use free-turning weights or gyroscopes on exquisitely low-friction bearings, with some sort of optical or magnetic scale read-out, but these sorts of machines are expensive and relatively high-maintenance.