Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Basic Tracefile with Dynamic Vehicle Entry/Exit + some other fixes #37

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions MainFiles/mainInit.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

%% Init of active vehicles and states
% Move IDvehicle from simValues to station Management
stationManagement.activeIDs = simValues.IDvehicle;
simValues = rmfield(simValues,'IDvehicle');
stationManagement.activeIDs = [];

% The simulation starts at time '0'
timeManagement.timeNow = 0;
Expand Down Expand Up @@ -54,6 +53,8 @@
stationManagement.activeIDsCV2X = stationManagement.activeIDsCV2X(stationManagement.activeIDsCV2X>0);
stationManagement.activeIDs11p = stationManagement.activeIDs.*(stationManagement.vehicleState(stationManagement.activeIDs)~=constants.V_STATE_LTE_TXRX);
stationManagement.activeIDs11p = stationManagement.activeIDs11p(stationManagement.activeIDs11p>0);
stationManagement.activeIDsEnter = [];
stationManagement.activeIDsExit = [];
stationManagement.indexInActiveIDs_ofLTEnodes = zeros(length(stationManagement.activeIDsCV2X),1);
for i=1:length(stationManagement.activeIDsCV2X)
stationManagement.indexInActiveIDs_ofLTEnodes(i) = find(stationManagement.activeIDs==stationManagement.activeIDsCV2X(i));
Expand All @@ -64,10 +65,11 @@
end

%% Number of vehicles at the current time
outputValues.Nvehicles = length(stationManagement.activeIDs);
outputValues.Nvehicles = simValues.maxID;
outputValues.NvehiclesTOT = outputValues.NvehiclesTOT + outputValues.Nvehicles;
outputValues.NvehiclesLTE = outputValues.NvehiclesLTE + length(stationManagement.activeIDsCV2X);
outputValues.Nvehicles11p = outputValues.Nvehicles11p + length(stationManagement.activeIDs11p);
outputValues.meanPositionError = zeros(outputValues.Nvehicles, 1);

%% Initialization of packets management
% Number of packets in the queue of each node
Expand Down Expand Up @@ -201,15 +203,22 @@
end

% Copy real coordinates into estimated coordinates at eNodeB (no positioning error)
simValues.XvehicleEstimated = positionManagement.XvehicleReal;
simValues.YvehicleEstimated = positionManagement.YvehicleReal;
positionManagement.XvehicleEstimated = positionManagement.XvehicleReal;
positionManagement.YvehicleEstimated = positionManagement.YvehicleReal;

% Call function to compute distances
% computeDistance(i,j): computeDistance from vehicle with index i to vehicle with index j
% positionManagement.distance matrix has dimensions equal to simValues.IDvehicle x simValues.IDvehicle in order to
% speed up the computation (only vehicles present at the considered instant)
% positionManagement.distance(i,j): positionManagement.distance from vehicle with index i to vehicle with index j
[positionManagement,stationManagement] = computeDistance (simParams,simValues,stationManagement,positionManagement);
[positionManagement] = computeDistance (positionManagement);

% Position Delay
% timetables of old positions
n_vehicles = length(positionManagement.XvehicleReal);
positionManagement.XvehicleHistory = cell(n_vehicles, 1);
positionManagement.YvehicleHistory = cell(n_vehicles, 1);
for i = 1:n_vehicles
positionManagement.XvehicleHistory{i} = timetable(Size=[0 1], VariableTypes={'double'}, TimeStep=seconds(simParams.positionTimeResolution));
positionManagement.YvehicleHistory{i} = timetable(Size=[0 1], VariableTypes={'double'}, TimeStep=seconds(simParams.positionTimeResolution));
end


% Save positionManagement.distance matrix
positionManagement.XvehicleRealOld = positionManagement.XvehicleReal;
Expand All @@ -219,7 +228,7 @@

