function robotWorkLoopTInvariantGui %ROBOTWORKLOOPTINVARIANTGUI GUI for a repeating % robot work loop with Petri net. % % Example cycle: % T1: Pick up part % T2: Weld part % T3: Drop off part % % T-invariant condition: % A * x = 0 % % State equation: % Mk = M0 + A * x % % For x = [1; 1; 1]^T, the system returns to M0. % placeNames = ["P1: Part Waiting"; "P2: Part Picked"; "P3: Part Welded"]; transitionNames = ["T1: Pick up part"; "T2: Weld part"; "T3: Drop off part"]; Pre = eye(3); Post = [0 0 1; 1 0 0; 0 1 0]; A = Post - Pre; sampleM0 = [1; 0; 0]; sampleM = sampleM0; sampleX = [1; 1; 1]; fig = uifigure('Name', 'Repeating Robot Work Loop', 'Position', [100 100 1360 820]); mainGrid = uigridlayout(fig, [2 2]); mainGrid.RowHeight = {'1x', 28}; mainGrid.ColumnWidth = {'0.95x', '1.05x'}; mainGrid.Padding = [10 10 10 10]; mainGrid.RowSpacing = 8; mainGrid.ColumnSpacing = 10; inputPanel = uipanel(mainGrid, 'Title', 'Robot Cell Controls'); inputPanel.Layout.Row = 1; inputPanel.Layout.Column = 1; outputPanel = uipanel(mainGrid, 'Title', 'Petri Net and T-Invariant'); outputPanel.Layout.Row = 1; outputPanel.Layout.Column = 2; statusLabel = uilabel(mainGrid, 'Text', 'Ready.'); statusLabel.Layout.Row = 2; statusLabel.Layout.Column = [1 2]; inputGrid = uigridlayout(inputPanel, [9 2]); inputGrid.RowHeight = {22, 28, 22, 28, 22, 28, 30, 30, 30}; inputGrid.ColumnWidth = {'1x', '1x'}; inputGrid.Padding = [8 8 8 8]; inputGrid.RowSpacing = 8; inputGrid.ColumnSpacing = 8; uilabel(inputGrid, 'Text', 'Initial marking M0'); uilabel(inputGrid, 'Text', 'T-invariant x'); m0Field = uieditfield(inputGrid, 'text', ... 'Value', vectorToString(sampleM0), ... 'ValueChangedFcn', @refreshAll); m0Field.Layout.Row = 2; m0Field.Layout.Column = 1; xField = uieditfield(inputGrid, 'text', ... 'Value', vectorToString(sampleX), ... 'ValueChangedFcn', @refreshAll); xField.Layout.Row = 2; xField.Layout.Column = 2; uilabel(inputGrid, 'Text', 'Current marking M'); uilabel(inputGrid, 'Text', 'Transition to fire'); p1Field = uieditfield(inputGrid, 'numeric', ... 'Value', sampleM(1), ... 'Limits', [0 inf], ... 'RoundFractionalValues', 'on', ... 'ValueChangedFcn', @refreshAll); p1Field.Layout.Row = 4; p1Field.Layout.Column = 1; transitionDropDown = uidropdown(inputGrid, ... 'Items', transitionNames, ... 'Value', transitionNames(1), ... 'ValueChangedFcn', @refreshAll); transitionDropDown.Layout.Row = 4; transitionDropDown.Layout.Column = 2; p2Field = uieditfield(inputGrid, 'numeric', ... 'Value', sampleM(2), ... 'Limits', [0 inf], ... 'RoundFractionalValues', 'on', ... 'ValueChangedFcn', @refreshAll); p2Field.Layout.Row = 5; p2Field.Layout.Column = 1; fireButton = uibutton(inputGrid, ... 'Text', 'Fire Selected Transition', ... 'ButtonPushedFcn', @fireTransition); fireButton.Layout.Row = 5; fireButton.Layout.Column = 2; p3Field = uieditfield(inputGrid, 'numeric', ... 'Value', sampleM(3), ... 'Limits', [0 inf], ... 'RoundFractionalValues', 'on', ... 'ValueChangedFcn', @refreshAll); p3Field.Layout.Row = 6; p3Field.Layout.Column = 1; cycleButton = uibutton(inputGrid, ... 'Text', 'Run Example Cycle T1 -> T2 -> T3', ... 'ButtonPushedFcn', @runExampleCycle); cycleButton.Layout.Row = 6; cycleButton.Layout.Column = 2; resetButton = uibutton(inputGrid, ... 'Text', 'Reset to M0', ... 'ButtonPushedFcn', @resetCurrentMarking); resetButton.Layout.Row = 7; resetButton.Layout.Column = [1 2]; refreshButton = uibutton(inputGrid, ... 'Text', 'Refresh', ... 'ButtonPushedFcn', @refreshAll); refreshButton.Layout.Row = 8; refreshButton.Layout.Column = [1 2]; noteLabel = uilabel(inputGrid, ... 'Text', 'Cycle meaning: the same workpiece returns to the start state after T1, T2, T3.'); noteLabel.Layout.Row = 9; noteLabel.Layout.Column = [1 2]; outputGrid = uigridlayout(outputPanel, [6 1]); outputGrid.RowHeight = {92, '1x', 300, 28, 60, '1x'}; outputGrid.Padding = [8 8 8 8]; formulaText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', [ "Transition invariant condition:" "A * x = 0" "State equation:" "Mk = M0 + A * x" "For x = [1; 1; 1]^T, the system returns to M0." "This models a repeating robot work loop." ]); formulaText.Layout.Row = 1; uilabel(outputGrid, 'Text', 'Incidence matrix A = Post - Pre'); incidenceTable = uitable(outputGrid, 'Data', A); incidenceTable.Layout.Row = 2; netAxes = uiaxes(outputGrid); netAxes.Layout.Row = 3; netAxes.Box = 'on'; netAxes.Visible = 'off'; netAxes.XTick = []; netAxes.YTick = []; resultLabel = uilabel(outputGrid, 'Text', ''); resultLabel.Layout.Row = 4; basisText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', strings(0,1)); basisText.Layout.Row = 5; detailText = uitextarea(outputGrid, ... 'Editable', 'off', ... 'Value', strings(0,1)); detailText.Layout.Row = 6; refreshAll(); function refreshAll(~, ~) try M0 = parseVector(m0Field.Value, 3, 'M0'); x = parseVector(xField.Value, 3, 'x'); M = [p1Field.Value; p2Field.Value; p3Field.Value]; requireNonnegativeIntegerVector(M0, 'M0'); requireNonnegativeIntegerVector(M, 'M'); requireNonnegativeIntegerVector(x, 'x'); A = Post - Pre; incidenceTable.Data = A; residual = A * x; Mk = M0 + residual; selectedTransition = find(strcmp(transitionNames, string(transitionDropDown.Value)), 1); if isempty(selectedTransition) selectedTransition = 1; end enabled = all(M >= Pre, 1); drawPetriNet(netAxes, M, enabled, selectedTransition); isTInvariant = norm(residual, inf) <= 1e-12; if isTInvariant resultLabel.Text = 'x is a T-invariant: A*x = 0, so the loop returns to M0.'; else resultLabel.Text = 'x is not a T-invariant: A*x is nonzero.'; end basis = null(A); if isempty(basis) basisText.Value = [ "No null-space basis found." "This net may not have a nontrivial T-invariant." ]; else for k = 1:size(basis, 2) basis(:, k) = normalizeBasisVector(basis(:, k)); end lines = strings(size(basis, 2) + 1, 1); lines(1) = "Candidate basis vectors for null(A):"; for k = 1:size(basis, 2) lines(k + 1) = "v" + string(k) + " = " + vectorToString(basis(:, k)); end basisText.Value = lines; end detailText.Value = [ "M0 = " + vectorToString(M0) "Current M = " + vectorToString(M) "x = " + vectorToString(x) "A*x = " + vectorToString(residual) "Mk = M0 + A*x = " + vectorToString(Mk) "If x = [1 1 1]^T, then Mk = M0." ]; statusLabel.Text = 'Ready.'; catch ME statusLabel.Text = "Error: " + string(ME.message); resultLabel.Text = ''; basisText.Value = strings(0,1); detailText.Value = strings(0,1); end end function fireTransition(~, ~) try M = [p1Field.Value; p2Field.Value; p3Field.Value]; requireNonnegativeIntegerVector(M, 'M'); t = find(strcmp(transitionNames, string(transitionDropDown.Value)), 1); if isempty(t) error('Select a valid transition.'); end if ~all(M >= Pre(:, t)) statusLabel.Text = "Transition " + string(transitionNames(t)) + ' is not enabled.'; return; end M = M + A(:, t); setMarking(M); refreshAll(); statusLabel.Text = "Fired " + string(transitionNames(t)) + '.'; catch ME statusLabel.Text = "Error: " + string(ME.message); end end function runExampleCycle(~, ~) try M = parseVector(m0Field.Value, 3, 'M0'); requireNonnegativeIntegerVector(M, 'M0'); for t = 1:3 if ~all(M >= Pre(:, t)) error('The example cycle is not enabled from M0.'); end M = M + A(:, t); setMarking(M); drawnow; end refreshAll(); statusLabel.Text = 'Completed the example cycle T1 -> T2 -> T3.'; catch ME statusLabel.Text = "Error: " + string(ME.message); end end function resetCurrentMarking(~, ~) setMarking(sampleM0); transitionDropDown.Value = transitionNames(1); refreshAll(); end function setMarking(Mvec) p1Field.Value = Mvec(1); p2Field.Value = Mvec(2); p3Field.Value = Mvec(3); end function drawPetriNet(ax, Mvec, enabled, selectedTransition) cla(ax); hold(ax, 'on'); axis(ax, 'equal'); axis(ax, [0 10 0 7]); ax.Visible = 'off'; placePos = [2 5; 8 5; 5 1.8]; transPos = [5 5; 6.9 3.0; 3.1 3.0]; placeR = 0.65; transW = 0.35; transH = 1.0; drawArrow(ax, placePos(1,:), transPos(1,:)); drawArrow(ax, transPos(1,:), placePos(2,:)); drawArrow(ax, placePos(2,:), transPos(2,:)); drawArrow(ax, transPos(2,:), placePos(3,:)); drawArrow(ax, placePos(3,:), transPos(3,:)); drawArrow(ax, transPos(3,:), placePos(1,:)); for i = 1:3 rectangle(ax, ... 'Position', [placePos(i,1)-placeR, placePos(i,2)-placeR, 2*placeR, 2*placeR], ... 'Curvature', [1 1], ... 'FaceColor', [0.95 0.97 1.0], ... 'EdgeColor', [0.1 0.2 0.6], ... 'LineWidth', 2); text(ax, placePos(i,1), placePos(i,2)+1.05, placeNames(i), ... 'HorizontalAlignment', 'center', ... 'FontWeight', 'bold'); text(ax, placePos(i,1), placePos(i,2)-1.05, "M = " + string(Mvec(i)), ... 'HorizontalAlignment', 'center'); drawTokens(ax, placePos(i,:), Mvec(i), placeR); end for j = 1:3 if enabled(j) faceColor = [0.65 0.9 0.65]; else faceColor = [0.85 0.85 0.85]; end if j == selectedTransition edgeColor = [0.0 0.3 0.8]; lineWidth = 2.5; else edgeColor = [0 0 0]; lineWidth = 1.5; end rectangle(ax, ... 'Position', [transPos(j,1)-transW/2, transPos(j,2)-transH/2, transW, transH], ... 'FaceColor', faceColor, ... 'EdgeColor', edgeColor, ... 'LineWidth', lineWidth); text(ax, transPos(j,1), transPos(j,2)-0.95, transitionNames(j), ... 'HorizontalAlignment', 'center', ... 'FontSize', 9); end text(ax, 5, 6.6, 'Petri Net for Repeating Robot Work Loop', ... 'HorizontalAlignment', 'center', ... 'FontWeight', 'bold', ... 'FontSize', 12); end function drawArrow(ax, p1, p2) plot(ax, [p1(1) p2(1)], [p1(2) p2(2)], 'k-', 'LineWidth', 1.4); v = p2 - p1; mid = (p1 + p2) / 2; quiver(ax, mid(1), mid(2), 0.18*v(1), 0.18*v(2), 0, ... 'Color', 'k', ... 'LineWidth', 1.0, ... 'MaxHeadSize', 0.8, ... 'AutoScale', 'off'); end function drawTokens(ax, center, count, radius) if count <= 0 return; end tokenRadius = 0.11; offsets = [0 0; -0.18 0.14; 0.18 0.14; -0.18 -0.14; 0.18 -0.14]; for k = 1:count idx = min(k, size(offsets, 1)); pos = center + offsets(idx, :) * radius; rectangle(ax, ... 'Position', [pos(1)-tokenRadius, pos(2)-tokenRadius, 2*tokenRadius, 2*tokenRadius], ... 'Curvature', [1 1], ... 'FaceColor', [0.85 0.25 0.25], ... 'EdgeColor', [0.85 0.25 0.25]); end end function v = parseVector(textValue, expectedLength, name) if isstring(textValue) textValue = char(textValue); end textValue = strtrim(textValue); textValue = regexprep(textValue, '[\[\];,]', ' '); parts = split(string(textValue)); parts(parts == "") = []; if isempty(parts) error('Invalid %s.', name); end v = str2double(parts); if any(isnan(v)) error('Invalid %s.', name); end v = v(:); if numel(v) ~= expectedLength error('The %s vector must have %d elements.', name, expectedLength); end end function requireNonnegativeIntegerVector(v, name) tol = 1e-12; if any(v(:) < 0) error('%s must be nonnegative.', name); end if any(abs(v(:) - round(v(:))) > tol) error('%s must contain integers.', name); end end function w = normalizeBasisVector(v) v = real(v); idx = find(abs(v) > 1e-12, 1); if isempty(idx) w = v; return; end if v(idx) < 0 v = -v; end w = v ./ max(1, max(abs(v))); end function s = vectorToString(v) v = v(:).'; if isempty(v) s = "[]"; return; end s = "[" + join(string(compose('%g', v)), " ") + "]"; end end