Write and deploy code to control your machine

Once you have configured a machine, start writing code to perform actions with the components or services of a machine.

Viam has SDKs for Python, Golang, C++, TypeScript, and Flutter.

After configuring your resource, navigate to your machine’s CONNECT tab. Click on any of the listed languages and follow the instructions to install the SDK.

Error

Prerequisites

A running and configured machine. Click to see instructions.
Follow the instructions in Build simple smart machines.

Write code to control your machine

1. Install an SDK

The easiest way to get started is to go to the Code sample page of the CONNECT tab on your machine’s page in the Viam app.

Follow the instructions there to install your preferred Viam SDK on your Linux or macOS development machine or single-board computer:

If you are using the Python SDK, set up a virtual environment to package the SDK inside before running your code, avoiding conflicts with other projects or your system.

For macOS (both Intel x86_64 and Apple Silicon) or Linux (x86, aarch64, armv6l), run the following commands:

python3 -m venv .venv
source .venv/bin/activate
pip install viam-sdk

Windows is not supported. If you are using Windows, use the Windows Subsystem for Linux (WSL) and install the Python SDK using the preceding instructions for Linux. For other unsupported systems, see Installing from source.

If you intend to use the ML (machine learning) model service, use the following command instead, which installs additional required dependencies along with the Python SDK:

pip install 'viam-sdk[mlmodel]'
go get go.viam.com/rdk/robot/client
npm install @viamrobotics/sdk
flutter pub add viam_sdk

2. Copy the connection code

With the SDK installed, copy the code sample from the Code sample page of the CONNECT tab on your machine’s page.

The sample code will show you how to authenticate and connect to a machine, as well as some of the methods you can use on your configured components and services.

You can run this code directly on the machine or from a separate computer.

3. Use component and service APIs

Viam’s APIs are standardized across all models of a given component or service. This means that regardless of the configured hardware, you use the same APIs.

For example, you can send the same SetPower command to any kind of motor, using any of the available SDKs:

my_motor = Motor.from_robot(robot=robot, name="my_motor")
# Set the power to 40% forwards.
await my_motor.set_power(power=0.4)
myMotorComponent, err := motor.FromRobot(machine, "my_motor")
// Set the motor power to 40% forwards.
myMotorComponent.SetPower(context.Background(), 0.4, nil)
final base = Motor.fromRobot(client, "my_motor");
// Set the power to  40% forwards.
await myMotor.setPower(0.4);
const myMotor = new VIAM.MotorClient(client, "my_motor");
// Set the power to  40% forwards.
await myMotor.setPower(0.4);
std::shared_ptr<Motor> motor = robot->resource_by_name<Motor>("my_motor");
// Set the power to  40% forwards.
motor->set_power(0.4);

See component APIs and service APIs for a full list of available API methods.

Run your code

There are two different ways you can run code to control your machine:

1. Run code remotely or on your machine

You can run the code to control your machine on any computer where you have an SDK installed.

If the computer that viam-server runs on has enough compute power, you can also configure your machine to run your code as a managed process whenever it boots. See Processes for more information.

2. (Recommended) Wrap your code in a module

Once you have written code to control your machine and tested it, you can then wrap your custom functionality by creating a module. In wrapping your code into a module, you will be able to:

  • deploy it across one or more machines
  • use it in fragments
  • version it

You can package any files, code, or executable into a module. When you add a module to your machine’s configuration, the entrypoint defined for the module is run which can start your code.

If the code you have written augments what a component does, such as, for example, adding an overlay to a camera stream, you can create your own camera model inside your module and amend the API methods to have your custom functionality. For an example of this, see the facial-detection module which wraps its logic into a custom vision service.

If your functionality does not conform to existing API types such as the motor or camera API, you can use a generic API to wrap your code.

For more information, see How to create and deploy a new module.

Next steps

Now that you have a running machine that you can control, make your machine better and smarter:

To see full sample projects, that configure and control machines, check out these tutorials:

Have questions, or want to meet other people working on robots? Join our Community Discord.

If you notice any issues with the documentation, feel free to file an issue or edit this file.