viam.components.board.board

Module Contents

Classes

Board

Board represents a physical general purpose compute board that contains various

class viam.components.board.board.Board(name: str)[source]

Bases: viam.components.component_base.ComponentBase

Board represents a physical general purpose compute board that contains various components such as analog readers, and digital interrupts.

This acts as an abstract base class for any drivers representing specific board implementations. This cannot be used on its own. If the __init__() function is overridden, it must call the super().__init__() function.

from viam.components.board import Board
class AnalogReader(name: str)[source]

AnalogReader represents an analog pin reader that resides on a Board.

name: str

The name of the analog reader

abstract async read(*, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs) int[source]

Read the current value.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.gpio_pin_by_name(name="15")

# Get if it is true or false that the pin is set to high.
duty_cycle = await pin.get_pwm()

# Get the AnalogReader "my_example_analog_reader".
reader = await my_board.analog_reader_by_name(
    name="my_example_analog_reader")

# Get the value of the digital signal "my_example_analog_reader" has most
# recently measured.
reading = reader.read()
Returns:

The current value.

Return type:

int

class DigitalInterrupt(name: str)[source]

DigitalInterrupt represents a configured interrupt on the Board that when interrupted, calls the added callbacks. Post processors can be added to modify what Value it ultimately returns.

name: str

The name of the digital interrupt

abstract async value(*, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs) int[source]

Get the current value of the interrupt, which is based on the type of interrupt.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt = await my_board.digital_interrupt_by_name(
    name="my_example_digital_interrupt")

# Get the amount of times this DigitalInterrupt has been interrupted with a
# tick.
count = await interrupt.value()
Returns:

The current value.

Return type:

int

class GPIOPin(name: str)[source]

Abstract representation of an individual GPIO pin on a board

name: str

The name of the GPIO pin

abstract async set(high: bool, *, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs)[source]

Set the pin to either low or high.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.gpio_pin_by_name(name="15")

# Set the pin to high.
await pin.set(high="true")
Parameters:

high (bool) – When true, sets the pin to high. When false, sets the pin to low.

abstract async get(*, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs) bool[source]

Get the high/low state of the pin.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.gpio_pin_by_name(name="15")

# Get if it is true or false that the state of the pin is high.
high = await pin.get()
Returns:

Indicates if the state of the pin is high.

Return type:

bool

abstract async get_pwm(*, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs) float[source]

Get the pin’s given duty cycle.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.gpio_pin_by_name(name="15")

# Get if it is true or false that the state of the pin is high.
duty_cycle = await pin.get_pwm()
Returns:

The duty cycle.

Return type:

float

abstract async set_pwm(duty_cycle: float, *, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs)[source]

Set the pin to the given duty_cycle.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.gpio_pin_by_name(name="15")

# Set the duty cycle to .6, meaning that this pin will be in the high state for
# 60% of the duration of the PWM interval period.
await pin.set_pwm(cycle=.6)
Parameters:

duty_cycle (float) – The duty cycle.

abstract async get_pwm_frequency(*, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs) int[source]

Get the PWM frequency of the pin.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.gpio_pin_by_name(name="15")

# Get the PWM frequency of this pin.
freq = await pin.get_pwm_frequency()
Returns:

The PWM frequency.

Return type:

int

abstract async set_pwm_frequency(frequency: int, *, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs)[source]

Set the pin to the given PWM frequency (in Hz). When frequency is 0, it will use the board’s default PWM frequency.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.gpio_pin_by_name(name="15")

# Set the PWM frequency of this pin to 1600 Hz.
high = await pin.set_pwm_frequency(frequency=1600)
Parameters:

frequency (int) – The frequency, in Hz.

SUBTYPE: Final
abstract async analog_reader_by_name(name: str) AnalogReader[source]

Get an AnalogReader by name.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the AnalogReader "my_example_analog_reader".
reader = await my_board.analog_reader_by_name(name="my_example_analog_reader")
Parameters:

name (str) – Name of the analog reader to be retrieved.

Returns:

The analog reader.

Return type:

AnalogReader

abstract async digital_interrupt_by_name(name: str) DigitalInterrupt[source]

Get a DigitalInterrupt by name.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the DigitalInterrupt "my_example_digital_interrupt".
interrupt = await my_board.digital_interrupt_by_name(
    name="my_example_digital_interrupt")
Parameters:

