Figure 1. High level description of the system.
This system is considered a cyber-physical system, since it is comprised of physical systems, the dynamics of the vehicles, and cyber systems, the finite state machines responsible for computing the direction based on the acquired target measurements. This system has to take in consideration the sampling rate of each vehicle and the random transmission delay generated by the network.
Matlab Implementation (Change the extension to .zip)
]]>
Figure 1 shows a simple example of interconnection of many DC/AC inverters. This kind of interconnection is typical in distributed power generation with photovoltaic panels. The panels generate DC voltage, and it must be converted to AC. All of the inverters are hooked to a single load to increase overall power. However, a significant amount of efficiency loss is introduced if generated AC voltages by each of the inverters are off-phase.
Figure 1 – interconnection of many DC/AC inverters
This project addresses an issue of unsynchronized power generators and proposes a solution. The interconnection was modeled as a cyber-physical system and was described by the difference and differential equations. The correctness of the proposed solution is based on the MATLAB simulation of the model. The simulation results are available in Results section.
]]>
Hybrid Model of Platoon Formation
Below, you can find the simulation code for the multi agent case.
function [value discrete] = C(x) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab M-file % % Description: Flow set % Return 0 if outside of C, and 1 if inside C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% K = round(length(x)/3); d = x(1:K); u = x(K+1:2*K); p = x(2*K+1:3*K); value = 0; for i = 1:K-1 if d(i+1) = d(i) if p(i+1) ~= p(i) inside = 1; end end end end
function inside = D(x) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab M-file % % Description: Jump set % Return 0 if outside of D, and 1 if inside D %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% K = round(length(x)/3); d = x(1:K); u = x(K+1:2*K); p = x(2*K+1:3*K); inside = 0; for i = 1:K-1 if d(i+1) >= d(i) if p(i+1) ~= p(i) inside = 1; end end end end
function xdot = f(x) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab M-file % % Description: Flow map %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % state K = round(length(x)/3); d = x(1:K); u = x(K+1:2*K); p = x(2*K+1:3*K); % differential equations xdot = [u;zeros(2*K,1)]; end
function xplus = g(x) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab M-file % % Description: Jump map %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % state K = round(length(x)/3); d = x(1:K); u = x(K+1:2*K); p = x(2*K+1:3*K); u_min = 10; u_max = 20; % decide whether u_min or u_max will be used in the set valued case r = rand(1); if r > 0.5 bar_u = u_max; else bar_u = u_min; end % search for the platoons that caused the jump for i = 1:K-1 if d(i+1) >= d(i) if p(i+1) ~= p(i) break; end end end % find all agents in these platoons N = find(p == p(i+1) | p == p(i)); p(N) = p(i); first_agent = sum(N == 1); last_agent = sum(N == K); if first_agent && last_agent || ~first_agent && ~last_agent % we're in the middel or one platoon u(N) = bar_u; elseif first_agent && ~last_agent % that's the first platoon u(N) = u_min; elseif ~first_agent && last_agent % that's the las tplatoon u(N) = u_max; end % the adjustment in order to end up in the flow set after the jump d(N) = d(N(1)); xplus = [d;u;p]; end
clear all % function run u_min = 10; u_max = 20; K = 9; % initial conditions d = cumsum(rand(1,K))*5; d = d(K:-1:1); d = d.'; u(1) = u_min; des = rand(1,K-2); u(2:K-1) = u_min*(des > 0.5) + u_max*(des priority for jumps % rule = 2 -> priority for flows rule = 1; options = odeset('RelTol',1e-6,'MaxStep',.1); maxStepCoefficient = .1; % set the maximum step length. At each run of the % integrator the option 'MaxStep' is set to % (time length of last integration)*maxStepCoefficient. % Default value = 0.1 % simulate [t x j] = HyEQsolver( @f,@g,@C,@D,x0,TSPAN,JSPAN,rule,options,maxStepCoefficient); % plot solution figure; subplot(2,1,1) plot(t,x(:,1:K)); grid on; % legend('d'interpreter','tex'); ylabel('$d_i$','interpreter','latex'); xlabel('time'); subplot(2,1,2); plot(t,x(:,2*K+1:end)); xlabel('time'); ylabel('$p_i$','interpreter','latex'); ylim([0,K+1]);]]>
EECI_HCS2014_Project_Eelco_van_Horssen
Software:
SEM_hybrid.rar (please change the extension from .rar.doc to .rar)
]]>>> install
———————————–
INSTALLATION HyEQ_Toolbox_V2_01
———————————–
Reading the archiv
—————————-
ERROR OCCURED
during installation
—————————-
Error using install (line 80)
The installation file is corrupt!
Ensure that the archive container was not modified (check FTP/
proxy/ firewall settings, anti-virus scanner for emails etc.)!
Thus, to solve this problem and install the toolbox, it is necessary that the user create its own install file in her/his computer. Here are the steps how to create and install the installation file.
I hope this is usefull for those who found the error, please if you have questions leave a comment, and I’ll try to help you.
Best,
Pablo.
PD: Thanks Sean and Yuchun for all your help.
]]>function xdot = f(x) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab M-file Project: HyEQ Toolbox @ Hybrid Dynamics and Control % Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software % % Project: Simulation of a hybrid system % % Name: f.m % % Description: Flow map % % Version: 1.0 % Required files: - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % flow map: xdot=f(x,u,parameters); x1=x(1); x2=x(2); length=1; gamma=-9.81; xdot=[x(2);gamma/length*sin(x(1))];
Flow set: C.m
function v = C(x) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab M-file Project: HyEQ Toolbox @ Hybrid Dynamics and Control % Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software % % Project: Simulation of a hybrid system % % Name: C.m % % Description: Flow set % % Version: 1.0 % Required files: - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Check on flow conditions % E.g., % if (x(1) >= u(1)) % flow condition % v = 1; % report flow % else % v = 0; % do not report flow % end x1=x(1); x2=x(2); if x1 >= 0; v = 1; else v=0; end
Jump map: g.m
function xplus = g(x) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab M-file Project: HyEQ Toolbox @ Hybrid Dynamics and Control % Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software % % Project: Simulation of a hybrid system % % Name: g.m % % Description: Jump map % % Version: 1.0 % Required files: - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % jump map: xplus = g(x,u,parameters); x2=x(2); xplus = [0.001;-.8*x2];
Jump set: D.m
function v = D(x) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab M-file Project: HyEQ Toolbox @ Hybrid Dynamics and Control % Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software % % Project: Simulation of a hybrid system % % Name: D.m % % Description: Jump set % % Version: 1.0 % Required files: - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Check on flow conditions % % E.g., % if (x(1) <= u(1)) && (x(2) <= 0) % jump condition % v = 1; % report jump % else % v = 0; % do not report jump % end x1=x(1); x2=x(2); if x1 <= 0 && x2 <= 0 v=1; else v = 0; end
Initialization script: initialization_pendulum.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab M-file Project: HyEQ Toolbox @ Hybrid Dynamics and Control % Lab, http://www.u.arizona.edu/~sricardo/index.php?n=Main.Software % % Filename: initialization.m % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % initialization for bouncing ball example % clear all clc % initial conditions x0 = [pi/4;0]; % physical variables global gamma lambda gamma = -9.81; % gravity constant lambda = 0.8; % restitution coefficent % simulation horizon T = 10; J = 20; % rule for jumps % rule = 1 -> priority for jumps % rule = 2 -> priority for flows % rule = 3 -> no priority, random selection when simultaneous conditions rule = 1; %solver tolerances RelTol = 1e-6; MaxStep = 1e-3;
Plot script: postprocessing_pendulum.m
%postprocessing for the bouncing ball example % plot solution figure(1) clf subplot(2,1,1),plotflows(t,j,x(:,1)) grid on ylabel('x1') subplot(2,1,2),plotjumps(t,j,x(:,1)) grid on ylabel('x1') figure(2) clf subplot(2,1,1),plotflows(t,j,x(:,2)) grid on ylabel('x2') subplot(2,1,2),plotjumps(t,j,x(:,2)) grid on ylabel('x2') % plot hybrid arc plotHybridArc(t,j,x(:,1)) xlabel('j') ylabel('t') zlabel('x1')]]>
The steps that I suggested to update the system are the following:
Setup the C-compiler in matlab following the steps in the instructions (in the HyEQ instructions file).
Open Simulink and try to run an example:
it worked? yes = you are done :), no= continue with step 8 :S
In the Matlab’s command prompt >> mex -setup, and follow the steps in the instructions file.
I hope this could be useful for someone, (now my Maverics+Matlab2013b+HyEQ simulator is working)
]]>
where the sampling time is not constant. A new sample is obtained when , . The memory remains constant until a new sample is obtained.
In order to model the closed loop model as a hybrid system, we must define
State Vector:
The simulation results are shown in the following images:
Simulation code
%file name: run.m clear all; close all; %% % planta con S&H, se guarda una muestra de la planta en "m". % No se tomara otra muestra hasta que la distancia entre la salida de la % planta y m es mayor que DELTA %planta: A = -.5; %B=0; %realimentación K = 1; DELTA = 3; %% Define initial conditions: x0 = [10; 0; -10]; %% Simulation horizon TSPAN = [0 10]; JSPAN = [0 15]; rule = 1; options = odeset('RelTol', 1e-6, 'MaxStep', .1); [t j x] = HyEQsolver(@f,@g,@C,@D,x0,TSPAN,JSPAN,rule,options); %% plotHybridArc(t,j,x(:,3)) title('Input') xlabel('j') ylabel('t') zlabel('entrada') plotHybridArc(t,j,abs(x(:,2)-x(:,1))) title('Delta') xlabel('j') ylabel('t') zlabel('delta ') plotHybridArc(t,j,x(:,1)) title('Output') xlabel('j') ylabel('t') zlabel('z')
flow map
%file name: f.m function [ xdot ] = f( x ) %F Summary of this function goes here % Detailed explanation goes here A = -0.5; %B=0; K = 1; DELTA = 3; z = x(1); zdot = A*z; xdot = [zdot;0;0]; end
jump map
function [ xPlus] = g(x) %UNTITLED Summary of this function goes here % Detailed explanation goes here A = -.5; %B=0; K = 1; DELTA = 3; z = x(1); zPlus = z; mPlus = z; uPlus = -K*z; xPlus = [zPlus; mPlus; uPlus]; end
flow condition
function [ inC ] = C( x) %INC Summary of this function goes here % Detailed explanation goes here A = -0.5; %B=0; K = 1; DELTA = 3; %%% z = x(1); m = x(2); if abs(z-m)<DELTA inC = true; else inC = false; end end
jump condition
function [ inD ] = D( x) %INC Summary of this function goes here % Detailed explanation goes here A = -0.5; %B=0; K = 1; DELTA = 3; %%% z = x(1); m = x(2); if abs(z-m)>=DELTA inD = true; else inD = false; end end]]>
Let’s consider a pneumatic system, which consists of a flat tire and an air compressor to inflate it. The system can be modeled as follows:
: is the compressor’s pneumatic resistance.
: models the tire’s capacitance
: models the losses due to the puncture.
To develop a hybrid model I chose the following variables:
: stands for the manometric pressure in the tire.
: stands for the state of the compressor (‘on’ or ‘off’).
The compressor turns on if the pressure falls below and it turns off when the pressure inside the tire reaches . Between this two limits the pressure flows according to the following equation:
This equation was obtained under the following assumptions:
• The variation of the tire’s volume is negligible, then tire’s capacitance is constant.
• The compressor is a constant pressure source of value
• The pressure never exceeds the limits or
The hybrid model is then defined by:
The Matlab code written to simulate the system using HyEQ Toolbox is presented below. The values of the parameters were chosen in order to obtain results that could be quickly visualized, but they must be changed for more appropiate values.
function inC = C(x) %check if in C p = x(1); eps = 0.12; pmin = 8; pmax = 11; if (p>=pmin+eps && p<=pmax-eps) || p>=pmax+eps inC = 1; else inC = 0; end end
function inD = D(x) %check if in D p = x(1); eps = 0.12; eps = 0.12; pmin = 8; pmax = 11; if (p<=pmin+eps && p>=pmin-eps ) || (p<=pmax+eps && p>=pmax-eps ) inD = 1; else inD = 0; end end
function xdot = f(x) %flow map p = x(1); q = x(2); if q == 1 %pdot=-(Rc+Rp)/(Rc*Rp*Cc)*p+psource/(Cc*Rc) pdot = -0.2*p+0.3*7.5; else %pdot=-Cc/Rp*p pdot = -0.1*p; end qdot = 0; xdot = [pdot qdot]'; end
function xplus = g(x) % jump map p = x(1); q = x(2); pmin = 8; pmax = 11; eps = 0.12; if p<=pmax+eps && p>=pmax-eps qplus = 0; pplus = pmax-eps; elseif p<=pmin+eps && p>=pmin-eps qplus = 1; pplus = pmin+eps; end xplus = [pplus qplus]'; end
where:
: Position and speed of the load respectively.
: Position and speed of the motor shaft respectively.
: Dynamic friction coefficient of the load and the motor respectively.
: Inertia moment of the load and the motor respectively.
: Elastic torsion coefficient of the spring.
The states of system are the position and speed of the motor and the load respectively, and the input is the voltage applied. With this and using Newtons Law and the electrical equations for the motor, the system in state space is:
To control the position in a point of reference a sliding mode control with next structure is developed
Because of the implemented control the signal input to plant is not continuous, and it can be modelled as a switching signal with hysteresis, that jumps between two differences values depending of signal .
The close loop system can be modelled as a hybrid system, if an extra variable which count the time between jumps is considered and is reseted when jump occurs. the variable that jumps is called .
Where are the levels of hystereses and is the positive bounded of the switching signal.
% run.m to simulate timer with reset clear all global u global r %Reference Position [rad] r=5; %Initial Value for input for de switching signal u=0; %Define inicial conditions of the states x0=[0;0;0;0;0;1]; %simulation horizon TSPAN = [0 4]; JSPAN = [0 15]; %ruls for jump %rule = 1 -> priority for jumps %rule = 2 -> priority for flows %rule = 3 -> ramdom priority rule=1; options = odeset('RelTol',1e-6,'MaxStep',.1); %run simulation [t j x] = HyEQsolver(@f,@g,@C,@D,x0,TSPAN,JSPAN,rule,options); figure(1) clf subplot(2,1,1),plotflows(t,j,x(:,1)) grid on ylabel('x1') subplot(2,1,2),plotjumps(t,j,x(:,1)) grid on ylabel('x1') % plot hybrid arc plotHybridArc(t,j,x) xlabel('j') ylabel('t') zlabel('x')
function inD = D(x) % Jump set % Check if in D global u % Hysterisis level hbar=1; q=x(6); if ((q == -1) && (u >= hbar)) inD = 1; elseif ((q==1) && (u <= -hbar)) inD = 1; else inD=0; end
function inC = C(x) %flow set %checck if in C inC=1; end
function xdot = f(x) %flow map global u global r z1=x(1); z2=x(2); z3=x(3); z4=x(4); tau=x(5); q=x(6); z=[z1;z2;z3;z4]; % Linear System A=[0 1 0 0 ; -90.8 -0.7 90.8 0 ; 0 0 0 1 ; 55,41 0 -55.41 - 105.6]; B=[0 ; 0 ; 0 ; 220.7]; K=[2.2895 0.2714 0.8352 0.0045]; u = r-(K*z); %Flow xdot = [(A*z)+(B*(q*24)) ; 1 ; 0]; end]]>