Viam SDK
    Preparing search index...

    Interface Encoder

    Represents a physical encoder.

    interface Encoder {
        name: string;
        doCommand(command: Struct | Record<string, JsonValue>): Promise<JsonValue>;
        getPosition(
            positionType?: PositionType,
            extra?: Struct,
        ): Promise<readonly [number, PositionType]>;
        getProperties(extra?: Struct): Promise<encoderApi.GetPropertiesResponse>;
        getStatus(): Promise<JsonValue>;
        resetPosition(extra?: Struct): Promise<void>;
    }

    Hierarchy (View Summary)

    Implemented by

    Index

    Properties

    name: string

    The name of the resource.

    Methods

    • Send/Receive arbitrary commands to the resource.

      Parameters

      • command: Struct | Record<string, JsonValue>

        The command to execute. Accepts either a Struct or a plain object, which will be converted automatically.

      Returns Promise<JsonValue>

      // Plain object (recommended)
      const result = await resource.doCommand({
      myCommand: { key: 'value' },
      });

      // Struct (still supported)
      import { Struct } from '@viamrobotics/sdk';

      const result = await resource.doCommand(Struct.fromJson({ myCommand: { key: 'value' } }));
    • Return the current position either in relative units (ticks away from a zero position) or absolute units (degrees along a circle).

      Parameters

      • OptionalpositionType: PositionType

        The type of position the encoder returns (ticks or degrees)

      • Optionalextra: Struct

      Returns Promise<readonly [number, PositionType]>

      const encoder = new VIAM.EncoderClient(machine, 'my_encoder');

      // Get the position of the encoder in ticks
      const [position, posType] = await encoder.getPosition(
      EncoderPositionType.POSITION_TYPE_TICKS_COUNT,
      );
      console.log('The encoder position is currently', position, posType);

      For more information, see Encoder API.

    • Set the current position of the encoder as the new zero position.

      Parameters

      Returns Promise<void>

      const encoder = new VIAM.EncoderClient(machine, 'my_encoder');

      // Reset the zero position of the encoder
      await encoder.resetPosition();

      For more information, see Encoder API.