roboglia.base.BaseDevice

class BaseDevice(name='DEVICE', bus=None, dev_id=None, model=None, path=None, inits=[], **kwargs)[source]

Bases: object

A base virtual class for all devices.

A BaseDevice is a surrogate representation of an actual device, characterized by a number of internal registers that can be read or written to by the means of a comunication bus. Any device is based on a model that identifies the .yml file describing the structure of the device (the registers).

Parameters
  • name (str) – The name of the device

  • bus (BaseBus or subclass) – The bus object where the device is attached to

  • id (int) – The device ID on the bus. Typically it is an int but some buses may use a different identifier. The processing should still work fine.

  • model (str) – A string used to identify the device description. Please see the note bellow regarding the position of the device description files.

  • path (str) – A path to the model file in case you want to use custom defined devices that are not available in the roboglia repository. Please see the note bellow regarding the position of the device description files.

  • inits (list) –

    A list of init templates to be applied to the device’s registers when the open() method is called, where template names were defined earier in the robot definition in the inits section. Please note the initialization values should be provided in the external format of the register as they will be used as:

    register.value = dict_value
    

    As no syncs are currently implemented this will automatically trigger a write call to store that value in the device.

Raises

KeyError – if mandatory parameters are not found or unexpected values are used (ex. for boolean)

cache = {}

A chache of device models that is updated when a new model is encountered and reused when the same model is requested during device creation.

__init__(name='DEVICE', bus=None, dev_id=None, model=None, path=None, inits=[], **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

property name

Device name.

Returns

The name of the device

Return type

str

property registers

Device registers as dict.

Returns

The dictionary of registers with the register name as key.

Return type

dict

register_by_address(address)[source]

Returns the register identified by the given address. If the address is not available in the device it will return None.

Returns

The device at address or None if no register with that address exits.

Return type

BaseDevice or subclass or None

property dev_id

The device number.

Returns

The device number

Return type

int

property bus

The bus where the device is connected to.

Returns

The bus object using this device.

Return type

BaseBus or SharedBus or subclass

get_model_path()[source]

Builds the path to the device description documents.

By default it will return the path to the roboglia/base/devices/ directory.

Returns

A full document path.

Return type

str

default_register()[source]

Default register for the device in case is not explicitly provided in the device definition file.

Subclasses of BaseDevice can overide the method to derive their own class.

BaseDevice suggests as default register BaseRegister.

read_register(register)[source]

Implements the read of a register using the associated bus. More complex devices should overwrite the method to provide specific functionality.

BaseDevice simply calls the bus’s read function and returns the value received.

write_register(register, value)[source]

Implements the write of a register using the associated bus. More complex devices should overwrite the method to provide specific functionality.

BaseDevice simply calls the bus’s write function and returns the value received.

open()[source]

Performs initialization of the device by reading all registers that are not flagged for sync replication and, if init parameter provided initializes the indicated registers with the values from the init paramters.

close()[source]

Perform device closure. BaseDevice implementation does nothing.

__str__()[source]

Return str(self).