liveBook · Manning

Map

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

A dictionary is a mapping data structure that associates keys with corresponding values.

A dictionary keeps values under named or numeric keys, making robot code easier to read and change. Use a key to retrieve the setting, hardware object, color, button name, or axis name that it represents. section 3.5 section 2.3 section 7.5.3

Use dictionaries to separate readable names from the values a program needs:

  • Store right and left motor power adjustments under 'R' and 'L', then retrieve both the adjustment and motor by the same name. section 3.5
  • Store RGB hexadecimal values under color names, then loop through the dictionary to create a multicolor LED effect. section 2.3
  • Map joystick button and axis numbers to readable labels; use get for a button or axis that has no defined name. section 7.5.3

Adjust motors by name

Keep the motor object and its calibration value in separate dictionaries keyed by 'R' and 'L'. A function receiving a motor name can select the object and multiply its throttle by the matching adjustment, letting one function control either wheel. section 3.5

Listing 3.2 envforward.py: Reading configuration values from environment variables
#!/usr/bin/env python3
from adafruit_crickit import crickit
import time
import os
 
DC_ADJUST_R = float(os.environ.get('ROBO_DC_ADJUST_R', '1'))
DC_ADJUST_L = float(os.environ.get('ROBO_DC_ADJUST_L', '1'))
ADJUST = dict(R=DC_ADJUST_R, L=DC_ADJUST_L)
MOTOR = dict(R=crickit.dc_motor_1, L=crickit.dc_motor_2)
 
def set_throttle(name, value):
    MOTOR[name].throttle = value * ADJUST[name]
 
def forward():
    set_throttle('R', 0.9)
    set_throttle('L', 0.9)
    time.sleep(0.2)
    set_throttle('R', 0)
    set_throttle('L', 0)
 
forward()

This runnable script combines environment-based adjustment values with ADJUST and MOTOR dictionaries, then drives both motors through a shared function. section 3.5

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Demonstrates creating dictionaries, indexing and get lookups, and iterating keys in runnable robot programs; does not discuss implementation internals.
Examples
Uses motor calibration maps, RGB color names for a blinking LED, and button and axis number maps for joystick output.
What you can do after
Apply it via Python dictionaries to retrieve robot configuration, hardware, and human-readable event labels by key.
How it connects
Connects dictionaries with environment variables, DC motors, Neopixel LEDs, joystick events, namedtuple, and KeyError.

Also explained in

Compare how 42 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.