Drive a rover in a square in 2 minutes | Viam Rover

Drive a rover in a square in 2 minutes

In this guide you’ll write code that makes a rover drive in a square.

You will learn

Requirements

You don’t need to buy or own any hardware to complete this tutorial. You only need the following:

Instructions

Follow these steps to get your rover ready and write code to control it:

Step 1: Borrow a Viam Rover

Go to Try Viam and borrow a rover. If a rover is available, the rover will take up to 30 seconds to be configured for you.

Important

If you are using your own robot for this tutorial instead of borrowing one, be sure to follow the setup instructions and install viam-server on it, and connect and configure its hardware before proceeding with this tutorial.

Step 2: Install an SDK

Navigate to your machine’s CONNECT tab. Click on any of the listed languages and follow the instructions to install the SDK.

To install your preferred Viam SDK on your Linux or macOS development machine or single-board computer, run one of the following commands in your terminal:

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

For other unsupported systems, see Installing from source.

go get go.viam.com/rdk/robot/client
npm install @viamrobotics/sdk
flutter pub add viam_sdk

Follow the instructions on the GitHub repository.

Step 3: Copy and run the sample code

The sample code on the CONNECT tab 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.

Go to the CONNECT tab and select Python. Save your API key and API key ID as environment variables or include them in the code:

To show your machine’s API key in the sample code, toggle Include API key.

Caution: Keep your API key safe

We strongly recommend that you add your API key as an environment variable. Anyone with your API key can access your machine, and the computer running your machine.

Copy the code into a file called square.py and run the sample code to connect to your machine:

python3 square.py

The program prints an array of resources. These are the components and services that the machine is configured with on Viam.

python3 square.py
2024-08-09 13:21:52,423    INFO    viam.rpc.dial (dial.py:293)    Connecting to socket: /tmp/proxy-BzFWLZQ2.sock
Resources:
[<viam.proto.common.ResourceName rdk:service:sensors/builtin at 0x105b12700>, <viam.proto.common.ResourceName rdk:component:motor/left at 0x105b122a0>, <viam.proto.common.ResourceName rdk:component:camera/cam at 0x105b12390>, <viam.proto.common.ResourceName rdk:component:board/local at 0x105b129d0>, <viam.proto.common.ResourceName rdk:component:base/viam_base at 0x105b12610>, <viam.proto.common.ResourceName rdk:service:motion/builtin at 0x105b12a20>, <viam.proto.common.ResourceName rdk:component:encoder/Lenc at 0x105b12a70>, <viam.proto.common.ResourceName rdk:component:motor/right at 0x105b12ac0>, <viam.proto.common.ResourceName rdk:component:encoder/Renc at 0x105b12b10>]
Step 4: Drive a rover in a square

Now that you have connected the rover to Viam with the SDK, you can write code to move the rover in a square:

The base is responsible for controlling the motors attached to the base of the rover. Ensure that the Base class is being imported:

from viam.components.base import Base

Then paste this snippet above your main() function, it will move any base passed to it in a square:

async def moveInSquare(base):
    for _ in range(4):
        await base.move_straight(velocity=500, distance=500)
        print("move straight")
        await base.spin(velocity=100, angle=90)
        print("spin 90 degrees")

If you have a borrowed Try Viam rover, navigate to your machine’s CONTROL tab, which allows you to interact with your machine’s resources.

Click on one of the camera panels and toggle the camera stream on so you can observe the rover’s movements. Then run your code and watch your rover move in a square.

Complete code

This is the complete code for the tutorial:




Images