Drones and self-driving basicsMiddleLesson 2828 min read

Sensors: GPS, IMU, cameras and lidar

Every sensor is good at some things and bad at others. Learn what each one measures, where it fails, and why machines combine several of them.

Lesson in motion

In 60 seconds

Sensors: GPS, IMU, cameras and lidar

Every sensor is good at some things and bad at others. Learn what each one measures, where it fails, and why machines combine several of them.

1/4
In simple words
Close your eyes and stand on one foot. It is hard, but your inner ear still tells you if you are tipping. Now open your eyes: much easier, because your eyes and inner ear work together. Machines do the same. Each sensor is a bit wrong in its own way, and together they give a better answer. This is called sensor fusion.
A drone or a self-driving car cannot see the world directly. It only has numbers from its sensors, and every sensor has noise (small random errors), bias (a steady error in one direction) or blind spots. Knowing these weaknesses is half of robotics.

The main sensors

SensorWhat it measuresGood atWeak at
GPS / satellite navigationPosition on Earth from satellite signalsWorks anywhere with open sky; error does not grow over timeOften a few metres off; worse near tall buildings and trees; does not work indoors or in tunnels; updates only a few times a second
IMU: accelerometerAcceleration, including the pull of gravityKnows which way is down when the machine is stillVery noisy with vibration; cannot tell tilt from speeding up
IMU: gyroscopeHow fast it is turningSmooth and fast; great for short momentsA small bias adds up over time, so the angle slowly drifts
Magnetometer (compass)Earth's magnetic fieldGives a headingConfused by motors, steel, wires and buildings
BarometerAir pressure, used for heightCheap height estimateChanges with weather and wind
CameraImages: colour, texture, signs, lane linesRich detail; cheap; can read signsNeeds light; struggles with glare, darkness, fog and rain; depth is hard from one camera
LidarDistance, by timing pulses of laser lightAccurate 3D shape; works in the darkCostly; heavy rain, fog and dust reduce range; glass and very dark surfaces can confuse it
RadarDistance and speed, using radio wavesWorks in rain, fog and dark; measures speed directlyLow detail; hard to tell what an object is
UltrasonicDistance, using sound echoesCheap; fine for very short range, like parkingShort range; soft or angled surfaces give weak echoes
Note
India has its own regional satellite navigation system, NavIC, built by ISRO. Some phones and receivers can use it along with GPS and other systems. Using several satellite systems together often gives a better fix.

Why one sensor is never enough

Look at the gyroscope and the accelerometer. The gyroscope is smooth, but it drifts: a tiny bias, added up every moment, slowly turns into a large error. The accelerometer does not drift, because gravity always points down, but it jumps around with every vibration from the motors. Their weaknesses are opposite. That makes them perfect partners.

Averaging: the simplest fusion

If readings have random noise and no bias, averaging several readings of the same thing reduces the noise. With four readings, the noise shrinks to about half. With sixteen, to about a quarter. Averaging does not remove bias: if every reading is 2 degrees too high, the average is also 2 degrees too high.
Python: averaging reduces random noise, not biaspython
import numpy as np

rng = np.random.default_rng(1)
true_value = 20.0          # say, a true tilt of 20 degrees
noise, bias = 3.0, 0.0     # try bias = 2.0 as well

for n in [1, 4, 16, 64]:
    # 2000 experiments, each averaging n readings
    readings = true_value + bias + rng.normal(0, noise, size=(2000, n))
    averages = readings.mean(axis=1)
    print(f"average of {n:2d} readings: typical error {averages.std():.2f}, "
          f"mean error {averages.mean() - true_value:+.2f}")

The complementary filter

A complementary filter blends the gyroscope and the accelerometer. At each step it takes the last angle, adds the gyroscope's turn, and then nudges the result a little towards the accelerometer's angle:
angle = alpha × (last angle + gyro rate × dt) + (1 − alpha) × accelerometer angle
With alpha close to 1, such as 0.98, the smooth gyroscope decides the short-term changes, and the accelerometer slowly pulls the angle back so it cannot drift away. Flight controllers use this idea or its big brother, the Kalman filter, which also keeps track of how uncertain each estimate is.
Weak design
  • Trust one sensor completely
  • Assume a sensor's number is the truth
  • Test only on a sunny day in an open field
Better design
  • Combine sensors whose weaknesses are different
  • Model noise and bias, and check them
  • Test in dark, rain, dust, near buildings and with vibration
Real example
A student team in Pune builds a line-following robot car with a camera. It works well in their lab. In the college corridor, afternoon sunlight makes bright patches on the floor, and the car keeps losing the line. They add a cheap distance sensor and slow down when the camera is unsure. The lesson: test where the machine will really run.
Try this. In the averaging code, set bias = 2.0 and run it again. Which number gets smaller as you average more, and which one does not?

Watch and read more

Lab

An average function and a working complementary filter that beats both the gyroscope and the accelerometer alone.

~25 min

The problem

This runs in your browser with made-up sensor data: a drone rocking gently, a gyroscope with a small bias, and a very noisy accelerometer.

  1. Finish average(readings).
  2. Finish complementary_filter: at each step, blend gyro_guess with the accelerometer angle using alpha.
  3. Run it and compare the three errors printed at the end. The filter should beat both sensors alone.
  4. Try alpha = 0.5 and 0.999. Write one sentence on what goes wrong at each end.

Practice lab

Edit the code and run it. Python runs in your browser — nothing is sent to our server.

Output

No output. Add print(...) to see values.

You are done when

Hard questions

Try to answer before you reveal. If you can answer these, you understood the lesson.

Q1Why can the accelerometer not be trusted for tilt when the drone speeds up or brakes?Reveal

An accelerometer feels gravity and the drone's own acceleration mixed together. When the drone speeds up, the extra push looks just like a tilt. When it is still or moving steadily, only gravity is left, so the tilt reading is right. That is why the filter uses it only as a slow correction, not for quick changes.

Q2How would you choose alpha for a real drone, not made-up data?Reveal

Record real flights, ideally with a reference such as a more accurate sensor or a motion-capture setup, and try several values. Look at the trade-off: lower alpha follows the accelerometer's vibration noise; higher alpha lets gyroscope drift grow. The best value depends on the sample rate, vibration and how big the gyroscope bias is. A Kalman filter can adjust this balance automatically from noise estimates.

Please sign in to continue.

Questions people ask

What does IMU stand for?

Inertial measurement unit. It usually combines an accelerometer and a gyroscope, and often a magnetometer. A small IMU chip is inside most phones, which is how your phone knows when you turn it sideways.

Why not use only lidar, if it is so accurate?

Lidar gives good shape and distance, but it cannot read a traffic light's colour or a sign's text, it is costly, and bad weather reduces its range. Cameras add colour and meaning; radar adds speed and bad-weather range. Different teams choose different mixes, and each choice has trade-offs.

Can a phone be used as a robot's sensor?

Yes, for learning. A phone has an IMU, a camera and satellite navigation. Several free apps can stream or log these readings, which is a cheap way to see real noise and drift for yourself.

What is the difference between noise and bias?

Noise is random: sometimes too high, sometimes too low. Averaging helps. Bias is steady: always off in the same direction. Averaging does not help; you need calibration or another sensor to correct it.

Lesson test

5 questions. Get 3 right (60%) to pass and complete this lesson.

Sign in to take the test and save your progress