Skip to content
Tactiverge

School/○ Innovator/Week 22

Choosing a sample rate for a real robot

Sampling at 10 kHz is safe but drains the battery. Sampling at 20 Hz is cheap but might lie. How do you pick?

About 40 min · 3 goals · Checked by automated tests

1.Why should I care?

In a real robot, every sensor competes for processor time, bus bandwidth and battery. Good engineers sample each signal just fast enough, with the right filter in front, and can explain the choice to the rest of the team.

  • ◇ Choose a sample rate from the signal's highest frequency, the filter available and the system's budget.
  • ◇ Demonstrate aliasing in code and show that a pre-filter or faster sampling removes it.
  • ◇ Estimate the data and processing cost of a sampling choice.

2.Discover

In the Code Lab, generate one second of a 3 Hz 'tilt' plus a 47 Hz 'vibration'. Sample it at 50 Hz and at 500 Hz and print the dominant frequency you see in each. Then apply a 9-sample moving average to the 500 Hz data before keeping every 10th sample. What happened to the vibration?

Can't use the interactive? Read a description instead

Code Lab task, described: a 3 Hz tilt plus a 47 Hz vibration sampled at 50 Hz shows a false 3 Hz component from the vibration (|47 − 50| = 3 Hz) on top of the real tilt, so the two are confused. At 500 Hz both frequencies are seen correctly. Averaging 9 samples at 500 Hz before keeping every 10th sample strongly reduces the 47 Hz vibration, leaving the 3 Hz tilt.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

A heart-rate sensor's useful content is below 8 Hz, and an anti-alias filter removes everything above 15 Hz. Which sample rate is the sensible minimum?

4.Apply it

Write a one-paragraph sampling plan for a line-following robot with a light sensor array (line edges pass at up to 40 Hz at full speed) and a motor current sensor (PWM ripple at 20 kHz). Give each a sample rate, a filter, and the data rate.

5.Understand

Start from the frequencies

List what the signal contains: the frequencies you want (the tilt, the heartbeat, the wall getting closer) and the ones you don't (motor vibration, mains hum at 50 or 60 Hz, switching noise). The sample rate must exceed twice the highest frequency that reaches the sampler, wanted or not.

Filter, then sample

An analogue low-pass filter in front of the converter cuts unwanted high frequencies so they can't alias. A common digital pattern is oversample, filter, decimate: sample fast, low-pass filter digitally, then keep every Mth sample. The filter must run on the fast samples, before you throw any away.

Count the cost

Data rate = channels × sample rate × bytes per sample. Six 16-bit channels at 1 kHz is 12 kB/s, about 43 MB an hour. Processing cost scales the same way. Control loops also need the delay between sample and action to be small compared with the system's response time.

Explain it another way

Before photographing a race with a slow camera, close the curtains on the flashing lights behind it: remove what you can't sample properly, then sample what's left.

Worked example 1

Mains hum at 50 Hz leaks into a sensor sampled at 52 Hz. What appears?

|50 − 52| = 2 Hz: a slow, convincing 2 Hz wobble. Either filter the hum out before sampling, sample far faster (e.g. 1 kHz) and filter digitally, or choose fₛ so the hum is easy to spot and remove.

Common mix-ups

  • Choose the sample rate from the signal you want only.
    ✓ Everything that reaches the sampler counts, including vibration and interference you don't want.
  • Keeping every 10th sample is a harmless way to save memory.
    ✓ It is sampling at a tenth of the rate and aliases unless you filter first.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A 46 Hz vibration is sampled at 20 Hz with no anti-aliasing filter. At what frequency does it appear in the samples, in Hz?

Hz

Practice 3

Four 12-bit sensors (stored as 2 bytes each) sampled at 400 Hz. How many bytes per second?

B/s

Practice 4

Mains hum at 50 Hz is sampled at 45 Hz. At what frequency does it appear, in Hz?

Hz

Practice 5

You sample at 1 kHz and want to keep only every 10th sample. When must you low-pass filter?

7.Predict

A balance controller must react to tilts that change at up to 5 Hz, but the IMU also picks up motor vibration at 180 Hz. Which plan is safest?

Guess first. Being wrong here is how the surprise works.

8.Reflect

Concepts: Sampling rate and aliasing · Real-time computing on robots · Analog-to-digital conversion