Skip to content

About

PID controller for Beam Bots

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Beam Bots Logo

Beam Bots PID Controller

CI License: Apache 2.0 Hex version badge Hexdocs badge REUSE status Ask DeepWiki

A general-purpose PID controller for the Beam Bots robotics framework.

This library provides a BB.Controller implementation that subscribes to configurable topics for setpoint and measurement values, runs a periodic PID loop, and publishes output to a configurable topic. One controller instance = one PID loop.

Features

  • Configurable subscriptions - subscribe to any topic/message combination for setpoint and measurement
  • Flexible path extraction - extract values from nested message fields or list indices
  • Configurable output - publish to any topic with any numeric message field
  • Runtime parameter updates - PID gains can be changed at runtime via BB parameters
  • Validation - validates configuration at init time with clear error messages

Installation

Install it into a Beam Bots project with Igniter, naming the controller:

mix igniter.install bb_pid_controller --name shoulder_pid

Requirements

  • BB framework (~> 0.35)

Usage

The installer adds a controller to your robot, with its gains wired to a [:config, :shoulder_pid] parameter group so you can tune them at runtime:

controllers do
  controller :shoulder_pid,
             {BB.PID.Controller,
              kp: param([:config, :shoulder_pid, :kp]),
              ki: param([:config, :shoulder_pid, :ki]),
              kd: param([:config, :shoulder_pid, :kd]),
              output_min: -1.0,
              output_max: 1.0,
              setpoint_topic: [:TODO],
              setpoint_message: BB.Message.Actuator.Command.Position,
              setpoint_path: [:position],
              measurement_topic: [:TODO],
              measurement_message: BB.Message.Sensor.JointState,
              measurement_path: [:positions, 0],
              output_topic: [:TODO],
              output_message: BB.Message.Actuator.Command.Velocity,
              output_field: :velocity,
              output_frame_id: :TODO,
              rate: 100}
end

parameters do
  group :config do
    group :shoulder_pid do
      param :kp, type: :float, default: 1.0, min: 0.0, doc: "Proportional gain"
      param :ki, type: :float, default: 0.0, min: 0.0, doc: "Integral gain"
      param :kd, type: :float, default: 0.0, min: 0.0, doc: "Derivative gain"
    end
  end
end

Replace the :TODOs with the joint you want to control. For a shoulder joint with a motor actuator and an encoder sensor:

setpoint_topic: [:actuator, :base_link, :shoulder, :pid],
measurement_topic: [:sensor, :base_link, :shoulder, :encoder],
output_topic: [:actuator, :base_link, :shoulder, :motor],
output_frame_id: :shoulder,

Run the installer again with another --name for each further control loop. The Getting Started tutorial walks through the whole thing.

Controllers are omitted in simulation by default. Add simulation: :start to the controller to run it under simulation.

Sending Setpoints

Arm the robot, then publish position setpoints to the controller's setpoint topic:

{:ok, cmd} = MyRobot.Robot.arm()
{:ok, :armed, _opts} = BB.Command.await(cmd)

{:ok, msg} =
  BB.Message.new(
    BB.Message.Actuator.Command.Position,
    :shoulder,
    position: 1.57
  )

BB.publish(MyRobot.Robot, [:actuator, :base_link, :shoulder, :pid], msg)

The controller will compute the PID output and publish velocity commands to the motor.

Tuning

The gains are robot parameters, so you can change them on a running robot:

BB.Parameter.set(MyRobot.Robot, [:config, :shoulder_pid, :kp], 2.0)

Configuration Options

PID Gains

Option Type Default Description
kp float required Proportional gain
ki float 0.0 Integral gain
kd float 0.0 Derivative gain
tau float 1.0 Derivative low-pass filter (0-1, 1=no filter)
output_min float -1.0 Output clamp minimum
output_max float 1.0 Output clamp maximum

Setpoint Subscription

Option Type Description
setpoint_topic [atom] Topic path to subscribe to
setpoint_message module Message module to filter for
setpoint_path [atom | integer] Path to value in payload

