com.qualcomm.robotcore.hardware

## Class HardwareMap

- java.lang.Object
- - com.qualcomm.robotcore.hardware.HardwareMap

- All Implemented Interfaces:
    java.lang.Iterable<HardwareDevice>

      public class HardwareMap
      extends java.lang.Object
      implements java.lang.Iterable<HardwareDevice>

  HardwareMap provides a means of retrieving runtime HardwareDevice instances according to the names with which the corresponding physical devices were associated during robot configuration.

  A HardwareMap also contains an associated application context in which it was instantiated. Through their `hardwareMap`, this provides access to a `Context` for OpModes, as such an appropriate instance is needed by various system APIs.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `class` | `HardwareMap.DeviceMapping<DEVICE_TYPE extends HardwareDevice>` A DeviceMapping contains a subcollection of the devices registered in a `HardwareMap` comprised of all the devices of a particular device type

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `HardwareMap.DeviceMapping<AccelerationSensor>` | `accelerationSensor`
    `java.util.List<HardwareMap.DeviceMapping<? extends HardwareDevice>>` | `allDeviceMappings`
    `protected java.util.List<HardwareDevice>` | `allDevicesList`
    `protected java.util.Map<java.lang.String,java.util.List<HardwareDevice>>` | `allDevicesMap`
    `HardwareMap.DeviceMapping<AnalogInput>` | `analogInput`
    `HardwareMap.DeviceMapping<AnalogOutput>` | `analogOutput`
    `Context` | `appContext`
    `HardwareMap.DeviceMapping<ColorSensor>` | `colorSensor`
    `HardwareMap.DeviceMapping<CompassSensor>` | `compassSensor`
    `HardwareMap.DeviceMapping<CRServo>` | `crservo`
    `HardwareMap.DeviceMapping<DcMotor>` | `dcMotor`
    `HardwareMap.DeviceMapping<DcMotorController>` | `dcMotorController`
    `HardwareMap.DeviceMapping<DeviceInterfaceModule>` | `deviceInterfaceModule`
    `protected java.util.Map<HardwareDevice,java.util.Set<java.lang.String>>` | `deviceNames`
    `HardwareMap.DeviceMapping<DigitalChannel>` | `digitalChannel`
    `HardwareMap.DeviceMapping<GyroSensor>` | `gyroSensor`
    `HardwareMap.DeviceMapping<I2cDevice>` | `i2cDevice`
    `HardwareMap.DeviceMapping<I2cDeviceSynch>` | `i2cDeviceSynch`
    `HardwareMap.DeviceMapping<IrSeekerSensor>` | `irSeekerSensor`
    `HardwareMap.DeviceMapping<LED>` | `led`
    `HardwareMap.DeviceMapping<LegacyModule>` | `legacyModule`
    `HardwareMap.DeviceMapping<LightSensor>` | `lightSensor`
    `protected java.lang.Object` | `lock`
    `HardwareMap.DeviceMapping<OpticalDistanceSensor>` | `opticalDistanceSensor`
    `HardwareMap.DeviceMapping<PWMOutput>` | `pwmOutput`
    `protected java.util.Map<SerialNumber,HardwareDevice>` | `serialNumberMap`
    `HardwareMap.DeviceMapping<Servo>` | `servo`
    `HardwareMap.DeviceMapping<ServoController>` | `servoController`
    `HardwareMap.DeviceMapping<TouchSensor>` | `touchSensor`
    `HardwareMap.DeviceMapping<TouchSensorMultiplexer>` | `touchSensorMultiplexer`
    `HardwareMap.DeviceMapping<UltrasonicSensor>` | `ultrasonicSensor`
    `HardwareMap.DeviceMapping<VoltageSensor>` | `voltageSensor`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `HardwareMap(Context appContext)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `<T> T` | `get(java.lang.Class<? extends T> classOrInterface, SerialNumber serialNumber)` (Advanced) Returns the device with the indicated `SerialNumber`, if it exists, cast to the indicated class or interface; otherwise, null.
    `<T> T` | `get(java.lang.Class<? extends T> classOrInterface, java.lang.String deviceName)` Retrieves the (first) device with the indicated name which is also an instance of the indicated class or interface.
    `HardwareDevice` | `get(java.lang.String deviceName)` Returns the (first) device with the indicated name.
    `<T> java.util.List<T>` | `getAll(java.lang.Class<? extends T> classOrInterface)` Returns all the devices which are instances of the indicated class or interface.
    `java.util.Set<java.lang.String>` | `getNamesOf(HardwareDevice device)` Returns all the names by which the device is known.
    `protected void` | `internalPut(SerialNumber serialNumber, java.lang.String deviceName, HardwareDevice device)`
    `java.util.Iterator<HardwareDevice>` | `iterator()` Returns an iterator of all the devices in the HardwareMap.
    `void` | `logDevices()`
    `void` | `put(SerialNumber serialNumber, java.lang.String deviceName, HardwareDevice device)` (Advanced) Puts a device in the overall map without having it also reside in a type-specific DeviceMapping.
    `void` | `put(java.lang.String deviceName, HardwareDevice device)` Puts a device in the overall map without having it also reside in a type-specific DeviceMapping.
    `protected void` | `rebuildDeviceNamesIfNecessary()`
    `protected void` | `recordDeviceName(java.lang.String deviceName, HardwareDevice device)`
    `boolean` | `remove(SerialNumber serialNumber, java.lang.String deviceName, HardwareDevice device)` (Advanced) Removes a device from the overall map, if present.
    `boolean` | `remove(java.lang.String deviceName, HardwareDevice device)` (Advanced) Removes a device from the overall map, if present.
    `int` | `size()` Returns the number of unique device objects currently found in this HardwareMap.
    `<T> T` | `tryGet(java.lang.Class<? extends T> classOrInterface, java.lang.String deviceName)` Retrieves the (first) device with the indicated name which is also an instance of the indicated class or interface.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface java.lang.Iterable

      `forEach, spliterator`

- - ### Field Detail

    - #### dcMotorController

          public HardwareMap.DeviceMapping<DcMotorController> dcMotorController

    - #### dcMotor

          public HardwareMap.DeviceMapping<DcMotor> dcMotor

    - #### servoController

          public HardwareMap.DeviceMapping<ServoController> servoController

    - #### servo

          public HardwareMap.DeviceMapping<Servo> servo

    - #### crservo

          public HardwareMap.DeviceMapping<CRServo> crservo

    - #### legacyModule

          public HardwareMap.DeviceMapping<LegacyModule> legacyModule

    - #### touchSensorMultiplexer

          public HardwareMap.DeviceMapping<TouchSensorMultiplexer> touchSensorMultiplexer

    - #### deviceInterfaceModule

          public HardwareMap.DeviceMapping<DeviceInterfaceModule> deviceInterfaceModule

    - #### analogInput

          public HardwareMap.DeviceMapping<AnalogInput> analogInput

    - #### digitalChannel

          public HardwareMap.DeviceMapping<DigitalChannel> digitalChannel

    - #### opticalDistanceSensor

          public HardwareMap.DeviceMapping<OpticalDistanceSensor> opticalDistanceSensor

    - #### touchSensor

          public HardwareMap.DeviceMapping<TouchSensor> touchSensor

    - #### pwmOutput

          public HardwareMap.DeviceMapping<PWMOutput> pwmOutput

    - #### i2cDevice

          public HardwareMap.DeviceMapping<I2cDevice> i2cDevice

    - #### i2cDeviceSynch

          public HardwareMap.DeviceMapping<I2cDeviceSynch> i2cDeviceSynch

    - #### analogOutput

          public HardwareMap.DeviceMapping<AnalogOutput> analogOutput

    - #### colorSensor

          public HardwareMap.DeviceMapping<ColorSensor> colorSensor

    - #### led

          public HardwareMap.DeviceMapping<LED> led

    - #### accelerationSensor

          public HardwareMap.DeviceMapping<AccelerationSensor> accelerationSensor

    - #### compassSensor

          public HardwareMap.DeviceMapping<CompassSensor> compassSensor

    - #### gyroSensor

          public HardwareMap.DeviceMapping<GyroSensor> gyroSensor

    - #### irSeekerSensor

          public HardwareMap.DeviceMapping<IrSeekerSensor> irSeekerSensor

    - #### lightSensor

          public HardwareMap.DeviceMapping<LightSensor> lightSensor

    - #### ultrasonicSensor

          public HardwareMap.DeviceMapping<UltrasonicSensor> ultrasonicSensor

    - #### voltageSensor

          public HardwareMap.DeviceMapping<VoltageSensor> voltageSensor

    - #### allDevicesMap

          protected java.util.Map<java.lang.String,java.util.List<HardwareDevice>> allDevicesMap

    - #### allDevicesList

          protected java.util.List<HardwareDevice> allDevicesList

    - #### deviceNames

          protected java.util.Map<HardwareDevice,java.util.Set<java.lang.String>> deviceNames

    - #### serialNumberMap

          protected java.util.Map<SerialNumber,HardwareDevice> serialNumberMap

    - #### allDeviceMappings

          public final java.util.List<HardwareMap.DeviceMapping<? extends HardwareDevice>> allDeviceMappings

    - #### appContext

          public final Context appContext

    - #### lock

          protected final java.lang.Object lock

  - ### Constructor Detail

    - #### HardwareMap

          public HardwareMap(Context appContext)

  - ### Method Detail

    - #### get

          public <T> T get(java.lang.Class<? extends T> classOrInterface,
                           java.lang.String deviceName)

      Retrieves the (first) device with the indicated name which is also an instance of the indicated class or interface. If no such device is found, an exception is thrown. Example:

              DcMotor motorLeft = hardwareMap.get(DcMotor.class, "motorLeft");
              ColorSensor colorSensor = hardwareMap.get(ColorSensor.class, "myColorSensor");

      Parameters:
        `classOrInterface` - the class or interface indicating the type of the device object to be retrieved
        `deviceName` - the name of the device object to be retrieved
      Returns:
        a device with the indicated name which is an instance of the indicated class or interface
      See Also:
        `get(String)`, `getAll(Class)`, `HardwareMap.DeviceMapping.get(String)`, `tryGet(Class, String)`

    - #### tryGet

          public <T> T tryGet(java.lang.Class<? extends T> classOrInterface,
                              java.lang.String deviceName)

      Retrieves the (first) device with the indicated name which is also an instance of the indicated class or interface. If no such device is found, null is returned. This is not commonly used; `get(java.lang.Class<? extends T>, java.lang.String)` is the usual method for retreiving items from the map.
      See Also:
        `get(Class, String)`

    - #### get

          public <T> T get(java.lang.Class<? extends T> classOrInterface,
                           SerialNumber serialNumber)

      (Advanced) Returns the device with the indicated `SerialNumber`, if it exists, cast to the indicated class or interface; otherwise, null.

    - #### get

          public HardwareDevice get(java.lang.String deviceName)

      Returns the (first) device with the indicated name. If no such device is found, an exception is thrown. Note that the compile-time type of the return value of this method is `HardwareDevice`, which is usually not what is desired in user code. Thus, the programmer usually casts the return type to the target type that the programmer knows the returned value to be:

              DcMotor motorLeft = (DcMotor)hardwareMap.get("motorLeft");
              ColorSensor colorSensor = (ColorSensor)hardwareMap.get("myColorSensor");

      Parameters:
        `deviceName` - the name of the device object to be retrieved
      Returns:
        a device with the indicated name.
      See Also:
        `get(Class, String)`, `HardwareMap.DeviceMapping.get(String)`

    - #### getAll

          public <T> java.util.List<T> getAll(java.lang.Class<? extends T> classOrInterface)

      Returns all the devices which are instances of the indicated class or interface.
      Parameters:
        `classOrInterface` - the class or interface indicating the type of the device object to be retrieved
      Returns:
        all the devices registered in the map which are instances of classOrInterface
      See Also:
        `get(Class, String)`

    - #### put

          public void put(java.lang.String deviceName,
                          HardwareDevice device)

      Puts a device in the overall map without having it also reside in a type-specific DeviceMapping.
      Parameters:
        `deviceName` - the name by which the device is to be known (case sensitive)
        `device` - the device to be stored by that name

    - #### put

          public void put(SerialNumber serialNumber,
                          java.lang.String deviceName,
                          HardwareDevice device)

      (Advanced) Puts a device in the overall map without having it also reside in a type-specific DeviceMapping.
      Parameters:
        `serialNumber` - the `SerialNumber` of the device
        `deviceName` - the name by which the device is to be known (case sensitive)
        `device` - the device to be stored by that name

    - #### internalPut

          protected void internalPut(SerialNumber serialNumber,
                                     java.lang.String deviceName,
                                     HardwareDevice device)

    - #### remove

          public boolean remove(java.lang.String deviceName,
                                HardwareDevice device)

      (Advanced) Removes a device from the overall map, if present. If the device is also present in a DeviceMapping, then the device should be removed using `HardwareMap.DeviceMapping.remove(java.lang.String)` instead of calling this method.

      This is normally called only by code in the SDK itself, not by user code.

      Parameters:
        `deviceName` - the name of the device to remove
        `device` - the device to remove under that name
      Returns:
        whether a device was removed or not

    - #### remove

          public boolean remove(SerialNumber serialNumber,
                                java.lang.String deviceName,
                                HardwareDevice device)

      (Advanced) Removes a device from the overall map, if present. If the device is also present in a DeviceMapping, then the device should be removed using `HardwareMap.DeviceMapping.remove(java.lang.String)` instead of calling this method.

      This is normally called only by code in the SDK itself, not by user code.

      Parameters:
        `serialNumber` - (optional) the serial number of the device
        `deviceName` - the name of the device to remove
        `device` - the device to remove under that name
      Returns:
        whether a device was removed or not

    - #### getNamesOf

          public java.util.Set<java.lang.String> getNamesOf(HardwareDevice device)

      Returns all the names by which the device is known. Virtually always, there is but a single name.
      Parameters:
        `device` - the device whose names are desired.
      Returns:
        the set of names by which that device is known

    - #### recordDeviceName

          protected void recordDeviceName(java.lang.String deviceName,
                                          HardwareDevice device)

    - #### rebuildDeviceNamesIfNecessary

          protected void rebuildDeviceNamesIfNecessary()

    - #### size

          public int size()

      Returns the number of unique device objects currently found in this HardwareMap.
      Returns:
        the number of unique device objects currently found in this HardwareMap.
      See Also:
        `iterator()`

    - #### iterator

          public java.util.Iterator<HardwareDevice> iterator()

      Returns an iterator of all the devices in the HardwareMap.
      Specified by:
        `iterator` in interface `java.lang.Iterable<HardwareDevice>`
      Returns:
        an iterator of all the devices in the HardwareMap.
      See Also:
        `size()`

    - #### logDevices

          public void logDevices()