liveBook · Manning

Map

As explained in Robotics for Programmers

A lookup table is a data structure that associates input values or keys with precomputed output values.

A dictionary acts as a lookup table: it associates an input key with a precomputed output value. In robot sensing, voltage readings can be converted into distance values by discretizing voltage and indexing an array. section 3.2.1

Converting sensor voltage to distance

A voltage-to-distance table can map 0.4 V to 0.7 m, 0.5 V to 0.6 m, and so on. Sensor code rounds the measured voltage, uses the result as an index, and returns the stored distance. section 3.2.1

Listing 3.3 ​
voltage_to_distance = [0.0] * 32
voltage_to_distance[0] = 0.8  # 0.0 V -> 0.8 m
...
voltage_to_distance[4] = 0.7  # 0.4 V -> 0.7 m
voltage_to_distance[5] = 0.6  # 0.5 V -> 0.6 m
voltage_to_distance[6] = 0.5  # 0.6 V -> 0.5 m
voltage_to_distance[7] = 0.4  # 0.7 V -> 0.4 m
...
voltage_to_distance[31] = 0.05  # 3.1 V -> 0.05 m

def distance():
  discrete_voltage = round(input.value() * 10)
  distance = voltage_to_distance[discrete_voltage]
  return distance

Inspect the compact indexed-array implementation of the voltage-to-distance lookup table. section 3.2.1

Use lookup tables carefully in physical sensing:

  • Fill entries from the sensor’s measurement diagram, and account for nonlinear sensitivity across the measurement range. section 3.2.1
  • Add bounds checking and consider interpolation or greater resolution instead of relying on a manually populated, coarse table. section 3.2.1

How Robotics for Programmers covers it

How deep it goes
Defines indexed lookup and demonstrates implementation, while stopping short of production-ready error handling and interpolation.
Examples
Uses a voltage-to-distance array derived from an analog optical distance sensor’s measurement diagram.
What it assumes
Assumes familiarity with analog voltage and distance measurements, while demonstrating the conversion method directly.
What you can do after
Build it from scratch by discretizing sensor input, indexing stored values, and returning converted measurements.
How it connects
Connects analog input, sensor calibration, nonlinear measurement functions, resolution, and bounds checking.

Also explained in

Compare how 49 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Robotics for Programmers. The section links point to the parts of the book it draws on.