Measurement Subscription

Option Type Description
measurement_topic [atom] Topic path to subscribe to
measurement_message module Message module to filter for
measurement_path [atom | integer] Path to value in payload

Output Publication

Option Type Description
output_topic [atom] Topic path to publish to
output_message module Message module to construct
output_field atom Field name for output value
output_frame_id atom frame_id for constructed messages

Control Loop

Option Type Default Description
rate pos_integer 100 Control loop frequency (Hz)

Path Extraction

The *_path options support atoms (field names) and integers (list indices):

# Simple field access
setpoint_path: [:position]           # payload.position

# List index access
measurement_path: [:positions, 0]    # payload.positions |> Enum.at(0)

# Nested access
path: [:data, :readings, 0, :value]  # payload.data.readings[0].value

How It Works

Architecture

Setpoint Topic ─────────────────────────┐
(configurable message/field)            │
                                        ▼
                               ┌─────────────────┐
Measurement Topic ────────────►│ BB.PID.Controller│
(configurable message/field)   │                  │
                               │  BB.PID.Kernel   │
                               │  (scalar / defn) │
                               └────────┬─────────┘
                                        │
                                        ▼
                               Output Topic
                               (configurable message/field)

Message Flow

  1. Controller subscribes to setpoint_topic and measurement_topic at init
  2. When a message arrives on setpoint_topic with matching setpoint_message type, the setpoint value is extracted and stored
  3. When a message arrives on measurement_topic with matching measurement_message type, the measurement value is extracted and stored
  4. Every 1000/rate ms, if both setpoint and measurement exist:
    • PID step is computed: output = Kp*error + Ki*integral + Kd*derivative
    • Output is clamped to [output_min, output_max]
    • Output message is constructed and published to output_topic

Validation

The controller validates configuration at init time:

  1. Unique sources - {setpoint_topic, setpoint_message} must differ from {measurement_topic, measurement_message}
  2. Non-empty paths - setpoint_path and measurement_path cannot be empty
  3. Valid output field - output_field must exist in output_message schema and be numeric

Common Use Cases

Each of these is an installer run (mix igniter.install bb_pid_controller --name <name>) with its topics filled in. The output_min, output_max and rate lines are left out for brevity.

Position Control with Velocity Output

Use encoder feedback to control position, outputting velocity commands:

controller :joint_pid,
           {BB.PID.Controller,
            kp: param([:config, :joint_pid, :kp]),
            ki: param([:config, :joint_pid, :ki]),
            kd: param([:config, :joint_pid, :kd]),
            setpoint_topic: [:actuator, :joint, :pid],
            setpoint_message: BB.Message.Actuator.Command.Position,
            setpoint_path: [:position],
            measurement_topic: [:sensor, :joint, :encoder],
            measurement_message: BB.Message.Sensor.JointState,
            measurement_path: [:positions, 0],
            output_topic: [:actuator, :joint, :motor],
            output_message: BB.Message.Actuator.Command.Velocity,
            output_field: :velocity,
            output_frame_id: :joint}

Velocity Control with Effort Output

Use velocity feedback to control velocity, outputting effort commands:

controller :velocity_pid,
           {BB.PID.Controller,
            kp: param([:config, :velocity_pid, :kp]),
            ki: param([:config, :velocity_pid, :ki]),
            kd: param([:config, :velocity_pid, :kd]),
            setpoint_topic: [:actuator, :wheel, :velocity_cmd],
            setpoint_message: BB.Message.Actuator.Command.Velocity,
            setpoint_path: [:velocity],
            measurement_topic: [:sensor, :wheel, :encoder],
            measurement_message: BB.Message.Sensor.JointState,
            measurement_path: [:velocities, 0],
            output_topic: [:actuator, :wheel, :motor],
            output_message: BB.Message.Actuator.Command.Effort,
            output_field: :effort,
            output_frame_id: :wheel}

Documentation

Full documentation is available at HexDocs.

About

PID controller for Beam Bots

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages