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
















