% Computation of the channel gain
% 'dUpdate': vector used for the calculation of correlated shadowing
dUpdate = zeros(outputValues.Nvehicles,outputValues.Nvehicles);
dUpdate = zeros(0, 0);
[sinrManagement,simValues.Xmap,simValues.Ymap,phyParams.LOS] = computeChannelGain(sinrManagement,stationManagement,positionManagement,phyParams,simParams,dUpdate);

% Update of the neighbors
Expand Down Expand Up @@ -397,7 +406,7 @@
timeManagement.timeNextCV2X = inf;

% if not only 11p
if ismember(constants.V_STATE_LTE_TXRX, stationManagement.vehicleState(stationManagement.activeIDs))
if ismember(constants.V_STATE_LTE_TXRX, stationManagement.vehicleState)
% Initialization of resouce allocation algorithms in LTE-V2X
if ismember(simParams.BRAlgorithm, [constants.REASSIGN_BR_REUSE_DIS_SCHEDULED_VEH,...
constants.REASSIGN_BR_MAX_REUSE_DIS, constants.REASSIGN_BR_MIN_REUSE_POW])
Expand Down Expand Up @@ -445,11 +454,11 @@
% Must be ordered with respect to the packet generation instant
% (Vittorio 5.5.3)
% subframeGen = ceil(timeManagement.timeNextPacket/phyParams.Tsf);
TTIGen = ceil(timeManagement.timeNextPacket/phyParams.TTI);
TTI_BR = ceil(stationManagement.BRid/appParams.NbeaconsF);
stationManagement.BRid = stationManagement.BRid + (TTI_BR<=TTIGen) * appParams.Nbeacons;
stationManagement.BRid = sort(stationManagement.BRid,2);
stationManagement.BRid = stationManagement.BRid - (stationManagement.BRid>appParams.Nbeacons) * appParams.Nbeacons;
TTIGen = ceil(timeManagement.timeNextPacket(stationManagement.activeIDs)/phyParams.TTI);
TTI_BR = ceil(stationManagement.BRid(stationManagement.activeIDs)/appParams.NbeaconsF);
stationManagement.BRid(stationManagement.activeIDs) = stationManagement.BRid(stationManagement.activeIDs) + (TTI_BR<=TTIGen) * appParams.Nbeacons;
stationManagement.BRid(stationManagement.activeIDs) = sort(stationManagement.BRid(stationManagement.activeIDs),2);
stationManagement.BRid(stationManagement.activeIDs) = stationManagement.BRid(stationManagement.activeIDs) - (stationManagement.BRid(stationManagement.activeIDs)>appParams.Nbeacons) * appParams.Nbeacons;

% vector correctSCImatrixCV2X created
stationManagement.correctSCImatrixCV2X = [];
Expand Down Expand Up @@ -483,7 +492,7 @@
% of the first TTI in 0
timeManagement.timeNextCV2X = 0;
timeManagement.ttiCV2Xstarts = true;

% The channel busy ratio of C-V2X is initialized
sinrManagement.cbrCV2X = zeros(simValues.maxID,1);
sinrManagement.cbrLTE_coexLTEonly = zeros(simValues.maxID,1);
Expand Down
55 changes: 38 additions & 17 deletions MainFiles/mainPositionUpdate.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@
% Call function to update vehicles positions
[indexNewVehicles,indexOldVehicles,indexOldVehiclesToOld,stationManagement.activeIDsExit,positionManagement] = updatePosition(timeManagement.timeNow,stationManagement.activeIDs,simParams.positionTimeResolution,positionManagement,simValues,outParams,simParams);
else
% Store IDs of vehicles at the previous beacon period and update positions
[positionManagement.XvehicleReal,positionManagement.YvehicleReal,stationManagement.activeIDs,indexNewVehicles,indexOldVehicles,indexOldVehiclesToOld,stationManagement.activeIDsExit,positionManagement.v,positionManagement.direction] = updatePositionFile( ...
round(timeManagement.timeNextPosUpdate,2), ...
simValues.dataTrace,stationManagement.activeIDs, ...
positionManagement.XvehicleReal,positionManagement.YvehicleReal, ...
round(timeManagement.timeNextPosUpdate,2)-simParams.positionTimeResolution,simValues,outParams);
%% ONLY LTE
if sum(stationManagement.vehicleState(stationManagement.activeIDs)==100)>0
%if simParams.technology ~= 2 % not only 11p
% Update stationManagement.BRid vector (variable number of vehicles in the scenario)
[stationManagement.BRid] = updateBRidFile(stationManagement.BRid,stationManagement.activeIDs,indexNewVehicles);
end
% Interpolate position of all vehicles from current trace file
[positions] = updatePositionFile(simValues.trafficTraceTimetable, timeManagement.timeNow, simValues.IDvehicle);
positionManagement.XvehicleReal = positions.X;
positionManagement.YvehicleReal = positions.Y;
positionManagement.v = positions.V;
positionManagement.direction = NaN(height(positions));

