Working and exportable PID controller

This commit is contained in:
2026-03-11 16:37:50 +01:00
parent 71722127f1
commit 9e124a5e92
4 changed files with 350 additions and 78 deletions

129
test.mo
View File

@@ -1,27 +1,112 @@
model test
Modelica.Blocks.Sources.Sine sine(amplitude = 1) annotation(
Placement(transformation(origin = {-100, 0}, extent = {{-10, -10}, {10, 10}})));
EmbeddedControl.Math.DDifference dDifferentiate(x(start = 0)) annotation(
Placement(transformation(origin = {20, 0}, extent = {{-10, -10}, {10, 10}})));
EmbeddedControl.Boundary.DAC dac(stepSize = 0.02) annotation(
EmbeddedControl.Boundary.DAC dac(stepSize = 0.001, scale = -1) annotation(
Placement(transformation(origin = {106, 0}, extent = {{-10, -10}, {10, 10}})));
EmbeddedControl.Boundary.ADC adc(stepSize = 0.001) annotation(
Placement(transformation(origin = {10, 0}, extent = {{-10, -10}, {10, 10}})));
model Controller
extends EmbeddedControl.Bases.ControlBase;
Modelica.Blocks.Interfaces.IntegerInput u annotation(
Placement(transformation(origin = {-120, 0}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {-120, 0}, extent = {{-20, -20}, {20, 20}})));
Modelica.Blocks.Interfaces.IntegerOutput y annotation(
Placement(transformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}})));
EmbeddedControl.Math.Gain P(k = 350) annotation(
Placement(transformation(origin = {20, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Interfaces.IntegerInput setpoint annotation(
Placement(transformation(origin = {-120, 48}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {-120, 80}, extent = {{-20, -20}, {20, 20}})));
Modelica.Blocks.MathInteger.Sum sum(nu = 2, k = {1, -1}) annotation(
Placement(transformation(origin = {-70, 0}, extent = {{-10, -10}, {10, 10}})));
EmbeddedControl.Math.DDifferentiate dDifferentiate(scaleNumerator = 1000000) annotation(
Placement(transformation(origin = {-20, 40}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.MathInteger.Sum sum1(nu = 3) annotation(
Placement(transformation(origin = {70, 0}, extent = {{-10, -10}, {10, 10}})));
EmbeddedControl.Math.Gain D(k = 25) annotation(
Placement(transformation(origin = {20, 40}, extent = {{-10, -10}, {10, 10}})));
EmbeddedControl.Math.DIntegrate dIntegrate(scaleDenominator = 1000000) annotation(
Placement(transformation(origin = {-18, -40}, extent = {{-10, -10}, {10, 10}})));
EmbeddedControl.Math.Gain I(k = 150) annotation(
Placement(transformation(origin = {20, -40}, extent = {{-10, -10}, {10, 10}})));
equation
connect(u, sum.u[1]) annotation(
Line(points = {{-120, 0}, {-80, 0}}, color = {255, 127, 0}));
connect(setpoint, sum.u[2]) annotation(
Line(points = {{-120, 48}, {-96, 48}, {-96, 0}, {-80, 0}}, color = {255, 127, 0}));
connect(sum.y, dDifferentiate.u) annotation(
Line(points = {{-58, 0}, {-40, 0}, {-40, 40}, {-32, 40}}, color = {255, 127, 0}));
connect(P.y, sum1.u[1]) annotation(
Line(points = {{31, 0}, {60, 0}}, color = {255, 127, 0}));
connect(sum1.y, y) annotation(
Line(points = {{82, 0}, {110, 0}}, color = {255, 127, 0}));
connect(sum.y, P.u) annotation(
Line(points = {{-58, 0}, {8, 0}}, color = {255, 127, 0}));
connect(dDifferentiate.y, D.u) annotation(
Line(points = {{-8, 40}, {8, 40}}, color = {255, 127, 0}));
connect(D.y, sum1.u[2]) annotation(
Line(points = {{31, 40}, {40, 40}, {40, 0}, {60, 0}}, color = {255, 127, 0}));
connect(sum.y, dIntegrate.u) annotation(
Line(points = {{-58, 0}, {-40, 0}, {-40, -40}, {-30, -40}}, color = {255, 127, 0}));
connect(dIntegrate.y, I.u) annotation(
Line(points = {{-6, -40}, {8, -40}}, color = {255, 127, 0}));
connect(I.y, sum1.u[3]) annotation(
Line(points = {{32, -40}, {40, -40}, {40, 0}, {60, 0}}, color = {255, 127, 0}));
annotation(
Diagram(coordinateSystem(extent = {{-140, 20}, {120, -20}})),
experiment(StartTime = 0, StopTime = 1, Tolerance = 1e-06, Interval = 0.002));
end Controller;
Controller controller annotation(
Placement(transformation(origin = {60, 0}, extent = {{-10, -10}, {10, 10}})));
EmbeddedControl.Boundary.ADC adc(stepSize = 0.02) annotation(
Placement(transformation(origin = {-20, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Discrete.Sampler sampler(samplePeriod(displayUnit = "ms") = 0.01) annotation(
Placement(transformation(origin = {-60, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Discrete.ZeroOrderHold zeroOrderHold(samplePeriod(displayUnit = "ms") = 0.01) annotation(
Placement(transformation(origin = {100, 0}, extent = {{-10, -10}, {10, 10}})));
EmbeddedControl.Boundary.tickGenerator tickGenerator(period(displayUnit = "ms") = 0.01) annotation(
Placement(transformation(origin = {-12, -40}, extent = {{-10, -10}, {10, 10}})));
inner Modelica.Mechanics.MultiBody.World world annotation(
Placement(transformation(origin = {-122, -30}, extent = {{-10, -10}, {10, 10}})));
Modelica.Mechanics.MultiBody.Joints.Revolute revolute(useAxisFlange = true, phi(start = 0, fixed = true), w(start = 0, fixed = true)) annotation(
Placement(transformation(origin = {-92, -30}, extent = {{-10, -10}, {10, 10}})));
Modelica.Mechanics.Rotational.Sensors.AngleSensor angleSensor annotation(
Placement(transformation(origin = {-72, 0}, extent = {{-10, -10}, {10, 10}})));
Modelica.Mechanics.Rotational.Components.Damper damper(d = 0.1) annotation(
Placement(transformation(origin = {-94, 26}, extent = {{-10, -10}, {10, 10}})));
Modelica.Mechanics.MultiBody.Parts.Body body(r_CM = {0.5, 0, 0}, m = 1) annotation(
Placement(transformation(origin = {-62, -30}, extent = {{-10, -10}, {10, 10}})));
Modelica.Mechanics.Rotational.Sources.Torque torque(useSupport = true) annotation(
Placement(transformation(origin = {-94, 52}, extent = {{-10, -10}, {10, 10}})));
Modelica.Blocks.Sources.IntegerConstant integerConstant(k = 0) annotation(
Placement(transformation(origin = {10, 30}, extent = {{-10, -10}, {10, 10}})));
equation
connect(adc.y, dDifferentiate.u) annotation(
Line(points = {{-9, 0}, {7, 0}}, color = {255, 127, 0}));
connect(sine.y, sampler.u) annotation(
Line(points = {{-89, 0}, {-73, 0}}, color = {0, 0, 127}));
connect(sampler.y, adc.u) annotation(
Line(points = {{-49, 0}, {-33, 0}}, color = {0, 0, 127}));
connect(dDifferentiate.y, dac.u) annotation(
Line(points = {{31, 0}, {47, 0}}, color = {255, 127, 0}));
connect(dac.y, zeroOrderHold.u) annotation(
Line(points = {{71, 0}, {87, 0}}, color = {0, 0, 127}));
connect(adc.y, controller.u) annotation(
Line(points = {{21, 0}, {48, 0}}, color = {255, 127, 0}));
connect(tickGenerator.y, adc.tickInput) annotation(
Line(points = {{-1, -40}, {10, -40}, {10, -12}}, color = {255, 0, 255}));
connect(tickGenerator.y, controller.tickInput) annotation(
Line(points = {{-1, -40}, {60, -40}, {60, -12}}, color = {255, 0, 255}));
connect(tickGenerator.y, dac.tickInput) annotation(
Line(points = {{-1, -40}, {106, -40}, {106, -12}}, color = {255, 0, 255}));
connect(tickGenerator.tickPeriodUs, controller.tickPeriodIn) annotation(
Line(points = {{-1, -48}, {52, -48}, {52, -12}}, color = {0, 0, 127}));
connect(world.frame_b, revolute.frame_a) annotation(
Line(points = {{-112, -30}, {-102, -30}}, color = {95, 95, 95}));
connect(angleSensor.phi, adc.u) annotation(
Line(points = {{-61, 0}, {-2, 0}}, color = {0, 0, 127}));
connect(revolute.axis, angleSensor.flange) annotation(
Line(points = {{-92, -20}, {-92, 0}, {-82, 0}}));
connect(damper.flange_a, revolute.support) annotation(
Line(points = {{-104, 26}, {-112, 26}, {-112, -10}, {-98, -10}, {-98, -20}}));
connect(revolute.axis, damper.flange_b) annotation(
Line(points = {{-92, -20}, {-92, 14}, {-78, 14}, {-78, 26}, {-84, 26}}));
connect(revolute.frame_b, body.frame_a) annotation(
Line(points = {{-82, -30}, {-72, -30}}, color = {95, 95, 95}));
connect(controller.y, dac.u) annotation(
Line(points = {{71, 0}, {94, 0}}, color = {255, 127, 0}));
connect(torque.support, damper.flange_a) annotation(
Line(points = {{-94, 42}, {-94, 36}, {-104, 36}, {-104, 26}}));
connect(torque.flange, damper.flange_b) annotation(
Line(points = {{-84, 52}, {-84, 26}}));
connect(dac.y, torque.tau) annotation(
Line(points = {{118, 0}, {128, 0}, {128, 74}, {-118, 74}, {-118, 52}, {-106, 52}}, color = {0, 0, 127}));
connect(integerConstant.y, controller.setpoint) annotation(
Line(points = {{22, 30}, {32, 30}, {32, 8}, {48, 8}}, color = {255, 127, 0}));
annotation(
uses(Modelica(version = "4.1.0")));
uses(Modelica(version = "4.1.0")),
Diagram(coordinateSystem(extent = {{-140, 40}, {120, -60}})),
version = "",
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-06, Interval = 0.02));
end test;