Contents
Automatic Code Generation Example
This example shows how to use the ACG for simulating a networked control system.
For a detailed description of ACG please refer to the class reference.
close all clear all clc
Define number and positions of sensors and actuators
Ns = 3; Na = 3; Xpos = rand((Ns+Na)*2,1); Ypos = rand((Ns+Na)*2,1);
Set the mane to give to the generated .mld file
name='Example';
Create a new instance of ACG obj
acg_obj=ACG(Ns,Na,name); acg_obj.RemoveOldCode; acg_obj.GenerateCode;
Warning: Block diagram 'Example' contains disabled library links. Use Model Advisor to find the disabled links in non-library models. The diagram has been saved but may not contain what you intended. Warning: Block diagram 'Example' contains disabled library links. Use Model Advisor to find the disabled links in non-library models. The diagram has been saved but may not contain what you intended.
Setup plant parameters
A=diag([0.1 .21 .3])+0.01*rand(3); B=diag([.1 .3 .6]); C=diag([2 4 1]); D=zeros(3); x0=[10 10 40];
Create and configure a the Plant and Controller submodels of the generated simulik model. Here the generation is performed in a script fashion for automatization proposes, however the user may use the graphical interface.
add_block('simulink/Signal Routing/Mux',[name '/Plant/Mux']); set_param([name '/Plant/Mux'],'inputs','3'); add_block('simulink/Signal Routing/Demux',[name '/Plant/Demux']); set_param([name '/Plant/Demux'],'outputs','3'); add_block('simulink/Continuous/State-Space',[name '/Plant/State-Space']); add_line([name '/Plant'],'Mux/1','State-Space/1'); add_line([name '/Plant'],'State-Space/1','Demux/1'); for i=1:3 add_line([name '/Plant'],['Actuator' num2str(i*2+5) '/1'],['Mux/'... num2str(i)]); add_line([name '/Plant'],['Demux/' num2str(i)],['Sensor' ... num2str(2*i-1) '/1']); end set_param([name '/Plant/State-Space'],'A','A','B','B','C','C','D',... 'D','x0','x0');
Configure the controller
add_block('simulink/Signal Routing/Demux',[name '/Controller/Demux']); set_param([name '/Controller/Demux'],'outputs','3'); for i=1:3 ii=num2str(i); add_block('simulink/Math Operations/Sum',[name '/Controller/Sum' ii]); set_param([name '/Controller/Sum' ii],'ListOfSigns','+-|'); add_block('simulink/Math Operations/Gain',[name '/Controller/Gain' ii]); add_line([name '/Controller'],['Demux/' ii],['Sum' ii '/1']); add_line([name '/Controller'],['Sensor' num2str(2*i-1) ,'/1'],['Sum'... ii '/2']); add_line([name '/Controller'],['Sum' ii '/1'],['Gain' ii '/1']); add_line([name '/Controller'],['Gain' ii '/1'],['Actuator' ... num2str(2*i+5) '/1']); end add_line([name '/Controller'],'Reference/1','Demux/1'); set_param([name '/Controller/Gain1'],'gain','30'); set_param([name '/Controller/Gain2'],'gain','6'); set_param([name '/Controller/Gain3'],'gain','60');
Setup simulation paramaters
Reference
ref = [8 -5 35]';
Antennas transmit power
set_param([name '/Wireless Network/TrueTime Wireless Network'],... 'TransPower','-30'); save_system(name)
Warning: Block diagram 'Example' contains disabled library links. Use Model Advisor to find the disabled links in non-library models. The diagram has been saved but may not contain what you intended.
Start simulation
sim(name); plot(simout.time,simout.signals.values,'b',simout.time,ref*ones(1,... length(simout.time)),'r'); disp('Few packets have gone lost using the current transmission power.') disp(['Press any key for use a lower transmission power and observe'... ' the effects.']) pause
------------------------------------------------------- TrueTime, Version 1.5 Copyright (c) 2007 Martin Ohlin, Dan Henriksson and Anton Cervin Department of Automatic Control LTH Lund University, Sweden ------------------------------------------------------- Wireless network data: Transmit power is: -30.00 dbm <==> 0.00 mW Receiver threshold is: -48.00 dbm <==> 1.58e-005 mW Maximum signal reach is calculated to: 62.10 m Few packets have gone lost using the current transmission power. Press any key for use a lower transmission power and observe the effects.

Decrease antenna transmit power
set_param([name '/Wireless Network/TrueTime Wireless Network'],... 'TransPower','-46'); save_system(name) sim(name); hold on plot(simout.time,simout.signals.values,'--b',simout.time,... ref*ones(1,length(simout.time)),'r');
Warning: Block diagram 'Example' contains disabled library links. Use Model Advisor to find the disabled links in non-library models. The diagram has been saved but may not contain what you intended. ------------------------------------------------------- TrueTime, Version 1.5 Copyright (c) 2007 Martin Ohlin, Dan Henriksson and Anton Cervin Department of Automatic Control LTH Lund University, Sweden ------------------------------------------------------- Wireless network data: Transmit power is: -46.00 dbm <==> 0.00 mW Receiver threshold is: -48.00 dbm <==> 1.58e-005 mW Maximum signal reach is calculated to: 0.58 m

Conclusions
Figure 1 shows how the transmit power determine the performance of the control actions. In particular for the continous line (-30db transmit power) convergence is achieved, while for the lower trasmit power case (-46 db, depicted in dashed line) is possible to observe divergence of 2 of the 3 states.