old_active_ids = stationManagement.activeIDs;
% Mark vehicles coming in and out of bounds as active/inactive
[activeIds, enteredVehicles, exitedVehicles] = checkVehicleBounds(positions, stationManagement.activeIDs, ...
"XMin", simParams.XminTrace, "XMax", simParams.XmaxTrace, "YMin", simParams.YminTrace, "YMax", simParams.YmaxTrace);
indexNewVehicles = find(ismember(activeIds, enteredVehicles));
indexOldVehicles = find(~ismember(activeIds, enteredVehicles));
oldVehicles = activeIds(indexOldVehicles);
[~ , indexOldVehiclesToOld] = ismember(oldVehicles, old_active_ids);
stationManagement.activeIDs = activeIds;
stationManagement.activeIDsExit = exitedVehicles;
stationManagement.activeIDsEnter = enteredVehicles;
stationManagement.indexOldVehiclesToOld = indexOldVehiclesToOld;
end


% Vectors IDvehicleLTE and IDvehicle11p are updated
stationManagement.activeIDsCV2X = stationManagement.activeIDs.*(stationManagement.vehicleState(stationManagement.activeIDs)==100);
stationManagement.activeIDsCV2X = stationManagement.activeIDsCV2X(stationManagement.activeIDsCV2X>0);
Expand Down Expand Up @@ -68,20 +76,25 @@
end

% Add LTE positioning delay (if selected)
[simValues.XvehicleEstimated,simValues.YvehicleEstimated,PosUpdateIndex] = addPosDelay(simValues.XvehicleEstimated,simValues.YvehicleEstimated,positionManagement.XvehicleReal,positionManagement.YvehicleReal,stationManagement.activeIDs,indexNewVehicles,indexOldVehicles,indexOldVehiclesToOld,positionManagement.posUpdateAllVehicles,simParams.positionTimeResolution);

[positionManagement.XvehicleEstimated, positionManagement.YvehicleEstimated,...
positionManagement.XvehicleHistory, positionManagement.YvehicleHistory] = addPosDelay(positionManagement.XvehicleReal, positionManagement.YvehicleReal, positionManagement.XvehicleHistory, positionManagement.YvehicleHistory, timeManagement.timeNow, simParams.posDelay, simParams.posPacketLoss);

% Add LTE positioning error (if selected)
% (Xvehicle, Yvehicle): fictitious vehicles' position seen by the eNB
[simValues.XvehicleEstimated(PosUpdateIndex),simValues.YvehicleEstimated(PosUpdateIndex)] = addPosError(positionManagement.XvehicleReal(PosUpdateIndex),positionManagement.YvehicleReal(PosUpdateIndex),simParams.sigmaPosError);
[positionManagement.XvehicleEstimated,positionManagement.YvehicleEstimated] = addPosError(positionManagement.XvehicleEstimated, positionManagement.YvehicleEstimated,simParams.sigmaPosError);

% Record the cumulative mean of positioning error
this_instance_error = sqrt((positionManagement.XvehicleReal - positionManagement.XvehicleEstimated).^2 + (positionManagement.YvehicleReal - positionManagement.YvehicleEstimated).^2);
outputValues.meanPositionError = (this_instance_error + positionManagement.NposUpdates*outputValues.meanPositionError)/(positionManagement.NposUpdates+1);
end

