function vgp(block)
setup(block);
function setup(block)
block.NumDialogPrms = 3;
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
block.InputPort(1).DatatypeID = 0;
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).Dimensions = 1;
block.OutputPort(1).DatatypeID = 0;
block.OutputPort(1).Complexity = 'Real';
block.OutputPort(1).Dimensions = 1;
block.SampleTimes = [-2 0];
block.SimStateCompliance = 'CustomSimState';
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
block.RegBlockMethod('InitializeConditions', @InitializeConditions);
block.RegBlockMethod('Start', @Start);
block.RegBlockMethod('Outputs', @Outputs);
block.RegBlockMethod('Update', @Update);
function DoPostPropSetup(block)
block.NumDworks = 7;
block.Dwork(1).Name = 'CurrentState';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0;
block.Dwork(1).Complexity = 'Real';
block.Dwork(1).UsedAsDiscState = true;
block.Dwork(2).Name = 'NextSample';
block.Dwork(2).Dimensions =1;
block.Dwork(2).DataTypeID =0;
block.Dwork(2).Complexity ='Real';
block.Dwork(2).UsedAsDiscState = true;
block.Dwork(3).Name = 'PhaseDelay';
block.Dwork(3).Dimensions = 1;
block.Dwork(3).DataTypeID = 0;
block.Dwork(3).Complexity ='Real';
block.Dwork(3).UsedAsDiscState = false;
block.Dwork(4).Name = 'DutyCycle';
block.Dwork(4).Dimensions = 1;
block.Dwork(4).DataTypeID = 0;
block.Dwork(4).Complexity = 'Real';
block.Dwork(4).UsedAsDiscState = true;
block.Dwork(5).Name ='RiseCheck';
block.Dwork(5).Dimensions = 1;
block.Dwork(5).DataTypeID = 0;
block.Dwork(5).Complexity ='Real';
block.Dwork(5).UsedAsDiscState = true;
block.Dwork(6).Name ='DeadTime';
block.Dwork(6).Dimensions = 1;
block.Dwork(6).DataTypeID = 0;
block.Dwork(6).Complexity ='Real';
block.Dwork(6).UsedAsDiscState = true;
block.Dwork(7).Name ='SampleTime';
block.Dwork(7).Dimensions = 1;
block.Dwork(7).DataTypeID = 0;
block.Dwork(7).Complexity ='Real';
block.Dwork(7).UsedAsDiscState = true;
function Start(block)
block.Dwork(1).data = 0;
block.Dwork(3).data = 0;
block.Dwork(2).data = 0+eps;
block.Dwork(5).data = 0;
function InitializeConditions(block)
block.Dwork(4).data = block.DialogPrm(1).data;
block.Dwork(6).data = block.DialogPrm(2).data;
block.Dwork(7).data = block.DialogPrm(3).data:
function Outputs(block)
block.OutputPort(1).data = block.Dwork(1).data;
block.NextTimeHit = block.Dwork(2).data;
function Update(block)
block.Dwork(3).data = block.InputPort(1).data;
block.Dwork(3).data = block.Dwork(3).data / 360;
offtime = 0.5*(1-block.Dwork(4).data);
Ts = block.Dwork(7).data;
DeadTime = block.Dwork(6).data;
if block.Dwork(1).data == 0 && block.Dwork(5).data == 0
block.Dwork(1).data = 0;
block.Dwork(5).data = 1;
block.Dwork(2).data = block.Dwork(2).data + DeadTime + (offtime + block.Dwork(3).data)*Ts;
elseif block.Dwork(1).data == 0 && block.Dwork(5).data == 1
block.Dwork(1).data = 1;
block.Dwork(2).data = block.Dwork(2).data + block.Dwork(4).data*Ts - DeadTime;
elseif block.Dwork(1).data == 1 && block.Dwork(5).data == 1
block.Dwork(1).data = 0;
block.Dwork(5).data = 0;
block.Dwork(2).data = block.Dwork(2).data + (offtime - block.Dwork(3).data)*Ts;
end