ANN: SymbolicControlSystems.jl

SymbolicControlSystems.jl is a newly registered package in the JuliaControl org that

  • Makes it easier to work with control systems with symbolic coefficients
  • Write C-code for your controller or filter

We currently support C-code generation from LTI transfer functions and statespace systems, as well as basic code generation for gain-scheduled / interpolated systems.

Example:

The following defines a filter that has symbolic parameters and writes C-code for filtering. The symbolic parameters appearing in the filter transfer function will be expected as inputs to the C-code

using ControlSystems, SymbolicControlSystems
@vars w T d     # Define symbolic variables
h        = 0.01 # Sample time
G        = tf([w^2], [1, 2*d*w, w^2]) * tf(1, [T, 1])
Gd       = tustin(G, h) # Discretize
code     = SymbolicControlSystems.ccode(Gd, cse=true)

C-code output:

#include <stdio.h>

#include <math.h>

double transfer_function(double ui, double T, double d, double w) {
    static double u[4] = {0};
    static double y[4] = {0};
    int i;
    for (i=3; i > 0; --i) {
        u[i] = u[i-1];
    }
    u[0] = ui;
    for (i=3; i > 0; --i) {
        y[i] = y[i-1];
    }
    y[0] = 0;
    double x0 = pow(w, 2);
    double x1 = 8000000.0*T;
    double x2 = 200.0*T*x0;
    double x3 = d*w;
    double x4 = 400.0*x3;
    double x5 = x2 + x4;
    double x6 = 80000.0*T*x3;
    double x7 = 1.0*x0 + x6 + 40000.0;
    double x8 = 1.0/(x1 + x5 + x7);
    double x9 = x0*x8;
    double x10 = 3*x9;
    double x11 = 24000000.0*T;
    double x12 = 3.0*x0 - x6 - 40000.0;
    double x13 = -x2 - x4;
    y[0] += (x9)*u[0];
    y[0] += (x10)*u[1];
    y[0] += (x10)*u[2];
    y[0] += (x9)*u[3];
    y[0] += (-x8*(-x11 + x12 + x5))*y[1];
    y[0] += (-x8*(x11 + x12 + x13))*y[2];
    y[0] += (-x8*(-x1 + x13 + x7))*y[3];
    return y[0];
}
17 Likes