% Call function to compute the distances
[positionManagement,stationManagement] = computeDistance (simParams,simValues,stationManagement,positionManagement);
[positionManagement] = computeDistance(positionManagement);

% Call function to update positionManagement.distance matrix where D(i,j) is the
% change in positionManagement.distance of link i to j from time n-1 to time n and used
% for updating Shadowing matrix
[dUpdate,sinrManagement.Shadowing_dB,positionManagement.distanceRealOld] = updateDistanceChangeForShadowing(positionManagement.distanceReal,positionManagement.distanceRealOld,indexOldVehicles,indexOldVehiclesToOld,sinrManagement.Shadowing_dB,phyParams.stdDevShadowLOS_dB);
[dUpdate,sinrManagement.Shadowing_dB,positionManagement.distanceRealOld] = updateDistanceChangeForShadowing(positionManagement.distanceReal,positionManagement.distanceRealOld,phyParams.stdDevShadowLOS_dB, activeIds, enteredVehicles, exitedVehicles);

% Calculation of channel and then received power
[sinrManagement,simValues.Xmap,simValues.Ymap,phyParams.LOS] = computeChannelGain(sinrManagement,stationManagement,positionManagement,phyParams,simParams,dUpdate);
Expand Down Expand Up @@ -232,6 +245,14 @@
stationManagement.pckNextAttempt(stationManagement.activeIDsExit) = 1;
stationManagement.pckTxOccurring(stationManagement.activeIDsExit) = 0;

% resize stationManagement.hasTransmissionThisSlot
new_hasTransmissionThisSlot = zeros(numel(stationManagement.activeIDs), 1);
new_hasTransmissionThisSlot(indexOldVehicles) = stationManagement.hasTransmissionThisSlot(indexOldVehiclesToOld);
stationManagement.hasTransmissionThisSlot = new_hasTransmissionThisSlot;

% Relinquish the BR for vehicles who exited
stationManagement.BRid(stationManagement.activeIDsExit) = -2;

%% CBR settings for the new vehicles
if simParams.cbrActive && (outParams.printCBR || (simParams.technology==constants.TECH_COEX_STD_INTERF && simParams.coexMethod~=constants.COEX_METHOD_NON && simParams.coex_slotManagement==constants.COEX_SLOT_DYNAMIC))
timeManagement.cbr11p_timeStartMeasInterval(stationManagement.activeIDs(indexNewVehicles)) = timeManagement.timeNow;
Expand Down
7 changes: 5 additions & 2 deletions MainFiles/mainV2X.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
% indexEvent is the index of the vector IDvehicle
% idEvent is the ID of the vehicle of the current event
[timeEvent, indexEvent] = min(timeManagement.timeNextEvent(stationManagement.activeIDs));
if isempty(timeEvent)
timeEvent = timeManagement.timeNextPosUpdate;
end
idEvent = stationManagement.activeIDs(indexEvent);

% If the next C-V2X event is earlier than timeEvent, set the time to the
Expand All @@ -56,7 +59,7 @@
% to the position update
% With LTE, it must necessarily be done after the end of a subframe and
% before the next one
if timeEvent >= (timeManagement.timeNextPosUpdate-1e-9) && ...
if any(timeEvent >= (timeManagement.timeNextPosUpdate-1e-9)) && ...
(isempty(stationManagement.activeIDsCV2X) || (isfield(timeManagement, "ttiCV2Xstarts") && timeManagement.ttiCV2Xstarts==true))
timeEvent = timeManagement.timeNextPosUpdate;
end
Expand Down Expand Up @@ -195,7 +198,7 @@
%printDebugEvents(timeEvent,'LTE subframe starts',-1);
%fprintf('Starts\n');

