Product
February 26, 2024

Use feedback control with Viam components

Written by
Esha Maharishi
Product Manager

One of the many interesting parts of smart machines is how they interact with the physical world. 

We often want to give our machine a command such as “go forward at 5 meters per second.” This will then need to be translated to an amount of power to apply on the motors.

However, depending on the conditions around the machine (Is the machine on an incline or decline? Is it in an area of high friction, like sand, or a very smooth surface?), the amount of power needed to achieve that velocity will differ.

If the machine has a sensor that allows it to determine how fast it is actually moving, that can be provided as feedback to the part of its system that is determining how much power to apply.

This is the main idea behind “feedback control”, which is now available on the ‘GPIO’ model of Viam’s Motor component and ‘sensor-controlled’ model of Viam’s Base component.

How feedback control works

At a high level, a feedback control loop involves a few basic parts:

  • A target value you would like the system to achieve (like a velocity of 5 m/s)
  • An output whose aim is to achieve the target value (like spinning the motors at 50% power)
  • A sensor that measures the actual value being achieved (like wheel encoders that determine the machine is actually moving at 4.8 m/s)
  • A “controller” that takes the difference between the target and actual values (also called the “error”) and computes a correction to the output (like spinning the motors at 53% power)

Though there are many ways to implement the “controller,” one of the most common is a “PID” control loop, where “PID” stands for “Proportional, Integral, Derivative.”

As its name suggests, a PID control loop involves looking at the error through three terms, each multiplied by a coefficient:

  • A “proportional” term that is simply the current error
  • An “integral” term that is the total cumulative error
  • A “derivative” term that is the rate of change of the error

By tuning the coefficients on each of these terms, you can adjust how the system converges towards the target value: how quickly the system reaches the target value and how much the system overshoots when approaching the target value.

Viam components and feedback control

The Motor component has the API methods GoFor (which lets you control how many revolutions you want the motor to turn, and at what velocity), and GoTo (which lets you control what position you want the motor to turn to, and at what velocity).

The Base component has the API method SetVelocity, which lets you specify the desired linear and angular velocity of the base.

Under the hood, these methods all end up calling SetPower on the machine’s motors.

Before PID control, we actually had a more simple form of control for the ‘GPIO’ model of Motor that raised or reduced the power by 10% until the desired velocity was reached. However, this would result in somewhat jerky behavior due to the stepwise increase or decrease, alongside a possible constant change between moving slightly too fast and slightly too slow.

With PID control, the controller will compute much more fine-grained adjustments to the power, resulting in smoothly approaching the target velocity and maintaining it once it’s achieved.

How to get started with PID control

Setting up PID control with Viam components can be done in two steps.

First, you need to set up a movement sensor that will provide the feedback.

For the ‘GPIO’ model of the Motor component, set up wheel encoders that report the distance traveled. For the ‘sensor-controlled’ model of the Base component, create a Movement Sensor component that reports the current velocity.

Second, you need to set the p, i, and d values.

You can test out different values for p, i, and d yourself, or you can set all the values to 0, which will cause viam-server to spin the motors for several seconds on startup and log suggested values for p, i, and d.

See the docs for ‘GPIO’ motors with encoders and ‘sensor-controlled’ base for more info.

Try feedback control with Viam components out

Feedback control is a powerful way to make your machine’s interaction with the world more accurate, and PID control in particular allows for smoothly adjusting to environmental factors.

To illustrate this more vividly, I created a short demo video that explains how you can start taking advantage of Viam’s feedback control feature now:

Viam is free to use, and we invite you to try this out for yourself. If you’re ready to start using PID control in your smart machine project, sign up here and feel free to drop us a line on our Discord with any questions!

On this page

Get started with Viam today!