name (str) – Name of the digital interrupt.

Returns:

the digital interrupt.

Return type:

DigitalInterrupt

abstract async gpio_pin_by_name(name: str) GPIOPin[source]

Get a GPIO Pin by name.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the GPIOPin with pin number 15.
pin = await my_board.gpio_pin_by_name(name="15")
Parameters:

name (str) – Name of the GPIO pin.

Returns:

the pin.

Return type:

GPIOPin

abstract async analog_reader_names() List[str][source]

Get the names of all known analog readers.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the name of every AnalogReader configured on the board.
names = await my_board.analog_reader_names()
Returns:

The names of the analog readers..

Return type:

List[str]

abstract async digital_interrupt_names() List[str][source]

Get the names of all known digital interrupts.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the name of every DigitalInterrupt configured on the board.
names = await my_board.digital_interrupt_names()
Returns:

The names of the digital interrupts.

Return type:

List[str]

abstract async status(*, extra: Dict[str, Any] | None = None, timeout: float | None = None, **kwargs) viam.proto.common.BoardStatus[source]

Return the current status of the board.

my_board = Board.from_robot(robot=robot, name="my_board")

# Get the current status of the board.
status = await my_board.status()
Returns:

the status.

Return type:

viam.proto.common.BoardStatus

abstract async set_power_mode(mode: viam.proto.component.board.PowerMode.ValueType, duration: datetime.timedelta | None = None, *, timeout: float | None = None, **kwargs)[source]

Set the board to the indicated power mode.

my_board = Board.from_robot(robot=robot, name="my_board")

# Set the power mode of the board to OFFLINE_DEEP.
status = await my_board.set_power_mode(mode=PowerMode.POWER_MODE_OFFLINE_DEEP)
Parameters:

mode – the desired power mode

abstract async write_analog(pin: str, value: int, *, timeout: float | None = None, **kwargs)[source]

Write an analog value to a pin on the board.

my_board = Board.from_robot(robot=robot, name="my_board")

# Set pin 11 to value 48.
await my_board.write_analog(pin="11", value=48)
Parameters:
  • pin (str) – name of the pin.

  • value (int) – value to write.

classmethod from_robot(robot: viam.robot.client.RobotClient, name: str) typing_extensions.Self

Get the component named name from the provided robot.

Parameters:
  • robot (RobotClient) – The robot

  • name (str) – The name of the component

Returns:

The component, if it exists on the robot

Return type:

Self

abstract async do_command(command: Mapping[str, ValueTypes], *, timeout: float | None = None, **kwargs) Mapping[str, ValueTypes]

Send/Receive arbitrary commands to the Resource

command = {"cmd": "test", "data1": 500}
result = component.do(command)
Parameters:

command (Mapping[str, ValueTypes]) – The command to execute

Raises:

NotImplementedError – Raised if the Resource does not support arbitrary commands

Returns:

Result of the executed command

Return type:

Mapping[str, ValueTypes]

async get_geometries(*, extra: Dict[str, Any] | None = None, timeout: float | None = None) List[viam.proto.common.Geometry]

Get all geometries associated with the Component, in their current configuration, in the frame of the Component.

geometries = await component.get_geometries()

if geometries:
    # Get the center of the first geometry
    print(f"Pose of the first geometry's centerpoint: {geometries[0].center}")
Returns:

The geometries associated with the Component.

Return type:

List[Geometry]

classmethod get_resource_name(name: str) viam.proto.common.ResourceName

Get the ResourceName for this Resource with the given name

# Can be used with any resource, using an arm as an example
my_arm_name = my_arm.get_resource_name("my_arm")
Parameters:

name (str) – The name of the Resource

get_operation(kwargs: Mapping[str, Any]) viam.operations.Operation

Get the Operation associated with the currently running function.

When writing custom resources, you should get the Operation by calling this function and check to see if it’s cancelled. If the Operation is cancelled, then you can perform any necessary (terminating long running tasks, cleaning up connections, etc. ).

Parameters:

kwargs (Mapping[str, Any]) – The kwargs object containing the operation

Returns:

The operation associated with this function

Return type:

viam.operations.Operation

async close()

Safely shut down the resource and prevent further use.

Close must be idempotent. Later configuration may allow a resource to be “open” again. If a resource does not want or need a close function, it is assumed that the resource does not need to return errors when future non-Close methods are called.

await component.close()