if timeManagement.timeNow>0
if timeManagement.timeNow > 0 && any(setdiff(stationManagement.activeIDsCV2X, stationManagement.activeIDsEnter))
[phyParams,simValues,outputValues,sinrManagement,stationManagement,timeManagement] = ...
mainCV2XttiEnds(appParams,simParams,phyParams,outParams,simValues,outputValues,timeManagement,positionManagement,sinrManagement,stationManagement);
end
Expand Down
16 changes: 6 additions & 10 deletions MainFilesCV2X/elaborateFateRxCV2X.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
% transmissions
% [ID TX, ID RX, BRid, distance]

distance = positionManagement.distanceReal(stationManagement.vehicleState(stationManagement.activeIDs)==100,stationManagement.vehicleState(stationManagement.activeIDs)==100);
LTEIndices = find(stationManagement.vehicleState == constants.V_STATE_LTE_TXRX);
ActiveIndices = stationManagement.activeIDs;
ActiveLTEIndices = intersect(LTEIndices, ActiveIndices);
distance = positionManagement.distanceReal(ActiveLTEIndices, ActiveLTEIndices);

Ntx = length(IDvehicleTXLTE); % Number of tx vehicles
%resultingList = zeros(Ntx*length(neighborsID(1,:)),5); % Initialize error matrix
Expand All @@ -15,13 +18,6 @@
end

for i = 1:Ntx

%if IDvehicleTXLTE(i)==39
% fp = fopen('Temp.txt','a');
% fprintf(fp,'T=%f: new tx, attemmpt %d\n',timeManagement.timeNow,stationManagement.pckTxOccurring(IDvehicleTXLTE(i)));
% fclose(fp);
%end

% Find indexes of receiving vehicles in neighborsID
indexNeighborsRX = find(neighborsID(indexVehicleTX(i),:));

Expand Down Expand Up @@ -53,7 +49,7 @@
resultingList(indexRaw,1) = IDvehicleTXLTE(i);
resultingList(indexRaw,2) = IDvehicleRX;
resultingList(indexRaw,3) = stationManagement.BRid(IDvehicleTXLTE(i),stationManagement.pckTxOccurring(IDvehicleTXLTE(i)));
resultingList(indexRaw,4) = distance(indexVehicleTX(i),stationManagement.activeIDsCV2X==IDvehicleRX);
resultingList(indexRaw,4) = distance(indexVehicleTX(i), IDvehicleRX);
resultingList(indexRaw,5) = 1; % COLUMN 5=1 IS "CORRECT"
% Mark that this packet has been received
stationManagement.pckReceived(IDvehicleRX,IDvehicleTXLTE(i))=1;
Expand All @@ -67,7 +63,7 @@
resultingList(indexRaw,1) = IDvehicleTXLTE(i);
resultingList(indexRaw,2) = IDvehicleRX;
resultingList(indexRaw,3) = stationManagement.BRid(IDvehicleTXLTE(i),stationManagement.pckTxOccurring(IDvehicleTXLTE(i)));
resultingList(indexRaw,4) = distance(indexVehicleTX(i),stationManagement.activeIDsCV2X==IDvehicleRX);
resultingList(indexRaw,4) = distance(indexVehicleTX(i), IDvehicleRX);
resultingList(indexRaw,5) = 0; % COLUMN 5=0 IS "ERROR"
elseif ~isinf(phyParams.Ksi) || isempty(find(IDvehicleTXLTE == IDvehicleRX, 1))
% ERROR IN ATTEMPT (NOT THE LAST ONE) AND THE RECEIVER IS NOT
Expand Down
4 changes: 2 additions & 2 deletions MainFilesCV2X/initLastPowerCV2X.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

% ID rx vehicle
IDrx = stationManagement.neighborsIDLTE(stationManagement.indexInActiveIDsOnlyLTE_OfTxLTE(i_tx),j_neigh);
RxPowerIDrx = RXpower_MHz_ofLTE(stationManagement.activeIDsCV2X==IDrx,stationManagement.indexInActiveIDsOnlyLTE_OfTxLTE);
RxPowerIDrx = RXpower_MHz_ofLTE(IDrx,stationManagement.indexInActiveIDsOnlyLTE_OfTxLTE);

% Find BRT in use by rx vehicle j
%BRTrx = BRidT(IDrx);
Expand Down Expand Up @@ -120,4 +120,4 @@
% sinrManagement.neighPowerInterfLastLTE(i_tx,j_neigh) = (selfI + Itot) * phyParams.BwMHz_cv2xBR + Ifrom11p;
end
end
end
end
10 changes: 5 additions & 5 deletions MainFilesCV2X/mainCV2XttiEnds.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
% geneated in a subframe during which the station is transmitting is
% not correctly managed
for idLte = stationManagement.activeIDsCV2X'
if stationManagement.pckBuffer(idLte)>1
if stationManagement.pckBuffer(idLte) > appParams.bufferLength
[stationManagement,outputValues] = bufferOverflowLTE(idLte,timeManagement,positionManagement,stationManagement,phyParams,appParams,outputValues,outParams);
stationManagement.pckNextAttempt(idLte) = 1;
end
Expand Down Expand Up @@ -48,12 +48,12 @@
stationManagement.BRid(stationManagement.activeIDsCV2X(hasNewPacketThisTbeacon),:) = BRidModified;
end

elseif mod(timeManagement.elapsedTime_TTIs,appParams.NbeaconsT)==0
elseif timeManagement.elapsedTime_TTIs == 1 || mod(timeManagement.elapsedTime_TTIs,appParams.NbeaconsT) == 0
% All other algorithms except standard Mode 4
% TODO not checked in version 5.X

%% Radio Resources Reassignment
if simParams.BRAlgorithm==constants.REASSIGN_BR_REUSE_DIS_SCHEDULED_VEH || simParams.BRAlgorithm==constants.REASSIGN_BR_MAX_REUSE_DIS || simParams.BRAlgorithm==constants.REASSIGN_BR_MIN_REUSE_POW
if ismember(simParams.BRAlgorithm, constants.REASSIGN_BR_ALGORITHMS_CONTROLLED)

if timeManagement.elapsedTime_TTIs > 0
% Current scheduled reassign period
Expand All @@ -79,7 +79,7 @@

% BRs reassignment (CONTROLLED with MAXIMUM REUSE DISTANCE)
%[stationManagement.BRid,Nreassign] = BRreassignmentControlledMaxReuse(stationManagement.activeIDsCV2X,stationManagement.BRid,scheduledID,stationManagement.neighborsIDLTE,appParams.NbeaconsT,appParams.NbeaconsF);
[stationManagement.BRid,Nreassign] = BRreassignmentControlledMaxReuse(stationManagement.activeIDsCV2X,stationManagement.BRid,scheduledID,stationManagement.allNeighborsID,appParams.NbeaconsT,appParams.NbeaconsF);
[stationManagement.BRid,Nreassign] = BRreassignmentControlledMaxReuse(stationManagement.activeIDsCV2X,stationManagement.BRid,scheduledID,stationManagement.allNeighborsIDEstimated,appParams.NbeaconsT,appParams.NbeaconsF);

elseif simParams.BRAlgorithm == constants.REASSIGN_BR_MIN_REUSE_POW

Expand Down Expand Up @@ -113,7 +113,7 @@
elseif simParams.BRAlgorithm==constants.REASSIGN_BR_ORDERED_ALLOCATION

% Call Benchmark Algorithm 102 (ORDERED ALLOCATION)
[stationManagement.BRid,Nreassign] = BRreassignmentOrdered(positionManagement.XvehicleReal,stationManagement.activeIDsCV2X,stationManagement.BRid,appParams.NbeaconsT,appParams.NbeaconsF);
[stationManagement.BRid,Nreassign] = BRreassignmentOrdered(positionManagement.XvehicleEstimated,stationManagement.activeIDsCV2X,stationManagement.BRid,appParams.NbeaconsT,appParams.NbeaconsF);

end

Expand Down
Loading